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