Skip to content

Commit 1b45ab6

Browse files
committed
[lldb] returning command completions up to a maximum
- Adding `max_return_elements` field to `CompletionRequest`. - adding maximum checks to `SymbolCompleter` and `SourceFileCompleter`.
1 parent 87322c9 commit 1b45ab6

File tree

4 files changed

+62
-5
lines changed

4 files changed

+62
-5
lines changed

lldb/include/lldb/Utility/CompletionRequest.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,25 @@ class CompletionRequest {
115115
CompletionRequest(llvm::StringRef command_line, unsigned raw_cursor_pos,
116116
CompletionResult &result);
117117

118+
/// Constructs a completion request.
119+
///
120+
/// \param [in] command_line
121+
/// The command line the user has typed at this point.
122+
///
123+
/// \param [in] raw_cursor_pos
124+
/// The position of the cursor in the command line string. Index 0 means
125+
/// the cursor is at the start of the line. The completion starts from
126+
/// this cursor position.
127+
///
128+
/// \param [in] max_return_elements
129+
/// The maximum number of completions that should be returned.
130+
///
131+
/// \param [out] result
132+
/// The CompletionResult that will be filled with the results after this
133+
/// request has been handled.
134+
CompletionRequest(llvm::StringRef command_line, unsigned raw_cursor_pos,
135+
size_t max_return_elements, CompletionResult &result);
136+
118137
/// Returns the raw user input used to create this CompletionRequest cut off
119138
/// at the cursor position. The cursor will be at the end of the raw line.
120139
llvm::StringRef GetRawLine() const {
@@ -157,6 +176,23 @@ class CompletionRequest {
157176

158177
size_t GetCursorIndex() const { return m_cursor_index; }
159178

179+
size_t GetMaxReturnElements() const { return m_max_return_elements; }
180+
181+
/// Returns true if the maximum number of completions has been reached
182+
/// already.
183+
bool ShouldStopAddingResults() const {
184+
return m_result.GetNumberOfResults() >= m_max_return_elements;
185+
}
186+
187+
/// Returns the maximum number of completions that need to be added
188+
/// until reaching the maximum
189+
size_t GetMaxNumberOfResultsToAdd() const {
190+
const size_t number_of_results = m_result.GetNumberOfResults();
191+
if (number_of_results >= m_max_return_elements)
192+
return 0;
193+
return m_max_return_elements - number_of_results;
194+
}
195+
160196
/// Adds a possible completion string. If the completion was already
161197
/// suggested before, it will not be added to the list of results. A copy of
162198
/// the suggested completion is stored, so the given string can be free'd
@@ -231,6 +267,8 @@ class CompletionRequest {
231267
size_t m_cursor_index;
232268
/// The cursor position in the argument indexed by m_cursor_index.
233269
size_t m_cursor_char_position;
270+
/// The maximum number of completions that should be returned.
271+
size_t m_max_return_elements;
234272

235273
/// The result this request is supposed to fill out.
236274
/// We keep this object private to ensure that no backend can in any way

lldb/source/API/SBCommandInterpreter.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,13 @@ int SBCommandInterpreter::HandleCompletionWithDescriptions(
263263
if (!IsValid())
264264
return 0;
265265

266+
if (0 >= max_return_elements)
267+
return 0;
268+
266269
lldb_private::StringList lldb_matches, lldb_descriptions;
267270
CompletionResult result;
268-
CompletionRequest request(current_line, cursor - current_line, result);
271+
CompletionRequest request(current_line, cursor - current_line,
272+
static_cast<size_t>(max_return_elements), result);
269273
m_opaque_ptr->HandleCompletion(request);
270274
result.GetMatches(lldb_matches);
271275
result.GetDescriptions(lldb_descriptions);

lldb/source/Commands/CommandCompletions.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ bool CommandCompletions::InvokeCommonCompletionCallbacks(
9191
nullptr} // This one has to be last in the list.
9292
};
9393

94-
for (int i = 0;; i++) {
94+
for (int i = 0; !request.ShouldStopAddingResults(); i++) {
9595
if (common_completions[i].type == lldb::eTerminatorCompletion)
9696
break;
9797
else if ((common_completions[i].type & completion_mask) ==
@@ -167,7 +167,9 @@ class SourceFileCompleter : public Completer {
167167
m_matching_files.AppendIfUnique(context.comp_unit->GetPrimaryFile());
168168
}
169169
}
170-
return Searcher::eCallbackReturnContinue;
170+
return m_matching_files.GetSize() >= m_request.GetMaxNumberOfResultsToAdd()
171+
? Searcher::eCallbackReturnStop
172+
: Searcher::eCallbackReturnContinue;
171173
}
172174

173175
void DoCompletion(SearchFilter *filter) override {
@@ -230,6 +232,10 @@ class SymbolCompleter : public Completer {
230232

231233
// Now add the functions & symbols to the list - only add if unique:
232234
for (const SymbolContext &sc : sc_list) {
235+
if (m_match_set.size() >= m_request.GetMaxNumberOfResultsToAdd()) {
236+
break;
237+
}
238+
233239
ConstString func_name = sc.GetFunctionName(Mangled::ePreferDemangled);
234240
// Ensure that the function name matches the regex. This is more than
235241
// a sanity check. It is possible that the demangled function name
@@ -239,7 +245,9 @@ class SymbolCompleter : public Completer {
239245
m_match_set.insert(func_name);
240246
}
241247
}
242-
return Searcher::eCallbackReturnContinue;
248+
return m_match_set.size() >= m_request.GetMaxNumberOfResultsToAdd()
249+
? Searcher::eCallbackReturnStop
250+
: Searcher::eCallbackReturnContinue;
243251
}
244252

245253
void DoCompletion(SearchFilter *filter) override {

lldb/source/Utility/CompletionRequest.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@ using namespace lldb_private;
1414
CompletionRequest::CompletionRequest(llvm::StringRef command_line,
1515
unsigned raw_cursor_pos,
1616
CompletionResult &result)
17+
: CompletionRequest(std::move(command_line), raw_cursor_pos, SIZE_MAX,
18+
result) {}
19+
20+
CompletionRequest::CompletionRequest(llvm::StringRef command_line,
21+
unsigned raw_cursor_pos,
22+
size_t max_return_elements,
23+
CompletionResult &result)
1724
: m_command(command_line), m_raw_cursor_pos(raw_cursor_pos),
18-
m_result(result) {
25+
m_max_return_elements(max_return_elements), m_result(result) {
1926
assert(raw_cursor_pos <= command_line.size() && "Out of bounds cursor?");
2027

2128
// We parse the argument up to the cursor, so the last argument in

0 commit comments

Comments
 (0)