File: CompletionHelper.cpp

package info (click to toggle)
codelite 17.0.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 136,244 kB
  • sloc: cpp: 491,547; ansic: 280,393; php: 10,259; sh: 8,930; lisp: 7,664; vhdl: 6,518; python: 6,020; lex: 4,920; yacc: 3,123; perl: 2,385; javascript: 1,715; cs: 1,193; xml: 1,110; makefile: 804; cobol: 741; sql: 709; ruby: 620; f90: 566; ada: 534; asm: 464; fortran: 350; objc: 289; tcl: 258; java: 157; erlang: 61; pascal: 51; ml: 49; awk: 44; haskell: 36
file content (996 lines) | stat: -rw-r--r-- 30,106 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
#include "CompletionHelper.hpp"

#include "CxxScannerTokens.h"
#include "CxxTokenizer.h"
#include "file_logger.h"

#include <vector>
#include <wx/regex.h>

namespace
{
#define PREPEND_STRING(tokn) expression.insert(expression.begin(), tokn)
#define MAX_TIP_LINE_SIZE 200

bool is_word_char(wxChar ch)
{
    return (ch >= 65 && ch <= 90)     // uppercase A-Z
           || (ch >= 97 && ch <= 122) // lowercase a-z
           || (ch >= 48 && ch <= 57)  // 0-9
           || (ch == '_');
}

wxString wrap_lines(const wxString& str)
{
    wxString wrappedString;

    int curLineBytes(0);
    wxString::const_iterator iter = str.begin();
    for(; iter != str.end(); iter++) {
        if(*iter == '\t') {
            wrappedString << " ";

        } else if(*iter == '\n') {
            wrappedString << "\n";
            curLineBytes = 0;

        } else if(*iter == '\r') {
            // Skip it

        } else {
            wrappedString << *iter;
        }
        curLineBytes++;

        if(curLineBytes == MAX_TIP_LINE_SIZE) {

            // Wrap the lines
            if(wrappedString.IsEmpty() == false && wrappedString.Last() != '\n') {
                wrappedString << "\n";
            }
            curLineBytes = 0;
        }
    }
    return wrappedString;
}

} // namespace

CompletionHelper::CompletionHelper() {}

CompletionHelper::~CompletionHelper() {}

