1 //===-- CompletionRequest.cpp -----------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Utility/CompletionRequest.h"
12 using namespace lldb_private;
14 CompletionRequest::CompletionRequest(llvm::StringRef command_line,
15 unsigned raw_cursor_pos,
16 int match_start_point,
17 int max_return_elements,
18 CompletionResult &result)
19 : m_command(command_line), m_raw_cursor_pos(raw_cursor_pos),
20 m_match_start_point(match_start_point),
21 m_max_return_elements(max_return_elements), m_result(result) {
23 // We parse the argument up to the cursor, so the last argument in
24 // parsed_line is the one containing the cursor, and the cursor is after the
26 m_parsed_line = Args(command_line);
27 m_partial_parsed_line = Args(command_line.substr(0, raw_cursor_pos));
29 m_cursor_index = m_partial_parsed_line.GetArgumentCount() - 1;
31 if (m_cursor_index == -1)
32 m_cursor_char_position = 0;
34 m_cursor_char_position =
35 strlen(m_partial_parsed_line.GetArgumentAtIndex(m_cursor_index));
37 const char *cursor = command_line.data() + raw_cursor_pos;
38 if (raw_cursor_pos > 0 && cursor[-1] == ' ') {
39 // We are just after a space. If we are in an argument, then we will
40 // continue parsing, but if we are between arguments, then we have to
41 // complete whatever the next element would be. We can distinguish the two
42 // cases because if we are in an argument (e.g. because the space is
43 // protected by a quote) then the space will also be in the parsed
46 const char *current_elem =
47 m_partial_parsed_line.GetArgumentAtIndex(m_cursor_index);
48 if (m_cursor_char_position == 0 ||
49 current_elem[m_cursor_char_position - 1] != ' ') {
50 m_parsed_line.InsertArgumentAtIndex(m_cursor_index + 1, llvm::StringRef(),
53 m_cursor_char_position = 0;
58 std::string CompletionResult::Completion::GetUniqueKey() const {
60 // We build a unique key for this pair of completion:description. We
61 // prefix the key with the length of the completion string. This prevents
62 // that we could get any collisions from completions pairs such as these:
63 // "foo:", "bar" would be "foo:bar", but will now be: "4foo:bar"
64 // "foo", ":bar" would be "foo:bar", but will now be: "3foo:bar"
67 result.append(std::to_string(m_completion.size()));
68 result.append(m_completion);
69 result.append(m_descripton);
73 void CompletionResult::AddResult(llvm::StringRef completion,
74 llvm::StringRef description) {
75 Completion r(completion, description);
77 // Add the completion if we haven't seen the same value before.
78 if (m_added_values.insert(r.GetUniqueKey()).second)
79 m_results.push_back(r);
82 void CompletionResult::GetMatches(StringList &matches) const {
84 for (const Completion &completion : m_results)
85 matches.AppendString(completion.m_completion);
88 void CompletionResult::GetDescriptions(StringList &descriptions) const {
90 for (const Completion &completion : m_results)
91 descriptions.AppendString(completion.m_descripton);