LLDB  mainline
CompletionRequest.h
Go to the documentation of this file.
1 //===-- CompletionRequest.h -------------------------------------*- C++ -*-===//
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 
9 #ifndef LLDB_UTILITY_COMPLETIONREQUEST_H
10 #define LLDB_UTILITY_COMPLETIONREQUEST_H
11 
12 #include "lldb/Utility/Args.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/ADT/StringSet.h"
17 
18 namespace lldb_private {
20  /// A single completion and all associated data.
21  struct Completion {
22  Completion(llvm::StringRef completion, llvm::StringRef description)
23  : m_completion(completion.str()), m_descripton(description.str()) {}
24 
25  std::string m_completion;
26  std::string m_descripton;
27 
28  /// Generates a string that uniquely identifies this completion result.
29  std::string GetUniqueKey() const;
30  };
31  std::vector<Completion> m_results;
32 
33  /// List of added completions so far. Used to filter out duplicates.
34  llvm::StringSet<> m_added_values;
35 
36 public:
37  void AddResult(llvm::StringRef completion, llvm::StringRef description);
38 
39  /// Adds all collected completion matches to the given list.
40  /// The list will be cleared before the results are added. The number of
41  /// results here is guaranteed to be equal to GetNumberOfResults().
42  void GetMatches(StringList &matches) const;
43 
44  /// Adds all collected completion descriptions to the given list.
45  /// The list will be cleared before the results are added. The number of
46  /// results here is guaranteed to be equal to GetNumberOfResults().
47  void GetDescriptions(StringList &descriptions) const;
48 
49  std::size_t GetNumberOfResults() const { return m_results.size(); }
50 };
51 
52 /// \class CompletionRequest CompletionRequest.h
53 /// "lldb/Utility/ArgCompletionRequest.h"
54 ///
55 /// Contains all information necessary to complete an incomplete command
56 /// for the user. Will be filled with the generated completions by the different
57 /// completions functions.
58 ///
60 public:
61  /// Constructs a completion request.
62  ///
63  /// \param [in] command_line
64  /// The command line the user has typed at this point.
65  ///
66  /// \param [in] raw_cursor_pos
67  /// The position of the cursor in the command line string. Index 0 means
68  /// the cursor is at the start of the line. The completion starts from
69  /// this cursor position.
70  ///
71  /// \param [in] match_start_point
72  /// \param [in] max_return_elements
73  /// If there is a match that is expensive to compute, these are here to
74  /// allow you to compute the completions in batches. Start the
75  /// completion from match_start_point, and return match_return_elements
76  /// elements.
77  ///
78  /// \param [out] result
79  /// The CompletionResult that will be filled with the results after this
80  /// request has been handled.
81  CompletionRequest(llvm::StringRef command_line, unsigned raw_cursor_pos,
82  int match_start_point, int max_return_elements,
83  CompletionResult &result);
84 
85  llvm::StringRef GetRawLine() const { return m_command; }
86 
87  unsigned GetRawCursorPos() const { return m_raw_cursor_pos; }
88 
89  const Args &GetParsedLine() const { return m_parsed_line; }
90 
91  Args &GetParsedLine() { return m_parsed_line; }
92 
93  const Args &GetPartialParsedLine() const { return m_partial_parsed_line; }
94 
95  void SetCursorIndex(int i) { m_cursor_index = i; }
96  int GetCursorIndex() const { return m_cursor_index; }
97 
98  void SetCursorCharPosition(int pos) { m_cursor_char_position = pos; }
99  int GetCursorCharPosition() const { return m_cursor_char_position; }
100 
101  int GetMatchStartPoint() const { return m_match_start_point; }
102 
103  int GetMaxReturnElements() const { return m_max_return_elements; }
104 
105  bool GetWordComplete() { return m_word_complete; }
106 
107  void SetWordComplete(bool v) { m_word_complete = v; }
108 
109  /// Adds a possible completion string. If the completion was already
110  /// suggested before, it will not be added to the list of results. A copy of
111  /// the suggested completion is stored, so the given string can be free'd
112  /// afterwards.
113  ///
114  /// \param match The suggested completion.
115  /// \param match An optional description of the completion string. The
116  /// description will be displayed to the user alongside the completion.
117  void AddCompletion(llvm::StringRef completion,
118  llvm::StringRef description = "") {
119  m_result.AddResult(completion, description);
120  }
121 
122  /// Adds multiple possible completion strings.
123  ///
124  /// \param completions The list of completions.
125  ///
126  /// \see AddCompletion
127  void AddCompletions(const StringList &completions) {
128  for (std::size_t i = 0; i < completions.GetSize(); ++i)
129  AddCompletion(completions.GetStringAtIndex(i));
130  }
131 
132  /// Adds multiple possible completion strings alongside their descriptions.
133  ///
134  /// The number of completions and descriptions must be identical.
135  ///
136  /// \param completions The list of completions.
137  /// \param completions The list of descriptions.
138  ///
139  /// \see AddCompletion
140  void AddCompletions(const StringList &completions,
141  const StringList &descriptions) {
142  lldbassert(completions.GetSize() == descriptions.GetSize());
143  for (std::size_t i = 0; i < completions.GetSize(); ++i)
144  AddCompletion(completions.GetStringAtIndex(i),
145  descriptions.GetStringAtIndex(i));
146  }
147 
148  std::size_t GetNumberOfMatches() const {
149  return m_result.GetNumberOfResults();
150  }
151 
152  llvm::StringRef GetCursorArgument() const {
153  return GetParsedLine().GetArgumentAtIndex(GetCursorIndex());
154  }
155 
156  llvm::StringRef GetCursorArgumentPrefix() const {
157  return GetCursorArgument().substr(0, GetCursorCharPosition());
158  }
159 
160 private:
161  /// The raw command line we are supposed to complete.
162  llvm::StringRef m_command;
163  /// The cursor position in m_command.
164  unsigned m_raw_cursor_pos;
165  /// The command line parsed as arguments.
166  Args m_parsed_line;
167  /// The command line until the cursor position parsed as arguments.
168  Args m_partial_parsed_line;
169  /// The index of the argument in which the completion cursor is.
170  int m_cursor_index;
171  /// The cursor position in the argument indexed by m_cursor_index.
172  int m_cursor_char_position;
173  /// If there is a match that is expensive
174  /// to compute, these are here to allow you to compute the completions in
175  /// batches. Start the completion from \amatch_start_point, and return
176  /// \amatch_return_elements elements.
177  // FIXME: These two values are not implemented.
178  int m_match_start_point;
179  int m_max_return_elements;
180  /// \btrue if this is a complete option value (a space will be inserted
181  /// after the completion.) \bfalse otherwise.
182  bool m_word_complete = false;
183 
184  /// The result this request is supposed to fill out.
185  /// We keep this object private to ensure that no backend can in any way
186  /// depend on already calculated completions (which would make debugging and
187  /// testing them much more complicated).
188  CompletionResult &m_result;
189 };
190 
191 } // namespace lldb_private
192 
193 #endif // LLDB_UTILITY_COMPLETIONREQUEST_H
llvm::StringRef GetRawLine() const
A command line argument class.
Definition: Args.h:32
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
#define lldbassert(x)
Definition: LLDBAssert.h:15
std::size_t GetNumberOfResults() const
void AddCompletions(const StringList &completions)
Adds multiple possible completion strings.
std::size_t GetNumberOfMatches() const
void AddCompletion(llvm::StringRef completion, llvm::StringRef description="")
Adds a possible completion string.
"lldb/Utility/ArgCompletionRequest.h"
llvm::StringRef GetCursorArgument() const
llvm::StringRef GetCursorArgumentPrefix() const
void AddResult(llvm::StringRef completion, llvm::StringRef description)
void AddCompletions(const StringList &completions, const StringList &descriptions)
Adds multiple possible completion strings alongside their descriptions.
const char * GetStringAtIndex(size_t idx) const
Definition: StringList.cpp:82
const Args & GetPartialParsedLine() const
void GetMatches(StringList &matches) const
Adds all collected completion matches to the given list.
size_t GetSize() const
Definition: StringList.cpp:70
void GetDescriptions(StringList &descriptions) const
Adds all collected completion descriptions to the given list.
const Args & GetParsedLine() const