wxString CompletionHelper::get_expression(const wxString& file_content, bool for_calltip, wxString* last_word) const
{
#define LAST_TOKEN_IS(token_type) (!types.empty() && (types[types.size() - 1] == token_type))
    // tokenize the text
    CxxTokenizer tokenizer;
    tokenizer.Reset(file_content);

    CxxLexerToken token;
    std::vector<std::pair<wxString, int>> tokens;
    tokens.reserve(10000);

    while(tokenizer.NextToken(token)) {
        tokens.push_back({ token.GetWXString(), token.GetType() });
    }

    int i = static_cast<int>(tokens.size() - 1);
    bool cont = true;
    int parentheses_depth = 0;
    if(for_calltip) {
        // read backwards until we find the first open parentheses
        for(; (i >= 0) && cont; --i) {
            switch(tokens[i].second) {
            case '(':
                if(parentheses_depth == 0) {
                    cont = false;
                } else {
                    parentheses_depth--;
                }
                break;
            case ')':
                parentheses_depth++;
                break;
            default:
                break;
            }
        }
    }

    std::vector<wxString> expression;
    std::vector<int> types;
    int depth = 0;
    cont = true;
    for(; (i >= 0) && cont; --i) {
        const wxString& text = tokens[i].first;
        int type = tokens[i].second;
        switch(type) {
        case '[':
        case '{':
        case '(':
        case '<':
            if(depth == 0) {
                cont = false;
            } else {
                PREPEND_STRING(text);
                depth--;
            }
            break;
        case '>':
        case ']':
        case ')':
            if(depth == 0 && LAST_TOKEN_IS(T_IDENTIFIER)) {
                cont = false;
            } else {
                PREPEND_STRING(text);
                depth++;
            }
            break;
        case '}':
            if(depth == 0) {
                cont = false;
            } else {
                PREPEND_STRING(text);
                depth++;
            }
            break;
        case ';':
        case T_PP_DEFINE:
        case T_PP_DEFINED:
        case T_PP_IF:
        case T_PP_IFNDEF:
        case T_PP_IFDEF:
        case T_PP_ELSE:
        case T_PP_ELIF:
        case T_PP_LINE:
        case T_PP_PRAGMA:
        case T_PP_UNDEF:
        case T_PP_ERROR:
        case T_PP_ENDIF:
        case T_PP_IDENTIFIER:
        case T_PP_DEC_NUMBER:
        case T_PP_OCTAL_NUMBER:
        case T_PP_HEX_NUMBER:
        case T_PP_FLOAT_NUMBER:
        case T_PP_STRING:
        case T_PP_AND:
        case T_PP_OR:
        case T_PP_STATE_EXIT:
        case T_PP_INCLUDE_FILENAME:
        case T_PP_INCLUDE:
        case T_PP_GT:
        case T_PP_GTEQ:
        case T_PP_LT:
        case T_PP_LTEQ:
            cont = false;
            break;
        case '=':
            if(depth == 0) {
                cont = false;
            } else {
                PREPEND_STRING(text);
            }
            break;
        case T_IDENTIFIER:
            if(LAST_TOKEN_IS(T_IDENTIFIER)) {
                // we do not allow two consecutive T_IDENTIFIER
                cont = false;
                break;
            } else {
                PREPEND_STRING(text);
            }
            break;
        case T_ALIGNAS:
        case T_ALIGNOF:
        case T_AND:
        case T_AND_EQ:
        case T_ASM:
        case T_AUTO:
        case T_BITAND:
        case T_BITOR:
        case T_BOOL:
        case T_BREAK:
        case T_CATCH:
        case T_CHAR:
        case T_CHAR16_T:
        case T_CHAR32_T:
        case T_CLASS:
        case T_COMPL:
        case T_CONST:
        case T_CONSTEXPR:
        case T_CONTINUE:
        case T_DECLTYPE:
        case T_DEFAULT:
        case T_DELETE:
        case T_DO:
        case T_DOUBLE:
        case T_ELSE:
        case T_ENUM:
        case T_EXPLICIT:
        case T_EXPORT:
        case T_EXTERN:
        case T_FALSE:
        case T_FINAL:
        case T_FLOAT:
        case T_FOR:
        case T_FRIEND:
        case T_GOTO:
        case T_IF:
        case T_INLINE:
        case T_INT:
        case T_LONG:
        case T_MUTABLE:
        case T_NAMESPACE:
        case T_NEW:
        case T_NOEXCEPT:
        case T_NOT:
        case T_NOT_EQ:
        case T_NULLPTR:
        case T_OPERATOR:
        case T_OR:
        case T_OR_EQ:
        case T_OVERRIDE:
        case T_PRIVATE:
        case T_PROTECTED:
        case T_PUBLIC:
        case T_REGISTER:
        case T_CASE:
        case T_SHORT:
        case T_SIGNED:
        case T_SIZEOF:
        case T_STATIC:
        case T_STATIC_ASSERT:
        case T_STRUCT:
        case T_SWITCH:
        case T_TEMPLATE:
        case T_THREAD_LOCAL:
        case T_THROW:
        case T_TRUE:
        case T_TRY:
        case T_TYPEDEF:
        case T_TYPEID:
        case T_TYPENAME:
        case T_UNION:
        case T_UNSIGNED:
        case T_USING:
        case T_VIRTUAL:
        case T_VOID:
        case T_VOLATILE:
        case T_WCHAR_T:
        case T_WHILE:
        case T_XOR:
        case T_XOR_EQ:
        case T_STRING:
        case T_DOT_STAR:
        case T_ARROW_STAR:
        case T_PLUS_PLUS:
        case T_MINUS_MINUS:
        case T_LS:
        case T_LE:
        case T_GE:
        case T_EQUAL:
        case T_NOT_EQUAL:
        case T_AND_AND:
        case T_OR_OR:
        case T_STAR_EQUAL:
        case T_SLASH_EQUAL:
        case T_DIV_EQUAL:
        case T_PLUS_EQUAL:
        case T_MINUS_EQUAL:
        case T_LS_ASSIGN:
        case T_RS_ASSIGN:
        case T_AND_EQUAL:
        case T_POW_EQUAL:
        case T_OR_EQUAL:
        case T_3_DOTS:
        case T_RETURN:
        case ',':
        case '*':
        case '&':
        case '!':
        case '~':
        case '+':
        case '^':
        case '|':
        case '%':
        case '?':
        case '/':
        case ':':
        case '-':
        case '@':
            if(depth <= 0) {
                cont = false;
            } else {
                PREPEND_STRING(text);
            }
            break;
        case T_CONST_CAST:
        case T_DYNAMIC_CAST:
        case T_REINTERPRET_CAST:
        case T_STATIC_CAST:
        default:
            PREPEND_STRING(text);
            break;
        }
        types.push_back(type);
    }

    // build the expression from the vector
    wxString expression_string;
    for(const auto& t : expression) {
        expression_string << t;
    }
    if(last_word && !expression.empty()) {
        *last_word = expression[expression.size() - 1];
    }
    return expression_string;
#undef LAST_TOKEN_IS
}

