| 1 |
#include "CompletionHelper.hpp" |
| 2 |
|
| 3 |
#include "CxxScannerTokens.h" |
| 4 |
#include "CxxTokenizer.h" |
| 5 |
#include "file_logger.h" |
| 6 |
|
| 7 |
#include <vector> |
| 8 |
#include <wx/regex.h> |
| 9 |
|
| 10 |
namespace |
| 11 |
{ |
| 12 |
#define PREPEND_STRING(tokn) expression.insert(expression.begin(), tokn) |
| 13 |
#define MAX_TIP_LINE_SIZE 200 |
| 14 |
|
| 15 |
bool is_word_char(wxChar ch) |
| 16 |
{ |
| 17 |
return (ch >= 65 && ch <= 90) // uppercase A-Z |
| 18 |
|| (ch >= 97 && ch <= 122) // lowercase a-z |
| 19 |
|| (ch >= 48 && ch <= 57) // 0-9 |
| 20 |
|| (ch == '_'); |
| 21 |
} |
| 22 |
|
| 23 |
wxString wrap_lines(const wxString& str) |
| 24 |
{ |
| 25 |
wxString wrappedString; |
| 26 |
|
| 27 |
int curLineBytes(0); |
| 28 |
wxString::const_iterator iter = str.begin(); |
| 29 |
for(; iter != str.end(); iter++) { |
| 30 |
if(*iter == '\t') { |
| 31 |
wrappedString << " "; |
| 32 |
|
| 33 |
} else if(*iter == '\n') { |
| 34 |
wrappedString << "\n"; |
| 35 |
curLineBytes = 0; |
| 36 |
|
| 37 |
} else if(*iter == '\r') { |
| 38 |
// Skip it |
| 39 |
|
| 40 |
} else { |
| 41 |
wrappedString << *iter; |
| 42 |
} |
| 43 |
curLineBytes++; |
| 44 |
|
| 45 |
if(curLineBytes == MAX_TIP_LINE_SIZE) { |
| 46 |
|
| 47 |
// Wrap the lines |
| 48 |
if(wrappedString.IsEmpty() == false && wrappedString.Last() != '\n') { |
| 49 |
wrappedString << "\n"; |
| 50 |
} |
| 51 |
curLineBytes = 0; |
| 52 |
} |
| 53 |
} |
| 54 |
return wrappedString; |
| 55 |
} |
| 56 |
|
| 57 |
} // namespace |
| 58 |
|
| 59 |
CompletionHelper::CompletionHelper() {} |
| 60 |
|
| 61 |
CompletionHelper::~CompletionHelper() {} |
| 62 |
|
| 63 |
wxString CompletionHelper::get_expression(const wxString& file_content, bool for_calltip, wxString* last_word) const |
| 64 |
{ |
| 65 |
#define LAST_TOKEN_IS(token_type) (!types.empty() && (types[types.size() - 1] == token_type)) |
| 66 |
// tokenize the text |
| 67 |
CxxTokenizer tokenizer; |
| 68 |
tokenizer.Reset(file_content); |
| 69 |
|
| 70 |
CxxLexerToken token; |
| 71 |
std::vector<std::pair<wxString, int>> tokens; |
| 72 |
tokens.reserve(10000); |
| 73 |
|
| 74 |
while(tokenizer.NextToken(token)) { |
| 75 |
tokens.push_back({ token.GetWXString(), token.GetType() }); |
| 76 |
} |
| 77 |
|
| 78 |
int i = static_cast<int>(tokens.size() - 1); |
| 79 |
bool cont = true; |
| 80 |
int parentheses_depth = 0; |
| 81 |
if(for_calltip) { |
| 82 |
// read backwards until we find the first open parentheses |
| 83 |
for(; (i >= 0) && cont; --i) { |
| 84 |
switch(tokens[i].second) { |
| 85 |
case '(': |
| 86 |
if(parentheses_depth == 0) { |
| 87 |
cont = false; |
| 88 |
} else { |
| 89 |
parentheses_depth--; |
| 90 |
} |
| 91 |
break; |
| 92 |
case ')': |
| 93 |
parentheses_depth++; |
| 94 |
break; |
| 95 |
default: |
| 96 |
break; |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
std::vector<wxString> expression; |
| 102 |
std::vector<int> types; |
| 103 |
int depth = 0; |
| 104 |
cont = true; |
| 105 |
for(; (i >= 0) && cont; --i) { |
| 106 |
const wxString& text = tokens[i].first; |
| 107 |
int type = tokens[i].second; |
| 108 |
switch(type) { |
| 109 |
case '[': |
| 110 |
case '{': |
| 111 |
case '(': |
| 112 |
case '<': |
| 113 |
if(depth == 0) { |
| 114 |
cont = false; |
| 115 |
} else { |
| 116 |
PREPEND_STRING(text); |
| 117 |
depth--; |
| 118 |
} |
| 119 |
break; |
| 120 |
case '>': |
| 121 |
case ']': |
| 122 |
case ')': |
| 123 |
if(depth == 0 && LAST_TOKEN_IS(T_IDENTIFIER)) { |
| 124 |
cont = false; |
| 125 |
} else { |
| 126 |
PREPEND_STRING(text); |
| 127 |
depth++; |
| 128 |
} |
| 129 |
break; |
| 130 |
case '}': |
| 131 |
if(depth == 0) { |
| 132 |
cont = false; |
| 133 |
} else { |
| 134 |
PREPEND_STRING(text); |
| 135 |
depth++; |
| 136 |
} |
| 137 |
break; |
| 138 |
case ';': |
| 139 |
case T_PP_DEFINE: |
| 140 |
case T_PP_DEFINED: |
| 141 |
case T_PP_IF: |
| 142 |
case T_PP_IFNDEF: |
| 143 |
case T_PP_IFDEF: |
| 144 |
case T_PP_ELSE: |
| 145 |
case T_PP_ELIF: |
| 146 |
case T_PP_LINE: |
| 147 |
case T_PP_PRAGMA: |
| 148 |
case T_PP_UNDEF: |
| 149 |
case T_PP_ERROR: |
| 150 |
case T_PP_ENDIF: |
| 151 |
case T_PP_IDENTIFIER: |
| 152 |
case T_PP_DEC_NUMBER: |
| 153 |
case T_PP_OCTAL_NUMBER: |
| 154 |
case T_PP_HEX_NUMBER: |
| 155 |
case T_PP_FLOAT_NUMBER: |
| 156 |
case T_PP_STRING: |
| 157 |
case T_PP_AND: |
| 158 |
case T_PP_OR: |
| 159 |
case T_PP_STATE_EXIT: |
| 160 |
case T_PP_INCLUDE_FILENAME: |
| 161 |
case T_PP_INCLUDE: |
| 162 |
case T_PP_GT: |
| 163 |
case T_PP_GTEQ: |
| 164 |
case T_PP_LT: |
| 165 |
case T_PP_LTEQ: |
| 166 |
cont = false; |
| 167 |
break; |
| 168 |
case '=': |
| 169 |
if(depth == 0) { |
| 170 |
cont = false; |
| 171 |
} else { |
| 172 |
PREPEND_STRING(text); |
| 173 |
} |
| 174 |
break; |
| 175 |
case T_IDENTIFIER: |
| 176 |
if(LAST_TOKEN_IS(T_IDENTIFIER)) { |
| 177 |
// we do not allow two consecutive T_IDENTIFIER |
| 178 |
cont = false; |
| 179 |
break; |
| 180 |
} else { |
| 181 |
PREPEND_STRING(text); |
| 182 |
} |
| 183 |
break; |
| 184 |
case T_ALIGNAS: |
| 185 |
case T_ALIGNOF: |
| 186 |
case T_AND: |
| 187 |
case T_AND_EQ: |
| 188 |
case T_ASM: |
| 189 |
case T_AUTO: |
| 190 |
case T_BITAND: |
| 191 |
case T_BITOR: |
| 192 |
case T_BOOL: |
| 193 |
case T_BREAK: |
| 194 |
case T_CATCH: |
| 195 |
case T_CHAR: |
| 196 |
case T_CHAR16_T: |
| 197 |
case T_CHAR32_T: |
| 198 |
case T_CLASS: |
| 199 |
case T_COMPL: |
| 200 |
case T_CONST: |
| 201 |
case T_CONSTEXPR: |
| 202 |
case T_CONTINUE: |
| 203 |
case T_DECLTYPE: |
| 204 |
case T_DEFAULT: |
| 205 |
case T_DELETE: |
| 206 |
case T_DO: |
| 207 |
case T_DOUBLE: |
| 208 |
case T_ELSE: |
| 209 |
case T_ENUM: |
| 210 |
case T_EXPLICIT: |
| 211 |
case T_EXPORT: |
| 212 |
case T_EXTERN: |
| 213 |
case T_FALSE: |
| 214 |
case T_FINAL: |
| 215 |
case T_FLOAT: |
| 216 |
case T_FOR: |
| 217 |
case T_FRIEND: |
| 218 |
case T_GOTO: |
| 219 |
case T_IF: |
| 220 |
case T_INLINE: |
| 221 |
case T_INT: |
| 222 |
case T_LONG: |
| 223 |
case T_MUTABLE: |
| 224 |
case T_NAMESPACE: |
| 225 |
case T_NEW: |
| 226 |
case T_NOEXCEPT: |
| 227 |
case T_NOT: |
| 228 |
case T_NOT_EQ: |
| 229 |
case T_NULLPTR: |
| 230 |
case T_OPERATOR: |
| 231 |
case T_OR: |
| 232 |
case T_OR_EQ: |
| 233 |
case T_OVERRIDE: |
| 234 |
case T_PRIVATE: |
| 235 |
case T_PROTECTED: |
| 236 |
case T_PUBLIC: |
| 237 |
case T_REGISTER: |
| 238 |
case T_CASE: |
| 239 |
case T_SHORT: |
| 240 |
case T_SIGNED: |
| 241 |
case T_SIZEOF: |
| 242 |
case T_STATIC: |
| 243 |
case T_STATIC_ASSERT: |
| 244 |
case T_STRUCT: |
| 245 |
case T_SWITCH: |
| 246 |
case T_TEMPLATE: |
| 247 |
case T_THREAD_LOCAL: |
| 248 |
case T_THROW: |
| 249 |
case T_TRUE: |
| 250 |
case T_TRY: |
| 251 |
case T_TYPEDEF: |
| 252 |
case T_TYPEID: |
| 253 |
case T_TYPENAME: |
| 254 |
case T_UNION: |
| 255 |
case T_UNSIGNED: |
| 256 |
case T_USING: |
| 257 |
case T_VIRTUAL: |
| 258 |
case T_VOID: |
| 259 |
case T_VOLATILE: |
| 260 |
case T_WCHAR_T: |
| 261 |
case T_WHILE: |
| 262 |
case T_XOR: |
| 263 |
case T_XOR_EQ: |
| 264 |
case T_STRING: |
| 265 |
case T_DOT_STAR: |
| 266 |
case T_ARROW_STAR: |
| 267 |
case T_PLUS_PLUS: |
| 268 |
case T_MINUS_MINUS: |
| 269 |
case T_LS: |
| 270 |
case T_LE: |
| 271 |
case T_GE: |
| 272 |
case T_EQUAL: |
| 273 |
case T_NOT_EQUAL: |
| 274 |
case T_AND_AND: |
| 275 |
case T_OR_OR: |
| 276 |
case T_STAR_EQUAL: |
| 277 |
case T_SLASH_EQUAL: |
| 278 |
case T_DIV_EQUAL: |
| 279 |
case T_PLUS_EQUAL: |
| 280 |
case T_MINUS_EQUAL: |
| 281 |
case T_LS_ASSIGN: |
| 282 |
case T_RS_ASSIGN: |
| 283 |
case T_AND_EQUAL: |
| 284 |
case T_POW_EQUAL: |
| 285 |
case T_OR_EQUAL: |
| 286 |
case T_3_DOTS: |
| 287 |
case T_RETURN: |
| 288 |
case ',': |
| 289 |
case '*': |
| 290 |
case '&': |
| 291 |
case '!': |
| 292 |
case '~': |
| 293 |
case '+': |
| 294 |
case '^': |
| 295 |
case '|': |
| 296 |
case '%': |
| 297 |
case '?': |
| 298 |
case '/': |
| 299 |
case ':': |
| 300 |
case '-': |
| 301 |
case '@': |
| 302 |
if(depth <= 0) { |
| 303 |
cont = false; |
| 304 |
} else { |
| 305 |
PREPEND_STRING(text); |
| 306 |
} |
| 307 |
break; |
| 308 |
case T_CONST_CAST: |
| 309 |
case T_DYNAMIC_CAST: |
| 310 |
case T_REINTERPRET_CAST: |
| 311 |
case T_STATIC_CAST: |
| 312 |
default: |
| 313 |
PREPEND_STRING(text); |
| 314 |
break; |
| 315 |
} |
| 316 |
types.push_back(type); |
| 317 |
} |
| 318 |
|
| 319 |
// build the expression from the vector |
| 320 |
wxString expression_string; |
| 321 |
for(const auto& t : expression) { |
| 322 |
expression_string << t; |
| 323 |
} |
| 324 |
if(last_word && !expression.empty()) { |
| 325 |
*last_word = expression[expression.size() - 1]; |
| 326 |
} |
| 327 |
return expression_string; |
| 328 |
#undef LAST_TOKEN_IS |
| 329 |
} |
| 330 |
|
| 331 |
wxString CompletionHelper::truncate_file_to_location(const wxString& file_content, size_t line, size_t column, |
| 332 |
size_t flags) const |
| 333 |
{ |
| 334 |
size_t curline = 0; |
| 335 |
size_t offset = 0; |
| 336 |
|
| 337 |
// locate the line |
| 338 |
for(const auto& ch : file_content) { |
| 339 |
if(curline == line) { |
| 340 |
break; |
| 341 |
} |
| 342 |
switch(ch.GetValue()) { |
| 343 |
case '\n': |
| 344 |
++curline; |
| 345 |
++offset; |
| 346 |
break; |
| 347 |
default: |
| 348 |
++offset; |
| 349 |
break; |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
if(curline != line) { |
| 354 |
return wxEmptyString; |
| 355 |
} |
| 356 |
|
| 357 |
// columns |
| 358 |
offset += column; |
| 359 |
|
| 360 |
if(offset < file_content.size()) { |
| 361 |
if(flags & (TRUNCATE_COMPLETE_WORDS | TRUNCATE_COMPLETE_LINES)) { |
| 362 |
while(true) { |
| 363 |
size_t next_pos = offset; |
| 364 |
|
| 365 |
if(next_pos < file_content.size()) { |
| 366 |
wxChar next_char = file_content[next_pos]; |
| 367 |
if(flags & TRUNCATE_COMPLETE_WORDS) { |
| 368 |
// complete words only |
| 369 |
if(is_word_char(next_char)) { |
| 370 |
offset += 1; |
| 371 |
} else { |
| 372 |
break; |
| 373 |
} |
| 374 |
|
| 375 |
} else { |
| 376 |
// TRUNCATE_COMPLETE_LINES |
| 377 |
if(next_char == '\n') { |
| 378 |
break; |
| 379 |
} else { |
| 380 |
offset += 1; |
| 381 |
} |
| 382 |
} |
| 383 |
} else { |
| 384 |
break; |
| 385 |
} |
| 386 |
} |
| 387 |
} |
| 388 |
return file_content.Mid(0, offset); |
| 389 |
} |
| 390 |
return wxEmptyString; |
| 391 |
} |
| 392 |
|
| 393 |
thread_local std::unordered_set<wxString> words; |
| 394 |
namespace |
| 395 |
{ |
| 396 |
void populate_keywords() |
| 397 |
{ |
| 398 |
if(words.empty()) { |
| 399 |
words.insert("auto"); |
| 400 |
words.insert("break"); |
| 401 |
words.insert("case"); |
| 402 |
words.insert("char"); |
| 403 |
words.insert("const"); |
| 404 |
words.insert("continue"); |
| 405 |
words.insert("default"); |
| 406 |
words.insert("define"); |
| 407 |
words.insert("defined"); |
| 408 |
words.insert("do"); |
| 409 |
words.insert("double"); |
| 410 |
words.insert("elif"); |
| 411 |
words.insert("else"); |
| 412 |
words.insert("endif"); |
| 413 |
words.insert("enum"); |
| 414 |
words.insert("error"); |
| 415 |
words.insert("extern"); |
| 416 |
words.insert("float"); |
| 417 |
words.insert("for"); |
| 418 |
words.insert("goto"); |
| 419 |
words.insert("if"); |
| 420 |
words.insert("ifdef"); |
| 421 |
words.insert("ifndef"); |
| 422 |
words.insert("include"); |
| 423 |
words.insert("int"); |
| 424 |
words.insert("line"); |
| 425 |
words.insert("long"); |
| 426 |
words.insert("bool"); |
| 427 |
words.insert("pragma"); |
| 428 |
words.insert("register"); |
| 429 |
words.insert("return"); |
| 430 |
words.insert("short"); |
| 431 |
words.insert("signed"); |
| 432 |
words.insert("sizeof"); |
| 433 |
words.insert("static"); |
| 434 |
words.insert("struct"); |
| 435 |
words.insert("switch"); |
| 436 |
words.insert("typedef"); |
| 437 |
words.insert("undef"); |
| 438 |
words.insert("union"); |
| 439 |
words.insert("unsigned"); |
| 440 |
words.insert("void"); |
| 441 |
words.insert("volatile"); |
| 442 |
words.insert("while"); |
| 443 |
words.insert("class"); |
| 444 |
words.insert("namespace"); |
| 445 |
words.insert("delete"); |
| 446 |
words.insert("friend"); |
| 447 |
words.insert("inline"); |
| 448 |
words.insert("new"); |
| 449 |
words.insert("operator"); |
| 450 |
words.insert("overload"); |
| 451 |
words.insert("protected"); |
| 452 |
words.insert("private"); |
| 453 |
words.insert("public"); |
| 454 |
words.insert("this"); |
| 455 |
words.insert("virtual"); |
| 456 |
words.insert("template"); |
| 457 |
words.insert("typename"); |
| 458 |
words.insert("dynamic_cast"); |
| 459 |
words.insert("static_cast"); |
| 460 |
words.insert("const_cast"); |
| 461 |
words.insert("reinterpret_cast"); |
| 462 |
words.insert("using"); |
| 463 |
words.insert("throw"); |
| 464 |
words.insert("catch"); |
| 465 |
words.insert("nullptr"); |
| 466 |
words.insert("noexcept"); |
| 467 |
words.insert("override"); |
| 468 |
words.insert("explicit"); |
| 469 |
words.insert("constexpr"); |
| 470 |
words.insert("thread_local"); |
| 471 |
words.insert("true"); |
| 472 |
words.insert("false"); |
| 473 |
} |
| 474 |
} |
| 475 |
} // namespace |
| 476 |
bool CompletionHelper::is_cxx_keyword(const wxString& word) |
| 477 |
{ |
| 478 |
populate_keywords(); |
| 479 |
return words.count(word) != 0; |
| 480 |
} |
| 481 |
|
| 482 |
std::vector<wxString> CompletionHelper::split_function_signature(const wxString& signature, wxString* return_value, |
| 483 |
size_t flags) const |
| 484 |
{ |
| 485 |
// --------------------------------------------------------------------------------------------- |
| 486 |
// ----------------macros start------------------------------------------------------------------- |
| 487 |
// --------------------------------------------------------------------------------------------- |
| 488 |
#define ADD_CURRENT_PARAM(current_param) \ |
| 489 |
current_param->Trim().Trim(false); \ |
| 490 |
if(!current_param->empty()) { \ |
| 491 |
args.push_back(*current_param); \ |
| 492 |
} \ |
| 493 |
current_param->clear(); |
| 494 |
#define LAST_TOKEN_IS(token_type) (!types.empty() && (types[types.size() - 1] == token_type)) |
| 495 |
#define LAST_TOKEN_IS_ONE_OF_2(t1, t2) (LAST_TOKEN_IS(t1) || LAST_TOKEN_IS(t2)) |
| 496 |
#define LAST_TOKEN_IS_ONE_OF_3(t1, t2, t3) (LAST_TOKEN_IS_ONE_OF_2(t1, t2) || LAST_TOKEN_IS(t3)) |
| 497 |
#define LAST_TOKEN_IS_ONE_OF_4(t1, t2, t3, t4) (LAST_TOKEN_IS_ONE_OF_3(t1, t2, t3) || LAST_TOKEN_IS(t4)) |
| 498 |
#define LAST_TOKEN_IS_ONE_OF_5(t1, t2, t3, t4, t5) (LAST_TOKEN_IS_ONE_OF_4(t1, t2, t3, t4) || LAST_TOKEN_IS(t5)) |
| 499 |
#define LAST_TOKEN_IS_CLOSING_PARENTHESES() LAST_TOKEN_IS_ONE_OF_4('}', ']', '>', ')') |
| 500 |
#define LAST_TOKEN_IS_OPEN_PARENTHESES() LAST_TOKEN_IS_ONE_OF_4('{', '[', '<', '(') |
| 501 |
#define REMOVE_TRAILING_SPACE() \ |
| 502 |
if(!current_param->empty() && (*current_param)[current_param->size() - 1] == ' ') { \ |
| 503 |
current_param->RemoveLast(); \ |
| 504 |
} |
| 505 |
#define APPEND_SPACE_IF_MISSING() \ |
| 506 |
if(!current_param->empty() && ((*current_param)[current_param->size() - 1] != ' ')) { \ |
| 507 |
current_param->Append(" "); \ |
| 508 |
} |
| 509 |
|
| 510 |
// --------------------------------------------------------------------------------------------- |
| 511 |
// ----------------macros end------------------------------------------------------------------- |
| 512 |
// --------------------------------------------------------------------------------------------- |
| 513 |
CxxTokenizer tokenizer; |
| 514 |
tokenizer.Reset(signature); |
| 515 |
|
| 516 |
CxxLexerToken token; |
| 517 |
|
| 518 |
wxString cur_func_param; |
| 519 |
wxString* current_param = &cur_func_param; |
| 520 |
std::vector<wxString> args; |
| 521 |
std::vector<wxString> func_args; |
| 522 |
int depth = 0; |
| 523 |
|
| 524 |
std::vector<int> types; |
| 525 |
// search for the first opening brace |
| 526 |
while(tokenizer.NextToken(token)) { |
| 527 |
if(token.GetType() == '(') { |
| 528 |
depth = 1; |
| 529 |
break; |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
bool done_collecting_args = false; |
| 534 |
constexpr int STATE_NORMAL = 0; |
| 535 |
constexpr int STATE_DEFAULT_VALUE = 1; |
| 536 |
int state = STATE_NORMAL; |
| 537 |
while(tokenizer.NextToken(token)) { |
| 538 |
switch(state) { |
| 539 |
case STATE_DEFAULT_VALUE: |
| 540 |
// consume everything until we reach signature end |
| 541 |
// or until we hit a "," (where depth==1) |
| 542 |
switch(token.GetType()) { |
| 543 |
case '<': |
| 544 |
case '{': |
| 545 |
case '(': |
| 546 |
case '[': |
| 547 |
depth++; |
| 548 |
break; |
| 549 |
case '>': |
| 550 |
case '}': |
| 551 |
case ']': |
| 552 |
depth--; |
| 553 |
break; |
| 554 |
case ')': |
| 555 |
depth--; |
| 556 |
if(depth == 0) { |
| 557 |
// end of argument reading, switch back to the normal state |
| 558 |
tokenizer.UngetToken(); |
| 559 |
// restore the depth |
| 560 |
depth++; |
| 561 |
state = STATE_NORMAL; |
| 562 |
} |
| 563 |
break; |
| 564 |
case ',': |
| 565 |
if(depth == 1) { |
| 566 |
tokenizer.UngetToken(); |
| 567 |
state = STATE_NORMAL; |
| 568 |
} |
| 569 |
break; |
| 570 |
default: |
| 571 |
break; |
| 572 |
} |
| 573 |
break; |
| 574 |
case STATE_NORMAL: |
| 575 |
switch(token.GetType()) { |
| 576 |
case T_ALIGNAS: |
| 577 |
case T_ALIGNOF: |
| 578 |
case T_AND: |
| 579 |
case T_AND_EQ: |
| 580 |
case T_ASM: |
| 581 |
case T_AUTO: |
| 582 |
case T_BITAND: |
| 583 |
case T_BITOR: |
| 584 |
case T_BOOL: |
| 585 |
case T_BREAK: |
| 586 |
case T_CATCH: |
| 587 |
case T_CHAR: |
| 588 |
case T_CHAR16_T: |
| 589 |
case T_CHAR32_T: |
| 590 |
case T_CLASS: |
| 591 |
case T_COMPL: |
| 592 |
case T_CONST: |
| 593 |
case T_CONSTEXPR: |
| 594 |
case T_CONST_CAST: |
| 595 |
case T_CONTINUE: |
| 596 |
case T_DECLTYPE: |
| 597 |
case T_DEFAULT: |
| 598 |
case T_DELETE: |
| 599 |
case T_DO: |
| 600 |
case T_DOUBLE: |
| 601 |
case T_DYNAMIC_CAST: |
| 602 |
case T_ELSE: |
| 603 |
case T_ENUM: |
| 604 |
case T_EXPLICIT: |
| 605 |
case T_EXPORT: |
| 606 |
case T_EXTERN: |
| 607 |
case T_FALSE: |
| 608 |
case T_FINAL: |
| 609 |
case T_FLOAT: |
| 610 |
case T_FOR: |
| 611 |
case T_FRIEND: |
| 612 |
case T_GOTO: |
| 613 |
case T_IF: |
| 614 |
case T_INLINE: |
| 615 |
case T_INT: |
| 616 |
case T_LONG: |
| 617 |
case T_MUTABLE: |
| 618 |
case T_NAMESPACE: |
| 619 |
case T_NEW: |
| 620 |
case T_NOEXCEPT: |
| 621 |
case T_NOT: |
| 622 |
case T_NOT_EQ: |
| 623 |
case T_NULLPTR: |
| 624 |
case T_OPERATOR: |
| 625 |
case T_OR: |
| 626 |
case T_OR_EQ: |
| 627 |
case T_OVERRIDE: |
| 628 |
case T_PRIVATE: |
| 629 |
case T_PROTECTED: |
| 630 |
case T_PUBLIC: |
| 631 |
case T_REGISTER: |
| 632 |
case T_REINTERPRET_CAST: |
| 633 |
case T_CASE: |
| 634 |
case T_SHORT: |
| 635 |
case T_SIGNED: |
| 636 |
case T_SIZEOF: |
| 637 |
case T_STATIC: |
| 638 |
case T_STATIC_ASSERT: |
| 639 |
case T_STATIC_CAST: |
| 640 |
case T_STRUCT: |
| 641 |
case T_SWITCH: |
| 642 |
case T_TEMPLATE: |
| 643 |
case T_THREAD_LOCAL: |
| 644 |
case T_THROW: |
| 645 |
case T_TRUE: |
| 646 |
case T_TRY: |
| 647 |
case T_TYPEDEF: |
| 648 |
case T_TYPEID: |
| 649 |
case T_TYPENAME: |
| 650 |
case T_UNION: |
| 651 |
case T_UNSIGNED: |
| 652 |
case T_USING: |
| 653 |
case T_VIRTUAL: |
| 654 |
case T_VOID: |
| 655 |
case T_VOLATILE: |
| 656 |
case T_WCHAR_T: |
| 657 |
case T_WHILE: |
| 658 |
case T_XOR: |
| 659 |
case T_XOR_EQ: |
| 660 |
case T_STRING: |
| 661 |
case T_DOT_STAR: |
| 662 |
case T_ARROW_STAR: |
| 663 |
case T_PLUS_PLUS: |
| 664 |
case T_MINUS_MINUS: |
| 665 |
case T_LS: |
| 666 |
case T_LE: |
| 667 |
case T_GE: |
| 668 |
case T_EQUAL: |
| 669 |
case T_NOT_EQUAL: |
| 670 |
case T_AND_AND: |
| 671 |
case T_OR_OR: |
| 672 |
case T_STAR_EQUAL: |
| 673 |
case T_SLASH_EQUAL: |
| 674 |
case T_DIV_EQUAL: |
| 675 |
case T_PLUS_EQUAL: |
| 676 |
case T_MINUS_EQUAL: |
| 677 |
case T_LS_ASSIGN: |
| 678 |
case T_RS_ASSIGN: |
| 679 |
case T_AND_EQUAL: |
| 680 |
case T_POW_EQUAL: |
| 681 |
case T_OR_EQUAL: |
| 682 |
case T_3_DOTS: |
| 683 |
case '&': |
| 684 |
case ':': |
| 685 |
current_param->Append(token.GetWXString()); |
| 686 |
APPEND_SPACE_IF_MISSING(); |
| 687 |
break; |
| 688 |
case '*': |
| 689 |
if(LAST_TOKEN_IS('*')) { |
| 690 |
REMOVE_TRAILING_SPACE(); |
| 691 |
} |
| 692 |
current_param->Append(token.GetWXString()); |
| 693 |
APPEND_SPACE_IF_MISSING(); |
| 694 |
break; |
| 695 |
case T_IDENTIFIER: { |
| 696 |
wxString peeked_token_text; |
| 697 |
bool add_identifier = true; |
| 698 |
int next_token_type = tokenizer.PeekToken(peeked_token_text); |
| 699 |
// Check if we want to ignore the argument name |
| 700 |
if((depth == 1) && (next_token_type == ',' || next_token_type == '=' || next_token_type == ')') && |
| 701 |
(flags & STRIP_NO_NAME)) { |
| 702 |
// two consecutive T_IDENTIFIER, dont add it |
| 703 |
add_identifier = false; |
| 704 |
} else if(LAST_TOKEN_IS_CLOSING_PARENTHESES() || LAST_TOKEN_IS_ONE_OF_2(T_IDENTIFIER, '*')) { |
| 705 |
APPEND_SPACE_IF_MISSING(); |
| 706 |
} |
| 707 |
|
| 708 |
if(add_identifier) { |
| 709 |
current_param->Append(token.GetWXString()); |
| 710 |
} |
| 711 |
} break; |
| 712 |
case T_ARROW: |
| 713 |
if(done_collecting_args) { |
| 714 |
// we are collecting function return value now, disregard it |
| 715 |
} else { |
| 716 |
current_param->Append(token.GetWXString()); |
| 717 |
} |
| 718 |
break; |
| 719 |
case ',': |
| 720 |
if(depth == 1) { |
| 721 |
ADD_CURRENT_PARAM(current_param); |
| 722 |
} else { |
| 723 |
current_param->Append(","); |
| 724 |
APPEND_SPACE_IF_MISSING(); |
| 725 |
} |
| 726 |
break; |
| 727 |
case '>': |
| 728 |
case ']': |
| 729 |
case '}': |
| 730 |
depth--; |
| 731 |
REMOVE_TRAILING_SPACE(); |
| 732 |
current_param->Append(token.GetWXString()); |
| 733 |
break; |
| 734 |
case ')': |
| 735 |
depth--; |
| 736 |
if(!done_collecting_args && depth == 0) { |
| 737 |
// reached signature end |
| 738 |
ADD_CURRENT_PARAM(current_param); |
| 739 |
func_args.swap(args); |
| 740 |
done_collecting_args = true; |
| 741 |
} else { |
| 742 |
if(LAST_TOKEN_IS_CLOSING_PARENTHESES() || LAST_TOKEN_IS(T_IDENTIFIER)) { |
| 743 |
REMOVE_TRAILING_SPACE(); |
| 744 |
} |
| 745 |
current_param->Append(token.GetWXString()); |
| 746 |
} |
| 747 |
break; |
| 748 |
case '[': |
| 749 |
case '<': |
| 750 |
case '(': |
| 751 |
case '{': |
| 752 |
depth++; |
| 753 |
REMOVE_TRAILING_SPACE(); |
| 754 |
current_param->Append(token.GetWXString()); |
| 755 |
break; |
| 756 |
case '=': |
| 757 |
if((depth == 1) && (flags & STRIP_NO_DEFAULT_VALUES)) { |
| 758 |
state = STATE_DEFAULT_VALUE; |
| 759 |
} else { |
| 760 |
APPEND_SPACE_IF_MISSING(); |
| 761 |
current_param->Append("= "); |
| 762 |
} |
| 763 |
break; |
| 764 |
default: |
| 765 |
current_param->Append(token.GetWXString()); |
| 766 |
break; |
| 767 |
} |
| 768 |
|
| 769 |
break; |
| 770 |
} |
| 771 |
types.push_back(token.GetType()); |
| 772 |
} |
| 773 |
|
| 774 |
if(!done_collecting_args) { |
| 775 |
// we did not complete |
| 776 |
func_args.swap(args); |
| 777 |
} else { |
| 778 |
// check if we have a return value |
| 779 |
ADD_CURRENT_PARAM(current_param); |
| 780 |
if(!args.empty() && return_value) { |
| 781 |
*return_value = args[0].Trim().Trim(false); |
| 782 |
} |
| 783 |
} |
| 784 |
return func_args; |
| 785 |
#undef ADD_CURRENT_PARAM |
| 786 |
#undef LAST_TOKEN_IS |
| 787 |
#undef LAST_TOKEN_IS_ONE_OF_2 |
| 788 |
#undef LAST_TOKEN_IS_ONE_OF_3 |
| 789 |
#undef LAST_TOKEN_IS_ONE_OF_4 |
| 790 |
#undef LAST_TOKEN_IS_ONE_OF_5 |
| 791 |
#undef LAST_TOKEN_IS_CLOSING_PARENTHESES |
| 792 |
#undef LAST_TOKEN_IS_OPEN_PARENTHESES |
| 793 |
#undef REMOVE_TRAILING_SPACE |
| 794 |
} |
| 795 |
|
| 796 |
namespace |
| 797 |
{ |
| 798 |
thread_local wxRegEx reDoxyParam("([@\\\\]{1}param)[ \t]+([_a-z][a-z0-9_]*)?", wxRE_DEFAULT | wxRE_ICASE); |
| 799 |
thread_local wxRegEx reDoxyBrief("([@\\\\]{1}(brief|details))[ \t]*", wxRE_DEFAULT | wxRE_ICASE); |
| 800 |
thread_local wxRegEx reDoxyThrow("([@\\\\]{1}(throw|throws))[ \t]*", wxRE_DEFAULT | wxRE_ICASE); |
| 801 |
thread_local wxRegEx reDoxyReturn("([@\\\\]{1}(return|retval|returns))[ \t]*", wxRE_DEFAULT | wxRE_ICASE); |
| 802 |
thread_local wxRegEx reDoxyToDo("([@\\\\]{1}todo)[ \t]*", wxRE_DEFAULT | wxRE_ICASE); |
| 803 |
thread_local wxRegEx reDoxyRemark("([@\\\\]{1}(remarks|remark))[ \t]*", wxRE_DEFAULT | wxRE_ICASE); |
| 804 |
thread_local wxRegEx reDate("([@\\\\]{1}date)[ \t]*", wxRE_DEFAULT | wxRE_ICASE); |
| 805 |
thread_local wxRegEx reFN("([@\\\\]{1}fn)[ \t]*", wxRE_DEFAULT | wxRE_ICASE); |
| 806 |
} // namespace |
| 807 |
|
| 808 |
wxString CompletionHelper::format_comment(TagEntryPtr tag, const wxString& input_comment) const |
| 809 |
{ |
| 810 |
return format_comment(tag.Get(), input_comment); |
| 811 |
} |
| 812 |
|
| 813 |
wxString CompletionHelper::format_comment(TagEntry* tag, const wxString& input_comment) const |
| 814 |
{ |
| 815 |
wxString beautified_comment; |
| 816 |
if(tag) { |
| 817 |
if(tag->IsMethod()) { |
| 818 |
auto args = split_function_signature(tag->GetSignature(), nullptr); |
| 819 |
beautified_comment << "```\n"; |
| 820 |
if(args.empty()) { |
| 821 |
beautified_comment << tag->GetName() << "()\n"; |
| 822 |
} else { |
| 823 |
beautified_comment << tag->GetName() << "(\n"; |
| 824 |
for(const wxString& arg : args) { |
| 825 |
beautified_comment << " " << arg << ",\n"; |
| 826 |
} |
| 827 |
|
| 828 |
if(beautified_comment.EndsWith(",\n")) { |
| 829 |
beautified_comment.RemoveLast(2); |
| 830 |
} |
| 831 |
beautified_comment << ")\n"; |
| 832 |
} |
| 833 |
beautified_comment << "```\n"; |
| 834 |
} else if(tag->GetKind() == "variable" || tag->GetKind() == "member" || tag->IsLocalVariable()) { |
| 835 |
wxString clean_pattern = tag->GetPatternClean(); |
| 836 |
clean_pattern.Trim().Trim(false); |
| 837 |
|
| 838 |
if(!clean_pattern.empty()) { |
| 839 |
beautified_comment << "```\n"; |
| 840 |
beautified_comment << clean_pattern << "\n"; |
| 841 |
beautified_comment << "```\n"; |
| 842 |
} else { |
| 843 |
beautified_comment << tag->GetKind() << "\n"; |
| 844 |
} |
| 845 |
} else { |
| 846 |
// other |
| 847 |
wxString clean_pattern = tag->GetPatternClean(); |
| 848 |
clean_pattern.Trim().Trim(false); |
| 849 |
if(!clean_pattern.empty()) { |
| 850 |
beautified_comment << "```\n"; |
| 851 |
beautified_comment << clean_pattern << "\n"; |
| 852 |
beautified_comment << "```\n"; |
| 853 |
} |
| 854 |
} |
| 855 |
} |
| 856 |
wxString formatted_comment; |
| 857 |
if(!input_comment.empty()) { |
| 858 |
formatted_comment = wrap_lines(input_comment); |
| 859 |
|
| 860 |
if(reDoxyParam.IsValid() && reDoxyParam.Matches(formatted_comment)) { |
| 861 |
reDoxyParam.ReplaceAll(&formatted_comment, "\nParameter\n`\\2`"); |
| 862 |
} |
| 863 |
|
| 864 |
if(reDoxyBrief.IsValid() && reDoxyBrief.Matches(formatted_comment)) { |
| 865 |
reDoxyBrief.ReplaceAll(&formatted_comment, ""); |
| 866 |
} |
| 867 |
|
| 868 |
if(reDoxyThrow.IsValid() && reDoxyThrow.Matches(formatted_comment)) { |
| 869 |
reDoxyThrow.ReplaceAll(&formatted_comment, "\n`Throws:`\n"); |
| 870 |
} |
| 871 |
|
| 872 |
if(reDoxyReturn.IsValid() && reDoxyReturn.Matches(formatted_comment)) { |
| 873 |
reDoxyReturn.ReplaceAll(&formatted_comment, "\n`Returns:`\n"); |
| 874 |
} |
| 875 |
|
| 876 |
if(reDoxyToDo.IsValid() && reDoxyToDo.Matches(formatted_comment)) { |
| 877 |
reDoxyToDo.ReplaceAll(&formatted_comment, "\nTODO\n"); |
| 878 |
} |
| 879 |
|
| 880 |
if(reDoxyRemark.IsValid() && reDoxyRemark.Matches(formatted_comment)) { |
| 881 |
reDoxyRemark.ReplaceAll(&formatted_comment, "\n "); |
| 882 |
} |
| 883 |
|
| 884 |
if(reDate.IsValid() && reDate.Matches(formatted_comment)) { |
| 885 |
reDate.ReplaceAll(&formatted_comment, "Date "); |
| 886 |
} |
| 887 |
|
| 888 |
if(reFN.IsValid() && reFN.Matches(formatted_comment)) { |
| 889 |
size_t fnStart, fnLen, fnEnd; |
| 890 |
if(reFN.GetMatch(&fnStart, &fnLen)) { |
| 891 |
fnEnd = formatted_comment.find('\n', fnStart); |
| 892 |
if(fnEnd != wxString::npos) { |
| 893 |
// remove the string from fnStart -> fnEnd (including ther terminating \n) |
| 894 |
formatted_comment.Remove(fnStart, (fnEnd - fnStart) + 1); |
| 895 |
} |
| 896 |
} |
| 897 |
} |
| 898 |
|
| 899 |
// horizontal line |
| 900 |
if(!beautified_comment.empty()) { |
| 901 |
beautified_comment << "---\n"; |
| 902 |
} |
| 903 |
beautified_comment << formatted_comment; |
| 904 |
} |
| 905 |
return beautified_comment; |
| 906 |
} |
| 907 |
|
| 908 |
thread_local wxRegEx reIncludeFile("include *[\\\"\\<]{1}([a-zA-Z0-9_/\\.\\+\\-]*)"); |
| 909 |
|
| 910 |
bool CompletionHelper::is_line_include_statement(const wxString& line, wxString* file_name, wxString* suffix) const |
| 911 |
{ |
| 912 |
wxString tmp_line = line; |
| 913 |
|
| 914 |
tmp_line.Trim().Trim(false); |
| 915 |
tmp_line.Replace("\t", " "); |
| 916 |
|
| 917 |
// search for the "#" |
| 918 |
wxString remainder; |
| 919 |
if(!tmp_line.StartsWith("#", &remainder)) { |
| 920 |
return false; |
| 921 |
} |
| 922 |
|
| 923 |
if(!reIncludeFile.Matches(remainder)) { |
| 924 |
return false; |
| 925 |
} |
| 926 |
|
| 927 |
if(file_name) { |
| 928 |
*file_name = reIncludeFile.GetMatch(remainder, 1); |
| 929 |
} |
| 930 |
|
| 931 |
if(suffix) { |
| 932 |
if(tmp_line.Contains("<")) { |
| 933 |
*suffix = ">"; |
| 934 |
} else { |
| 935 |
*suffix = "\""; |
| 936 |
} |
| 937 |
} |
| 938 |
return true; |
| 939 |
} |
| 940 |
|
| 941 |
bool CompletionHelper::is_include_statement(const wxString& f_content, wxString* file_name, wxString* suffix) const |
| 942 |
{ |
| 943 |
// read backward until we find LF |
| 944 |
if(f_content.empty()) { |
| 945 |
return false; |
| 946 |
} |
| 947 |
|
| 948 |
int i = f_content.size() - 1; |
| 949 |
for(; i >= 0; --i) { |
| 950 |
if(f_content[i] == '\n') { |
| 951 |
break; |
| 952 |
} |
| 953 |
} |
| 954 |
|
| 955 |
wxString line = f_content.Mid(i); |
| 956 |
return is_line_include_statement(line, file_name, suffix); |
| 957 |
} |
| 958 |
|
| 959 |
wxString CompletionHelper::normalize_function(const TagEntry* tag, size_t flags) |
| 960 |
{ |
| 961 |
wxString return_value; |
| 962 |
wxString fullname; |
| 963 |
|
| 964 |
wxString name = tag->GetName(); |
| 965 |
wxString signature = tag->GetSignature(); |
| 966 |
fullname << name << "("; |
| 967 |
std::vector<wxString> args = split_function_signature(signature, &return_value, flags); |
| 968 |
wxString funcsig; |
| 969 |
for(const wxString& arg : args) { |
| 970 |
funcsig << arg << ", "; |
| 971 |
} |
| 972 |
|
| 973 |
if(funcsig.EndsWith(", ")) { |
| 974 |
funcsig.RemoveLast(2); |
| 975 |
} |
| 976 |
|
| 977 |
fullname << funcsig << ")"; |
| 978 |
if(tag->is_const()) { |
| 979 |
fullname << " const"; |
| 980 |
} |
| 981 |
return fullname; |
| 982 |
} |
| 983 |
|
| 984 |
wxString CompletionHelper::normalize_function(TagEntryPtr tag, size_t flags) |
| 985 |
{ |
| 986 |
return normalize_function(tag.Get(), flags); |
| 987 |
} |
| 988 |
|
| 989 |
void CompletionHelper::get_cxx_keywords(std::vector<wxString>& keywords) |
| 990 |
{ |
| 991 |
populate_keywords(); |
| 992 |
keywords.reserve(words.size()); |
| 993 |
for(const wxString& word : words) { |
| 994 |
keywords.push_back(word); |
| 995 |
} |
| 996 |
} |