Summary: We currently allow any completion handler to read and manipulate the list of matches we calculated so far. This leads to a few problems: Firstly, a completion handler's logic can now depend on previously calculated results by another handlers. No completion handler should have such an implicit dependency, but the current API makes it likely that this could happen (or already happens). Especially the fact that some completion handler deleted all previously calculated results can mess things up right now. Secondly, all completion handlers have knowledge about our internal data structures with this API. This makes refactoring this internal data structure much harder than it should be. Especially planned changes like the support of descriptions for completions are currently giant patches because we have to refactor every single completion handler. This patch narrows the contract the CompletionRequest has with the different handlers to: 1. A handler can suggest a completion. 2. A handler can ask how many suggestions we already have. Point 2 obviously means we still have a dependency left between the different handlers, but getting rid of this is too large to just append it to this patch. Otherwise this patch just completely hides the internal StringList to the different handlers. The CompletionRequest API now also ensures that the list of completions is unique and we don't suggest the same value multiple times to the user. This property has been so far only been ensured by the `Option` handler, but is now applied globally. This is part of this patch as the OptionHandler is no longer able to implement this functionality itself. Reviewers: jingham, davide, labath Reviewed By: davide Subscribers: lldb-commits Differential Revision: https://reviews.llvm.org/D49322 llvm-svn: 338151
104 lines
3.4 KiB
C++
104 lines
3.4 KiB
C++
//===-- CompletionRequestTest.cpp -------------------------------*- C++ -*-===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include "lldb/Utility/CompletionRequest.h"
|
|
using namespace lldb_private;
|
|
|
|
TEST(CompletionRequest, Constructor) {
|
|
std::string command = "a bad c";
|
|
const unsigned cursor_pos = 3;
|
|
const int arg_index = 1;
|
|
const int arg_cursor_pos = 1;
|
|
const int match_start = 2345;
|
|
const int match_max_return = 12345;
|
|
StringList matches;
|
|
|
|
CompletionRequest request(command, cursor_pos, match_start, match_max_return,
|
|
matches);
|
|
|
|
EXPECT_STREQ(request.GetRawLine().str().c_str(), command.c_str());
|
|
EXPECT_EQ(request.GetRawCursorPos(), cursor_pos);
|
|
EXPECT_EQ(request.GetCursorIndex(), arg_index);
|
|
EXPECT_EQ(request.GetCursorCharPosition(), arg_cursor_pos);
|
|
EXPECT_EQ(request.GetMatchStartPoint(), match_start);
|
|
EXPECT_EQ(request.GetMaxReturnElements(), match_max_return);
|
|
EXPECT_EQ(request.GetWordComplete(), false);
|
|
|
|
EXPECT_EQ(request.GetPartialParsedLine().GetArgumentCount(), 2u);
|
|
EXPECT_STREQ(request.GetPartialParsedLine().GetArgumentAtIndex(1), "b");
|
|
}
|
|
|
|
TEST(CompletionRequest, DuplicateFiltering) {
|
|
std::string command = "a bad c";
|
|
const unsigned cursor_pos = 3;
|
|
StringList matches;
|
|
|
|
CompletionRequest request(command, cursor_pos, 0, 0, matches);
|
|
|
|
EXPECT_EQ(0U, request.GetNumberOfMatches());
|
|
|
|
// Add foo twice
|
|
request.AddCompletion("foo");
|
|
EXPECT_EQ(1U, request.GetNumberOfMatches());
|
|
EXPECT_EQ(1U, matches.GetSize());
|
|
EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
|
|
|
|
request.AddCompletion("foo");
|
|
EXPECT_EQ(1U, request.GetNumberOfMatches());
|
|
EXPECT_EQ(1U, matches.GetSize());
|
|
EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
|
|
|
|
// Add bar twice
|
|
request.AddCompletion("bar");
|
|
EXPECT_EQ(2U, request.GetNumberOfMatches());
|
|
EXPECT_EQ(2U, matches.GetSize());
|
|
EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
|
|
EXPECT_STREQ("bar", matches.GetStringAtIndex(1));
|
|
|
|
request.AddCompletion("bar");
|
|
EXPECT_EQ(2U, request.GetNumberOfMatches());
|
|
EXPECT_EQ(2U, matches.GetSize());
|
|
EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
|
|
EXPECT_STREQ("bar", matches.GetStringAtIndex(1));
|
|
|
|
// Add foo again.
|
|
request.AddCompletion("foo");
|
|
EXPECT_EQ(2U, request.GetNumberOfMatches());
|
|
EXPECT_EQ(2U, matches.GetSize());
|
|
EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
|
|
EXPECT_STREQ("bar", matches.GetStringAtIndex(1));
|
|
|
|
// Add something with an existing prefix
|
|
request.AddCompletion("foobar");
|
|
EXPECT_EQ(3U, request.GetNumberOfMatches());
|
|
EXPECT_EQ(3U, matches.GetSize());
|
|
EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
|
|
EXPECT_STREQ("bar", matches.GetStringAtIndex(1));
|
|
EXPECT_STREQ("foobar", matches.GetStringAtIndex(2));
|
|
}
|
|
|
|
TEST(CompletionRequest, TestCompletionOwnership) {
|
|
std::string command = "a bad c";
|
|
const unsigned cursor_pos = 3;
|
|
StringList matches;
|
|
|
|
CompletionRequest request(command, cursor_pos, 0, 0, matches);
|
|
|
|
std::string Temporary = "bar";
|
|
request.AddCompletion(Temporary);
|
|
// Manipulate our completion. The request should have taken a copy, so that
|
|
// shouldn't influence anything.
|
|
Temporary[0] = 'f';
|
|
|
|
EXPECT_EQ(1U, request.GetNumberOfMatches());
|
|
EXPECT_STREQ("bar", matches.GetStringAtIndex(0));
|
|
}
|