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