File: LSPUtils.cpp

package info (click to toggle)
codelite 17.0.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: 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 (186 lines) | stat: -rw-r--r-- 6,632 bytes parent folder | download | duplicates (3)
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
#include "LSPUtils.hpp"

#include "SimpleTokenizer.hpp"
#include "macros.h"

#include <array>
#include <wx/filesys.h>

LSPUtils::LSPUtils() {}

LSPUtils::~LSPUtils() {}

void LSPUtils::encode_semantic_tokens(const std::vector<TokenWrapper>& tokens_vec, std::vector<int>* encoded_arr)
{
    TokenWrapper dummy;
    dummy.token = SimpleTokenizer::Token(0, 0, 0, 0);

    encoded_arr->reserve(tokens_vec.size() * 5);
    const auto* prev_token_ptr = &dummy.token;
    for(size_t i = 0; i < tokens_vec.size(); ++i) {
        auto* current_token_ptr = &tokens_vec[i].token;
        std::array<int, 5> info;

        bool changed_line = prev_token_ptr->line() != current_token_ptr->line();
        if(changed_line) {
            info[0] = current_token_ptr->line() - prev_token_ptr->line(); // keep the delta
        } else {
            // we are the same line, set it to 0
            info[0] = 0;
        }

        // represent each column relative the previous token start column
        if(changed_line) {
            info[1] = current_token_ptr->column();
        } else {
            info[1] = current_token_ptr->column() -
                      prev_token_ptr->column(); // keep the delta to the previous token on this line
        }

        // length
        info[2] = current_token_ptr->length();

        // token type
        info[3] = tokens_vec[i].type;

        // modifiers
        info[4] = changed_line ? 99 : 0;
        encoded_arr->insert(encoded_arr->end(), info.begin(), info.end());

        // update the previous token
        prev_token_ptr = current_token_ptr;
    }
}

LSP::eSymbolKind LSPUtils::get_symbol_kind(const TagEntry* tag)
{
    LSP::eSymbolKind kind = LSP::eSymbolKind::kSK_Variable;
    if(tag->IsMethod()) {
        kind = LSP::eSymbolKind::kSK_Method;
    } else if(tag->IsConstructor()) {
        kind = LSP::eSymbolKind::kSK_Class;
    } else if(tag->IsClass()) {
        kind = LSP::eSymbolKind::kSK_Class;
    } else if(tag->IsStruct()) {
        kind = LSP::eSymbolKind::kSK_Struct;
    } else if(tag->IsLocalVariable()) {
        kind = LSP::eSymbolKind::kSK_Variable;
    } else if(tag->IsTypedef()) {
        kind = LSP::eSymbolKind::kSK_TypeParameter;
    } else if(tag->IsMacro()) {
        kind = LSP::eSymbolKind::kSK_TypeParameter;
    } else if(tag->GetKind() == "namespace") {
        kind = LSP::eSymbolKind::kSK_Module;
    } else if(tag->GetKind() == "union") {
        kind = LSP::eSymbolKind::kSK_Struct;
    } else if(tag->GetKind() == "enum") {
        kind = LSP::eSymbolKind::kSK_Enum;
    } else if(tag->GetKind() == "enumerator") {
        kind = LSP::eSymbolKind::kSK_EnumMember;
    }
    return kind;
}

LSP::CompletionItem::eCompletionItemKind LSPUtils::get_completion_kind(const TagEntry* tag)
{
    LSP::CompletionItem::eCompletionItemKind kind = LSP::CompletionItem::kKindVariable;
    if(tag->IsMethod()) {
        kind = LSP::CompletionItem::kKindFunction;
    } else if(tag->IsConstructor()) {
        kind = LSP::CompletionItem::kKindClass;
    } else if(tag->IsClass()) {
        kind = LSP::CompletionItem::kKindClass;
    } else if(tag->IsStruct()) {
        kind = LSP::CompletionItem::kKindStruct;
    } else if(tag->IsLocalVariable()) {
        kind = LSP::CompletionItem::kKindVariable;
    } else if(tag->IsTypedef()) {
        kind = LSP::CompletionItem::kKindTypeParameter;
    } else if(tag->IsMacro()) {
        kind = LSP::CompletionItem::kKindTypeParameter;
    } else if(tag->GetKind() == "namespace") {
        kind = LSP::CompletionItem::kKindModule;
    } else if(tag->GetKind() == "union") {
        kind = LSP::CompletionItem::kKindStruct;
    } else if(tag->GetKind() == "enum") {
        kind = LSP::CompletionItem::kKindEnum;
    } else if(tag->GetKind() == "enumerator") {
        kind = LSP::CompletionItem::kKindEnumMember;
    } else if(tag->GetKind() == "file") {
        kind = LSP::CompletionItem::kKindTypeParameter;
    }
    return kind;
}

std::vector<LSP::SymbolInformation> LSPUtils::to_symbol_information_array(const std::vector<TagEntryPtr>& tags,
                                                                          bool for_tree_view)
{
    std::vector<LSP::SymbolInformation> result;
    result.reserve(tags.size());

    wxStringSet_t parent_seen;
    for(auto tag : tags) {
        LSP::SymbolInformation symbol_information;
        to_symbol_information(tag.Get(), symbol_information, for_tree_view ? &parent_seen : nullptr);
        result.push_back(symbol_information);
    }
    return result;
}

std::vector<LSP::SymbolInformation> LSPUtils::to_symbol_information_array(const std::vector<TagEntry>& tags,
                                                                          bool for_tree_view)
{
    std::vector<LSP::SymbolInformation> result;
    result.reserve(tags.size());

    wxStringSet_t parent_seen;
    for(const auto& tag : tags) {
        LSP::SymbolInformation symbol_information;
        to_symbol_information(&tag, symbol_information, for_tree_view ? &parent_seen : nullptr);
        result.push_back(symbol_information);
    }
    return result;
}

void LSPUtils::to_symbol_information(const TagEntry* tag, LSP::SymbolInformation& symbol_information,
                                     std::unordered_set<wxString>* parents_seen)
{
    if(parents_seen && tag->IsContainer()) {
        parents_seen->insert(tag->GetName());
    }

    LSP::Location loc;
    LSP::Range range;
    range.SetStart({ tag->GetLine() - 1, 0 });
    range.SetEnd({ tag->GetLine() - 1, 0 });
    loc.SetRange(range);
    loc.SetPath(FileUtils::FilePathToURI(tag->GetFile()));

    symbol_information.SetKind(get_symbol_kind(tag));

    wxString display_name = tag->GetDisplayName();
    wxString display_name_full = tag->GetFullDisplayName();

    if(tag->IsMethod() && tag->GetName().StartsWith("__anon")) {
        // Lambda
        display_name = display_name_full = "[lambda]" + tag->GetSignature();
    } else if(tag->IsNamespace() && tag->GetName().StartsWith("__anon")) {
        display_name = display_name_full = "(anonymous namespace)";
    }

    if(parents_seen) {
        if(parents_seen->count(tag->GetParent())) {
            symbol_information.SetContainerName(tag->GetParent());
            symbol_information.SetName(display_name);
        } else {
            // don't bother adding parent that we did not
            // see until this point
            symbol_information.SetName(display_name_full);
        }

    } else {
        symbol_information.SetContainerName(tag->GetParent());
        symbol_information.SetName(display_name);
    }
    symbol_information.SetLocation(loc);
}