LLDB  mainline
CompletionRequest.cpp
Go to the documentation of this file.
1 //===-- CompletionRequest.cpp ---------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 
11 using namespace lldb;
12 using namespace lldb_private;
13 
14 CompletionRequest::CompletionRequest(llvm::StringRef command_line,
15  unsigned raw_cursor_pos,
16  CompletionResult &result)
17  : m_command(command_line), m_raw_cursor_pos(raw_cursor_pos),
18  m_result(result) {
19  assert(raw_cursor_pos <= command_line.size() && "Out of bounds cursor?");
20 
21  // We parse the argument up to the cursor, so the last argument in
22  // parsed_line is the one containing the cursor, and the cursor is after the
23  // last character.
24  llvm::StringRef partial_command(command_line.substr(0, raw_cursor_pos));
25  m_parsed_line = Args(partial_command);
26 
27  if (GetParsedLine().GetArgumentCount() == 0) {
28  m_cursor_index = 0;
30  } else {
33  strlen(GetParsedLine().GetArgumentAtIndex(m_cursor_index));
34  }
35 
36  // The cursor is after a space but the space is not part of the argument.
37  // Let's add an empty fake argument to the end to make sure the completion
38  // code. Note: The space could be part of the last argument when it's quoted.
39  if (partial_command.endswith(" ") &&
40  !GetCursorArgumentPrefix().endswith(" "))
42 }
43 
45 
46  // We build a unique key for this pair of completion:description. We
47  // prefix the key with the length of the completion string. This prevents
48  // that we could get any collisions from completions pairs such as these:
49  // "foo:", "bar" would be "foo:bar", but will now be: "4foo:bar"
50  // "foo", ":bar" would be "foo:bar", but will now be: "3foo:bar"
51 
52  std::string result;
53  result.append(std::to_string(m_completion.size()));
54  result.append(m_completion);
55  result.append(std::to_string(static_cast<int>(m_mode)));
56  result.append(":");
57  result.append(m_descripton);
58  return result;
59 }
60 
61 void CompletionResult::AddResult(llvm::StringRef completion,
62  llvm::StringRef description,
63  CompletionMode mode) {
64  Completion r(completion, description, mode);
65 
66  // Add the completion if we haven't seen the same value before.
67  if (m_added_values.insert(r.GetUniqueKey()).second)
68  m_results.push_back(r);
69 }
70 
72  matches.Clear();
73  for (const Completion &completion : m_results)
74  matches.AppendString(completion.GetCompletion());
75 }
76 
77 void CompletionResult::GetDescriptions(StringList &descriptions) const {
78  descriptions.Clear();
79  for (const Completion &completion : m_results)
80  descriptions.AppendString(completion.GetDescription());
81 }
lldb_private::StringList::Clear
void Clear()
Definition: StringList.cpp:99
lldb_private::CompletionRequest::GetParsedLine
const Args & GetParsedLine() const
Definition: CompletionRequest.h:134
lldb_private::StringList::AppendString
void AppendString(const std::string &s)
Definition: StringList.cpp:43
lldb_private::CompletionResult::Completion
A single completion and all associated data.
Definition: CompletionRequest.h:41
lldb_private::CompletionRequest::m_parsed_line
Args m_parsed_line
The command line parsed as arguments.
Definition: CompletionRequest.h:227
lldb_private::CompletionRequest::AppendEmptyArgument
void AppendEmptyArgument()
Adds an empty argument at the end of the argument list and moves the cursor to this new argument.
Definition: CompletionRequest.h:150
lldb_private::Args
Definition: Args.h:33
lldb_private::CompletionResult::GetMatches
void GetMatches(StringList &matches) const
Adds all collected completion matches to the given list.
Definition: CompletionRequest.cpp:71
CompletionRequest.h
lldb_private::CompletionRequest::m_cursor_char_position
size_t m_cursor_char_position
The cursor position in the argument indexed by m_cursor_index.
Definition: CompletionRequest.h:231
lldb_private::CompletionResult::Completion::m_mode
CompletionMode m_mode
Definition: CompletionRequest.h:50
lldb_private::CompletionMode
CompletionMode
Definition: CompletionRequest.h:19
lldb_private::StringList
Definition: StringList.h:25
lldb_private::CompletionResult::Completion::m_descripton
std::string m_descripton
The description that should be displayed to the user alongside the completion text.
Definition: CompletionRequest.h:49
lldb_private::CompletionResult::Completion::GetUniqueKey
std::string GetUniqueKey() const
Generates a string that uniquely identifies this completion result.
Definition: CompletionRequest.cpp:44
lldb_private::CompletionRequest::GetCursorArgumentPrefix
llvm::StringRef GetCursorArgumentPrefix() const
Definition: CompletionRequest.h:217
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:39
lldb_private::CompletionResult::m_added_values
llvm::StringSet m_added_values
A set of the unique keys of all found completions so far.
Definition: CompletionRequest.h:72
lldb_private::CompletionResult::AddResult
void AddResult(llvm::StringRef completion, llvm::StringRef description, CompletionMode mode)
Definition: CompletionRequest.cpp:61
lldb_private::CompletionResult::m_results
std::vector< Completion > m_results
List of found completions.
Definition: CompletionRequest.h:67
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::CompletionResult
Definition: CompletionRequest.h:38
lldb_private::CompletionRequest::m_cursor_index
size_t m_cursor_index
The index of the argument in which the completion cursor is.
Definition: CompletionRequest.h:229
lldb_private::Args::GetArgumentCount
size_t GetArgumentCount() const
Gets the number of arguments left in this command object.
Definition: Args.h:118
lldb_private::CompletionResult::GetDescriptions
void GetDescriptions(StringList &descriptions) const
Adds all collected completion descriptions to the given list.
Definition: CompletionRequest.cpp:77
lldb
Definition: SBAddress.h:15
lldb_private::CompletionResult::Completion::m_completion
std::string m_completion
The actual text that should be completed.
Definition: CompletionRequest.h:46