1*80814287SRaphael Isemann //===-- CompletionRequest.cpp ---------------------------------------------===//
22443bbd4SRaphael Isemann //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
62443bbd4SRaphael Isemann //
72443bbd4SRaphael Isemann //===----------------------------------------------------------------------===//
82443bbd4SRaphael Isemann 
92443bbd4SRaphael Isemann #include "lldb/Utility/CompletionRequest.h"
102443bbd4SRaphael Isemann 
112443bbd4SRaphael Isemann using namespace lldb;
122443bbd4SRaphael Isemann using namespace lldb_private;
132443bbd4SRaphael Isemann 
CompletionRequest(llvm::StringRef command_line,unsigned raw_cursor_pos,CompletionResult & result)14a2e76c0bSRaphael Isemann CompletionRequest::CompletionRequest(llvm::StringRef command_line,
15a2e76c0bSRaphael Isemann                                      unsigned raw_cursor_pos,
167f88829cSRaphael Isemann                                      CompletionResult &result)
17a2e76c0bSRaphael Isemann     : m_command(command_line), m_raw_cursor_pos(raw_cursor_pos),
18b22860daSJonas Devlieghere       m_result(result) {
1919351b24SRaphael Isemann   assert(raw_cursor_pos <= command_line.size() && "Out of bounds cursor?");
20a2e76c0bSRaphael Isemann 
21a2e76c0bSRaphael Isemann   // We parse the argument up to the cursor, so the last argument in
22a2e76c0bSRaphael Isemann   // parsed_line is the one containing the cursor, and the cursor is after the
23a2e76c0bSRaphael Isemann   // last character.
2414f6465cSRaphael Isemann   llvm::StringRef partial_command(command_line.substr(0, raw_cursor_pos));
25ef06dd43SRaphael Isemann   m_parsed_line = Args(partial_command);
26a2e76c0bSRaphael Isemann 
27ef06dd43SRaphael Isemann   if (GetParsedLine().GetArgumentCount() == 0) {
2814f6465cSRaphael Isemann     m_cursor_index = 0;
29a2e76c0bSRaphael Isemann     m_cursor_char_position = 0;
3014f6465cSRaphael Isemann   } else {
31ef06dd43SRaphael Isemann     m_cursor_index = GetParsedLine().GetArgumentCount() - 1U;
32a2e76c0bSRaphael Isemann     m_cursor_char_position =
33ef06dd43SRaphael Isemann         strlen(GetParsedLine().GetArgumentAtIndex(m_cursor_index));
3414f6465cSRaphael Isemann   }
35a2e76c0bSRaphael Isemann 
3614f6465cSRaphael Isemann   // The cursor is after a space but the space is not part of the argument.
3714f6465cSRaphael Isemann   // Let's add an empty fake argument to the end to make sure the completion
381e315586SRaphael Isemann   // code. Note: The space could be part of the last argument when it's quoted.
3914f6465cSRaphael Isemann   if (partial_command.endswith(" ") &&
401e315586SRaphael Isemann       !GetCursorArgumentPrefix().endswith(" "))
411e315586SRaphael Isemann     AppendEmptyArgument();
42a2e76c0bSRaphael Isemann }
437f88829cSRaphael Isemann 
GetUniqueKey() const447f88829cSRaphael Isemann std::string CompletionResult::Completion::GetUniqueKey() const {
457f88829cSRaphael Isemann 
467f88829cSRaphael Isemann   // We build a unique key for this pair of completion:description. We
477f88829cSRaphael Isemann   // prefix the key with the length of the completion string. This prevents
487f88829cSRaphael Isemann   // that we could get any collisions from completions pairs such as these:
497f88829cSRaphael Isemann   // "foo:", "bar" would be "foo:bar", but will now be: "4foo:bar"
507f88829cSRaphael Isemann   // "foo", ":bar" would be "foo:bar", but will now be: "3foo:bar"
517f88829cSRaphael Isemann 
527f88829cSRaphael Isemann   std::string result;
537f88829cSRaphael Isemann   result.append(std::to_string(m_completion.size()));
547f88829cSRaphael Isemann   result.append(m_completion);
55ae34ed2cSRaphael Isemann   result.append(std::to_string(static_cast<int>(m_mode)));
56ae34ed2cSRaphael Isemann   result.append(":");
577f88829cSRaphael Isemann   result.append(m_descripton);
587f88829cSRaphael Isemann   return result;
597f88829cSRaphael Isemann }
607f88829cSRaphael Isemann 
AddResult(llvm::StringRef completion,llvm::StringRef description,CompletionMode mode)617f88829cSRaphael Isemann void CompletionResult::AddResult(llvm::StringRef completion,
62ae34ed2cSRaphael Isemann                                  llvm::StringRef description,
63ae34ed2cSRaphael Isemann                                  CompletionMode mode) {
64ae34ed2cSRaphael Isemann   Completion r(completion, description, mode);
657f88829cSRaphael Isemann 
667f88829cSRaphael Isemann   // Add the completion if we haven't seen the same value before.
677f88829cSRaphael Isemann   if (m_added_values.insert(r.GetUniqueKey()).second)
687f88829cSRaphael Isemann     m_results.push_back(r);
697f88829cSRaphael Isemann }
707f88829cSRaphael Isemann 
GetMatches(StringList & matches) const717f88829cSRaphael Isemann void CompletionResult::GetMatches(StringList &matches) const {
727f88829cSRaphael Isemann   matches.Clear();
737f88829cSRaphael Isemann   for (const Completion &completion : m_results)
74ae34ed2cSRaphael Isemann     matches.AppendString(completion.GetCompletion());
757f88829cSRaphael Isemann }
767f88829cSRaphael Isemann 
GetDescriptions(StringList & descriptions) const777f88829cSRaphael Isemann void CompletionResult::GetDescriptions(StringList &descriptions) const {
787f88829cSRaphael Isemann   descriptions.Clear();
797f88829cSRaphael Isemann   for (const Completion &completion : m_results)
80ae34ed2cSRaphael Isemann     descriptions.AppendString(completion.GetDescription());
817f88829cSRaphael Isemann }
82