wxString CompletionHelper::truncate_file_to_location(const wxString& file_content, size_t line, size_t column,
                                                     size_t flags) const
{
    size_t curline = 0;
    size_t offset = 0;

    // locate the line
    for(const auto& ch : file_content) {
        if(curline == line) {
            break;
        }
        switch(ch.GetValue()) {
        case '\n':
            ++curline;
            ++offset;
            break;
        default:
            ++offset;
            break;
        }
    }

    if(curline != line) {
        return wxEmptyString;
    }

    // columns
    offset += column;

    if(offset < file_content.size()) {
        if(flags & (TRUNCATE_COMPLETE_WORDS | TRUNCATE_COMPLETE_LINES)) {
            while(true) {
                size_t next_pos = offset;

                if(next_pos < file_content.size()) {
                    wxChar next_char = file_content[next_pos];
                    if(flags & TRUNCATE_COMPLETE_WORDS) {
                        // complete words only
                        if(is_word_char(next_char)) {
                            offset += 1;
                        } else {
                            break;
                        }

                    } else {
                        // TRUNCATE_COMPLETE_LINES
                        if(next_char == '\n') {
                            break;
                        } else {
                            offset += 1;
                        }
                    }
                } else {
                    break;
                }
            }
        }
        return file_content.Mid(0, offset);
    }
    return wxEmptyString;
}

thread_local std::unordered_set<wxString> words;
namespace
{
void populate_keywords()
{
    if(words.empty()) {
        words.insert("auto");
        words.insert("break");
        words.insert("case");
        words.insert("char");
        words.insert("const");
        words.insert("continue");
        words.insert("default");
        words.insert("define");
        words.insert("defined");
        words.insert("do");
        words.insert("double");
        words.insert("elif");
        words.insert("else");
        words.insert("endif");
        words.insert("enum");
        words.insert("error");
        words.insert("extern");
        words.insert("float");
        words.insert("for");
        words.insert("goto");
        words.insert("if");
        words.insert("ifdef");
        words.insert("ifndef");
        words.insert("include");
        words.insert("int");
        words.insert("line");
        words.insert("long");
        words.insert("bool");
        words.insert("pragma");
        words.insert("register");
        words.insert("return");
        words.insert("short");
        words.insert("signed");
        words.insert("sizeof");
        words.insert("static");
        words.insert("struct");
        words.insert("switch");
        words.insert("typedef");
        words.insert("undef");
        words.insert("union");
        words.insert("unsigned");
        words.insert("void");
        words.insert("volatile");
        words.insert("while");
        words.insert("class");
        words.insert("namespace");
        words.insert("delete");
        words.insert("friend");
        words.insert("inline");
        words.insert("new");
        words.insert("operator");
        words.insert("overload");
        words.insert("protected");
        words.insert("private");
        words.insert("public");
        words.insert("this");
        words.insert("virtual");
        words.insert("template");
        words.insert("typename");
        words.insert("dynamic_cast");
        words.insert("static_cast");
        words.insert("const_cast");
        words.insert("reinterpret_cast");
        words.insert("using");
        words.insert("throw");
        words.insert("catch");
        words.insert("nullptr");
        words.insert("noexcept");
        words.insert("override");
        words.insert("explicit");
        words.insert("constexpr");
        words.insert("thread_local");
        words.insert("true");
        words.insert("false");
    }
}
} // namespace
bool CompletionHelper::is_cxx_keyword(const wxString& word)
{
    populate_keywords();
    return words.count(word) != 0;
}

