]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Utility/CompletionRequest.h
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / Utility / CompletionRequest.h
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/StringList.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/ADT/StringSet.h"
17
18 namespace lldb_private {
19
20 //----------------------------------------------------------------------
21 /// @class CompletionRequest CompletionRequest.h
22 ///   "lldb/Utility/ArgCompletionRequest.h"
23 ///
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.
27 ///
28 //----------------------------------------------------------------------
29 class CompletionRequest {
30 public:
31   //----------------------------------------------------------
32   /// Constructs a completion request.
33   ///
34   /// @param [in] command_line
35   ///     The command line the user has typed at this point.
36   ///
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.
41   ///
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
47   ///     elements.
48   ///
49   /// @param [out] matches
50   ///     A list of matches that will be filled by the different completion
51   ///     handlers.
52   //----------------------------------------------------------
53   CompletionRequest(llvm::StringRef command_line, unsigned raw_cursor_pos,
54                     int match_start_point, int max_return_elements,
55                     StringList &matches);
56
57   llvm::StringRef GetRawLine() const { return m_command; }
58
59   unsigned GetRawCursorPos() const { return m_raw_cursor_pos; }
60
61   const Args &GetParsedLine() const { return m_parsed_line; }
62
63   Args &GetParsedLine() { return m_parsed_line; }
64
65   const Args &GetPartialParsedLine() const { return m_partial_parsed_line; }
66
67   void SetCursorIndex(int i) { m_cursor_index = i; }
68   int GetCursorIndex() const { return m_cursor_index; }
69
70   void SetCursorCharPosition(int pos) { m_cursor_char_position = pos; }
71   int GetCursorCharPosition() const { return m_cursor_char_position; }
72
73   int GetMatchStartPoint() const { return m_match_start_point; }
74
75   int GetMaxReturnElements() const { return m_max_return_elements; }
76
77   bool GetWordComplete() { return m_word_complete; }
78
79   void SetWordComplete(bool v) { m_word_complete = v; }
80
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
84   /// afterwards.
85   ///
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);
91   }
92
93   /// Adds multiple possible completion strings.
94   ///
95   /// \param completions The list of completions.
96   ///
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));
101   }
102
103   std::size_t GetNumberOfMatches() const { return m_matches->GetSize(); }
104
105   llvm::StringRef GetCursorArgument() const {
106     return GetParsedLine().GetArgumentAtIndex(GetCursorIndex());
107   }
108
109   llvm::StringRef GetCursorArgumentPrefix() const {
110     return GetCursorArgument().substr(0, GetCursorCharPosition());
111   }
112
113 private:
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.
119   Args m_parsed_line;
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.
123   int m_cursor_index;
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;
136
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;
142
143   /// List of added completions so far. Used to filter out duplicates.
144   llvm::StringSet<> m_match_set;
145 };
146
147 } // namespace lldb_private
148
149 #endif // LLDB_UTILITY_COMPLETIONREQUEST_H