1 //===-- CompletionRequest.h -------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef LLDB_UTILITY_COMPLETIONREQUEST_H 11 #define LLDB_UTILITY_COMPLETIONREQUEST_H 12 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" 18 19 namespace lldb_private { 20 class CompletionResult { 21 //---------------------------------------------------------- 22 /// A single completion and all associated data. 23 //---------------------------------------------------------- 24 struct Completion { CompletionCompletion25 Completion(llvm::StringRef completion, llvm::StringRef description) 26 : m_completion(completion.str()), m_descripton(description.str()) {} 27 28 std::string m_completion; 29 std::string m_descripton; 30 31 /// Generates a string that uniquely identifies this completion result. 32 std::string GetUniqueKey() const; 33 }; 34 std::vector<Completion> m_results; 35 36 /// List of added completions so far. Used to filter out duplicates. 37 llvm::StringSet<> m_added_values; 38 39 public: 40 void AddResult(llvm::StringRef completion, llvm::StringRef description); 41 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; 48 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; 55 GetNumberOfResults()56 std::size_t GetNumberOfResults() const { return m_results.size(); } 57 }; 58 59 //---------------------------------------------------------------------- 60 /// @class CompletionRequest CompletionRequest.h 61 /// "lldb/Utility/ArgCompletionRequest.h" 62 /// 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. 66 /// 67 //---------------------------------------------------------------------- 68 class CompletionRequest { 69 public: 70 //---------------------------------------------------------- 71 /// Constructs a completion request. 72 /// 73 /// @param [in] command_line 74 /// The command line the user has typed at this point. 75 /// 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. 80 /// 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 86 /// elements. 87 /// 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); 95 GetRawLine()96 llvm::StringRef GetRawLine() const { return m_command; } 97 GetRawCursorPos()98 unsigned GetRawCursorPos() const { return m_raw_cursor_pos; } 99 GetParsedLine()100 const Args &GetParsedLine() const { return m_parsed_line; } 101 GetParsedLine()102 Args &GetParsedLine() { return m_parsed_line; } 103 GetPartialParsedLine()104 const Args &GetPartialParsedLine() const { return m_partial_parsed_line; } 105 SetCursorIndex(int i)106 void SetCursorIndex(int i) { m_cursor_index = i; } GetCursorIndex()107 int GetCursorIndex() const { return m_cursor_index; } 108 SetCursorCharPosition(int pos)109 void SetCursorCharPosition(int pos) { m_cursor_char_position = pos; } GetCursorCharPosition()110 int GetCursorCharPosition() const { return m_cursor_char_position; } 111 GetMatchStartPoint()112 int GetMatchStartPoint() const { return m_match_start_point; } 113 GetMaxReturnElements()114 int GetMaxReturnElements() const { return m_max_return_elements; } 115 GetWordComplete()116 bool GetWordComplete() { return m_word_complete; } 117 SetWordComplete(bool v)118 void SetWordComplete(bool v) { m_word_complete = v; } 119 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 123 /// afterwards. 124 /// 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); 131 } 132 133 /// Adds multiple possible completion strings. 134 /// 135 /// \param completions The list of completions. 136 /// 137 /// @see AddCompletion AddCompletions(const StringList & completions)138 void AddCompletions(const StringList &completions) { 139 for (std::size_t i = 0; i < completions.GetSize(); ++i) 140 AddCompletion(completions.GetStringAtIndex(i)); 141 } 142 143 /// Adds multiple possible completion strings alongside their descriptions. 144 /// 145 /// The number of completions and descriptions must be identical. 146 /// 147 /// \param completions The list of completions. 148 /// \param completions The list of descriptions. 149 /// 150 /// @see AddCompletion AddCompletions(const StringList & completions,const StringList & descriptions)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)); 157 } 158 GetNumberOfMatches()159 std::size_t GetNumberOfMatches() const { 160 return m_result.GetNumberOfResults(); 161 } 162 GetCursorArgument()163 llvm::StringRef GetCursorArgument() const { 164 return GetParsedLine().GetArgumentAtIndex(GetCursorIndex()); 165 } 166 GetCursorArgumentPrefix()167 llvm::StringRef GetCursorArgumentPrefix() const { 168 return GetCursorArgument().substr(0, GetCursorCharPosition()); 169 } 170 171 private: 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. 177 Args m_parsed_line; 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. 181 int m_cursor_index; 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; 194 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; 200 }; 201 202 } // namespace lldb_private 203 204 #endif // LLDB_UTILITY_COMPLETIONREQUEST_H 205