1 //===-- CompletionRequest.cpp -----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Utility/CompletionRequest.h"
13 using namespace lldb_private;
15 CompletionRequest::CompletionRequest(llvm::StringRef command_line,
16 unsigned raw_cursor_pos,
17 int match_start_point,
18 int max_return_elements,
19 CompletionResult &result)
20 : m_command(command_line), m_raw_cursor_pos(raw_cursor_pos),
21 m_match_start_point(match_start_point),
22 m_max_return_elements(max_return_elements), m_result(result) {
24 // We parse the argument up to the cursor, so the last argument in
25 // parsed_line is the one containing the cursor, and the cursor is after the
27 m_parsed_line = Args(command_line);
28 m_partial_parsed_line = Args(command_line.substr(0, raw_cursor_pos));
30 m_cursor_index = m_partial_parsed_line.GetArgumentCount() - 1;
32 if (m_cursor_index == -1)
33 m_cursor_char_position = 0;
35 m_cursor_char_position =
36 strlen(m_partial_parsed_line.GetArgumentAtIndex(m_cursor_index));
38 const char *cursor = command_line.data() + raw_cursor_pos;
39 if (raw_cursor_pos > 0 && cursor[-1] == ' ') {
40 // We are just after a space. If we are in an argument, then we will
41 // continue parsing, but if we are between arguments, then we have to
42 // complete whatever the next element would be. We can distinguish the two
43 // cases because if we are in an argument (e.g. because the space is
44 // protected by a quote) then the space will also be in the parsed
47 const char *current_elem =
48 m_partial_parsed_line.GetArgumentAtIndex(m_cursor_index);
49 if (m_cursor_char_position == 0 ||
50 current_elem[m_cursor_char_position - 1] != ' ') {
51 m_parsed_line.InsertArgumentAtIndex(m_cursor_index + 1, llvm::StringRef(),
54 m_cursor_char_position = 0;
59 std::string CompletionResult::Completion::GetUniqueKey() const {
61 // We build a unique key for this pair of completion:description. We
62 // prefix the key with the length of the completion string. This prevents
63 // that we could get any collisions from completions pairs such as these:
64 // "foo:", "bar" would be "foo:bar", but will now be: "4foo:bar"
65 // "foo", ":bar" would be "foo:bar", but will now be: "3foo:bar"
68 result.append(std::to_string(m_completion.size()));
69 result.append(m_completion);
70 result.append(m_descripton);
74 void CompletionResult::AddResult(llvm::StringRef completion,
75 llvm::StringRef description) {
76 Completion r(completion, description);
78 // Add the completion if we haven't seen the same value before.
79 if (m_added_values.insert(r.GetUniqueKey()).second)
80 m_results.push_back(r);
83 void CompletionResult::GetMatches(StringList &matches) const {
85 for (const Completion &completion : m_results)
86 matches.AppendString(completion.m_completion);
89 void CompletionResult::GetDescriptions(StringList &descriptions) const {
91 for (const Completion &completion : m_results)
92 descriptions.AppendString(completion.m_descripton);