1 //===-- CompletionRequest.cpp -----------------------------------*- 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 #include "lldb/Utility/CompletionRequest.h"
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;
29     m_cursor_char_position = 0;
30   } else {
31     m_cursor_index = GetParsedLine().GetArgumentCount() - 1U;
32     m_cursor_char_position =
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(" ")) {
41     m_parsed_line.AppendArgument(llvm::StringRef());
42     // Set the cursor to the start of the fake argument.
43     m_cursor_index++;
44     m_cursor_char_position = 0;
45   }
46 }
47 
48 std::string CompletionResult::Completion::GetUniqueKey() const {
49 
50   // We build a unique key for this pair of completion:description. We
51   // prefix the key with the length of the completion string. This prevents
52   // that we could get any collisions from completions pairs such as these:
53   // "foo:", "bar" would be "foo:bar", but will now be: "4foo:bar"
54   // "foo", ":bar" would be "foo:bar", but will now be: "3foo:bar"
55 
56   std::string result;
57   result.append(std::to_string(m_completion.size()));
58   result.append(m_completion);
59   result.append(std::to_string(static_cast<int>(m_mode)));
60   result.append(":");
61   result.append(m_descripton);
62   return result;
63 }
64 
65 void CompletionResult::AddResult(llvm::StringRef completion,
66                                  llvm::StringRef description,
67                                  CompletionMode mode) {
68   Completion r(completion, description, mode);
69 
70   // Add the completion if we haven't seen the same value before.
71   if (m_added_values.insert(r.GetUniqueKey()).second)
72     m_results.push_back(r);
73 }
74 
75 void CompletionResult::GetMatches(StringList &matches) const {
76   matches.Clear();
77   for (const Completion &completion : m_results)
78     matches.AppendString(completion.GetCompletion());
79 }
80 
81 void CompletionResult::GetDescriptions(StringList &descriptions) const {
82   descriptions.Clear();
83   for (const Completion &completion : m_results)
84     descriptions.AppendString(completion.GetDescription());
85 }
86