1 //===-- CompletionRequest.h -------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLDB_UTILITY_COMPLETIONREQUEST_H
10 #define LLDB_UTILITY_COMPLETIONREQUEST_H
12 #include "lldb/Utility/Args.h"
13 #include "lldb/Utility/LLDBAssert.h"
14 #include "lldb/Utility/StringList.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/ADT/StringSet.h"
18 namespace lldb_private {
19 class CompletionResult {
20 /// A single completion and all associated data.
22 Completion(llvm::StringRef completion, llvm::StringRef description)
23 : m_completion(completion.str()), m_descripton(description.str()) {}
25 std::string m_completion;
26 std::string m_descripton;
28 /// Generates a string that uniquely identifies this completion result.
29 std::string GetUniqueKey() const;
31 std::vector<Completion> m_results;
33 /// List of added completions so far. Used to filter out duplicates.
34 llvm::StringSet<> m_added_values;
37 void AddResult(llvm::StringRef completion, llvm::StringRef description);
39 /// Adds all collected completion matches to the given list.
40 /// The list will be cleared before the results are added. The number of
41 /// results here is guaranteed to be equal to GetNumberOfResults().
42 void GetMatches(StringList &matches) const;
44 /// Adds all collected completion descriptions to the given list.
45 /// The list will be cleared before the results are added. The number of
46 /// results here is guaranteed to be equal to GetNumberOfResults().
47 void GetDescriptions(StringList &descriptions) const;
49 std::size_t GetNumberOfResults() const { return m_results.size(); }
52 /// \class CompletionRequest CompletionRequest.h
53 /// "lldb/Utility/ArgCompletionRequest.h"
55 /// Contains all information necessary to complete an incomplete command
56 /// for the user. Will be filled with the generated completions by the different
57 /// completions functions.
59 class CompletionRequest {
61 /// Constructs a completion request.
63 /// \param [in] command_line
64 /// The command line the user has typed at this point.
66 /// \param [in] raw_cursor_pos
67 /// The position of the cursor in the command line string. Index 0 means
68 /// the cursor is at the start of the line. The completion starts from
69 /// this cursor position.
71 /// \param [in] match_start_point
72 /// \param [in] max_return_elements
73 /// If there is a match that is expensive to compute, these are here to
74 /// allow you to compute the completions in batches. Start the
75 /// completion from match_start_point, and return match_return_elements
78 /// \param [out] result
79 /// The CompletionResult that will be filled with the results after this
80 /// request has been handled.
81 CompletionRequest(llvm::StringRef command_line, unsigned raw_cursor_pos,
82 int match_start_point, int max_return_elements,
83 CompletionResult &result);
85 llvm::StringRef GetRawLine() const { return m_command; }
87 unsigned GetRawCursorPos() const { return m_raw_cursor_pos; }
89 const Args &GetParsedLine() const { return m_parsed_line; }
91 Args &GetParsedLine() { return m_parsed_line; }
93 const Args &GetPartialParsedLine() const { return m_partial_parsed_line; }
95 void SetCursorIndex(int i) { m_cursor_index = i; }
96 int GetCursorIndex() const { return m_cursor_index; }
98 void SetCursorCharPosition(int pos) { m_cursor_char_position = pos; }
99 int GetCursorCharPosition() const { return m_cursor_char_position; }
101 int GetMatchStartPoint() const { return m_match_start_point; }
103 int GetMaxReturnElements() const { return m_max_return_elements; }
105 bool GetWordComplete() { return m_word_complete; }
107 void SetWordComplete(bool v) { m_word_complete = v; }
109 /// Adds a possible completion string. If the completion was already
110 /// suggested before, it will not be added to the list of results. A copy of
111 /// the suggested completion is stored, so the given string can be free'd
114 /// \param match The suggested completion.
115 /// \param match An optional description of the completion string. The
116 /// description will be displayed to the user alongside the completion.
117 void AddCompletion(llvm::StringRef completion,
118 llvm::StringRef description = "") {
119 m_result.AddResult(completion, description);
122 /// Adds multiple possible completion strings.
124 /// \param completions The list of completions.
126 /// \see AddCompletion
127 void AddCompletions(const StringList &completions) {
128 for (std::size_t i = 0; i < completions.GetSize(); ++i)
129 AddCompletion(completions.GetStringAtIndex(i));
132 /// Adds multiple possible completion strings alongside their descriptions.
134 /// The number of completions and descriptions must be identical.
136 /// \param completions The list of completions.
137 /// \param completions The list of descriptions.
139 /// \see AddCompletion
140 void AddCompletions(const StringList &completions,
141 const StringList &descriptions) {
142 lldbassert(completions.GetSize() == descriptions.GetSize());
143 for (std::size_t i = 0; i < completions.GetSize(); ++i)
144 AddCompletion(completions.GetStringAtIndex(i),
145 descriptions.GetStringAtIndex(i));
148 std::size_t GetNumberOfMatches() const {
149 return m_result.GetNumberOfResults();
152 llvm::StringRef GetCursorArgument() const {
153 return GetParsedLine().GetArgumentAtIndex(GetCursorIndex());
156 llvm::StringRef GetCursorArgumentPrefix() const {
157 return GetCursorArgument().substr(0, GetCursorCharPosition());
161 /// The raw command line we are supposed to complete.
162 llvm::StringRef m_command;
163 /// The cursor position in m_command.
164 unsigned m_raw_cursor_pos;
165 /// The command line parsed as arguments.
167 /// The command line until the cursor position parsed as arguments.
168 Args m_partial_parsed_line;
169 /// The index of the argument in which the completion cursor is.
171 /// The cursor position in the argument indexed by m_cursor_index.
172 int m_cursor_char_position;
173 /// If there is a match that is expensive
174 /// to compute, these are here to allow you to compute the completions in
175 /// batches. Start the completion from \amatch_start_point, and return
176 /// \amatch_return_elements elements.
177 // FIXME: These two values are not implemented.
178 int m_match_start_point;
179 int m_max_return_elements;
180 /// \btrue if this is a complete option value (a space will be inserted
181 /// after the completion.) \bfalse otherwise.
182 bool m_word_complete = false;
184 /// The result this request is supposed to fill out.
185 /// We keep this object private to ensure that no backend can in any way
186 /// depend on already calculated completions (which would make debugging and
187 /// testing them much more complicated).
188 CompletionResult &m_result;
191 } // namespace lldb_private
193 #endif // LLDB_UTILITY_COMPLETIONREQUEST_H