Debian Codegraph @

CodeLite/CxxCodeCompletion.hpp

raw
1 #ifndef CXXCODECOMPLETION_HPP
2 #define CXXCODECOMPLETION_HPP
3
4 #include "CxxExpression.hpp"
5 #include "codelite_exports.h"
6 #include "entry.h"
7 #include "istorage.h"
8 #include "macros.h"
9
10 #include <memory>
11 #include <vector>
12 #include <wx/string.h>
13
14 class CxxCodeCompletion;
15
16 struct TemplateManager {
17 CxxCodeCompletion* m_completer = nullptr;
18 std::vector<wxStringMap_t> m_table;
19
20 TemplateManager(CxxCodeCompletion* completer)
21 : m_completer(completer)
22 {
23 }
24 void clear();
25 void add_placeholders(const wxStringMap_t& table, const std::vector<wxString>& visible_scopes);
26 wxString resolve(const wxString& name, const std::vector<wxString>& visible_scopes) const;
27 typedef std::shared_ptr<TemplateManager> ptr_t;
28 };
29
30 // internal to this TU
31 struct FileScope {
32 private:
33 std::unordered_map<wxString, TagEntryPtr> static_members; // static members (`string Foo::m_str`)
34 std::unordered_map<wxString, TagEntryPtr> function_parameters; // function parameters
35 std::vector<wxString> local_scopes;
36
37 public:
38 void clear()
39 {
40 local_scopes.clear();
41 static_members.clear();
42 function_parameters.clear();
43 }
44
45 void add_static_member(TagEntryPtr tag)
46 {
47 if(static_members.count(tag->GetName())) {
48 static_members.erase(tag->GetName());
49 }
50 static_members.insert({ tag->GetName(), tag });
51 }
52
53 bool is_static_member(const wxString& name) const { return static_members.count(name); }
54 bool is_function_parameter(const wxString& name) const { return function_parameters.count(name); }
55 void set_function_parameters(const std::vector<TagEntryPtr>& params)
56 {
57 function_parameters.reserve(params.size());
58 for(auto t : params) {
59 function_parameters.insert({ t->GetName(), t });
60 }
61 }
62
63 TagEntryPtr get_static_member(const wxString& name) const
64 {
65 if(is_static_member(name)) {
66 return static_members.find(name)->second;
67 }
68 return nullptr;
69 }
70 TagEntryPtr get_function_parameter(const wxString& name) const
71 {
72 if(is_function_parameter(name)) {
73 return function_parameters.find(name)->second;
74 }
75 return nullptr;
76 }
77
78 /**
79 * @brief return all function paramters that match the filter
80 * if filter is empty, return all of the parameters
81 */
82 std::vector<TagEntryPtr> get_function_parameters(const wxString& filter) const
83 {
84 std::vector<TagEntryPtr> tags;
85 tags.reserve(function_parameters.size());
86
87 wxString lowercase_filter = filter.Lower();
88 for(const auto& vt : function_parameters) {
89 if(filter.empty()) {
90 tags.push_back(vt.second);
91 } else if(vt.second->GetName().Lower().StartsWith(lowercase_filter)) {
92 tags.push_back(vt.second);
93 }
94 }
95 return tags;
96 }
97
98 const std::vector<wxString>& get_file_scopes() const { return local_scopes; }
99 void set_file_scopes(const wxStringSet_t& scopes)
100 {
101 local_scopes.clear();
102 local_scopes.insert(local_scopes.end(), scopes.begin(), scopes.end());
103 }
104 };
105
106 class WXDLLIMPEXP_CL CxxCodeCompletion
107 {
108 friend struct TemplateManager;
109
110 private:
111 struct __local {
112 private:
113 wxString _type_name;
114 wxString _assignment;
115 wxString _assignment_raw;
116 wxString _name;
117 bool _is_auto = false;
118 wxString _pattern;
119 int _line_numner = wxNOT_FOUND;
120
121 public:
122 bool is_auto() const { return _is_auto; }
123 const wxString& type_name() const
124 {
125 if(is_auto()) {
126 return _assignment;
127 } else {
128 return _type_name;
129 }
130 }
131
132 void set_type_name(const wxString& s) { _type_name = s; }
133 void set_assignment(const wxString& s) { _assignment = s; }
134 void set_assignment_raw(const wxString& s) { _assignment_raw = s; }
135 void set_is_auto(bool b) { _is_auto = b; }
136 void set_name(const wxString& name) { _name = name; }
137 const wxString& name() const { return _name; }
138
139 void set_pattern(const wxString& pattern) { _pattern = pattern; }
140 const wxString& pattern() const { return _pattern; }
141
142 int line_number() const { return _line_numner; }
143 void set_line_number(int l) { _line_numner = l; }
144 };
145
146 private:
147 ITagsStoragePtr m_lookup;
148 std::unordered_map<wxString, CxxCodeCompletion::__local> m_locals;
149 FileScope m_file_only_tags;
150 wxString m_filename;
151 int m_line_number = 0;
152 TagEntryPtr m_current_function_tag;
153 TagEntryPtr m_current_container_tag;
154 size_t m_recurse_protector = 0;
155 bool m_text_parsed = false;
156 std::vector<std::pair<wxString, wxString>>
157 m_types_table; // helper table to solve what we cant (usually the limitations are coming from ctags)
158 wxStringMap_t m_macros_table_map;
159 std::vector<std::pair<wxString, wxString>> m_macros_table;
160 TemplateManager::ptr_t m_template_manager;
161 bool m_first_time = true;
162 wxString m_codelite_indexer;
163
164 private:
165 /**
166 * @brief prepend scope to the list of scopes. Place it first
167 */
168 void prepend_scope(std::vector<wxString>& scopes, const wxString& scope) const;
169 /**
170 * @brief prepend the file specific scopes + global scope (if missing) to the requested visible scopes
171 */
172 std::vector<wxString> prepend_extra_scopes(const std::vector<wxString>& visible_scopes);
173
174 TagEntryPtr lookup_symbol(CxxExpression& curexpr, const std::vector<wxString>& visible_scopes, TagEntryPtr parent);
175 TagEntryPtr lookup_symbol_by_kind(const wxString& name, const std::vector<wxString>& visible_scopes,
176 const std::vector<wxString>& kinds);
177 TagEntryPtr lookup_operator_arrow(TagEntryPtr parent, const std::vector<wxString>& visible_scopes);
178 TagEntryPtr lookup_subscript_operator(TagEntryPtr parent, const std::vector<wxString>& visible_scopes);
179
180 TagEntryPtr lookup_child_symbol(TagEntryPtr parent, TemplateManager::ptr_t template_manager,
181 const wxString& child_symbol, const std::vector<wxString>& visible_scopes,
182 const std::vector<wxString>& kinds);
183
184 wxString typedef_from_tag(TagEntryPtr tag) const;
185 void shrink_scope(const wxString& text, std::unordered_map<wxString, __local>* locals, FileScope* file_tags) const;
186 TagEntryPtr resolve_expression(CxxExpression& curexp, TagEntryPtr parent,
187 const std::vector<wxString>& visible_scopes);
188 TagEntryPtr resolve_compound_expression(std::vector<CxxExpression>& expression,
189 const std::vector<wxString>& visible_scopes,
190 const CxxExpression& orig_expression);
191
192 const wxStringMap_t& get_tokens_map() const;
193
194 wxString do_get_return_value(const wxString& pattern, const wxString& name) const;
195
196 bool read_template_definition(CxxTokenizer& tokenizer, wxString* definition) const;
197
198 /**
199 * @brief apply user hacks
200 */
201 bool resolve_user_type(const wxString& type, const std::vector<wxString>& visible_scopes, wxString* resolved) const;
202
203 std::vector<wxString> update_visible_scope(const std::vector<wxString>& curscopes, TagEntryPtr tag);
204 void update_template_table(TagEntryPtr resolved, CxxExpression& curexpr,
205 const std::vector<wxString>& visible_scopes, wxStringSet_t& visited);
206
207 size_t parse_locals(const wxString& text, std::unordered_map<wxString, __local>* locals) const;
208
209 /**
210 * @brief given `parent` node, return list of all its parents, in order
211 * of inheritance. the list contains the `parent` itself as the first entry
212 */
213 std::vector<TagEntryPtr> get_scopes(TagEntryPtr parent, const std::vector<wxString>& visible_scopes);
214
215 std::vector<TagEntryPtr> get_children_of_scope(TagEntryPtr parent, const std::vector<wxString>& kinds,
216 const wxString& filter, const std::vector<wxString>& visible_scopes);
217
218 /**
219 * @brief return parsable pattern from a tag
220 */
221 wxString normalize_pattern(TagEntryPtr tag) const;
222
223 size_t get_anonymous_tags(const wxString& name, const wxArrayString& kinds, std::vector<TagEntryPtr>& tags) const;
224 size_t get_keywords_tags(const wxString& name, std::vector<TagEntryPtr>& tags);
225
226 TagEntryPtr on_method(CxxExpression& curexp, TagEntryPtr tag, const std::vector<wxString>& visible_scopes);
227 TagEntryPtr on_typedef(CxxExpression& curexp, TagEntryPtr tag, const std::vector<wxString>& visible_scopes);
228 TagEntryPtr on_member(CxxExpression& curexp, TagEntryPtr tag, const std::vector<wxString>& visible_scopes);
229 TagEntryPtr on_static_local(CxxExpression& curexp, const std::vector<wxString>& visible_scopes);
230 TagEntryPtr on_local(CxxExpression& curexp, const std::vector<wxString>& visible_scopes);
231 TagEntryPtr on_parameter(CxxExpression& curexp, const std::vector<wxString>& visible_scopes);
232 TagEntryPtr on_this(CxxExpression& curexp, const std::vector<wxString>& visible_scopes);
233
234 /**
235 * @brief similar to CxxExpression::from_expression however, this method also passes
236 * the result tokens in the pre_processor() code to replace any macros before serving them
237 * for processing
238 */
239 std::vector<CxxExpression> from_expression(const wxString& expression, CxxRemainder* remainder);
240 /**
241 * @brief attempt to perform simple substituation of `name` with
242 * a matached entry from the `m_macros_table_map`
243 */
244 wxString& simple_pre_process(wxString& name) const;
245
246 /**
247 * @brief return the direct parents of a tag
248 */
249 std::vector<TagEntryPtr> get_parents_of_tag_no_recurse(TagEntryPtr parent, TemplateManager::ptr_t template_manager,
250 const std::vector<wxString>& visible_scopes);
251
252 TagEntryPtr find_scope_tag(CxxExpression& curexp, const std::vector<wxString>& visible_scopes);
253 bool is_scope_tag(CxxExpression& curexp, const std::vector<wxString>& visible_scopes)
254 {
255 return find_scope_tag(curexp, visible_scopes);
256 }
257 TagEntryPtr find_scope_tag_externvar(CxxExpression& curexp, const std::vector<wxString>& visible_scopes);
258 TagEntryPtr on_extern_var(CxxExpression& curexp, TagEntryPtr var, const std::vector<wxString>& visible_scopes);
259
260 public:
261 typedef std::shared_ptr<CxxCodeCompletion> ptr_t;
262
263 public:
264 CxxCodeCompletion(ITagsStoragePtr lookup, const wxString& codelite_indexer,
265 const std::unordered_map<wxString, TagEntryPtr>& unit_tests_db);
266 CxxCodeCompletion(ITagsStoragePtr lookup, const wxString& codelite_indexer);
267 ~CxxCodeCompletion();
268
269 /**
270 * @brief return the return value of a tag (of type function)
271 */
272 wxString get_return_value(TagEntryPtr tag) const;
273
274 TagEntryPtr get_current_function_tag() const { return m_current_function_tag; }
275 TagEntryPtr get_current_scope_tag() const { return m_current_container_tag; }
276
277 /**
278 * @brief sort input list of tags to a reasonable order
279 */
280 void sort_tags(const std::vector<TagEntryPtr>& tags, std::vector<TagEntryPtr>& sorted_tags, bool include_ctor_dtor,
281 const wxStringSet_t& visible_files);
282
283 /**
284 * @brief reset the completer (clear all cached data)
285 */
286 void reset();
287
288 /**
289 * determine the current scope and return it
290 */
291 void determine_current_scope();
292
293 /**
294 * @brief set the typedef helper table
295 */
296 void set_types_table(const std::vector<std::pair<wxString, wxString>>& t) { m_types_table = t; }
297
298 /**
299 * @brief set macros table
300 */
301 void set_macros_table(const std::vector<std::pair<wxString, wxString>>& t);
302
303 /**
304 * @brief replace the text
305 * this method resets the completer
306 */
307 void set_text(const wxString& text, const wxString& filename, int current_line);
308
309 /**
310 * @brief parse an expression and resolve it into a TagEntry. If the expression does not end with
311 * an operand, return the remainder
312 * @param expression
313 * @param visible_scopes [in] addition visible scopes (e.g. "std")
314 * @param remainder [output] holds the last part that was not used. `a.foo().bar().get` -> the remainder will hold
315 * `get`
316 * @return a tag representing the scope
317 */
318 TagEntryPtr code_complete(const wxString& expression, const std::vector<wxString>& visible_scopes,
319 CxxRemainder* remainder = nullptr);
320
321 /**
322 * @brief search the database for list of constructors
323 */
324 size_t get_class_constructors(TagEntryPtr tag, std::vector<TagEntryPtr>& tags);
325
326 /**
327 * @brief word completion. differ from code_complete by the expression. code_complete is called when user hit
328 * -> or . , while word_completion is used when user types something or in a middle of a word
329 * @return list of tags matches the context and filter placed in: `candidates`
330 */
331 size_t word_complete(const wxString& filepath, int line, const wxString& expression, const wxString& text,
332 const std::vector<wxString>& visible_scopes, bool exact_match,
333 std::vector<TagEntryPtr>& candidates, const wxStringSet_t& visible_files = {});
334 /**
335 * return list of files for completion based on the prefix typed
336 */
337 size_t get_file_completions(const wxString& user_typed, std::vector<TagEntryPtr>& files, const wxString& suffix);
338 /**
339 * @brief return the local variables available for the current scope (passed in the c-tor)
340 */
341 std::vector<TagEntryPtr> get_locals(const wxString& filter) const;
342
343 /**
344 * @brief return list local functions
345 */
346 size_t get_local_tags(const wxString& filter, const wxStringSet_t& kinds, std::vector<TagEntryPtr>& tags) const;
347
348 /**
349 * @brief return list of completions filtered by name for a given parent
350 */
351 size_t get_completions(TagEntryPtr parent, const wxString& operand_string, const wxString& filter,
352 std::vector<TagEntryPtr>& candidates, const std::vector<wxString>& visible_scopes,
353 size_t limit = (size_t)-1);
354 /**
355 * @brief return children tag of the current scope and any other relevant scopes
356 * this method calls internally to `determine_current_scope()`
357 * so make sure you called `set_text` with file and line number
358 */
359 size_t get_children_of_current_scope(const std::vector<wxString>& kinds, const wxString& filter,
360 const std::vector<wxString>& visible_scopes,
361 std::vector<TagEntryPtr>* current_scope_children,
362 std::vector<TagEntryPtr>* other_scopes_children,
363 std::vector<TagEntryPtr>* global_scope_children);
364
365 /**
366 * return list of completion for non expression attempt
367 */
368 size_t get_word_completions(const CxxRemainder& remainder, std::vector<TagEntryPtr>& candidates,
369 const std::vector<wxString>& visible_scopes, const wxStringSet_t& visible_files);
370
371 size_t find_definition(const wxString& filepath, int line, const wxString& expr, const wxString& text,
372 const std::vector<wxString>& visible_scopes, std::vector<TagEntryPtr>& matches);
373 };
374
375 #endif // CXXCODECOMPLETION_HPP