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/StringList.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/ADT/StringSet.h"
18 namespace lldb_private {
20 //----------------------------------------------------------------------
21 /// @class CompletionRequest CompletionRequest.h
22 /// "lldb/Utility/ArgCompletionRequest.h"
24 /// Contains all information necessary to complete an incomplete command
25 /// for the user. Will be filled with the generated completions by the different
26 /// completions functions.
28 //----------------------------------------------------------------------
29 class CompletionRequest {
31 //----------------------------------------------------------
32 /// Constructs a completion request.
34 /// @param [in] command_line
35 /// The command line the user has typed at this point.
37 /// @param [in] raw_cursor_pos
38 /// The position of the cursor in the command line string. Index 0 means
39 /// the cursor is at the start of the line. The completion starts from
40 /// this cursor position.
42 /// @param [in] match_start_point
43 /// @param [in] max_return_elements
44 /// If there is a match that is expensive to compute, these are here to
45 /// allow you to compute the completions in batches. Start the
46 /// completion from match_start_point, and return match_return_elements
49 /// @param [out] matches
50 /// A list of matches that will be filled by the different completion
52 //----------------------------------------------------------
53 CompletionRequest(llvm::StringRef command_line, unsigned raw_cursor_pos,
54 int match_start_point, int max_return_elements,
57 llvm::StringRef GetRawLine() const { return m_command; }
59 unsigned GetRawCursorPos() const { return m_raw_cursor_pos; }
61 const Args &GetParsedLine() const { return m_parsed_line; }
63 Args &GetParsedLine() { return m_parsed_line; }
65 const Args &GetPartialParsedLine() const { return m_partial_parsed_line; }
67 void SetCursorIndex(int i) { m_cursor_index = i; }
68 int GetCursorIndex() const { return m_cursor_index; }
70 void SetCursorCharPosition(int pos) { m_cursor_char_position = pos; }
71 int GetCursorCharPosition() const { return m_cursor_char_position; }
73 int GetMatchStartPoint() const { return m_match_start_point; }
75 int GetMaxReturnElements() const { return m_max_return_elements; }
77 bool GetWordComplete() { return m_word_complete; }
79 void SetWordComplete(bool v) { m_word_complete = v; }
81 /// Adds a possible completion string. If the completion was already
82 /// suggested before, it will not be added to the list of results. A copy of
83 /// the suggested completion is stored, so the given string can be free'd
86 /// @param match The suggested completion.
87 void AddCompletion(llvm::StringRef completion) {
88 // Add the completion if we haven't seen the same value before.
89 if (m_match_set.insert(completion).second)
90 m_matches->AppendString(completion);
93 /// Adds multiple possible completion strings.
95 /// \param completions The list of completions.
97 /// @see AddCompletion
98 void AddCompletions(const StringList &completions) {
99 for (std::size_t i = 0; i < completions.GetSize(); ++i)
100 AddCompletion(completions.GetStringAtIndex(i));
103 std::size_t GetNumberOfMatches() const { return m_matches->GetSize(); }
105 llvm::StringRef GetCursorArgument() const {
106 return GetParsedLine().GetArgumentAtIndex(GetCursorIndex());
109 llvm::StringRef GetCursorArgumentPrefix() const {
110 return GetCursorArgument().substr(0, GetCursorCharPosition());
114 /// The raw command line we are supposed to complete.
115 llvm::StringRef m_command;
116 /// The cursor position in m_command.
117 unsigned m_raw_cursor_pos;
118 /// The command line parsed as arguments.
120 /// The command line until the cursor position parsed as arguments.
121 Args m_partial_parsed_line;
122 /// The index of the argument in which the completion cursor is.
124 /// The cursor position in the argument indexed by m_cursor_index.
125 int m_cursor_char_position;
126 /// If there is a match that is expensive
127 /// to compute, these are here to allow you to compute the completions in
128 /// batches. Start the completion from \amatch_start_point, and return
129 /// \amatch_return_elements elements.
130 // FIXME: These two values are not implemented.
131 int m_match_start_point;
132 int m_max_return_elements;
133 /// \btrue if this is a complete option value (a space will be inserted
134 /// after the completion.) \bfalse otherwise.
135 bool m_word_complete = false;
137 // Note: This list is kept private. This is by design to prevent that any
138 // completion depends on any already computed completion from another backend.
139 // Note: We don't own the list. It's owned by the creator of the
140 // CompletionRequest object.
141 StringList *m_matches;
143 /// List of added completions so far. Used to filter out duplicates.
144 llvm::StringSet<> m_match_set;
147 } // namespace lldb_private
149 #endif // LLDB_UTILITY_COMPLETIONREQUEST_H