| 1 |
#include "CxxCodeCompletion.hpp" |
| 2 |
|
| 3 |
#include "CompletionHelper.hpp" |
| 4 |
#include "CxxExpression.hpp" |
| 5 |
#include "CxxScannerTokens.h" |
| 6 |
#include "CxxVariableScanner.h" |
| 7 |
#include "ctags_manager.h" |
| 8 |
#include "file_logger.h" |
| 9 |
#include "fileextmanager.h" |
| 10 |
#include "fileutils.h" |
| 11 |
#include "function.h" |
| 12 |
#include "language.h" |
| 13 |
|
| 14 |
#include <algorithm> |
| 15 |
#include <deque> |
| 16 |
|
| 17 |
namespace |
| 18 |
{ |
| 19 |
TagEntryPtr create_global_scope_tag() |
| 20 |
{ |
| 21 |
TagEntryPtr global_scope(new TagEntry()); |
| 22 |
global_scope->SetName("<global>"); |
| 23 |
global_scope->SetPath("<global>"); |
| 24 |
return global_scope; |
| 25 |
} |
| 26 |
|
| 27 |
wxArrayString to_wx_array_string(const std::vector<wxString>& v) |
| 28 |
{ |
| 29 |
wxArrayString a; |
| 30 |
a.reserve(v.size()); |
| 31 |
for(const wxString& s : v) { |
| 32 |
a.Add(s); |
| 33 |
} |
| 34 |
return a; |
| 35 |
} |
| 36 |
|
| 37 |
} // namespace |
| 38 |
|
| 39 |
#define RECURSE_GUARD_RETURN_NULLPTR() \ |
| 40 |
m_recurse_protector++; \ |
| 41 |
if(m_recurse_protector > 150) { \ |
| 42 |
return nullptr; \ |
| 43 |
} |
| 44 |
|
| 45 |
#define RECURSE_GUARD_RETURN() \ |
| 46 |
m_recurse_protector++; \ |
| 47 |
if(m_recurse_protector > 150) { \ |
| 48 |
return; \ |
| 49 |
} |
| 50 |
|
| 51 |
CxxCodeCompletion::CxxCodeCompletion(ITagsStoragePtr lookup, const wxString& codelite_indexer) |
| 52 |
: m_codelite_indexer(codelite_indexer) |
| 53 |
{ |
| 54 |
m_lookup = lookup; |
| 55 |
m_template_manager.reset(new TemplateManager(this)); |
| 56 |
} |
| 57 |
|
| 58 |
CxxCodeCompletion::CxxCodeCompletion(ITagsStoragePtr lookup, const wxString& codelite_indexer, |
| 59 |
const std::unordered_map<wxString, TagEntryPtr>& unit_tests_db) |
| 60 |
: m_codelite_indexer(codelite_indexer) |
| 61 |
{ |
| 62 |
m_lookup = lookup; |
| 63 |
m_template_manager.reset(new TemplateManager(this)); |
| 64 |
} |
| 65 |
|
| 66 |
CxxCodeCompletion::~CxxCodeCompletion() {} |
| 67 |
|
| 68 |
void CxxCodeCompletion::determine_current_scope() |
| 69 |
{ |
| 70 |
if(m_current_function_tag || m_filename.empty() || m_line_number == wxNOT_FOUND) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
if(!m_lookup) { |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
m_current_function_tag = m_lookup->GetScope(m_filename, m_line_number + 1); |
| 79 |
if(m_current_function_tag && m_current_function_tag->IsMethod()) { |
| 80 |
std::vector<TagEntryPtr> tmp_tags; |
| 81 |
m_lookup->GetTagsByPath(m_current_function_tag->GetScope(), tmp_tags, 1); |
| 82 |
if(tmp_tags.size() == 1) { |
| 83 |
m_current_container_tag = std::move(tmp_tags[0]); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
TagEntryPtr CxxCodeCompletion::code_complete(const wxString& expression, const std::vector<wxString>& visible_scopes, |
| 89 |
CxxRemainder* remainder) |
| 90 |
{ |
| 91 |
// build expression from the expression |
| 92 |
m_recurse_protector = 0; |
| 93 |
m_template_manager.reset(new TemplateManager(this)); |
| 94 |
|
| 95 |
std::vector<wxString> scopes = { visible_scopes.begin(), visible_scopes.end() }; |
| 96 |
std::vector<CxxExpression> expr_arr = from_expression(expression, remainder); |
| 97 |
|
| 98 |
// add extra scopes (global scope, file scopes) |
| 99 |
scopes = prepend_extra_scopes(scopes); |
| 100 |
|
| 101 |
// Check the current scope |
| 102 |
if(m_current_container_tag) { |
| 103 |
prepend_scope(scopes, m_current_container_tag->GetPath()); |
| 104 |
} |
| 105 |
|
| 106 |
clDEBUG() << "code_complete() called with scopes:" << scopes << endl; |
| 107 |
|
| 108 |
m_first_time = true; |
| 109 |
return resolve_compound_expression(expr_arr, scopes, {}); |
| 110 |
} |
| 111 |
|
| 112 |
TagEntryPtr CxxCodeCompletion::resolve_compound_expression(std::vector<CxxExpression>& expression, |
| 113 |
const std::vector<wxString>& visible_scopes, |
| 114 |
const CxxExpression& orig_expression) |
| 115 |
{ |
| 116 |
RECURSE_GUARD_RETURN_NULLPTR(); |
| 117 |
|
| 118 |
// resolve each expression |
| 119 |
if(expression.empty()) { |
| 120 |
return nullptr; |
| 121 |
} |
| 122 |
|
| 123 |
// handle global scope |
| 124 |
std::vector<wxString> scopes = visible_scopes; |
| 125 |
if(expression.size() == 1 && expression[0].type_name().empty() && expression[0].operand_string() == "::") { |
| 126 |
// return a dummy entry representing the global scope |
| 127 |
return create_global_scope_tag(); |
| 128 |
} else if(expression.size() >= 2 && expression[0].type_name().empty() && expression[0].operand_string() == "::") { |
| 129 |
// explicity requesting for the global namespace |
| 130 |
// clear the `scopes` and use only the global namespace (empty string) |
| 131 |
scopes.clear(); |
| 132 |
scopes.push_back(wxEmptyString); |
| 133 |
// in addition, we can remove the first expression from the array |
| 134 |
expression.erase(expression.begin()); |
| 135 |
} |
| 136 |
|
| 137 |
// copy the subscript operator to the last expression |
| 138 |
if(!expression.empty() && orig_expression.check_subscript_operator()) { |
| 139 |
expression.back().set_subscript_params(orig_expression.subscript_params()); |
| 140 |
} |
| 141 |
|
| 142 |
TagEntryPtr resolved; |
| 143 |
for(CxxExpression& curexpr : expression) { |
| 144 |
resolved = resolve_expression(curexpr, resolved, scopes); |
| 145 |
CHECK_PTR_RET_NULL(resolved); |
| 146 |
// once we resolved something we make it with this flag |
| 147 |
// this way we avoid checking for locals/global scope etc |
| 148 |
// since we already have some context |
| 149 |
m_first_time = false; |
| 150 |
} |
| 151 |
return resolved; |
| 152 |
} |
| 153 |
|
| 154 |
size_t CxxCodeCompletion::parse_locals(const wxString& text, std::unordered_map<wxString, __local>* locals) const |
| 155 |
{ |
| 156 |
shrink_scope(text, locals, nullptr); |
| 157 |
return locals->size(); |
| 158 |
} |
| 159 |
|
| 160 |
void CxxCodeCompletion::shrink_scope(const wxString& text, std::unordered_map<wxString, __local>* locals, |
| 161 |
FileScope* file_tags) const |
| 162 |
{ |
| 163 |
// parse local variables |
| 164 |
CxxVariableScanner scanner(text, eCxxStandard::kCxx11, get_tokens_map(), false); |
| 165 |
CxxVariable::Vec_t variables = scanner.GetVariables(false); |
| 166 |
locals->reserve(variables.size()); |
| 167 |
|
| 168 |
std::vector<TagEntryPtr> parameters; |
| 169 |
if(m_current_function_tag && m_current_function_tag->IsFunction()) { |
| 170 |
std::vector<TagEntryPtr> all_lambdas; |
| 171 |
std::vector<TagEntryPtr> lambdas; |
| 172 |
// get the current function parameters |
| 173 |
m_lookup->GetParameters(m_current_function_tag->GetPath(), parameters); |
| 174 |
m_lookup->GetLambdas(m_current_function_tag->GetPath(), all_lambdas); |
| 175 |
|
| 176 |
// read all lambdas paramteres |
| 177 |
std::unordered_map<wxString, TagEntryPtr> lambda_parameters_map; |
| 178 |
std::unordered_map<wxString, TagEntryPtr> function_parameters_map; |
| 179 |
|
| 180 |
for(auto param : parameters) { |
| 181 |
function_parameters_map.insert({ param->GetName(), param }); |
| 182 |
} |
| 183 |
|
| 184 |
for(auto lambda : all_lambdas) { |
| 185 |
if((lambda->GetLine() - 1) <= m_line_number) { |
| 186 |
// load this lambda parameters and add them |
| 187 |
std::vector<TagEntryPtr> lambda_parameters; |
| 188 |
m_lookup->GetParameters(lambda->GetPath(), lambda_parameters); |
| 189 |
for(auto param : lambda_parameters) { |
| 190 |
// if a function parameter with this name alrady exists, skip it |
| 191 |
if(function_parameters_map.count(param->GetName())) { |
| 192 |
continue; |
| 193 |
} |
| 194 |
|
| 195 |
// if we already encoutered a lambda parameter with this name, replace it |
| 196 |
if(lambda_parameters_map.count(param->GetName())) { |
| 197 |
lambda_parameters_map.erase(param->GetName()); |
| 198 |
} |
| 199 |
lambda_parameters_map.insert({ param->GetName(), param }); |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
// all the lambda paramters to the list of parameters |
| 205 |
for(const auto& vt : lambda_parameters_map) { |
| 206 |
parameters.emplace_back(vt.second); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
if(file_tags) { |
| 211 |
// set the function parameters (this includes any lambda parameters) |
| 212 |
file_tags->set_function_parameters(parameters); |
| 213 |
} |
| 214 |
|
| 215 |
// we also include the anonymous entries for this scope |
| 216 |
wxArrayString kinds; |
| 217 |
kinds.Add("class"); |
| 218 |
kinds.Add("struct"); |
| 219 |
kinds.Add("namespace"); |
| 220 |
kinds.Add("member"); |
| 221 |
kinds.Add("function"); |
| 222 |
kinds.Add("variable"); |
| 223 |
kinds.Add("enum"); |
| 224 |
kinds.Add("macro"); |
| 225 |
std::vector<TagEntryPtr> anonymous_tags; |
| 226 |
get_anonymous_tags(wxEmptyString, kinds, anonymous_tags); |
| 227 |
|
| 228 |
wxStringSet_t unique_scopes; |
| 229 |
// create a local variable from the anonymous tags |
| 230 |
for(auto tag : anonymous_tags) { |
| 231 |
if(tag->GetScope().StartsWith("__anon")) { |
| 232 |
unique_scopes.insert(tag->GetScope()); |
| 233 |
} |
| 234 |
if(tag->GetKind() == "variable") { |
| 235 |
CxxVariableScanner scanner(normalize_pattern(tag), eCxxStandard::kCxx11, m_macros_table_map, false); |
| 236 |
auto _variables = scanner.GetVariables(false); |
| 237 |
variables.insert(variables.end(), _variables.begin(), _variables.end()); |
| 238 |
} else if(file_tags && tag->GetKind() == "member") { |
| 239 |
file_tags->add_static_member(tag); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
if(file_tags) { |
| 244 |
file_tags->set_file_scopes(unique_scopes); |
| 245 |
} |
| 246 |
|
| 247 |
for(auto var : variables) { |
| 248 |
__local local; |
| 249 |
wxString assignement_expr_raw = var->GetDefaultValue(); |
| 250 |
// strip it from any keywords etc and only keep interesting parts |
| 251 |
|
| 252 |
local.set_assignment_raw(assignement_expr_raw); |
| 253 |
local.set_assignment(assignement_expr_raw); |
| 254 |
local.set_type_name(var->GetTypeAsString()); |
| 255 |
local.set_is_auto(var->IsAuto()); |
| 256 |
local.set_name(var->GetName()); |
| 257 |
local.set_pattern(var->ToString(CxxVariable::kToString_Name | CxxVariable::kToString_DefaultValue)); |
| 258 |
local.set_line_number(var->GetLineNumber()); |
| 259 |
locals->insert({ var->GetName(), local }); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
TagEntryPtr CxxCodeCompletion::lookup_operator_arrow(TagEntryPtr parent, const std::vector<wxString>& visible_scopes) |
| 264 |
{ |
| 265 |
return lookup_child_symbol(parent, m_template_manager, "operator->", visible_scopes, { "function", "prototype" }); |
| 266 |
} |
| 267 |
|
| 268 |
TagEntryPtr CxxCodeCompletion::lookup_subscript_operator(TagEntryPtr parent, |
| 269 |
const std::vector<wxString>& visible_scopes) |
| 270 |
{ |
| 271 |
CHECK_PTR_RET_NULL(m_lookup); |
| 272 |
std::vector<TagEntryPtr> scopes = get_scopes(parent, visible_scopes); |
| 273 |
for(auto scope : scopes) { |
| 274 |
std::vector<TagEntryPtr> tags; |
| 275 |
m_lookup->GetSubscriptOperator(scope->GetPath(), tags); |
| 276 |
if(!tags.empty()) { |
| 277 |
return tags[0]; |
| 278 |
} |
| 279 |
} |
| 280 |
return nullptr; |
| 281 |
} |
| 282 |
|
| 283 |
TagEntryPtr CxxCodeCompletion::lookup_child_symbol(TagEntryPtr parent, TemplateManager::ptr_t template_manager, |
| 284 |
const wxString& child_symbol, |
| 285 |
const std::vector<wxString>& visible_scopes, |
| 286 |
const std::vector<wxString>& kinds) |
| 287 |
{ |
| 288 |
CHECK_PTR_RET_NULL(m_lookup); |
| 289 |
auto resolved = lookup_symbol_by_kind(child_symbol, visible_scopes, kinds); |
| 290 |
if(resolved) { |
| 291 |
return resolved; |
| 292 |
} |
| 293 |
|
| 294 |
// try with the parent |
| 295 |
CHECK_PTR_RET_NULL(parent); |
| 296 |
|
| 297 |
// to avoid spaces and other stuff that depends on the user typing |
| 298 |
// we tokenize the symbol name |
| 299 |
std::vector<wxString> requested_symbol_tokens; |
| 300 |
{ |
| 301 |
CxxLexerToken tok; |
| 302 |
CxxTokenizer tokenizer; |
| 303 |
tokenizer.Reset(child_symbol); |
| 304 |
while(tokenizer.NextToken(tok)) { |
| 305 |
requested_symbol_tokens.push_back(tok.GetWXString()); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
auto compare_tokens_func = [&requested_symbol_tokens](const wxString& symbol_name) -> bool { |
| 310 |
CxxLexerToken tok; |
| 311 |
CxxTokenizer tokenizer; |
| 312 |
tokenizer.Reset(symbol_name); |
| 313 |
for(const wxString& token : requested_symbol_tokens) { |
| 314 |
if(!tokenizer.NextToken(tok)) { |
| 315 |
return false; |
| 316 |
} |
| 317 |
if(tok.GetWXString() != token) { |
| 318 |
return false; |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
// if we can read more tokens from the tokenizer, it means that |
| 323 |
// the comparison is not complete, it "contains" the requested name |
| 324 |
if(tokenizer.NextToken(tok)) { |
| 325 |
return false; |
| 326 |
} |
| 327 |
return true; |
| 328 |
}; |
| 329 |
|
| 330 |
std::deque<TagEntryPtr> q; |
| 331 |
q.push_front(parent); |
| 332 |
wxStringSet_t visited; |
| 333 |
|
| 334 |
while(!q.empty()) { |
| 335 |
auto t = q.front(); |
| 336 |
q.pop_front(); |
| 337 |
|
| 338 |
// avoid visiting the same tag twice |
| 339 |
if(!visited.insert(t->GetPath()).second) { |
| 340 |
continue; |
| 341 |
} |
| 342 |
|
| 343 |
std::vector<TagEntryPtr> tags; |
| 344 |
m_lookup->GetTagsByScope(t->GetPath(), tags); |
| 345 |
for(TagEntryPtr child : tags) { |
| 346 |
if(compare_tokens_func(child->GetName())) { |
| 347 |
return child; |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
// if we got here - try the parents |
| 352 |
auto parents = get_parents_of_tag_no_recurse(t, template_manager, visible_scopes); |
| 353 |
q.insert(q.end(), parents.begin(), parents.end()); |
| 354 |
} |
| 355 |
return nullptr; |
| 356 |
} |
| 357 |
|
| 358 |
TagEntryPtr CxxCodeCompletion::lookup_symbol_by_kind(const wxString& name, const std::vector<wxString>& visible_scopes, |
| 359 |
const std::vector<wxString>& kinds) |
| 360 |
{ |
| 361 |
std::vector<TagEntryPtr> tags; |
| 362 |
std::vector<wxString> scopes_to_check = visible_scopes; |
| 363 |
if(scopes_to_check.empty()) { |
| 364 |
scopes_to_check.push_back(wxEmptyString); |
| 365 |
} |
| 366 |
|
| 367 |
for(const wxString& scope : scopes_to_check) { |
| 368 |
wxString path; |
| 369 |
if(!scope.empty()) { |
| 370 |
path << scope << "::"; |
| 371 |
} |
| 372 |
path << name; |
| 373 |
m_lookup->GetTagsByPathAndKind(path, tags, kinds); |
| 374 |
if(tags.size() == 1) { |
| 375 |
// we got a match |
| 376 |
return tags[0]; |
| 377 |
} |
| 378 |
} |
| 379 |
return tags.empty() ? nullptr : tags[0]; |
| 380 |
} |
| 381 |
|
| 382 |
void CxxCodeCompletion::update_template_table(TagEntryPtr resolved, CxxExpression& curexpr, |
| 383 |
const std::vector<wxString>& visible_scopes, wxStringSet_t& visited) |
| 384 |
{ |
| 385 |
CHECK_PTR_RET(resolved); |
| 386 |
if(!visited.insert(resolved->GetPath()).second) { |
| 387 |
// already visited this node |
| 388 |
return; |
| 389 |
} |
| 390 |
|
| 391 |
// simple template instantiaion line |
| 392 |
if(curexpr.is_template()) { |
| 393 |
curexpr.parse_template_placeholders(resolved->GetTemplateDefinition()); |
| 394 |
wxStringMap_t M = curexpr.get_template_placeholders_map(); |
| 395 |
m_template_manager->add_placeholders(M, visible_scopes); |
| 396 |
} |
| 397 |
|
| 398 |
// Check if one of the parents is a template class |
| 399 |
std::vector<wxString> inhertiance_expressions = |
| 400 |
CxxExpression::split_subclass_expression(normalize_pattern(resolved)); |
| 401 |
for(const wxString& inherit : inhertiance_expressions) { |
| 402 |
std::vector<CxxExpression> more_expressions = from_expression(inherit + ".", nullptr); |
| 403 |
if(more_expressions.empty()) { |
| 404 |
continue; |
| 405 |
} |
| 406 |
|
| 407 |
auto match = lookup_symbol_by_kind(more_expressions[0].type_name(), visible_scopes, { "class", "struct" }); |
| 408 |
if(match) { |
| 409 |
update_template_table(match, more_expressions[0], visible_scopes, visited); |
| 410 |
} |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
TagEntryPtr CxxCodeCompletion::lookup_symbol(CxxExpression& curexpr, const std::vector<wxString>& visible_scopes, |
| 415 |
TagEntryPtr parent) |
| 416 |
{ |
| 417 |
wxString name_to_find = curexpr.type_name(); |
| 418 |
auto resolved_name = m_template_manager->resolve(name_to_find, visible_scopes); |
| 419 |
if(resolved_name != name_to_find) { |
| 420 |
name_to_find = resolved_name; |
| 421 |
auto expressions = from_expression(name_to_find + curexpr.operand_string(), nullptr); |
| 422 |
return resolve_compound_expression(expressions, visible_scopes, curexpr); |
| 423 |
} |
| 424 |
|
| 425 |
// try classes first |
| 426 |
auto resolved = lookup_child_symbol(parent, m_template_manager, name_to_find, visible_scopes, |
| 427 |
{ "typedef", "class", "struct", "namespace", "enum", "union" }); |
| 428 |
if(!resolved) { |
| 429 |
// try methods |
| 430 |
// `lookup_child_symbol` takes inheritance into consideration |
| 431 |
resolved = lookup_child_symbol(parent, m_template_manager, name_to_find, visible_scopes, |
| 432 |
{ "function", "prototype", "member", "enumerator" }); |
| 433 |
} |
| 434 |
|
| 435 |
if(resolved) { |
| 436 |
// update the template table |
| 437 |
wxStringSet_t visited; |
| 438 |
update_template_table(resolved, curexpr, visible_scopes, visited); |
| 439 |
|
| 440 |
// Check for operator-> overloading |
| 441 |
if(curexpr.check_subscript_operator()) { |
| 442 |
// we check for subscript before -> |
| 443 |
TagEntryPtr subscript_tag = lookup_subscript_operator(resolved, visible_scopes); |
| 444 |
if(subscript_tag) { |
| 445 |
resolved = subscript_tag; |
| 446 |
curexpr.pop_subscript_operator(); |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
if(curexpr.operand_string() == "->") { |
| 451 |
// search for operator-> overloading |
| 452 |
TagEntryPtr arrow_tag = lookup_operator_arrow(resolved, visible_scopes); |
| 453 |
if(arrow_tag) { |
| 454 |
resolved = arrow_tag; |
| 455 |
// change the operand from -> to . |
| 456 |
// to avoid extra resolving in case we are resolving expression like this: |
| 457 |
// ``` |
| 458 |
// shared_ptr<shared_ptr<wxString>> p; |
| 459 |
// p-> |
| 460 |
// ``` |
| 461 |
// without changing the operand, we will get completions for `wxString` |
| 462 |
curexpr.set_operand('.'); |
| 463 |
} |
| 464 |
} |
| 465 |
} else { |
| 466 |
// still failed to resolve it, try macro |
| 467 |
// try macro |
| 468 |
auto resolved = lookup_symbol_by_kind(curexpr.type_name(), {}, { "macro" }); |
| 469 |
if(resolved && !resolved->GetMacrodef().empty()) { |
| 470 |
// this is a macro |
| 471 |
auto expressions = from_expression(resolved->GetMacrodef() + curexpr.operand_string(), nullptr); |
| 472 |
return resolve_compound_expression(expressions, visible_scopes, curexpr); |
| 473 |
} |
| 474 |
} |
| 475 |
return resolved; |
| 476 |
} |
| 477 |
|
| 478 |
std::vector<wxString> CxxCodeCompletion::update_visible_scope(const std::vector<wxString>& curscopes, TagEntryPtr tag) |
| 479 |
{ |
| 480 |
std::vector<wxString> scopes; |
| 481 |
scopes.insert(scopes.end(), curscopes.begin(), curscopes.end()); |
| 482 |
|
| 483 |
if(tag && (tag->IsClass() || tag->IsStruct() || tag->IsNamespace() || tag->GetKind() == "union")) { |
| 484 |
prepend_scope(scopes, tag->GetPath()); |
| 485 |
} else if(tag && (tag->IsMethod() || tag->IsMember())) { |
| 486 |
prepend_scope(scopes, tag->GetScope()); |
| 487 |
} |
| 488 |
return scopes; |
| 489 |
} |
| 490 |
TagEntryPtr CxxCodeCompletion::on_local(CxxExpression& curexp, const std::vector<wxString>& visible_scopes) |
| 491 |
{ |
| 492 |
// local member |
| 493 |
if(m_locals.count(curexp.type_name()) == 0) { |
| 494 |
return nullptr; |
| 495 |
} |
| 496 |
|
| 497 |
wxString exprstr = m_locals.find(curexp.type_name())->second.type_name() + curexp.operand_string(); |
| 498 |
std::vector<CxxExpression> expr_arr = from_expression(exprstr, nullptr); |
| 499 |
return resolve_compound_expression(expr_arr, visible_scopes, curexp); |
| 500 |
} |
| 501 |
|
| 502 |
TagEntryPtr CxxCodeCompletion::on_parameter(CxxExpression& curexp, const std::vector<wxString>& visible_scopes) |
| 503 |
{ |
| 504 |
// local member |
| 505 |
if(!m_file_only_tags.is_function_parameter(curexp.type_name())) { |
| 506 |
return nullptr; |
| 507 |
} |
| 508 |
|
| 509 |
wxString exprstr = |
| 510 |
m_file_only_tags.get_function_parameter(curexp.type_name())->GetTypename() + curexp.operand_string(); |
| 511 |
std::vector<CxxExpression> expr_arr = from_expression(exprstr, nullptr); |
| 512 |
return resolve_compound_expression(expr_arr, visible_scopes, curexp); |
| 513 |
} |
| 514 |
|
| 515 |
TagEntryPtr CxxCodeCompletion::on_extern_var(CxxExpression& curexp, TagEntryPtr var, |
| 516 |
const std::vector<wxString>& visible_scopes) |
| 517 |
{ |
| 518 |
// local member |
| 519 |
if(!var) { |
| 520 |
return nullptr; |
| 521 |
} |
| 522 |
|
| 523 |
wxString exprstr = var->GetTypename() + curexp.operand_string(); |
| 524 |
std::vector<CxxExpression> expr_arr = from_expression(exprstr, nullptr); |
| 525 |
return resolve_compound_expression(expr_arr, visible_scopes, curexp); |
| 526 |
} |
| 527 |
|
| 528 |
TagEntryPtr CxxCodeCompletion::on_static_local(CxxExpression& curexp, const std::vector<wxString>& visible_scopes) |
| 529 |
{ |
| 530 |
if(!m_file_only_tags.is_static_member(curexp.type_name())) { |
| 531 |
return nullptr; |
| 532 |
} |
| 533 |
|
| 534 |
wxString exprstr = m_file_only_tags.get_static_member(curexp.type_name())->GetTypename() + curexp.operand_string(); |
| 535 |
std::vector<CxxExpression> expr_arr = from_expression(exprstr, nullptr); |
| 536 |
return resolve_compound_expression(expr_arr, visible_scopes, curexp); |
| 537 |
} |
| 538 |
|
| 539 |
TagEntryPtr CxxCodeCompletion::on_this(CxxExpression& curexp, const std::vector<wxString>& visible_scopes) |
| 540 |
{ |
| 541 |
// this can only work with -> |
| 542 |
if(curexp.operand_string() != "->") { |
| 543 |
return nullptr; |
| 544 |
} |
| 545 |
|
| 546 |
// replace "this" with the current scope name |
| 547 |
determine_current_scope(); |
| 548 |
wxString current_scope_name = m_current_container_tag ? m_current_container_tag->GetPath() : wxString(); |
| 549 |
wxString exprstr = current_scope_name + curexp.operand_string(); |
| 550 |
std::vector<CxxExpression> expr_arr = from_expression(exprstr, nullptr); |
| 551 |
return resolve_compound_expression(expr_arr, visible_scopes, curexp); |
| 552 |
} |
| 553 |
TagEntryPtr CxxCodeCompletion::find_scope_tag_externvar(CxxExpression& curexp, |
| 554 |
const std::vector<wxString>& visible_scopes) |
| 555 |
{ |
| 556 |
auto extern_var = lookup_symbol_by_kind(curexp.type_name(), visible_scopes, { "externvar" }); |
| 557 |
if(extern_var) { |
| 558 |
// we are inside a scope, use the scope as the parent |
| 559 |
// and call resolve_expression() again |
| 560 |
return extern_var; |
| 561 |
} |
| 562 |
return nullptr; |
| 563 |
} |
| 564 |
|
| 565 |
TagEntryPtr CxxCodeCompletion::find_scope_tag(CxxExpression& curexp, const std::vector<wxString>& visible_scopes) |
| 566 |
{ |
| 567 |
determine_current_scope(); |
| 568 |
|
| 569 |
// try to load the symbol this order: |
| 570 |
// current-scope::name (if we are in a scope) |
| 571 |
// other-scopes::name |
| 572 |
// global-scope::name |
| 573 |
std::vector<wxString> paths_to_try; |
| 574 |
std::vector<TagEntryPtr> parent_tags; |
| 575 |
wxStringSet_t visited; |
| 576 |
if(m_current_container_tag) { |
| 577 |
// get list of scopes to try (including parents) |
| 578 |
parent_tags = get_scopes(m_current_container_tag, visible_scopes); |
| 579 |
paths_to_try.reserve(parent_tags.size()); |
| 580 |
for(auto parent : parent_tags) { |
| 581 |
wxString fullpath = parent->GetPath() + "::" + curexp.type_name(); |
| 582 |
if(visited.insert(fullpath).second) { |
| 583 |
paths_to_try.push_back(fullpath); |
| 584 |
} |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
// add the other scopes |
| 589 |
for(auto scope : visible_scopes) { |
| 590 |
// build the path |
| 591 |
wxString path_to_try; |
| 592 |
if(!scope.empty()) { |
| 593 |
path_to_try << scope << "::"; |
| 594 |
} |
| 595 |
path_to_try << curexp.type_name(); |
| 596 |
if(visited.insert(path_to_try).second) { |
| 597 |
paths_to_try.push_back(path_to_try); |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
for(const auto& path_to_try : paths_to_try) { |
| 602 |
// note that we pass here empty visible_scopes since we already prepended it earlier |
| 603 |
auto scope_tag = |
| 604 |
lookup_symbol_by_kind(path_to_try, {}, { "class", "struct", "union", "prototype", "function", "member" }); |
| 605 |
if(scope_tag) { |
| 606 |
// we are inside a scope, use the scope as the parent |
| 607 |
// and call resolve_expression() again |
| 608 |
return scope_tag; |
| 609 |
} |
| 610 |
} |
| 611 |
return nullptr; |
| 612 |
} |
| 613 |
|
| 614 |
TagEntryPtr CxxCodeCompletion::resolve_expression(CxxExpression& curexp, TagEntryPtr parent, |
| 615 |
const std::vector<wxString>& visible_scopes) |
| 616 |
{ |
| 617 |
// test locals first, if its empty, its the first time we are entering here |
| 618 |
if(m_first_time && !parent) { |
| 619 |
if(curexp.is_this()) { |
| 620 |
return on_this(curexp, visible_scopes); |
| 621 |
|
| 622 |
} else if(curexp.operand_string() == "." || curexp.operand_string() == "->") { |
| 623 |
if(m_locals.count(curexp.type_name())) { |
| 624 |
return on_local(curexp, visible_scopes); |
| 625 |
|
| 626 |
} else if(m_file_only_tags.is_static_member(curexp.type_name())) { |
| 627 |
// static member to this file |
| 628 |
return on_static_local(curexp, visible_scopes); |
| 629 |
} else if(m_file_only_tags.is_function_parameter(curexp.type_name())) { |
| 630 |
// function parameter |
| 631 |
return on_parameter(curexp, visible_scopes); |
| 632 |
} else if(is_scope_tag(curexp, visible_scopes)) { |
| 633 |
// scoped tag (static variable, anonymous namespace tag etc) |
| 634 |
return resolve_expression(curexp, find_scope_tag(curexp, visible_scopes), visible_scopes); |
| 635 |
} else { |
| 636 |
auto externvar_tag = find_scope_tag_externvar(curexp, visible_scopes); |
| 637 |
if(externvar_tag) { |
| 638 |
return on_extern_var(curexp, externvar_tag, visible_scopes); |
| 639 |
} |
| 640 |
} |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
// update the visible scopes |
| 645 |
std::vector<wxString> scopes = update_visible_scope(visible_scopes, parent); |
| 646 |
|
| 647 |
auto resolved = lookup_symbol(curexp, scopes, parent); |
| 648 |
CHECK_PTR_RET_NULL(resolved); |
| 649 |
|
| 650 |
if(resolved->IsContainer()) { |
| 651 |
// nothing more to be done here |
| 652 |
return resolved; |
| 653 |
} |
| 654 |
|
| 655 |
// continue only if one of 3: member, method or typedef |
| 656 |
if(!resolved->IsMethod() && !resolved->IsMember() && !resolved->IsTypedef()) { |
| 657 |
return nullptr; |
| 658 |
} |
| 659 |
|
| 660 |
// after we resolved the expression, update the scope |
| 661 |
scopes = update_visible_scope(scopes, resolved); |
| 662 |
|
| 663 |
if(resolved->IsMethod()) { |
| 664 |
return on_method(curexp, resolved, scopes); |
| 665 |
|
| 666 |
} else if(resolved->IsTypedef()) { |
| 667 |
return on_typedef(curexp, resolved, scopes); |
| 668 |
|
| 669 |
} else if(resolved->IsMember()) { |
| 670 |
return on_member(curexp, resolved, scopes); |
| 671 |
} |
| 672 |
|
| 673 |
return nullptr; |
| 674 |
} |
| 675 |
|
| 676 |
TagEntryPtr CxxCodeCompletion::on_typedef(CxxExpression& curexp, TagEntryPtr tag, |
| 677 |
const std::vector<wxString>& visible_scopes) |
| 678 |
{ |
| 679 |
// substitude the type with the typeref |
| 680 |
wxString new_expr; |
| 681 |
if(!resolve_user_type(tag->GetPath(), visible_scopes, &new_expr)) { |
| 682 |
new_expr = typedef_from_tag(tag); |
| 683 |
} |
| 684 |
new_expr += curexp.operand_string(); |
| 685 |
std::vector<CxxExpression> expr_arr = from_expression(new_expr, nullptr); |
| 686 |
return resolve_compound_expression(expr_arr, visible_scopes, curexp); |
| 687 |
} |
| 688 |
|
| 689 |
TagEntryPtr CxxCodeCompletion::on_member(CxxExpression& curexp, TagEntryPtr tag, |
| 690 |
const std::vector<wxString>& visible_scopes) |
| 691 |
{ |
| 692 |
// replace the member variable by its type |
| 693 |
std::unordered_map<wxString, __local> locals_variables; |
| 694 |
if((parse_locals(normalize_pattern(tag), &locals_variables) == 0) || |
| 695 |
(locals_variables.count(tag->GetName()) == 0)) { |
| 696 |
return nullptr; |
| 697 |
} |
| 698 |
|
| 699 |
wxString new_expr = locals_variables[tag->GetName()].type_name() + curexp.operand_string(); |
| 700 |
std::vector<CxxExpression> expr_arr = from_expression(new_expr, nullptr); |
| 701 |
return resolve_compound_expression(expr_arr, visible_scopes, curexp); |
| 702 |
} |
| 703 |
|
| 704 |
TagEntryPtr CxxCodeCompletion::on_method(CxxExpression& curexp, TagEntryPtr tag, |
| 705 |
const std::vector<wxString>& visible_scopes) |
| 706 |
{ |
| 707 |
// parse the return value |
| 708 |
wxString new_expr = get_return_value(tag) + curexp.operand_string(); |
| 709 |
std::vector<CxxExpression> expr_arr = from_expression(new_expr, nullptr); |
| 710 |
return resolve_compound_expression(expr_arr, visible_scopes, curexp); |
| 711 |
} |
| 712 |
|
| 713 |
const wxStringMap_t& CxxCodeCompletion::get_tokens_map() const { return m_macros_table_map; } |
| 714 |
|
| 715 |
wxString CxxCodeCompletion::get_return_value(TagEntryPtr tag) const |
| 716 |
{ |
| 717 |
return tag->GetTypename(); |
| 718 |
// wxString pattern = normalize_pattern(tag); |
| 719 |
// wxString return_value = do_get_return_value(pattern, tag->GetName()); |
| 720 |
// if(return_value.empty()) { |
| 721 |
// |
| 722 |
// return_value = do_get_return_value(pattern, tag->GetName()); |
| 723 |
// } |
| 724 |
// return return_value; |
| 725 |
} |
| 726 |
|
| 727 |
namespace |
| 728 |
{ |
| 729 |
void remove_template_instantiation(std::vector<std::pair<int, wxString>>& tokens) |
| 730 |
{ |
| 731 |
bool is_template_inst = !tokens.empty() && tokens.back().first == '>'; |
| 732 |
if(!is_template_inst) { |
| 733 |
return; |
| 734 |
} |
| 735 |
|
| 736 |
// remove the open angle bracket |
| 737 |
tokens.pop_back(); |
| 738 |
|
| 739 |
int depth = 1; |
| 740 |
bool cont = true; |
| 741 |
while(cont && !tokens.empty()) { |
| 742 |
int type = tokens.back().first; |
| 743 |
switch(type) { |
| 744 |
case '<': |
| 745 |
depth--; |
| 746 |
if(depth == 0) { |
| 747 |
cont = false; |
| 748 |
} |
| 749 |
tokens.pop_back(); |
| 750 |
break; |
| 751 |
case '>': |
| 752 |
depth++; |
| 753 |
tokens.pop_back(); |
| 754 |
break; |
| 755 |
default: |
| 756 |
tokens.pop_back(); |
| 757 |
break; |
| 758 |
} |
| 759 |
} |
| 760 |
} |
| 761 |
} // namespace |
| 762 |
wxString CxxCodeCompletion::do_get_return_value(const wxString& pattern, const wxString& name) const |
| 763 |
{ |
| 764 |
// parse the function and extract the return type |
| 765 |
CxxTokenizer tokenizer; |
| 766 |
CxxLexerToken token; |
| 767 |
tokenizer.Reset(pattern); |
| 768 |
|
| 769 |
// strip template definition if any |
| 770 |
wxString definition; |
| 771 |
read_template_definition(tokenizer, &definition); |
| 772 |
|
| 773 |
// when to stop? |
| 774 |
// when we find our function name |
| 775 |
int depth = 0; |
| 776 |
bool cont = true; |
| 777 |
std::vector<std::pair<int, wxString>> tokens; |
| 778 |
const wxString& function_name = name; |
| 779 |
wxString peeked_token; |
| 780 |
while(cont && tokenizer.NextToken(token)) { |
| 781 |
wxString token_str = token.GetWXString(); |
| 782 |
switch(token.GetType()) { |
| 783 |
case T_OPERATOR: |
| 784 |
// operator method, we can stop now |
| 785 |
cont = false; |
| 786 |
break; |
| 787 |
case T_IDENTIFIER: |
| 788 |
if(depth == 0) { |
| 789 |
if(token_str == function_name && tokenizer.PeekToken(peeked_token) == '(') { |
| 790 |
// found "foo(" |
| 791 |
cont = false; |
| 792 |
break; |
| 793 |
} else { |
| 794 |
tokens.push_back({ token.GetType(), token_str }); |
| 795 |
} |
| 796 |
} else { |
| 797 |
tokens.push_back({ token.GetType(), token_str }); |
| 798 |
} |
| 799 |
break; |
| 800 |
case '<': |
| 801 |
case '[': |
| 802 |
case '{': |
| 803 |
case '(': |
| 804 |
depth++; |
| 805 |
tokens.push_back({ token.GetType(), token_str }); |
| 806 |
break; |
| 807 |
case '>': |
| 808 |
case ']': |
| 809 |
case '}': |
| 810 |
case ')': |
| 811 |
depth--; |
| 812 |
tokens.push_back({ token.GetType(), token_str }); |
| 813 |
break; |
| 814 |
default: |
| 815 |
tokens.push_back({ token.GetType(), token_str }); |
| 816 |
break; |
| 817 |
} |
| 818 |
} |
| 819 |
|
| 820 |
if(tokens.empty()) { |
| 821 |
return ""; |
| 822 |
} |
| 823 |
|
| 824 |
// remove the scope tokens |
| 825 |
// assume the signature looks like this: |
| 826 |
// |
| 827 |
// ``` |
| 828 |
// vector<string> foo::bar::baz::koo::kookoo:: |
| 829 |
// ``` |
| 830 |
// |
| 831 |
// check if the last token is T_DOUBLE_COLONS -> |
| 832 |
// remove it and the next one after it |
| 833 |
// another case: |
| 834 |
// ``` |
| 835 |
// template<typename T> T* CLASS<T>::FUNC |
| 836 |
// ``` |
| 837 |
while(!tokens.empty()) { |
| 838 |
if(tokens.back().first == T_DOUBLE_COLONS) { |
| 839 |
tokens.pop_back(); |
| 840 |
// handle template instantiation e.g. `template<typename T> T* CLASS<T>::FUNC` |
| 841 |
remove_template_instantiation(tokens); |
| 842 |
if(!tokens.empty()) { |
| 843 |
tokens.pop_back(); |
| 844 |
} |
| 845 |
} else { |
| 846 |
break; |
| 847 |
} |
| 848 |
} |
| 849 |
|
| 850 |
// conver the array into string |
| 851 |
wxString as_str; |
| 852 |
int last_type = 0; |
| 853 |
for(const auto& d : tokens) { |
| 854 |
CxxLexerToken t; |
| 855 |
t.SetType(d.first); |
| 856 |
if(t.is_keyword() || t.is_builtin_type()) { |
| 857 |
as_str << d.second << " "; |
| 858 |
} else if(d.first == T_IDENTIFIER && last_type == T_IDENTIFIER) { |
| 859 |
as_str << " " << d.second; |
| 860 |
} else { |
| 861 |
as_str << d.second; |
| 862 |
} |
| 863 |
|
| 864 |
last_type = d.first; |
| 865 |
} |
| 866 |
return as_str; |
| 867 |
} |
| 868 |
|
| 869 |
void CxxCodeCompletion::prepend_scope(std::vector<wxString>& scopes, const wxString& scope) const |
| 870 |
{ |
| 871 |
auto find_and_erase_cb = [&scopes](const wxString& scope_name) { |
| 872 |
auto where = find_if(scopes.begin(), scopes.end(), [=](const wxString& __s) { return __s == scope_name; }); |
| 873 |
if(where != scopes.end()) { |
| 874 |
scopes.erase(where); |
| 875 |
} |
| 876 |
}; |
| 877 |
|
| 878 |
// if this is a complex scope, break it and add all possible values |
| 879 |
wxArrayString parts = ::wxStringTokenize(scope, ":", wxStringTokenizerMode::wxTOKEN_STRTOK); |
| 880 |
wxString scope_name; |
| 881 |
for(const wxString& part : parts) { |
| 882 |
if(!scope_name.empty()) { |
| 883 |
scope_name << "::"; |
| 884 |
} |
| 885 |
scope_name << part; |
| 886 |
// LSP::Params |
| 887 |
// LSP |
| 888 |
find_and_erase_cb(scope_name); |
| 889 |
scopes.insert(scopes.begin(), scope_name); |
| 890 |
} |
| 891 |
} |
| 892 |
|
| 893 |
void CxxCodeCompletion::reset() |
| 894 |
{ |
| 895 |
m_locals.clear(); |
| 896 |
m_template_manager->clear(); |
| 897 |
m_file_only_tags.clear(); |
| 898 |
m_recurse_protector = 0; |
| 899 |
m_current_function_tag.Reset(nullptr); |
| 900 |
m_current_container_tag.Reset(nullptr); |
| 901 |
} |
| 902 |
|
| 903 |
namespace |
| 904 |
{ |
| 905 |
wxString read_n_lines_from_file(size_t from_line, size_t line_count, const wxString& filepath) |
| 906 |
{ |
| 907 |
wxString content; |
| 908 |
if(!FileUtils::ReadFileContent(filepath, content)) { |
| 909 |
return wxEmptyString; |
| 910 |
} |
| 911 |
wxArrayString lines = ::wxStringTokenize(content, "\n", wxTOKEN_RET_EMPTY_ALL); |
| 912 |
if(from_line >= lines.size()) { |
| 913 |
return wxEmptyString; |
| 914 |
} |
| 915 |
size_t first_line = from_line; |
| 916 |
size_t last_line = wxMin(from_line + line_count, lines.size() - 1); |
| 917 |
|
| 918 |
content.Clear(); |
| 919 |
for(size_t i = first_line; i < last_line; ++i) { |
| 920 |
content << lines[i]; |
| 921 |
} |
| 922 |
return content; |
| 923 |
} |
| 924 |
} // namespace |
| 925 |
|
| 926 |
wxString CxxCodeCompletion::typedef_from_tag(TagEntryPtr tag) const |
| 927 |
{ |
| 928 |
wxString typedef_str; |
| 929 |
CxxTokenizer tkzr; |
| 930 |
CxxLexerToken tk; |
| 931 |
|
| 932 |
if(!tag->GetTypename().empty()) { |
| 933 |
typedef_str = tag->GetTypename(); |
| 934 |
return typedef_str.Trim(); |
| 935 |
} |
| 936 |
|
| 937 |
wxString pattern = normalize_pattern(tag); |
| 938 |
tkzr.Reset(pattern); |
| 939 |
|
| 940 |
std::vector<wxString> V; |
| 941 |
wxString last_identifier; |
| 942 |
// consume the first token which is one of T_USING | T_TYPEDEF |
| 943 |
tkzr.NextToken(tk); |
| 944 |
|
| 945 |
bool parse_succeeded = false; |
| 946 |
if(tk.GetType() == T_TEMPLATE) { |
| 947 |
// instead of reading the pattern, read 10 lines from the source code and use that instead |
| 948 |
// (ctags is not that good at collecting typedef properly) |
| 949 |
pattern = read_n_lines_from_file(tag->GetLine() - 1, 10, tag->GetFile()); |
| 950 |
clDEBUG() << "read 10 lines from file:" << tag->GetLine() << endl; |
| 951 |
clDEBUG() << pattern << endl; |
| 952 |
// reset the tokenizer with the new buffer |
| 953 |
tkzr.Reset(pattern); |
| 954 |
tkzr.NextToken(tk); |
| 955 |
} |
| 956 |
|
| 957 |
if(tk.GetType() == T_USING) { |
| 958 |
// "using" syntax: |
| 959 |
// using element_type = _Tp; |
| 960 |
// read until the "="; |
| 961 |
|
| 962 |
// consume everything until we find the "=" |
| 963 |
while(tkzr.NextToken(tk)) { |
| 964 |
if(tk.GetType() == '=') { |
| 965 |
break; |
| 966 |
} |
| 967 |
} |
| 968 |
|
| 969 |
// read until the ";" |
| 970 |
while(tkzr.NextToken(tk)) { |
| 971 |
if(tk.GetType() == ';') { |
| 972 |
parse_succeeded = true; |
| 973 |
break; |
| 974 |
} |
| 975 |
if(tk.is_keyword()) { |
| 976 |
// dont pick keywords |
| 977 |
continue; |
| 978 |
} |
| 979 |
if(tk.is_builtin_type()) { |
| 980 |
V.push_back((V.empty() ? "" : " ") + tk.GetWXString() + " "); |
| 981 |
} else { |
| 982 |
V.push_back(tk.GetWXString()); |
| 983 |
} |
| 984 |
} |
| 985 |
|
| 986 |
} else if(tk.GetType() == T_TYPEDEF) { |
| 987 |
// standard "typedef" syntax: |
| 988 |
// typedef wxString MyString; |
| 989 |
while(tkzr.NextToken(tk)) { |
| 990 |
if(tk.is_keyword()) { |
| 991 |
continue; |
| 992 |
} |
| 993 |
if(tk.GetType() == ';') { |
| 994 |
// end of typedef |
| 995 |
if(!V.empty()) { |
| 996 |
V.pop_back(); |
| 997 |
} |
| 998 |
parse_succeeded = true; |
| 999 |
break; |
| 1000 |
} else { |
| 1001 |
if(tk.is_builtin_type()) { |
| 1002 |
V.push_back((V.empty() ? "" : " ") + tk.GetWXString() + " "); |
| 1003 |
} else { |
| 1004 |
V.push_back(tk.GetWXString()); |
| 1005 |
} |
| 1006 |
} |
| 1007 |
} |
| 1008 |
} |
| 1009 |
|
| 1010 |
if(!parse_succeeded) { |
| 1011 |
return wxEmptyString; |
| 1012 |
} else { |
| 1013 |
for(const wxString& s : V) { |
| 1014 |
typedef_str << s; |
| 1015 |
} |
| 1016 |
} |
| 1017 |
return typedef_str.Trim(); |
| 1018 |
} |
| 1019 |
|
| 1020 |
size_t CxxCodeCompletion::get_local_tags(const wxString& filter, const wxStringSet_t& kinds, |
| 1021 |
std::vector<TagEntryPtr>& tags) const |
| 1022 |
{ |
| 1023 |
return 0; |
| 1024 |
} |
| 1025 |
|
| 1026 |
std::vector<TagEntryPtr> CxxCodeCompletion::get_locals(const wxString& filter) const |
| 1027 |
{ |
| 1028 |
std::vector<TagEntryPtr> locals; |
| 1029 |
locals.reserve(m_locals.size()); |
| 1030 |
|
| 1031 |
wxString lowercase_filter = filter.Lower(); |
| 1032 |
for(const auto& vt : m_locals) { |
| 1033 |
const auto& local = vt.second; |
| 1034 |
TagEntryPtr tag(new TagEntry()); |
| 1035 |
tag->SetName(local.name()); |
| 1036 |
tag->SetKind("local"); |
| 1037 |
tag->SetParent("<local>"); |
| 1038 |
tag->SetScope(local.type_name()); |
| 1039 |
tag->SetAccess("public"); |
| 1040 |
tag->SetPattern("/^ " + local.pattern() + " $/"); |
| 1041 |
tag->SetLine(local.line_number()); |
| 1042 |
if(!tag->GetName().Lower().StartsWith(lowercase_filter)) { |
| 1043 |
continue; |
| 1044 |
} |
| 1045 |
locals.push_back(tag); |
| 1046 |
} |
| 1047 |
return locals; |
| 1048 |
} |
| 1049 |
|
| 1050 |
size_t CxxCodeCompletion::get_completions(TagEntryPtr parent, const wxString& operand_string, const wxString& filter, |
| 1051 |
std::vector<TagEntryPtr>& candidates, |
| 1052 |
const std::vector<wxString>& visible_scopes, size_t limit) |
| 1053 |
{ |
| 1054 |
if(!parent) { |
| 1055 |
return 0; |
| 1056 |
} |
| 1057 |
|
| 1058 |
std::vector<wxString> kinds = { "function", "prototype", "member", "enum", "enumerator", "class", |
| 1059 |
"struct", "union", "namespace", "typedef", "variable" }; |
| 1060 |
if(operand_string == "." || operand_string == "->") { |
| 1061 |
kinds = { "prototype", "function", "member" }; |
| 1062 |
} |
| 1063 |
// the global scope |
| 1064 |
candidates = get_children_of_scope(parent, kinds, filter, visible_scopes); |
| 1065 |
wxStringSet_t visited; |
| 1066 |
|
| 1067 |
// give prototypes a priority over functions |
| 1068 |
// why? most people will write their comment in the header |
| 1069 |
// file |
| 1070 |
std::vector<TagEntryPtr> ordered_candidates; |
| 1071 |
std::vector<TagEntryPtr> functions_candidates; // only of type `function` |
| 1072 |
|
| 1073 |
// this is done in O(n) |
| 1074 |
ordered_candidates.reserve(candidates.size()); |
| 1075 |
functions_candidates.reserve(candidates.size()); |
| 1076 |
for(TagEntryPtr tag : candidates) { |
| 1077 |
if(tag->IsFunction()) { |
| 1078 |
functions_candidates.emplace_back(tag); |
| 1079 |
} else { |
| 1080 |
ordered_candidates.emplace_back(tag); |
| 1081 |
} |
| 1082 |
} |
| 1083 |
|
| 1084 |
// join the lists placing all "functions" at the bottom |
| 1085 |
ordered_candidates.insert(ordered_candidates.end(), functions_candidates.begin(), functions_candidates.end()); |
| 1086 |
candidates.swap(ordered_candidates); |
| 1087 |
|
| 1088 |
std::vector<TagEntryPtr> unique_candidates; |
| 1089 |
unique_candidates.reserve(candidates.size()); |
| 1090 |
CompletionHelper helper; |
| 1091 |
for(TagEntryPtr tag : candidates) { |
| 1092 |
// by default, the path is enough |
| 1093 |
// however, for methods, we also include the method |
| 1094 |
// signature |
| 1095 |
wxString unique_path = tag->GetPath(); |
| 1096 |
if(tag->IsMethod()) { |
| 1097 |
unique_path = helper.normalize_function(tag); |
| 1098 |
} |
| 1099 |
|
| 1100 |
if(!visited.insert(unique_path).second) { |
| 1101 |
// already seen this |
| 1102 |
continue; |
| 1103 |
} |
| 1104 |
|
| 1105 |
unique_candidates.emplace_back(tag); |
| 1106 |
} |
| 1107 |
|
| 1108 |
// sort the matches |
| 1109 |
auto sort_cb = [=](TagEntryPtr a, TagEntryPtr b) { return a->GetName().CmpNoCase(b->GetName()) < 0; }; |
| 1110 |
sort(unique_candidates.begin(), unique_candidates.end(), sort_cb); |
| 1111 |
// truncate the list to match the limit |
| 1112 |
if(unique_candidates.size() > limit) { |
| 1113 |
unique_candidates.resize(limit); |
| 1114 |
} |
| 1115 |
candidates.swap(unique_candidates); |
| 1116 |
|
| 1117 |
// filter the results |
| 1118 |
std::vector<TagEntryPtr> sorted_tags; |
| 1119 |
sort_tags(candidates, sorted_tags, false, {}); |
| 1120 |
candidates.swap(sorted_tags); |
| 1121 |
return candidates.size(); |
| 1122 |
} |
| 1123 |
|
| 1124 |
std::vector<TagEntryPtr> CxxCodeCompletion::get_scopes(TagEntryPtr parent, const std::vector<wxString>& visible_scopes) |
| 1125 |
{ |
| 1126 |
std::vector<TagEntryPtr> scopes; |
| 1127 |
scopes.reserve(100); |
| 1128 |
|
| 1129 |
std::deque<TagEntryPtr> q; |
| 1130 |
q.push_front(parent); |
| 1131 |
wxStringSet_t visited; |
| 1132 |
|
| 1133 |
while(!q.empty()) { |
| 1134 |
auto t = q.front(); |
| 1135 |
q.pop_front(); |
| 1136 |
|
| 1137 |
// avoid visiting the same tag twice |
| 1138 |
if(!visited.insert(t->GetPath()).second) { |
| 1139 |
continue; |
| 1140 |
} |
| 1141 |
|
| 1142 |
scopes.push_back(t); |
| 1143 |
|
| 1144 |
auto parents = get_parents_of_tag_no_recurse(t, m_template_manager, visible_scopes); |
| 1145 |
q.insert(q.end(), parents.begin(), parents.end()); |
| 1146 |
} |
| 1147 |
return scopes; |
| 1148 |
} |
| 1149 |
|
| 1150 |
std::vector<TagEntryPtr> CxxCodeCompletion::get_children_of_scope(TagEntryPtr parent, |
| 1151 |
const std::vector<wxString>& kinds, |
| 1152 |
const wxString& filter, |
| 1153 |
const std::vector<wxString>& visible_scopes) |
| 1154 |
{ |
| 1155 |
if(!m_lookup) { |
| 1156 |
return {}; |
| 1157 |
} |
| 1158 |
|
| 1159 |
std::vector<TagEntryPtr> tags; |
| 1160 |
auto parents = get_scopes(parent, visible_scopes); |
| 1161 |
for(auto tag : parents) { |
| 1162 |
wxString scope = tag->GetPath(); |
| 1163 |
if(tag->IsMethod()) { |
| 1164 |
scope = tag->GetScope(); |
| 1165 |
} |
| 1166 |
std::vector<TagEntryPtr> parent_tags; |
| 1167 |
m_lookup->GetTagsByScopeAndKind(scope, to_wx_array_string(kinds), filter, parent_tags, true); |
| 1168 |
tags.reserve(tags.size() + parent_tags.size()); |
| 1169 |
tags.insert(tags.end(), parent_tags.begin(), parent_tags.end()); |
| 1170 |
} |
| 1171 |
return tags; |
| 1172 |
} |
| 1173 |
|
| 1174 |
void CxxCodeCompletion::set_text(const wxString& text, const wxString& filename, int current_line) |
| 1175 |
{ |
| 1176 |
m_locals.clear(); |
| 1177 |
m_file_only_tags.clear(); |
| 1178 |
m_filename = filename; |
| 1179 |
m_line_number = current_line; |
| 1180 |
m_current_container_tag = nullptr; |
| 1181 |
m_current_function_tag = nullptr; |
| 1182 |
|
| 1183 |
determine_current_scope(); |
| 1184 |
shrink_scope(text, &m_locals, &m_file_only_tags); |
| 1185 |
} |
| 1186 |
|
| 1187 |
namespace |
| 1188 |
{ |
| 1189 |
|
| 1190 |
bool find_wild_match(const std::vector<std::pair<wxString, wxString>>& table, const wxString& find_what, |
| 1191 |
wxString* match) |
| 1192 |
{ |
| 1193 |
for(const auto& p : table) { |
| 1194 |
// we support wildcard matching |
| 1195 |
if(::wxMatchWild(p.first, find_what)) { |
| 1196 |
*match = p.second; |
| 1197 |
return true; |
| 1198 |
} |
| 1199 |
} |
| 1200 |
return false; |
| 1201 |
} |
| 1202 |
|
| 1203 |
bool try_resovle_user_type_with_scopes(const std::vector<std::pair<wxString, wxString>>& table, const wxString& type, |
| 1204 |
const std::vector<wxString>& visible_scopes, wxString* resolved) |
| 1205 |
{ |
| 1206 |
for(const wxString& scope : visible_scopes) { |
| 1207 |
wxString user_type = scope; |
| 1208 |
if(!user_type.empty()) { |
| 1209 |
user_type << "::"; |
| 1210 |
} |
| 1211 |
user_type << type; |
| 1212 |
if(find_wild_match(table, type, resolved)) { |
| 1213 |
return true; |
| 1214 |
} |
| 1215 |
} |
| 1216 |
return false; |
| 1217 |
} |
| 1218 |
}; // namespace |
| 1219 |
|
| 1220 |
bool CxxCodeCompletion::resolve_user_type(const wxString& type, const std::vector<wxString>& visible_scopes, |
| 1221 |
wxString* resolved) const |
| 1222 |
{ |
| 1223 |
bool match_found = false; |
| 1224 |
wxStringSet_t visited; |
| 1225 |
*resolved = type; |
| 1226 |
while(true) { |
| 1227 |
if(!visited.insert(*resolved).second) { |
| 1228 |
// already tried this type |
| 1229 |
break; |
| 1230 |
} |
| 1231 |
|
| 1232 |
if(!try_resovle_user_type_with_scopes(m_types_table, *resolved, visible_scopes, resolved)) { |
| 1233 |
break; |
| 1234 |
} |
| 1235 |
match_found = true; |
| 1236 |
} |
| 1237 |
|
| 1238 |
if(match_found) { |
| 1239 |
return true; |
| 1240 |
} |
| 1241 |
return false; |
| 1242 |
} |
| 1243 |
|
| 1244 |
void TemplateManager::clear() { m_table.clear(); } |
| 1245 |
|
| 1246 |
void TemplateManager::add_placeholders(const wxStringMap_t& table, const std::vector<wxString>& visible_scopes) |
| 1247 |
{ |
| 1248 |
|
| 1249 |
// try to resolve any of the template before we insert them |
| 1250 |
// its important to do it now so we use the correct scope |
| 1251 |
wxStringMap_t M; |
| 1252 |
for(const auto& vt : table) { |
| 1253 |
wxString name = vt.first; |
| 1254 |
wxString value; |
| 1255 |
|
| 1256 |
auto resolved = m_completer->lookup_child_symbol( |
| 1257 |
nullptr, nullptr, vt.second, visible_scopes, |
| 1258 |
{ "class", "struct", "typedef", "union", "namespace", "enum", "enumerator" }); |
| 1259 |
if(resolved) { |
| 1260 |
// use the path found in the db |
| 1261 |
value = resolved->GetPath(); |
| 1262 |
} else { |
| 1263 |
// lets try and avoid pushing values that are templates |
| 1264 |
// consider |
| 1265 |
// template <typename _Tp> class vector : protected _Vector_base<_Tp> {..} |
| 1266 |
// Looking at the definitio of _Vector_base: |
| 1267 |
// template <typename _Tp> class _Vector_base {...} |
| 1268 |
// this will cause us to push {"_Tp", "_Tp"} (where _Tp is both the key and value) |
| 1269 |
// if the resolve will fail, it will return vt.second unmodified |
| 1270 |
value = this->resolve(vt.second, visible_scopes); |
| 1271 |
} |
| 1272 |
M.insert({ name, value }); |
| 1273 |
} |
| 1274 |
m_table.insert(m_table.begin(), M); |
| 1275 |
} |
| 1276 |
|
| 1277 |
#define STRIP_PLACEHOLDER(__ph) \ |
| 1278 |
stripped_placeholder.Replace("*", wxEmptyString); \ |
| 1279 |
stripped_placeholder.Replace("->", wxEmptyString); \ |
| 1280 |
stripped_placeholder.Replace("&&", wxEmptyString); |
| 1281 |
|
| 1282 |
namespace |
| 1283 |
{ |
| 1284 |
bool try_resolve_placeholder(const wxStringMap_t& table, const wxString& name, wxString* resolved) |
| 1285 |
{ |
| 1286 |
// strip operands from `s` |
| 1287 |
wxString stripped_placeholder = name; |
| 1288 |
STRIP_PLACEHOLDER(stripped_placeholder); |
| 1289 |
|
| 1290 |
if(table.count(name)) { |
| 1291 |
*resolved = table.find(name)->second; |
| 1292 |
return true; |
| 1293 |
} |
| 1294 |
return false; |
| 1295 |
} |
| 1296 |
}; // namespace |
| 1297 |
|
| 1298 |
wxString TemplateManager::resolve(const wxString& name, const std::vector<wxString>& visible_scopes) const |
| 1299 |
{ |
| 1300 |
wxStringSet_t visited; |
| 1301 |
wxString resolved = name; |
| 1302 |
for(const wxStringMap_t& table : m_table) { |
| 1303 |
try_resolve_placeholder(table, resolved, &resolved); |
| 1304 |
} |
| 1305 |
return resolved; |
| 1306 |
} |
| 1307 |
|
| 1308 |
void CxxCodeCompletion::set_macros_table(const std::vector<std::pair<wxString, wxString>>& t) |
| 1309 |
{ |
| 1310 |
m_macros_table = t; |
| 1311 |
m_macros_table_map.reserve(m_macros_table.size()); |
| 1312 |
for(const auto& d : m_macros_table) { |
| 1313 |
m_macros_table_map.insert(d); |
| 1314 |
} |
| 1315 |
} |
| 1316 |
|
| 1317 |
void CxxCodeCompletion::sort_tags(const std::vector<TagEntryPtr>& tags, std::vector<TagEntryPtr>& sorted_tags, |
| 1318 |
bool include_ctor_dtor, const wxStringSet_t& visible_files) |
| 1319 |
{ |
| 1320 |
TagEntryPtrVector_t publicTags; |
| 1321 |
TagEntryPtrVector_t protectedTags; |
| 1322 |
TagEntryPtrVector_t privateTags; |
| 1323 |
TagEntryPtrVector_t locals; |
| 1324 |
TagEntryPtrVector_t members; |
| 1325 |
|
| 1326 |
bool skip_tor = !include_ctor_dtor; |
| 1327 |
|
| 1328 |
// first: remove duplicates |
| 1329 |
std::unordered_set<int> visited_by_id; |
| 1330 |
std::unordered_set<wxString> visited_by_name; |
| 1331 |
|
| 1332 |
for(size_t i = 0; i < tags.size(); ++i) { |
| 1333 |
TagEntryPtr tag = tags[i]; |
| 1334 |
|
| 1335 |
// only include matches from the provided list of files |
| 1336 |
if(!visible_files.empty() && visible_files.count(tag->GetFile()) == 0) { |
| 1337 |
continue; |
| 1338 |
} |
| 1339 |
|
| 1340 |
if(skip_tor && (tag->IsConstructor() || tag->IsDestructor())) { |
| 1341 |
continue; |
| 1342 |
} |
| 1343 |
|
| 1344 |
bool is_local = tag->IsLocalVariable() || tag->GetScope() == "<local>" || tag->GetParent() == "<local>"; |
| 1345 |
// we filter local tags by name |
| 1346 |
if(is_local && !visited_by_name.insert(tag->GetName()).second) { |
| 1347 |
continue; |
| 1348 |
} |
| 1349 |
|
| 1350 |
// other tags (loaded from the db) are filtered by their ID |
| 1351 |
if(!is_local && !visited_by_id.insert(tag->GetId()).second) { |
| 1352 |
continue; |
| 1353 |
} |
| 1354 |
|
| 1355 |
// filter by type/access |
| 1356 |
wxString access = tag->GetAccess(); |
| 1357 |
wxString kind = tag->GetKind(); |
| 1358 |
|
| 1359 |
if(kind == "variable") { |
| 1360 |
locals.push_back(tag); |
| 1361 |
|
| 1362 |
} else if(kind == "member") { |
| 1363 |
members.push_back(tag); |
| 1364 |
|
| 1365 |
} else if(access == "private") { |
| 1366 |
privateTags.push_back(tag); |
| 1367 |
|
| 1368 |
} else if(access == "protected") { |
| 1369 |
protectedTags.push_back(tag); |
| 1370 |
|
| 1371 |
} else if(access == "public") { |
| 1372 |
if(tag->GetName().StartsWith("_") || tag->GetName().Contains("operator")) { |
| 1373 |
// methods starting with _ usually are meant to be private |
| 1374 |
// and also, put the "operator" methdos at the bottom |
| 1375 |
privateTags.push_back(tag); |
| 1376 |
} else { |
| 1377 |
publicTags.push_back(tag); |
| 1378 |
} |
| 1379 |
} else { |
| 1380 |
// assume private |
| 1381 |
privateTags.push_back(tag); |
| 1382 |
} |
| 1383 |
} |
| 1384 |
|
| 1385 |
auto sort_func = [=](TagEntryPtr a, TagEntryPtr b) { return a->GetName().CmpNoCase(b->GetName()) < 0; }; |
| 1386 |
|
| 1387 |
sort(privateTags.begin(), privateTags.end(), sort_func); |
| 1388 |
sort(publicTags.begin(), publicTags.end(), sort_func); |
| 1389 |
sort(protectedTags.begin(), protectedTags.end(), sort_func); |
| 1390 |
sort(members.begin(), members.end(), sort_func); |
| 1391 |
sort(locals.begin(), locals.end(), sort_func); |
| 1392 |
|
| 1393 |
sorted_tags.clear(); |
| 1394 |
sorted_tags.reserve(locals.size() + publicTags.size() + protectedTags.size() + privateTags.size() + members.size()); |
| 1395 |
sorted_tags.insert(sorted_tags.end(), locals.begin(), locals.end()); |
| 1396 |
sorted_tags.insert(sorted_tags.end(), publicTags.begin(), publicTags.end()); |
| 1397 |
sorted_tags.insert(sorted_tags.end(), protectedTags.begin(), protectedTags.end()); |
| 1398 |
sorted_tags.insert(sorted_tags.end(), privateTags.begin(), privateTags.end()); |
| 1399 |
sorted_tags.insert(sorted_tags.end(), members.begin(), members.end()); |
| 1400 |
} |
| 1401 |
|
| 1402 |
size_t CxxCodeCompletion::get_file_completions(const wxString& user_typed, std::vector<TagEntryPtr>& files, |
| 1403 |
const wxString& suffix) |
| 1404 |
{ |
| 1405 |
if(!m_lookup) { |
| 1406 |
return 0; |
| 1407 |
} |
| 1408 |
wxArrayString files_arr; |
| 1409 |
m_lookup->GetFilesForCC(user_typed, files_arr); |
| 1410 |
|
| 1411 |
wxString prefix; |
| 1412 |
if (user_typed.find('/') != user_typed.npos) { |
| 1413 |
prefix = user_typed.BeforeLast('/').append('/'); |
| 1414 |
} |
| 1415 |
|
| 1416 |
files.reserve(files_arr.size()); |
| 1417 |
for(const wxString& file : files_arr) { |
| 1418 |
// exclude source file |
| 1419 |
if(FileExtManager::GetType(file) == FileExtManager::TypeSourceC || |
| 1420 |
FileExtManager::GetType(file) == FileExtManager::TypeSourceCpp) { |
| 1421 |
continue; |
| 1422 |
} |
| 1423 |
TagEntryPtr tag(new TagEntry()); |
| 1424 |
|
| 1425 |
wxString display_name = file + suffix; |
| 1426 |
tag->SetKind("file"); |
| 1427 |
tag->SetName(display_name); // display name |
| 1428 |
if (display_name.StartsWith(prefix)) { |
| 1429 |
display_name = display_name.substr(prefix.length()); |
| 1430 |
} else { |
| 1431 |
display_name = display_name.AfterLast('/'); |
| 1432 |
} |
| 1433 |
tag->SetPattern(display_name); // insert text |
| 1434 |
tag->SetLine(-1); |
| 1435 |
files.push_back(tag); |
| 1436 |
} |
| 1437 |
return files.size(); |
| 1438 |
} |
| 1439 |
|
| 1440 |
size_t CxxCodeCompletion::get_children_of_current_scope(const std::vector<wxString>& kinds, const wxString& filter, |
| 1441 |
const std::vector<wxString>& visible_scopes, |
| 1442 |
std::vector<TagEntryPtr>* current_scope_children, |
| 1443 |
std::vector<TagEntryPtr>* other_scopes_children, |
| 1444 |
std::vector<TagEntryPtr>* global_scope_children) |
| 1445 |
{ |
| 1446 |
determine_current_scope(); |
| 1447 |
if(m_current_container_tag) { |
| 1448 |
*current_scope_children = get_children_of_scope(m_current_container_tag, kinds, filter, visible_scopes); |
| 1449 |
} |
| 1450 |
|
| 1451 |
// collect "other scopes" |
| 1452 |
wxArrayString wx_kinds = to_wx_array_string(kinds); |
| 1453 |
wxArrayString wx_scope = to_wx_array_string(visible_scopes); |
| 1454 |
m_lookup->GetTagsByScopeAndName(wx_scope, filter, true, *other_scopes_children); |
| 1455 |
|
| 1456 |
// global scope children |
| 1457 |
*global_scope_children = get_children_of_scope(create_global_scope_tag(), kinds, filter, visible_scopes); |
| 1458 |
return current_scope_children->size() + other_scopes_children->size() + global_scope_children->size(); |
| 1459 |
} |
| 1460 |
|
| 1461 |
size_t CxxCodeCompletion::get_word_completions(const CxxRemainder& remainder, std::vector<TagEntryPtr>& candidates, |
| 1462 |
const std::vector<wxString>& visible_scopes, |
| 1463 |
const wxStringSet_t& visible_files) |
| 1464 |
|
| 1465 |
{ |
| 1466 |
std::vector<TagEntryPtr> locals; |
| 1467 |
std::vector<TagEntryPtr> function_parameters; |
| 1468 |
std::vector<TagEntryPtr> keywords; |
| 1469 |
std::vector<TagEntryPtr> scope_members; |
| 1470 |
std::vector<TagEntryPtr> other_scopes_members; |
| 1471 |
std::vector<TagEntryPtr> global_scopes_members; |
| 1472 |
|
| 1473 |
std::vector<TagEntryPtr> sorted_locals; |
| 1474 |
std::vector<TagEntryPtr> sorted_scope_members; |
| 1475 |
std::vector<TagEntryPtr> sorted_other_scopes_members; |
| 1476 |
std::vector<TagEntryPtr> sorted_global_scopes_members; |
| 1477 |
|
| 1478 |
// include the file scopes |
| 1479 |
std::vector<wxString> scopes = prepend_extra_scopes(visible_scopes); |
| 1480 |
|
| 1481 |
locals = get_locals(remainder.filter); |
| 1482 |
function_parameters = m_file_only_tags.get_function_parameters(remainder.filter); |
| 1483 |
|
| 1484 |
std::vector<wxString> kinds; |
| 1485 |
// based on the lasts operand, build the list of items to fetch |
| 1486 |
determine_current_scope(); |
| 1487 |
bool add_keywords = false; |
| 1488 |
if(remainder.operand_string.empty()) { |
| 1489 |
kinds = { "function", "prototype", "class", "struct", "namespace", "union", "typedef", |
| 1490 |
"enum", "enumerator", "macro", "cenum", "variable", "externvar" }; |
| 1491 |
if(m_current_container_tag) { |
| 1492 |
// if we are inside a scope, add member types |
| 1493 |
kinds.push_back("member"); |
| 1494 |
} |
| 1495 |
add_keywords = true; |
| 1496 |
} else if(remainder.operand_string == "::") { |
| 1497 |
kinds = { "member", "function", "prototype", "class", "struct", |
| 1498 |
"namespace", "union", "typedef", "enum", "enumerator" }; |
| 1499 |
} else { |
| 1500 |
// contextual completions |
| 1501 |
kinds = { "member", "function", "prototype" }; |
| 1502 |
} |
| 1503 |
|
| 1504 |
// collect member variabels if we are within a scope |
| 1505 |
get_children_of_current_scope(kinds, remainder.filter, scopes, &scope_members, &other_scopes_members, |
| 1506 |
&global_scopes_members); |
| 1507 |
|
| 1508 |
// sort the matches: |
| 1509 |
sort_tags(locals, sorted_locals, true, {}); // locals are accepted, so dont pass list of files |
| 1510 |
sort_tags(scope_members, sorted_scope_members, true, {}); // members are all accepted |
| 1511 |
sort_tags(other_scopes_members, sorted_other_scopes_members, true, visible_files); |
| 1512 |
sort_tags(global_scopes_members, sorted_global_scopes_members, true, visible_files); |
| 1513 |
|
| 1514 |
if(add_keywords) { |
| 1515 |
get_keywords_tags(remainder.filter, keywords); |
| 1516 |
} |
| 1517 |
candidates.reserve(sorted_locals.size() + sorted_scope_members.size() + sorted_other_scopes_members.size() + |
| 1518 |
sorted_global_scopes_members.size() + keywords.size() + function_parameters.size()); |
| 1519 |
|
| 1520 |
// place the keywords first |
| 1521 |
candidates.insert(candidates.end(), sorted_locals.begin(), sorted_locals.end()); |
| 1522 |
candidates.insert(candidates.end(), function_parameters.begin(), function_parameters.end()); |
| 1523 |
candidates.insert(candidates.end(), keywords.begin(), keywords.end()); |
| 1524 |
candidates.insert(candidates.end(), sorted_scope_members.begin(), sorted_scope_members.end()); |
| 1525 |
candidates.insert(candidates.end(), sorted_other_scopes_members.begin(), sorted_other_scopes_members.end()); |
| 1526 |
candidates.insert(candidates.end(), sorted_global_scopes_members.begin(), sorted_global_scopes_members.end()); |
| 1527 |
return candidates.size(); |
| 1528 |
} |
| 1529 |
|
| 1530 |
size_t CxxCodeCompletion::find_definition(const wxString& filepath, int line, const wxString& expression, |
| 1531 |
const wxString& text, const std::vector<wxString>& visible_scopes, |
| 1532 |
std::vector<TagEntryPtr>& matches) |
| 1533 |
{ |
| 1534 |
std::vector<TagEntryPtr> candidates; |
| 1535 |
// optimization: |
| 1536 |
// check if we are currently (file:line) on a function definition / declaration |
| 1537 |
std::vector<TagEntryPtr> tmp_candidates; |
| 1538 |
m_lookup->GetTagsByFileAndLine(filepath, line, tmp_candidates); |
| 1539 |
if((tmp_candidates.size() == 1) && (tmp_candidates[0]->GetName() == expression) && |
| 1540 |
(tmp_candidates[0]->IsMethod())) { |
| 1541 |
// we found our tag, now |
| 1542 |
candidates.swap(tmp_candidates); |
| 1543 |
} else { |
| 1544 |
// first check if we are on a line |
| 1545 |
clDEBUG() << "find_definition(): calling word_complete(): is called for expression:" << expression << endl; |
| 1546 |
word_complete(filepath, line, expression, text, visible_scopes, true, candidates); |
| 1547 |
// filter all the tags |
| 1548 |
if(candidates.empty() || (candidates.size() == 1 && (candidates[0]->GetLine() == wxNOT_FOUND))) { |
| 1549 |
clDEBUG() << "Unable to complete, checking on the current lcoation" << endl; |
| 1550 |
candidates.clear(); |
| 1551 |
m_lookup->GetTagsByFileAndLine(filepath, line, candidates); |
| 1552 |
if(candidates.empty()) { |
| 1553 |
return 0; |
| 1554 |
} |
| 1555 |
clDEBUG() << "find_definition(): on a tag:" << candidates[0]->GetFullDisplayName() << "." |
| 1556 |
<< candidates[0]->IsMethod() << endl; |
| 1557 |
} |
| 1558 |
} |
| 1559 |
|
| 1560 |
// filter tags with no line numbers |
| 1561 |
std::vector<TagEntryPtr> good_tags; |
| 1562 |
TagEntryPtr first; |
| 1563 |
for(auto tag : candidates) { |
| 1564 |
if(tag->GetLine() != wxNOT_FOUND && !tag->GetFile().empty()) { |
| 1565 |
first = tag; |
| 1566 |
break; |
| 1567 |
} |
| 1568 |
} |
| 1569 |
|
| 1570 |
if(!first) { |
| 1571 |
return 0; |
| 1572 |
} |
| 1573 |
|
| 1574 |
if(first->IsMethod()) { |
| 1575 |
// we prefer the definition, unless we are already on it, and in that case, return the declaration |
| 1576 |
// locate both declaration + implementation |
| 1577 |
wxString path = first->GetPath(); |
| 1578 |
std::vector<TagEntryPtr> impl_vec; |
| 1579 |
std::vector<TagEntryPtr> decl_vec; |
| 1580 |
clDEBUG() << "Searching for path:" << path << endl; |
| 1581 |
m_lookup->GetTagsByPathAndKind(path, impl_vec, { "function" }, 100); |
| 1582 |
m_lookup->GetTagsByPathAndKind(path, decl_vec, { "prototype" }, 100); |
| 1583 |
|
| 1584 |
clDEBUG() << "impl:" << impl_vec.size() << "decl:" << decl_vec.size() << endl; |
| 1585 |
// if no impl found, return the decl vector |
| 1586 |
if(impl_vec.empty()) { |
| 1587 |
matches.swap(decl_vec); |
| 1588 |
return matches.size(); |
| 1589 |
} |
| 1590 |
|
| 1591 |
// if no decl found, return the impl vector (a more rare case, but this can happen) |
| 1592 |
if(decl_vec.empty()) { |
| 1593 |
matches.swap(impl_vec); |
| 1594 |
return matches.size(); |
| 1595 |
} |
| 1596 |
auto is_on_line = [](const std::vector<TagEntryPtr>& tags_vec, int line, const wxString& file) { |
| 1597 |
for(auto tag : tags_vec) { |
| 1598 |
if(tag->GetLine() == line && tag->GetFile() == file) { |
| 1599 |
return true; |
| 1600 |
} |
| 1601 |
} |
| 1602 |
return false; |
| 1603 |
}; |
| 1604 |
|
| 1605 |
if(is_on_line(impl_vec, line, filepath)) { |
| 1606 |
matches.swap(decl_vec); |
| 1607 |
return matches.size(); |
| 1608 |
} |
| 1609 |
|
| 1610 |
if(is_on_line(decl_vec, line, filepath)) { |
| 1611 |
matches.swap(impl_vec); |
| 1612 |
return matches.size(); |
| 1613 |
} |
| 1614 |
|
| 1615 |
// by default we return the "impl" |
| 1616 |
if(!impl_vec.empty()) { |
| 1617 |
matches.swap(impl_vec); |
| 1618 |
} else { |
| 1619 |
matches.swap(decl_vec); |
| 1620 |
} |
| 1621 |
return matches.size(); |
| 1622 |
} else { |
| 1623 |
// no need to manipulate this tag |
| 1624 |
matches.push_back(first); |
| 1625 |
return matches.size(); |
| 1626 |
} |
| 1627 |
} |
| 1628 |
|
| 1629 |
size_t CxxCodeCompletion::word_complete(const wxString& filepath, int line, const wxString& expression, |
| 1630 |
const wxString& text, const std::vector<wxString>& visible_scopes, |
| 1631 |
bool exact_match, std::vector<TagEntryPtr>& candidates, |
| 1632 |
const wxStringSet_t& visible_files) |
| 1633 |
{ |
| 1634 |
// ---------------------------------- |
| 1635 |
// word completion |
| 1636 |
// ---------------------------------- |
| 1637 |
clDEBUG() << "word_complete expression:" << expression << endl; |
| 1638 |
set_text(text, filepath, line); |
| 1639 |
|
| 1640 |
CxxRemainder remainder; |
| 1641 |
TagEntryPtr resolved = code_complete(expression, visible_scopes, &remainder); |
| 1642 |
|
| 1643 |
wxString filter = remainder.filter; |
| 1644 |
if(!resolved) { |
| 1645 |
// check if this is a contextual text (A::B or a->b or a.b etc) |
| 1646 |
// we only allow global completion of there is not expression list |
| 1647 |
// and the remainder has something in it (r.type_name() is not empty) |
| 1648 |
CxxRemainder r; |
| 1649 |
auto expressions = from_expression(expression, &r); |
| 1650 |
if(expressions.size() == 0 && !r.filter.empty()) { |
| 1651 |
clDEBUG() << "code_complete failed to resolve:" << expression << endl; |
| 1652 |
clDEBUG() << "filter:" << r.filter << endl; |
| 1653 |
get_word_completions(remainder, candidates, visible_scopes, visible_files); |
| 1654 |
} |
| 1655 |
} else if(resolved) { |
| 1656 |
// it was resolved into something... |
| 1657 |
clDEBUG() << "code_complete resolved:" << resolved->GetPath() << endl; |
| 1658 |
clDEBUG() << "filter:" << remainder.filter << endl; |
| 1659 |
get_completions(resolved, remainder.operand_string, remainder.filter, candidates, visible_scopes); |
| 1660 |
} |
| 1661 |
clDEBUG() << "Number of completion entries:" << candidates.size() << endl; |
| 1662 |
|
| 1663 |
// take the first one with the exact match |
| 1664 |
if(!exact_match) { |
| 1665 |
return candidates.size(); |
| 1666 |
} |
| 1667 |
|
| 1668 |
std::vector<TagEntryPtr> matches; |
| 1669 |
matches.reserve(candidates.size()); |
| 1670 |
for(TagEntryPtr t : candidates) { |
| 1671 |
if(t->GetName() == filter) { |
| 1672 |
matches.push_back(t); |
| 1673 |
} |
| 1674 |
} |
| 1675 |
candidates.swap(matches); |
| 1676 |
return candidates.size(); |
| 1677 |
} |
| 1678 |
|
| 1679 |
wxString CxxCodeCompletion::normalize_pattern(TagEntryPtr tag) const |
| 1680 |
{ |
| 1681 |
CxxTokenizer tokenizer; |
| 1682 |
CxxLexerToken tk; |
| 1683 |
|
| 1684 |
tokenizer.Reset(tag->GetPatternClean()); |
| 1685 |
wxString pattern; |
| 1686 |
while(tokenizer.NextToken(tk)) { |
| 1687 |
wxString str = tk.GetWXString(); |
| 1688 |
switch(tk.GetType()) { |
| 1689 |
case T_IDENTIFIER: |
| 1690 |
if(m_macros_table_map.count(str) && m_macros_table_map.find(str)->second.empty()) { |
| 1691 |
// skip this token |
| 1692 |
} else { |
| 1693 |
pattern << str << " "; |
| 1694 |
} |
| 1695 |
break; |
| 1696 |
default: |
| 1697 |
if(tk.is_keyword() || tk.is_builtin_type()) { |
| 1698 |
pattern << str << " "; |
| 1699 |
} else { |
| 1700 |
pattern << str; |
| 1701 |
} |
| 1702 |
break; |
| 1703 |
} |
| 1704 |
} |
| 1705 |
return pattern; |
| 1706 |
} |
| 1707 |
|
| 1708 |
size_t CxxCodeCompletion::get_anonymous_tags(const wxString& name, const wxArrayString& kinds, |
| 1709 |
std::vector<TagEntryPtr>& tags) const |
| 1710 |
{ |
| 1711 |
if(!m_lookup) { |
| 1712 |
return 0; |
| 1713 |
} |
| 1714 |
m_lookup->GetFileScopedTags(m_filename, name, kinds, tags); |
| 1715 |
return tags.size(); |
| 1716 |
} |
| 1717 |
|
| 1718 |
size_t CxxCodeCompletion::get_keywords_tags(const wxString& name, std::vector<TagEntryPtr>& tags) |
| 1719 |
{ |
| 1720 |
CompletionHelper helper; |
| 1721 |
std::vector<wxString> keywords; |
| 1722 |
helper.get_cxx_keywords(keywords); |
| 1723 |
tags.reserve(keywords.size()); |
| 1724 |
|
| 1725 |
for(const auto& keyword : keywords) { |
| 1726 |
if(keyword.StartsWith(name)) { |
| 1727 |
TagEntryPtr tag(new TagEntry()); |
| 1728 |
tag->SetName(keyword); |
| 1729 |
tag->SetPath(keyword); |
| 1730 |
tag->SetKind("keyword"); |
| 1731 |
tag->SetFile("<built-in>"); |
| 1732 |
tags.push_back(tag); |
| 1733 |
} |
| 1734 |
} |
| 1735 |
return tags.size(); |
| 1736 |
} |
| 1737 |
|
| 1738 |
#define CHECK_EXPECTED(Token, ExpectedType) \ |
| 1739 |
if(Token.IsEOF() || Token.GetType() != ExpectedType) \ |
| 1740 |
return false; |
| 1741 |
|
| 1742 |
bool CxxCodeCompletion::read_template_definition(CxxTokenizer& tokenizer, wxString* definition) const |
| 1743 |
{ |
| 1744 |
CxxLexerToken token; |
| 1745 |
tokenizer.NextToken(token); |
| 1746 |
if(token.GetType() != T_TEMPLATE || token.IsEOF()) { |
| 1747 |
tokenizer.UngetToken(); |
| 1748 |
return false; |
| 1749 |
} |
| 1750 |
|
| 1751 |
definition->clear(); |
| 1752 |
|
| 1753 |
// we are now expecting an open brace |
| 1754 |
tokenizer.NextToken(token); |
| 1755 |
CHECK_EXPECTED(token, '<'); |
| 1756 |
|
| 1757 |
int depth = 1; |
| 1758 |
while(tokenizer.NextToken(token)) { |
| 1759 |
if(token.is_keyword() || token.is_builtin_type()) { |
| 1760 |
definition->Append(" "); |
| 1761 |
definition->Append(token.GetWXString()); |
| 1762 |
continue; |
| 1763 |
} |
| 1764 |
switch(token.GetType()) { |
| 1765 |
case '<': |
| 1766 |
depth++; |
| 1767 |
definition->Append(token.GetWXString()); |
| 1768 |
break; |
| 1769 |
case '>': |
| 1770 |
depth--; |
| 1771 |
if(depth == 0) { |
| 1772 |
return true; |
| 1773 |
} else { |
| 1774 |
definition->Append(token.GetWXString()); |
| 1775 |
} |
| 1776 |
break; |
| 1777 |
default: |
| 1778 |
definition->Append(token.GetWXString()); |
| 1779 |
break; |
| 1780 |
} |
| 1781 |
} |
| 1782 |
return false; |
| 1783 |
} |
| 1784 |
#undef CHECK_EXPECTED |
| 1785 |
|
| 1786 |
size_t CxxCodeCompletion::get_class_constructors(TagEntryPtr tag, std::vector<TagEntryPtr>& tags) |
| 1787 |
{ |
| 1788 |
if(!tag->IsClass() && !tag->IsStruct()) { |
| 1789 |
tags.clear(); |
| 1790 |
return tags.size(); |
| 1791 |
} |
| 1792 |
m_lookup->GetTagsByPathAndKind(tag->GetPath() + "::" + tag->GetName(), tags, { "prototype", "function" }, 250); |
| 1793 |
|
| 1794 |
std::vector<TagEntryPtr> sorted_tags; |
| 1795 |
// filter duplicate |
| 1796 |
sort_tags(tags, sorted_tags, true, {}); |
| 1797 |
tags.swap(sorted_tags); |
| 1798 |
return tags.size(); |
| 1799 |
} |
| 1800 |
|
| 1801 |
std::vector<CxxExpression> CxxCodeCompletion::from_expression(const wxString& expression, CxxRemainder* remainder) |
| 1802 |
{ |
| 1803 |
auto arr = CxxExpression::from_expression(expression, remainder); |
| 1804 |
for(auto& exp : arr) { |
| 1805 |
simple_pre_process(exp.type_name()); |
| 1806 |
} |
| 1807 |
return arr; |
| 1808 |
} |
| 1809 |
|
| 1810 |
wxString& CxxCodeCompletion::simple_pre_process(wxString& name) const |
| 1811 |
{ |
| 1812 |
wxStringSet_t visited; |
| 1813 |
|
| 1814 |
while(true) { |
| 1815 |
if(!visited.insert(name).second) { |
| 1816 |
// entering a loop here... |
| 1817 |
break; |
| 1818 |
} |
| 1819 |
|
| 1820 |
if(m_macros_table_map.count(name) == 0) { |
| 1821 |
// no match |
| 1822 |
break; |
| 1823 |
} |
| 1824 |
|
| 1825 |
// perform the replacement |
| 1826 |
name = m_macros_table_map.find(name)->second; |
| 1827 |
} |
| 1828 |
return name; |
| 1829 |
} |
| 1830 |
|
| 1831 |
std::vector<TagEntryPtr> CxxCodeCompletion::get_parents_of_tag_no_recurse(TagEntryPtr parent, |
| 1832 |
TemplateManager::ptr_t template_manager, |
| 1833 |
const std::vector<wxString>& visible_scopes) |
| 1834 |
{ |
| 1835 |
wxArrayString inherits_with_template = parent->GetInheritsAsArrayWithTemplates(); |
| 1836 |
wxArrayString inherits = parent->GetInheritsAsArrayNoTemplates(); |
| 1837 |
std::vector<TagEntryPtr> parents; |
| 1838 |
parents.reserve(inherits.size()); |
| 1839 |
|
| 1840 |
for(size_t i = 0; i < inherits.size(); ++i) { |
| 1841 |
wxString& inherit = inherits[i]; |
| 1842 |
wxString to_search = simple_pre_process(inherit); |
| 1843 |
// run it in the template manager as well |
| 1844 |
if(template_manager) { |
| 1845 |
to_search = template_manager->resolve(to_search, visible_scopes); |
| 1846 |
} |
| 1847 |
|
| 1848 |
auto match = lookup_symbol_by_kind(to_search, visible_scopes, { "class", "struct" }); |
| 1849 |
if(match) { |
| 1850 |
if(template_manager && (inherits_with_template.size() == inherits.size()) && match->IsClassTemplate() && |
| 1851 |
(inherit != inherits_with_template[i])) { |
| 1852 |
// the goal of this `if` branch is to parse the template initialisation |
| 1853 |
// line and update the template manager in case we need this information |
| 1854 |
// later along the way |
| 1855 |
// |
| 1856 |
// parse the template initialisation |
| 1857 |
// class MyClass : public TemplateClass<String> {..} |
| 1858 |
// ^ ^ |
| 1859 |
// \__________________/ |
| 1860 |
// | |
| 1861 |
// template initialisation |
| 1862 |
// |
| 1863 |
// we want to extract the value `String` |
| 1864 |
// |
| 1865 |
auto exprs = CxxExpression::from_expression(inherits_with_template[i] + "::", nullptr); |
| 1866 |
if(!exprs.empty()) { |
| 1867 |
auto& expression = exprs[0]; |
| 1868 |
expression.parse_template_placeholders(match->GetTemplateDefinition()); |
| 1869 |
auto M = expression.get_template_placeholders_map(); |
| 1870 |
template_manager->add_placeholders(M, visible_scopes); |
| 1871 |
} |
| 1872 |
} |
| 1873 |
parents.emplace_back(match); |
| 1874 |
} |
| 1875 |
} |
| 1876 |
return parents; |
| 1877 |
} |
| 1878 |
|
| 1879 |
std::vector<wxString> CxxCodeCompletion::prepend_extra_scopes(const std::vector<wxString>& visible_scopes) |
| 1880 |
{ |
| 1881 |
std::vector<wxString> scopes = m_file_only_tags.get_file_scopes(); |
| 1882 |
wxStringSet_t unique_scopes{ scopes.begin(), scopes.end() }; |
| 1883 |
|
| 1884 |
// append only unique scopes |
| 1885 |
scopes.reserve(scopes.size() + visible_scopes.size() + 1); |
| 1886 |
for(const wxString& scope : visible_scopes) { |
| 1887 |
if(unique_scopes.insert(scope).second) { |
| 1888 |
scopes.push_back(scope); |
| 1889 |
} |
| 1890 |
} |
| 1891 |
|
| 1892 |
if(unique_scopes.count("") == 0) { |
| 1893 |
// add the global scope |
| 1894 |
scopes.push_back(wxEmptyString); |
| 1895 |
} |
| 1896 |
return scopes; |
| 1897 |
} |