1 //===-- CompletionRequest.h -------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLDB_UTILITY_COMPLETIONREQUEST_H
11 #define LLDB_UTILITY_COMPLETIONREQUEST_H
13 #include "lldb/Utility/Args.h"
14 #include "lldb/Utility/LLDBAssert.h"
15 #include "lldb/Utility/StringList.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/ADT/StringSet.h"
19 namespace lldb_private {
20 class CompletionResult {
21 //----------------------------------------------------------
22 /// A single completion and all associated data.
23 //----------------------------------------------------------
25 Completion(llvm::StringRef completion, llvm::StringRef description)
26 : m_completion(completion.str()), m_descripton(description.str()) {}
28 std::string m_completion;
29 std::string m_descripton;
31 /// Generates a string that uniquely identifies this completion result.
32 std::string GetUniqueKey() const;
34 std::vector<Completion> m_results;
36 /// List of added completions so far. Used to filter out duplicates.
37 llvm::StringSet<> m_added_values;
40 void AddResult(llvm::StringRef completion, llvm::StringRef description);
42 //----------------------------------------------------------
43 /// Adds all collected completion matches to the given list.
44 /// The list will be cleared before the results are added. The number of
45 /// results here is guaranteed to be equal to GetNumberOfResults().
46 //----------------------------------------------------------
47 void GetMatches(StringList &matches) const;
49 //----------------------------------------------------------
50 /// Adds all collected completion descriptions to the given list.
51 /// The list will be cleared before the results are added. The number of
52 /// results here is guaranteed to be equal to GetNumberOfResults().
53 //----------------------------------------------------------
54 void GetDescriptions(StringList &descriptions) const;
56 std::size_t GetNumberOfResults() const { return m_results.size(); }
59 //----------------------------------------------------------------------
60 /// @class CompletionRequest CompletionRequest.h
61 /// "lldb/Utility/ArgCompletionRequest.h"
63 /// Contains all information necessary to complete an incomplete command
64 /// for the user. Will be filled with the generated completions by the different
65 /// completions functions.
67 //----------------------------------------------------------------------
68 class CompletionRequest {
70 //----------------------------------------------------------
71 /// Constructs a completion request.
73 /// @param [in] command_line
74 /// The command line the user has typed at this point.
76 /// @param [in] raw_cursor_pos
77 /// The position of the cursor in the command line string. Index 0 means
78 /// the cursor is at the start of the line. The completion starts from
79 /// this cursor position.
81 /// @param [in] match_start_point
82 /// @param [in] max_return_elements
83 /// If there is a match that is expensive to compute, these are here to
84 /// allow you to compute the completions in batches. Start the
85 /// completion from match_start_point, and return match_return_elements
88 /// @param [out] result
89 /// The CompletionResult that will be filled with the results after this
90 /// request has been handled.
91 //----------------------------------------------------------
92 CompletionRequest(llvm::StringRef command_line, unsigned raw_cursor_pos,
93 int match_start_point, int max_return_elements,
94 CompletionResult &result);
96 llvm::StringRef GetRawLine() const { return m_command; }
98 unsigned GetRawCursorPos() const { return m_raw_cursor_pos; }
100 const Args &GetParsedLine() const { return m_parsed_line; }
102 Args &GetParsedLine() { return m_parsed_line; }
104 const Args &GetPartialParsedLine() const { return m_partial_parsed_line; }
106 void SetCursorIndex(int i) { m_cursor_index = i; }
107 int GetCursorIndex() const { return m_cursor_index; }
109 void SetCursorCharPosition(int pos) { m_cursor_char_position = pos; }
110 int GetCursorCharPosition() const { return m_cursor_char_position; }
112 int GetMatchStartPoint() const { return m_match_start_point; }
114 int GetMaxReturnElements() const { return m_max_return_elements; }
116 bool GetWordComplete() { return m_word_complete; }
118 void SetWordComplete(bool v) { m_word_complete = v; }
120 /// Adds a possible completion string. If the completion was already
121 /// suggested before, it will not be added to the list of results. A copy of
122 /// the suggested completion is stored, so the given string can be free'd
125 /// @param match The suggested completion.
126 /// @param match An optional description of the completion string. The
127 /// description will be displayed to the user alongside the completion.
128 void AddCompletion(llvm::StringRef completion,
129 llvm::StringRef description = "") {
130 m_result.AddResult(completion, description);
133 /// Adds multiple possible completion strings.
135 /// \param completions The list of completions.
137 /// @see AddCompletion
138 void AddCompletions(const StringList &completions) {
139 for (std::size_t i = 0; i < completions.GetSize(); ++i)
140 AddCompletion(completions.GetStringAtIndex(i));
143 /// Adds multiple possible completion strings alongside their descriptions.
145 /// The number of completions and descriptions must be identical.
147 /// \param completions The list of completions.
148 /// \param completions The list of descriptions.
150 /// @see AddCompletion
151 void AddCompletions(const StringList &completions,
152 const StringList &descriptions) {
153 lldbassert(completions.GetSize() == descriptions.GetSize());
154 for (std::size_t i = 0; i < completions.GetSize(); ++i)
155 AddCompletion(completions.GetStringAtIndex(i),
156 descriptions.GetStringAtIndex(i));
159 std::size_t GetNumberOfMatches() const {
160 return m_result.GetNumberOfResults();
163 llvm::StringRef GetCursorArgument() const {
164 return GetParsedLine().GetArgumentAtIndex(GetCursorIndex());
167 llvm::StringRef GetCursorArgumentPrefix() const {
168 return GetCursorArgument().substr(0, GetCursorCharPosition());
172 /// The raw command line we are supposed to complete.
173 llvm::StringRef m_command;
174 /// The cursor position in m_command.
175 unsigned m_raw_cursor_pos;
176 /// The command line parsed as arguments.
178 /// The command line until the cursor position parsed as arguments.
179 Args m_partial_parsed_line;
180 /// The index of the argument in which the completion cursor is.
182 /// The cursor position in the argument indexed by m_cursor_index.
183 int m_cursor_char_position;
184 /// If there is a match that is expensive
185 /// to compute, these are here to allow you to compute the completions in
186 /// batches. Start the completion from \amatch_start_point, and return
187 /// \amatch_return_elements elements.
188 // FIXME: These two values are not implemented.
189 int m_match_start_point;
190 int m_max_return_elements;
191 /// \btrue if this is a complete option value (a space will be inserted
192 /// after the completion.) \bfalse otherwise.
193 bool m_word_complete = false;
195 /// The result this request is supposed to fill out.
196 /// We keep this object private to ensure that no backend can in any way
197 /// depend on already calculated completions (which would make debugging and
198 /// testing them much more complicated).
199 CompletionResult &m_result;
202 } // namespace lldb_private
204 #endif // LLDB_UTILITY_COMPLETIONREQUEST_H