Path: blob/main/contrib/llvm-project/lldb/source/Utility/CompletionRequest.cpp
39587 views
//===-- CompletionRequest.cpp ---------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/Utility/CompletionRequest.h"910using namespace lldb;11using namespace lldb_private;1213CompletionRequest::CompletionRequest(llvm::StringRef command_line,14unsigned raw_cursor_pos,15CompletionResult &result)16: m_command(command_line), m_raw_cursor_pos(raw_cursor_pos),17m_result(result) {18assert(raw_cursor_pos <= command_line.size() && "Out of bounds cursor?");1920// We parse the argument up to the cursor, so the last argument in21// parsed_line is the one containing the cursor, and the cursor is after the22// last character.23llvm::StringRef partial_command(command_line.substr(0, raw_cursor_pos));24m_parsed_line = Args(partial_command);2526if (GetParsedLine().GetArgumentCount() == 0) {27m_cursor_index = 0;28m_cursor_char_position = 0;29} else {30m_cursor_index = GetParsedLine().GetArgumentCount() - 1U;31m_cursor_char_position =32strlen(GetParsedLine().GetArgumentAtIndex(m_cursor_index));33}3435// The cursor is after a space but the space is not part of the argument.36// Let's add an empty fake argument to the end to make sure the completion37// code. Note: The space could be part of the last argument when it's quoted.38if (partial_command.ends_with(" ") &&39!GetCursorArgumentPrefix().ends_with(" "))40AppendEmptyArgument();41}4243std::string CompletionResult::Completion::GetUniqueKey() const {4445// We build a unique key for this pair of completion:description. We46// prefix the key with the length of the completion string. This prevents47// that we could get any collisions from completions pairs such as these:48// "foo:", "bar" would be "foo:bar", but will now be: "4foo:bar"49// "foo", ":bar" would be "foo:bar", but will now be: "3foo:bar"5051std::string result;52result.append(std::to_string(m_completion.size()));53result.append(m_completion);54result.append(std::to_string(static_cast<int>(m_mode)));55result.append(":");56result.append(m_descripton);57return result;58}5960void CompletionResult::AddResult(llvm::StringRef completion,61llvm::StringRef description,62CompletionMode mode) {63Completion r(completion, description, mode);6465// Add the completion if we haven't seen the same value before.66if (m_added_values.insert(r.GetUniqueKey()).second)67m_results.push_back(r);68}6970void CompletionResult::GetMatches(StringList &matches) const {71matches.Clear();72for (const Completion &completion : m_results)73matches.AppendString(completion.GetCompletion());74}7576void CompletionResult::GetDescriptions(StringList &descriptions) const {77descriptions.Clear();78for (const Completion &completion : m_results)79descriptions.AppendString(completion.GetDescription());80}818283