std::vector<wxString> CompletionHelper::split_function_signature(const wxString& signature, wxString* return_value,
                                                                 size_t flags) const
{
    // ---------------------------------------------------------------------------------------------
    // ----------------macros start-------------------------------------------------------------------
    // ---------------------------------------------------------------------------------------------
#define ADD_CURRENT_PARAM(current_param) \
    current_param->Trim().Trim(false);   \
    if(!current_param->empty()) {        \
        args.push_back(*current_param);  \
    }                                    \
    current_param->clear();
#define LAST_TOKEN_IS(token_type) (!types.empty() && (types[types.size() - 1] == token_type))
#define LAST_TOKEN_IS_ONE_OF_2(t1, t2) (LAST_TOKEN_IS(t1) || LAST_TOKEN_IS(t2))
#define LAST_TOKEN_IS_ONE_OF_3(t1, t2, t3) (LAST_TOKEN_IS_ONE_OF_2(t1, t2) || LAST_TOKEN_IS(t3))
#define LAST_TOKEN_IS_ONE_OF_4(t1, t2, t3, t4) (LAST_TOKEN_IS_ONE_OF_3(t1, t2, t3) || LAST_TOKEN_IS(t4))
#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))
#define LAST_TOKEN_IS_CLOSING_PARENTHESES() LAST_TOKEN_IS_ONE_OF_4('}', ']', '>', ')')
#define LAST_TOKEN_IS_OPEN_PARENTHESES() LAST_TOKEN_IS_ONE_OF_4('{', '[', '<', '(')
#define REMOVE_TRAILING_SPACE()                                                         \
    if(!current_param->empty() && (*current_param)[current_param->size() - 1] == ' ') { \
        current_param->RemoveLast();                                                    \
    }
#define APPEND_SPACE_IF_MISSING()                                                         \
    if(!current_param->empty() && ((*current_param)[current_param->size() - 1] != ' ')) { \
        current_param->Append(" ");                                                       \
    }

    // ---------------------------------------------------------------------------------------------
    // ----------------macros end-------------------------------------------------------------------
    // ---------------------------------------------------------------------------------------------
    CxxTokenizer tokenizer;
    tokenizer.Reset(signature);

    CxxLexerToken token;

    wxString cur_func_param;
    wxString* current_param = &cur_func_param;
    std::vector<wxString> args;
    std::vector<wxString> func_args;
    int depth = 0;

    std::vector<int> types;
    // search for the first opening brace
    while(tokenizer.NextToken(token)) {
        if(token.GetType() == '(') {
            depth = 1;
            break;
        }
    }

    bool done_collecting_args = false;
    constexpr int STATE_NORMAL = 0;
    constexpr int STATE_DEFAULT_VALUE = 1;
    int state = STATE_NORMAL;
    while(tokenizer.NextToken(token)) {
        switch(state) {
        case STATE_DEFAULT_VALUE:
            // consume everything until we reach signature end
            // or until we hit a "," (where depth==1)
            switch(token.GetType()) {
            case '<':
            case '{':
            case '(':
            case '[':
                depth++;
                break;
            case '>':
            case '}':
            case ']':
                depth--;
                break;
            case ')':
                depth--;
                if(depth == 0) {
                    // end of argument reading, switch back to the normal state
                    tokenizer.UngetToken();
                    // restore the depth
                    depth++;
                    state = STATE_NORMAL;
                }
                break;
            case ',':
                if(depth == 1) {
                    tokenizer.UngetToken();
                    state = STATE_NORMAL;
                }
                break;
            default:
                break;
            }
            break;
        case STATE_NORMAL:
            switch(token.GetType()) {
            case T_ALIGNAS:
            case T_ALIGNOF:
            case T_AND:
            case T_AND_EQ:
            case T_ASM:
            case T_AUTO:
            case T_BITAND:
            case T_BITOR:
            case T_BOOL:
            case T_BREAK:
            case T_CATCH:
            case T_CHAR:
            case T_CHAR16_T:
            case T_CHAR32_T:
            case T_CLASS:
            case T_COMPL:
            case T_CONST:
            case T_CONSTEXPR:
            case T_CONST_CAST:
            case T_CONTINUE:
            case T_DECLTYPE:
            case T_DEFAULT:
            case T_DELETE:
            case T_DO:
            case T_DOUBLE:
            case T_DYNAMIC_CAST:
            case T_ELSE:
            case T_ENUM:
            case T_EXPLICIT:
            case T_EXPORT:
            case T_EXTERN:
            case T_FALSE:
            case T_FINAL:
            case T_FLOAT:
            case T_FOR:
            case T_FRIEND:
            case T_GOTO:
            case T_IF:
            case T_INLINE:
            case T_INT:
            case T_LONG:
            case T_MUTABLE:
            case T_NAMESPACE:
            case T_NEW:
            case T_NOEXCEPT:
            case T_NOT:
            case T_NOT_EQ:
            case T_NULLPTR:
            case T_OPERATOR:
            case T_OR:
            case T_OR_EQ:
            case T_OVERRIDE:
            case T_PRIVATE:
            case T_PROTECTED:
            case T_PUBLIC:
            case T_REGISTER:
            case T_REINTERPRET_CAST:
            case T_CASE:
            case T_SHORT:
            case T_SIGNED:
            case T_SIZEOF:
            case T_STATIC:
            case T_STATIC_ASSERT:
            case T_STATIC_CAST:
            case T_STRUCT:
            case T_SWITCH:
            case T_TEMPLATE:
            case T_THREAD_LOCAL:
            case T_THROW:
            case T_TRUE:
            case T_TRY:
            case T_TYPEDEF:
            case T_TYPEID:
            case T_TYPENAME:
            case T_UNION:
            case T_UNSIGNED:
            case T_USING:
            case T_VIRTUAL:
            case T_VOID:
            case T_VOLATILE:
            case T_WCHAR_T:
            case T_WHILE:
            case T_XOR:
            case T_XOR_EQ:
            case T_STRING:
            case T_DOT_STAR:
            case T_ARROW_STAR:
            case T_PLUS_PLUS:
            case T_MINUS_MINUS:
            case T_LS:
            case T_LE:
            case T_GE:
            case T_EQUAL:
            case T_NOT_EQUAL:
            case T_AND_AND:
            case T_OR_OR:
            case T_STAR_EQUAL:
            case T_SLASH_EQUAL:
            case T_DIV_EQUAL:
            case T_PLUS_EQUAL:
            case T_MINUS_EQUAL:
            case T_LS_ASSIGN:
            case T_RS_ASSIGN:
            case T_AND_EQUAL:
            case T_POW_EQUAL:
            case T_OR_EQUAL:
            case T_3_DOTS:
            case '&':
            case ':':
                current_param->Append(token.GetWXString());
                APPEND_SPACE_IF_MISSING();
                break;
            case '*':
                if(LAST_TOKEN_IS('*')) {
                    REMOVE_TRAILING_SPACE();
                }
                current_param->Append(token.GetWXString());
                APPEND_SPACE_IF_MISSING();
                break;
            case T_IDENTIFIER: {
                wxString peeked_token_text;
                bool add_identifier = true;
                int next_token_type = tokenizer.PeekToken(peeked_token_text);
                // Check if we want to ignore the argument name
                if((depth == 1) && (next_token_type == ',' || next_token_type == '=' || next_token_type == ')') &&
                   (flags & STRIP_NO_NAME)) {
                    // two consecutive T_IDENTIFIER, dont add it
                    add_identifier = false;
                } else if(LAST_TOKEN_IS_CLOSING_PARENTHESES() || LAST_TOKEN_IS_ONE_OF_2(T_IDENTIFIER, '*')) {
                    APPEND_SPACE_IF_MISSING();
                }

                if(add_identifier) {
                    current_param->Append(token.GetWXString());
                }
            } break;
            case T_ARROW:
                if(done_collecting_args) {
                    // we are collecting function return value now, disregard it
                } else {
                    current_param->Append(token.GetWXString());
                }
                break;
            case ',':
                if(depth == 1) {
                    ADD_CURRENT_PARAM(current_param);
                } else {
                    current_param->Append(",");
                    APPEND_SPACE_IF_MISSING();
                }
                break;
            case '>':
            case ']':
            case '}':
                depth--;
                REMOVE_TRAILING_SPACE();
                current_param->Append(token.GetWXString());
                break;
            case ')':
                depth--;
                if(!done_collecting_args && depth == 0) {
                    // reached signature end
                    ADD_CURRENT_PARAM(current_param);
                    func_args.swap(args);
                    done_collecting_args = true;
                } else {
                    if(LAST_TOKEN_IS_CLOSING_PARENTHESES() || LAST_TOKEN_IS(T_IDENTIFIER)) {
                        REMOVE_TRAILING_SPACE();
                    }
                    current_param->Append(token.GetWXString());
                }
                break;
            case '[':
            case '<':
            case '(':
            case '{':
                depth++;
                REMOVE_TRAILING_SPACE();
                current_param->Append(token.GetWXString());
                break;
            case '=':
                if((depth == 1) && (flags & STRIP_NO_DEFAULT_VALUES)) {
                    state = STATE_DEFAULT_VALUE;
                } else {
                    APPEND_SPACE_IF_MISSING();
                    current_param->Append("= ");
                }
                break;
            default:
                current_param->Append(token.GetWXString());
                break;
            }

            break;
        }
        types.push_back(token.GetType());
    }

    if(!done_collecting_args) {
        // we did not complete
        func_args.swap(args);
    } else {
        // check if we have a return value
        ADD_CURRENT_PARAM(current_param);
        if(!args.empty() && return_value) {
            *return_value = args[0].Trim().Trim(false);
        }
    }
    return func_args;
#undef ADD_CURRENT_PARAM
#undef LAST_TOKEN_IS
#undef LAST_TOKEN_IS_ONE_OF_2
#undef LAST_TOKEN_IS_ONE_OF_3
#undef LAST_TOKEN_IS_ONE_OF_4
#undef LAST_TOKEN_IS_ONE_OF_5
#undef LAST_TOKEN_IS_CLOSING_PARENTHESES
#undef LAST_TOKEN_IS_OPEN_PARENTHESES
#undef REMOVE_TRAILING_SPACE
}

