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 m_parsed_line = Args(command_line); 25 m_partial_parsed_line = Args(command_line.substr(0, raw_cursor_pos)); 26 27 m_cursor_index = m_partial_parsed_line.GetArgumentCount() - 1; 28 29 if (m_cursor_index == -1) 30 m_cursor_char_position = 0; 31 else 32 m_cursor_char_position = 33 strlen(m_partial_parsed_line.GetArgumentAtIndex(m_cursor_index)); 34 35 const char *cursor = command_line.data() + raw_cursor_pos; 36 if (raw_cursor_pos > 0 && cursor[-1] == ' ') { 37 // We are just after a space. If we are in an argument, then we will 38 // continue parsing, but if we are between arguments, then we have to 39 // complete whatever the next element would be. We can distinguish the two 40 // cases because if we are in an argument (e.g. because the space is 41 // protected by a quote) then the space will also be in the parsed 42 // argument... 43 44 const char *current_elem = 45 m_partial_parsed_line.GetArgumentAtIndex(m_cursor_index); 46 if (m_cursor_char_position == 0 || 47 current_elem[m_cursor_char_position - 1] != ' ') { 48 m_parsed_line.InsertArgumentAtIndex(m_cursor_index + 1, llvm::StringRef(), 49 '\0'); 50 m_cursor_index++; 51 m_cursor_char_position = 0; 52 } 53 } 54 } 55 56 std::string CompletionResult::Completion::GetUniqueKey() const { 57 58 // We build a unique key for this pair of completion:description. We 59 // prefix the key with the length of the completion string. This prevents 60 // that we could get any collisions from completions pairs such as these: 61 // "foo:", "bar" would be "foo:bar", but will now be: "4foo:bar" 62 // "foo", ":bar" would be "foo:bar", but will now be: "3foo:bar" 63 64 std::string result; 65 result.append(std::to_string(m_completion.size())); 66 result.append(m_completion); 67 result.append(std::to_string(static_cast<int>(m_mode))); 68 result.append(":"); 69 result.append(m_descripton); 70 return result; 71 } 72 73 void CompletionResult::AddResult(llvm::StringRef completion, 74 llvm::StringRef description, 75 CompletionMode mode) { 76 Completion r(completion, description, mode); 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.GetCompletion()); 87 } 88 89 void CompletionResult::GetDescriptions(StringList &descriptions) const { 90 descriptions.Clear(); 91 for (const Completion &completion : m_results) 92 descriptions.AppendString(completion.GetDescription()); 93 } 94