LLDB  mainline
CompletionRequest.cpp
Go to the documentation of this file.
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 
10 
11 using namespace lldb;
12 using namespace lldb_private;
13 
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) {
22 
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
25  // last character.
26  m_parsed_line = Args(command_line);
27  m_partial_parsed_line = Args(command_line.substr(0, raw_cursor_pos));
28 
29  m_cursor_index = m_partial_parsed_line.GetArgumentCount() - 1;
30 
31  if (m_cursor_index == -1)
32  m_cursor_char_position = 0;
33  else
34  m_cursor_char_position =
35  strlen(m_partial_parsed_line.GetArgumentAtIndex(m_cursor_index));
36 
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
44  // argument...
45 
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(),
51  '\0');
52  m_cursor_index++;
53  m_cursor_char_position = 0;
54  }
55  }
56 }
57 
58 std::string CompletionResult::Completion::GetUniqueKey() const {
59 
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"
65 
66  std::string result;
67  result.append(std::to_string(m_completion.size()));
68  result.append(m_completion);
69  result.append(m_descripton);
70  return result;
71 }
72 
73 void CompletionResult::AddResult(llvm::StringRef completion,
74  llvm::StringRef description) {
75  Completion r(completion, description);
76 
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);
80 }
81 
83  matches.Clear();
84  for (const Completion &completion : m_results)
85  matches.AppendString(completion.m_completion);
86 }
87 
88 void CompletionResult::GetDescriptions(StringList &descriptions) const {
89  descriptions.Clear();
90  for (const Completion &completion : m_results)
91  descriptions.AppendString(completion.m_descripton);
92 }
A command line argument class.
Definition: Args.h:32
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
size_t GetArgumentCount() const
Gets the number of arguments left in this command object.
Definition: Args.cpp:254
const char * GetArgumentAtIndex(size_t idx) const
Gets the NULL terminated C string argument pointer for the argument at index idx. ...
Definition: Args.cpp:256
void InsertArgumentAtIndex(size_t idx, llvm::StringRef arg_str, char quote_char='\0')
Insert the argument value at index idx to arg_cstr.
Definition: Args.cpp:325
void AddResult(llvm::StringRef completion, llvm::StringRef description)
void GetMatches(StringList &matches) const
Adds all collected completion matches to the given list.
void GetDescriptions(StringList &descriptions) const
Adds all collected completion descriptions to the given list.
Definition: SBAddress.h:15
void AppendString(const std::string &s)
Definition: StringList.cpp:43