namespace
{
thread_local wxRegEx reDoxyParam("([@\\\\]{1}param)[ \t]+([_a-z][a-z0-9_]*)?", wxRE_DEFAULT | wxRE_ICASE);
thread_local wxRegEx reDoxyBrief("([@\\\\]{1}(brief|details))[ \t]*", wxRE_DEFAULT | wxRE_ICASE);
thread_local wxRegEx reDoxyThrow("([@\\\\]{1}(throw|throws))[ \t]*", wxRE_DEFAULT | wxRE_ICASE);
thread_local wxRegEx reDoxyReturn("([@\\\\]{1}(return|retval|returns))[ \t]*", wxRE_DEFAULT | wxRE_ICASE);
thread_local wxRegEx reDoxyToDo("([@\\\\]{1}todo)[ \t]*", wxRE_DEFAULT | wxRE_ICASE);
thread_local wxRegEx reDoxyRemark("([@\\\\]{1}(remarks|remark))[ \t]*", wxRE_DEFAULT | wxRE_ICASE);
thread_local wxRegEx reDate("([@\\\\]{1}date)[ \t]*", wxRE_DEFAULT | wxRE_ICASE);
thread_local wxRegEx reFN("([@\\\\]{1}fn)[ \t]*", wxRE_DEFAULT | wxRE_ICASE);
} // namespace

