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 llvm::StringRef partial_command(command_line.substr(0, raw_cursor_pos)); 26 m_partial_parsed_line = Args(partial_command); 27 28 if (m_partial_parsed_line.GetArgumentCount() == 0) { 29 m_cursor_index = 0; 30 m_cursor_char_position = 0; 31 } else { 32 m_cursor_index = m_partial_parsed_line.GetArgumentCount() - 1U; 33 m_cursor_char_position = 34 strlen(m_partial_parsed_line.GetArgumentAtIndex(m_cursor_index)); 35 } 36 37 // The cursor is after a space but the space is not part of the argument. 38 // Let's add an empty fake argument to the end to make sure the completion 39 // code Note: The space could be part of the last argument when it's quoted. 40 if (partial_command.endswith(" ") && 41 !GetCursorArgumentPrefix().endswith(" ")) { 42 m_parsed_line.AppendArgument(llvm::StringRef()); 43 m_partial_parsed_line.AppendArgument(llvm::StringRef()); 44 // Set the cursor to the start of the fake argument. 45 m_cursor_index++; 46 m_cursor_char_position = 0; 47 } 48 } 49 50 std::string CompletionResult::Completion::GetUniqueKey() const { 51 52 // We build a unique key for this pair of completion:description. We 53 // prefix the key with the length of the completion string. This prevents 54 // that we could get any collisions from completions pairs such as these: 55 // "foo:", "bar" would be "foo:bar", but will now be: "4foo:bar" 56 // "foo", ":bar" would be "foo:bar", but will now be: "3foo:bar" 57 58 std::string result; 59 result.append(std::to_string(m_completion.size())); 60 result.append(m_completion); 61 result.append(std::to_string(static_cast<int>(m_mode))); 62 result.append(":"); 63 result.append(m_descripton); 64 return result; 65 } 66 67 void CompletionResult::AddResult(llvm::StringRef completion, 68 llvm::StringRef description, 69 CompletionMode mode) { 70 Completion r(completion, description, mode); 71 72 // Add the completion if we haven't seen the same value before. 73 if (m_added_values.insert(r.GetUniqueKey()).second) 74 m_results.push_back(r); 75 } 76 77 void CompletionResult::GetMatches(StringList &matches) const { 78 matches.Clear(); 79 for (const Completion &completion : m_results) 80 matches.AppendString(completion.GetCompletion()); 81 } 82 83 void CompletionResult::GetDescriptions(StringList &descriptions) const { 84 descriptions.Clear(); 85 for (const Completion &completion : m_results) 86 descriptions.AppendString(completion.GetDescription()); 87 } 88