Skip to content

Commit 2fc20f6

Browse files
committed
[lldb][NFC] Refactor remaining completion logic to use CompletionRequests
This patch moves the remaining completion functions from the old completion API (that used several variables) to just passing a single CompletionRequest. This is for the most part a simple change as we just replace the old arguments with a single CompletionRequest argument. There are a few places where I had to create new CompletionRequests in the called functions as CompletionRequests itself are immutable and don't expose their internal match list anymore. This means that if a function wanted to change the CompletionRequest or directly access the result list, we need to work around this by creating a new CompletionRequest and a temporary match/description list. Preparation work for rdar://53769355 llvm-svn: 369000
1 parent dc23c83 commit 2fc20f6

File tree

10 files changed

+89
-92
lines changed

10 files changed

+89
-92
lines changed

lldb/include/lldb/Core/IOHandler.h

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define liblldb_IOHandler_h_
1111

1212
#include "lldb/Core/ValueObjectList.h"
13+
#include "lldb/Utility/CompletionRequest.h"
1314
#include "lldb/Utility/ConstString.h"
1415
#include "lldb/Utility/Flags.h"
1516
#include "lldb/Utility/Predicate.h"
@@ -198,9 +199,8 @@ class IOHandlerDelegate {
198199

199200
virtual void IOHandlerDeactivated(IOHandler &io_handler) {}
200201

201-
virtual int IOHandlerComplete(IOHandler &io_handler, const char *current_line,
202-
const char *cursor, const char *last_char,
203-
StringList &matches, StringList &descriptions);
202+
virtual int IOHandlerComplete(IOHandler &io_handler,
203+
CompletionRequest &request);
204204

205205
virtual const char *IOHandlerGetFixIndentationCharacters() { return nullptr; }
206206

@@ -414,10 +414,7 @@ class IOHandlerEditline : public IOHandler {
414414
static int FixIndentationCallback(Editline *editline, const StringList &lines,
415415
int cursor_position, void *baton);
416416

417-
static int AutoCompleteCallback(const char *current_line, const char *cursor,
418-
const char *last_char,
419-
StringList &matches, StringList &descriptions,
420-
void *baton);
417+
static int AutoCompleteCallback(CompletionRequest &request, void *baton);
421418
#endif
422419

423420
protected:
@@ -448,9 +445,8 @@ class IOHandlerConfirm : public IOHandlerDelegate, public IOHandlerEditline {
448445

449446
bool GetResponse() const { return m_user_response; }
450447

451-
int IOHandlerComplete(IOHandler &io_handler, const char *current_line,
452-
const char *cursor, const char *last_char,
453-
StringList &matches, StringList &descriptions) override;
448+
int IOHandlerComplete(IOHandler &io_handler,
449+
CompletionRequest &request) override;
454450

455451
void IOHandlerInputComplete(IOHandler &io_handler,
456452
std::string &data) override;

lldb/include/lldb/Expression/REPL.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,8 @@ class REPL : public IOHandlerDelegate {
103103
void IOHandlerInputComplete(IOHandler &io_handler,
104104
std::string &line) override;
105105

106-
int IOHandlerComplete(IOHandler &io_handler, const char *current_line,
107-
const char *cursor, const char *last_char,
108-
StringList &matches, StringList &descriptions) override;
106+
int IOHandlerComplete(IOHandler &io_handler,
107+
CompletionRequest &request) override;
109108

110109
protected:
111110
static int CalculateActualIndentation(const StringList &lines);

lldb/include/lldb/Host/Editline.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include <vector>
5454

5555
#include "lldb/Host/ConnectionFileDescriptor.h"
56+
#include "lldb/Utility/CompletionRequest.h"
5657
#include "lldb/Utility/FileSpec.h"
5758
#include "lldb/Utility/Predicate.h"
5859

@@ -97,10 +98,7 @@ typedef int (*FixIndentationCallbackType)(Editline *editline,
9798
const StringList &lines,
9899
int cursor_position, void *baton);
99100

100-
typedef int (*CompleteCallbackType)(const char *current_line,
101-
const char *cursor, const char *last_char,
102-
StringList &matches,
103-
StringList &descriptions, void *baton);
101+
typedef int (*CompleteCallbackType)(CompletionRequest &request, void *baton);
104102

105103
/// Status used to decide when and how to start editing another line in
106104
/// multi-line sessions

lldb/include/lldb/Interpreter/CommandInterpreter.h

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -308,29 +308,17 @@ class CommandInterpreter : public Broadcaster,
308308

309309
CommandObject *GetCommandObjectForCommand(llvm::StringRef &command_line);
310310

311-
// This handles command line completion. You are given a pointer to the
312-
// command string buffer, to the current cursor, and to the end of the string
313-
// (in case it is not NULL terminated). You also passed in an StringList
314-
// object to fill with the returns. The first element of the array will be
315-
// filled with the string that you would need to insert at the cursor point
316-
// to complete the cursor point to the longest common matching prefix. If you
317-
// want to limit the number of elements returned, set max_return_elements to
318-
// the number of elements you want returned. Otherwise set
319-
// max_return_elements to -1. If you want to start some way into the match
320-
// list, then set match_start_point to the desired start point. Returns: -1
311+
// This handles command line completion. Returns: -1
321312
// if the completion character should be inserted -2 if the entire command
322313
// line should be deleted and replaced with matches.GetStringAtIndex(0)
323314
// INT_MAX if the number of matches is > max_return_elements, but it is
324315
// expensive to compute. Otherwise, returns the number of matches.
325316
//
326317
// FIXME: Only max_return_elements == -1 is supported at present.
327-
int HandleCompletion(const char *current_line, const char *cursor,
328-
const char *last_char, StringList &matches,
329-
StringList &descriptions);
318+
int HandleCompletion(CompletionRequest &request);
330319

331-
// This version just returns matches, and doesn't compute the substring. It
332-
// is here so the Help command can call it for the first argument. It uses
333-
// a CompletionRequest for simplicity reasons.
320+
// This version just returns matches, and doesn't compute the substring. It
321+
// is here so the Help command can call it for the first argument.
334322
int HandleCompletionMatches(CompletionRequest &request);
335323

336324
int GetCommandNamesMatchingPartialString(const char *cmd_cstr,

lldb/include/lldb/Utility/CompletionRequest.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ class CompletionRequest {
7575
CompletionResult &result);
7676

7777
llvm::StringRef GetRawLine() const { return m_command; }
78+
llvm::StringRef GetRawLineUntilCursor() const {
79+
return m_command.substr(0, m_cursor_index);
80+
}
7881

7982
unsigned GetRawCursorPos() const { return m_raw_cursor_pos; }
8083

lldb/source/API/SBCommandInterpreter.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,11 @@ int SBCommandInterpreter::HandleCompletionWithDescriptions(
371371

372372
if (IsValid()) {
373373
lldb_private::StringList lldb_matches, lldb_descriptions;
374-
num_completions = m_opaque_ptr->HandleCompletion(
375-
current_line, cursor, last_char, lldb_matches, lldb_descriptions);
374+
CompletionResult result;
375+
CompletionRequest request(current_line, cursor - current_line, result);
376+
num_completions = m_opaque_ptr->HandleCompletion(request);
377+
result.GetMatches(lldb_matches);
378+
result.GetDescriptions(lldb_descriptions);
376379

377380
SBStringList temp_matches_list(&lldb_matches);
378381
matches.AppendList(temp_matches_list);

lldb/source/Core/IOHandler.cpp

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -170,17 +170,15 @@ IOHandlerConfirm::IOHandlerConfirm(Debugger &debugger, llvm::StringRef prompt,
170170

171171
IOHandlerConfirm::~IOHandlerConfirm() = default;
172172

173-
int IOHandlerConfirm::IOHandlerComplete(
174-
IOHandler &io_handler, const char *current_line, const char *cursor,
175-
const char *last_char, StringList &matches, StringList &descriptions) {
176-
if (current_line == cursor) {
177-
if (m_default_response) {
178-
matches.AppendString("y");
179-
} else {
180-
matches.AppendString("n");
181-
}
173+
int IOHandlerConfirm::IOHandlerComplete(IOHandler &io_handler,
174+
CompletionRequest &request) {
175+
if (request.GetRawCursorPos() == 0) {
176+
if (m_default_response)
177+
request.AddCompletion("y");
178+
else
179+
request.AddCompletion("n");
182180
}
183-
return matches.GetSize();
181+
return request.GetNumberOfMatches();
184182
}
185183

186184
void IOHandlerConfirm::IOHandlerInputComplete(IOHandler &io_handler,
@@ -218,39 +216,43 @@ void IOHandlerConfirm::IOHandlerInputComplete(IOHandler &io_handler,
218216
}
219217
}
220218

221-
int IOHandlerDelegate::IOHandlerComplete(
222-
IOHandler &io_handler, const char *current_line, const char *cursor,
223-
const char *last_char, StringList &matches, StringList &descriptions) {
219+
int IOHandlerDelegate::IOHandlerComplete(IOHandler &io_handler,
220+
CompletionRequest &request) {
224221
switch (m_completion) {
225222
case Completion::None:
226223
break;
227224

228225
case Completion::LLDBCommand:
229226
return io_handler.GetDebugger().GetCommandInterpreter().HandleCompletion(
230-
current_line, cursor, last_char, matches, descriptions);
227+
request);
231228
case Completion::Expression: {
232229
CompletionResult result;
233-
CompletionRequest request(current_line, cursor - current_line, result);
230+
CompletionRequest subrequest(request.GetRawLine(),
231+
request.GetRawCursorPos(), result);
234232
CommandCompletions::InvokeCommonCompletionCallbacks(
235233
io_handler.GetDebugger().GetCommandInterpreter(),
236-
CommandCompletions::eVariablePathCompletion, request, nullptr);
234+
CommandCompletions::eVariablePathCompletion, subrequest, nullptr);
235+
StringList matches;
236+
StringList descriptions;
237237
result.GetMatches(matches);
238238
result.GetDescriptions(descriptions);
239239

240-
size_t num_matches = request.GetNumberOfMatches();
240+
size_t num_matches = subrequest.GetNumberOfMatches();
241241
if (num_matches > 0) {
242242
std::string common_prefix = matches.LongestCommonPrefix();
243-
const size_t partial_name_len = request.GetCursorArgumentPrefix().size();
243+
const size_t partial_name_len =
244+
subrequest.GetCursorArgumentPrefix().size();
244245

245246
// If we matched a unique single command, add a space... Only do this if
246247
// the completer told us this was a complete word, however...
247-
if (num_matches == 1 && request.GetWordComplete()) {
248+
if (num_matches == 1 && subrequest.GetWordComplete()) {
248249
common_prefix.push_back(' ');
249250
}
250251
common_prefix.erase(0, partial_name_len);
251-
matches.InsertStringAtIndex(0, std::move(common_prefix));
252+
request.AddCompletion(common_prefix);
252253
}
253-
return num_matches;
254+
request.AddCompletions(matches, descriptions);
255+
return request.GetNumberOfMatches();
254256
} break;
255257
}
256258

@@ -443,14 +445,12 @@ int IOHandlerEditline::FixIndentationCallback(Editline *editline,
443445
*editline_reader, lines, cursor_position);
444446
}
445447

446-
int IOHandlerEditline::AutoCompleteCallback(
447-
const char *current_line, const char *cursor, const char *last_char,
448-
StringList &matches, StringList &descriptions, void *baton) {
448+
int IOHandlerEditline::AutoCompleteCallback(CompletionRequest &request,
449+
void *baton) {
449450
IOHandlerEditline *editline_reader = (IOHandlerEditline *)baton;
450451
if (editline_reader)
451-
return editline_reader->m_delegate.IOHandlerComplete(
452-
*editline_reader, current_line, cursor, last_char, matches,
453-
descriptions);
452+
return editline_reader->m_delegate.IOHandlerComplete(*editline_reader,
453+
request);
454454
return 0;
455455
}
456456
#endif

lldb/source/Expression/REPL.cpp

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -433,28 +433,28 @@ void REPL::IOHandlerInputComplete(IOHandler &io_handler, std::string &code) {
433433
}
434434
}
435435

436-
int REPL::IOHandlerComplete(IOHandler &io_handler, const char *current_line,
437-
const char *cursor, const char *last_char,
438-
StringList &matches, StringList &descriptions) {
439-
matches.Clear();
440-
441-
llvm::StringRef line(current_line, cursor - current_line);
442-
436+
int REPL::IOHandlerComplete(IOHandler &io_handler, CompletionRequest &request) {
443437
// Complete an LLDB command if the first character is a colon...
444-
if (!line.empty() && line[0] == ':') {
438+
if (request.GetRawLine().startswith(":")) {
445439
Debugger &debugger = m_target.GetDebugger();
446440

447441
// auto complete LLDB commands
448-
const char *lldb_current_line = line.substr(1).data();
449-
return debugger.GetCommandInterpreter().HandleCompletion(
450-
lldb_current_line, cursor, last_char, matches, descriptions);
442+
llvm::StringRef new_line = request.GetRawLine().drop_front();
443+
CompletionResult sub_result;
444+
CompletionRequest sub_request(new_line, request.GetRawCursorPos() - 1,
445+
sub_result);
446+
int result = debugger.GetCommandInterpreter().HandleCompletion(sub_request);
447+
StringList matches, descriptions;
448+
sub_result.GetMatches(matches);
449+
sub_result.GetDescriptions(descriptions);
450+
request.AddCompletions(matches, descriptions);
451+
return result;
451452
}
452453

453454
// Strip spaces from the line and see if we had only spaces
454-
line = line.ltrim();
455-
if (line.empty()) {
455+
if (request.GetRawLineUntilCursor().trim().empty()) {
456456
// Only spaces on this line, so just indent
457-
matches.AppendString(m_indent_str);
457+
request.AddCompletion(m_indent_str);
458458
return 1;
459459
}
460460

@@ -477,12 +477,13 @@ int REPL::IOHandlerComplete(IOHandler &io_handler, const char *current_line,
477477
}
478478
}
479479

480-
if (cursor > current_line) {
481-
current_code.append("\n");
482-
current_code.append(current_line, cursor - current_line);
483-
}
480+
current_code.append("\n");
481+
current_code += request.GetRawLineUntilCursor();
484482

485-
return CompleteCode(current_code, matches);
483+
StringList matches;
484+
int result = CompleteCode(current_code, matches);
485+
request.AddCompletions(matches);
486+
return result;
486487
}
487488

488489
bool QuitCommandOverrideCallback(void *baton, const char **argv) {

lldb/source/Host/common/Editline.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "lldb/Host/Editline.h"
1515
#include "lldb/Host/FileSystem.h"
1616
#include "lldb/Host/Host.h"
17+
#include "lldb/Utility/CompletionRequest.h"
1718
#include "lldb/Utility/FileSpec.h"
1819
#include "lldb/Utility/LLDBAssert.h"
1920
#include "lldb/Utility/SelectHelper.h"
@@ -894,9 +895,17 @@ unsigned char Editline::TabCommand(int ch) {
894895
StringList completions, descriptions;
895896
int page_size = 40;
896897

897-
const int num_completions = m_completion_callback(
898-
line_info->buffer, line_info->cursor, line_info->lastchar,
899-
completions, descriptions, m_completion_callback_baton);
898+
llvm::StringRef line(line_info->buffer,
899+
line_info->lastchar - line_info->buffer);
900+
unsigned cursor_index = line_info->cursor - line_info->buffer;
901+
CompletionResult result;
902+
CompletionRequest request(line, cursor_index, result);
903+
904+
const int num_completions =
905+
m_completion_callback(request, m_completion_callback_baton);
906+
907+
result.GetMatches(completions);
908+
result.GetDescriptions(descriptions);
900909

901910
if (num_completions == 0)
902911
return CC_ERROR;

lldb/source/Interpreter/CommandInterpreter.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,18 +1814,16 @@ int CommandInterpreter::HandleCompletionMatches(CompletionRequest &request) {
18141814
return num_command_matches;
18151815
}
18161816

1817-
int CommandInterpreter::HandleCompletion(const char *current_line,
1818-
const char *cursor,
1819-
const char *last_char,
1820-
StringList &matches,
1821-
StringList &descriptions) {
1822-
1823-
llvm::StringRef command_line(current_line, last_char - current_line);
1817+
int CommandInterpreter::HandleCompletion(CompletionRequest &orig_request) {
1818+
// Start a new subrequest we can modify.
18241819
CompletionResult result;
1825-
CompletionRequest request(command_line, cursor - current_line, result);
1820+
CompletionRequest request(orig_request.GetRawLine(),
1821+
orig_request.GetRawCursorPos(), result);
18261822
// Don't complete comments, and if the line we are completing is just the
18271823
// history repeat character, substitute the appropriate history line.
18281824
const char *first_arg = request.GetParsedLine().GetArgumentAtIndex(0);
1825+
StringList matches, descriptions;
1826+
18291827
if (first_arg) {
18301828
if (first_arg[0] == m_comment_char)
18311829
return 0;
@@ -1872,6 +1870,8 @@ int CommandInterpreter::HandleCompletion(const char *current_line,
18721870
matches.InsertStringAtIndex(0, common_prefix.c_str());
18731871
descriptions.InsertStringAtIndex(0, "");
18741872
}
1873+
// Add completion to original request.
1874+
orig_request.AddCompletions(matches, descriptions);
18751875
return num_command_matches;
18761876
}
18771877

0 commit comments

Comments
 (0)