wxString CompletionHelper::format_comment(TagEntryPtr tag, const wxString& input_comment) const
{
    return format_comment(tag.Get(), input_comment);
}

wxString CompletionHelper::format_comment(TagEntry* tag, const wxString& input_comment) const
{
    wxString beautified_comment;
    if(tag) {
        if(tag->IsMethod()) {
            auto args = split_function_signature(tag->GetSignature(), nullptr);
            beautified_comment << "```\n";
            if(args.empty()) {
                beautified_comment << tag->GetName() << "()\n";
            } else {
                beautified_comment << tag->GetName() << "(\n";
                for(const wxString& arg : args) {
                    beautified_comment << "  " << arg << ",\n";
                }

                if(beautified_comment.EndsWith(",\n")) {
                    beautified_comment.RemoveLast(2);
                }
                beautified_comment << ")\n";
            }
            beautified_comment << "```\n";
        } else if(tag->GetKind() == "variable" || tag->GetKind() == "member" || tag->IsLocalVariable()) {
            wxString clean_pattern = tag->GetPatternClean();
            clean_pattern.Trim().Trim(false);

            if(!clean_pattern.empty()) {
                beautified_comment << "```\n";
                beautified_comment << clean_pattern << "\n";
                beautified_comment << "```\n";
            } else {
                beautified_comment << tag->GetKind() << "\n";
            }
        } else {
            // other
            wxString clean_pattern = tag->GetPatternClean();
            clean_pattern.Trim().Trim(false);
            if(!clean_pattern.empty()) {
                beautified_comment << "```\n";
                beautified_comment << clean_pattern << "\n";
                beautified_comment << "```\n";
            }
        }
    }
    wxString formatted_comment;
    if(!input_comment.empty()) {
        formatted_comment = wrap_lines(input_comment);

        if(reDoxyParam.IsValid() && reDoxyParam.Matches(formatted_comment)) {
            reDoxyParam.ReplaceAll(&formatted_comment, "\nParameter\n`\\2`");
        }

        if(reDoxyBrief.IsValid() && reDoxyBrief.Matches(formatted_comment)) {
            reDoxyBrief.ReplaceAll(&formatted_comment, "");
        }

        if(reDoxyThrow.IsValid() && reDoxyThrow.Matches(formatted_comment)) {
            reDoxyThrow.ReplaceAll(&formatted_comment, "\n`Throws:`\n");
        }

        if(reDoxyReturn.IsValid() && reDoxyReturn.Matches(formatted_comment)) {
            reDoxyReturn.ReplaceAll(&formatted_comment, "\n`Returns:`\n");
        }

        if(reDoxyToDo.IsValid() && reDoxyToDo.Matches(formatted_comment)) {
            reDoxyToDo.ReplaceAll(&formatted_comment, "\nTODO\n");
        }

        if(reDoxyRemark.IsValid() && reDoxyRemark.Matches(formatted_comment)) {
            reDoxyRemark.ReplaceAll(&formatted_comment, "\n  ");
        }

        if(reDate.IsValid() && reDate.Matches(formatted_comment)) {
            reDate.ReplaceAll(&formatted_comment, "Date ");
        }

        if(reFN.IsValid() && reFN.Matches(formatted_comment)) {
            size_t fnStart, fnLen, fnEnd;
            if(reFN.GetMatch(&fnStart, &fnLen)) {
                fnEnd = formatted_comment.find('\n', fnStart);
                if(fnEnd != wxString::npos) {
                    // remove the string from fnStart -> fnEnd (including ther terminating \n)
                    formatted_comment.Remove(fnStart, (fnEnd - fnStart) + 1);
                }
            }
        }

        // horizontal line
        if(!beautified_comment.empty()) {
            beautified_comment << "---\n";
        }
        beautified_comment << formatted_comment;
    }
    return beautified_comment;
}

