]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Utility/CompletionRequest.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Utility / CompletionRequest.cpp
1 //===-- CompletionRequest.cpp -----------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/Utility/CompletionRequest.h"
11
12 using namespace lldb;
13 using namespace lldb_private;
14
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) {
23
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
26   // last character.
27   m_parsed_line = Args(command_line);
28   m_partial_parsed_line = Args(command_line.substr(0, raw_cursor_pos));
29
30   m_cursor_index = m_partial_parsed_line.GetArgumentCount() - 1;
31
32   if (m_cursor_index == -1)
33     m_cursor_char_position = 0;
34   else
35     m_cursor_char_position =
36         strlen(m_partial_parsed_line.GetArgumentAtIndex(m_cursor_index));
37
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
45     // argument...
46
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(),
52                                           '\0');
53       m_cursor_index++;
54       m_cursor_char_position = 0;
55     }
56   }
57 }
58
59 std::string CompletionResult::Completion::GetUniqueKey() const {
60
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"
66
67   std::string result;
68   result.append(std::to_string(m_completion.size()));
69   result.append(m_completion);
70   result.append(m_descripton);
71   return result;
72 }
73
74 void CompletionResult::AddResult(llvm::StringRef completion,
75                                  llvm::StringRef description) {
76   Completion r(completion, description);
77
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);
81 }
82
83 void CompletionResult::GetMatches(StringList &matches) const {
84   matches.Clear();
85   for (const Completion &completion : m_results)
86     matches.AppendString(completion.m_completion);
87 }
88
89 void CompletionResult::GetDescriptions(StringList &descriptions) const {
90   descriptions.Clear();
91   for (const Completion &completion : m_results)
92     descriptions.AppendString(completion.m_descripton);
93 }