thread_local wxRegEx reIncludeFile("include *[\\\"\\<]{1}([a-zA-Z0-9_/\\.\\+\\-]*)");

bool CompletionHelper::is_line_include_statement(const wxString& line, wxString* file_name, wxString* suffix) const
{
    wxString tmp_line = line;

    tmp_line.Trim().Trim(false);
    tmp_line.Replace("\t", " ");

    // search for the "#"
    wxString remainder;
    if(!tmp_line.StartsWith("#", &remainder)) {
        return false;
    }

    if(!reIncludeFile.Matches(remainder)) {
        return false;
    }

    if(file_name) {
        *file_name = reIncludeFile.GetMatch(remainder, 1);
    }

    if(suffix) {
        if(tmp_line.Contains("<")) {
            *suffix = ">";
        } else {
            *suffix = "\"";
        }
    }
    return true;
}

bool CompletionHelper::is_include_statement(const wxString& f_content, wxString* file_name, wxString* suffix) const
{
    // read backward until we find LF
    if(f_content.empty()) {
        return false;
    }

    int i = f_content.size() - 1;
    for(; i >= 0; --i) {
        if(f_content[i] == '\n') {
            break;
        }
    }

    wxString line = f_content.Mid(i);
    return is_line_include_statement(line, file_name, suffix);
}

wxString CompletionHelper::normalize_function(const TagEntry* tag, size_t flags)
{
    wxString return_value;
    wxString fullname;

    wxString name = tag->GetName();
    wxString signature = tag->GetSignature();
    fullname << name << "(";
    std::vector<wxString> args = split_function_signature(signature, &return_value, flags);
    wxString funcsig;
    for(const wxString& arg : args) {
        funcsig << arg << ", ";
    }

    if(funcsig.EndsWith(", ")) {
        funcsig.RemoveLast(2);
    }

    fullname << funcsig << ")";
    if(tag->is_const()) {
        fullname << " const";
    }
    return fullname;
}

wxString CompletionHelper::normalize_function(TagEntryPtr tag, size_t flags)
{
    return normalize_function(tag.Get(), flags);
}

void CompletionHelper::get_cxx_keywords(std::vector<wxString>& keywords)
{
    populate_keywords();
    keywords.reserve(words.size());
    for(const wxString& word : words) {
        keywords.push_back(word);
    }
}