aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/languageserverprotocol/semantictokens.h
blob: 1010a4582a68a54e1bdc6198343afc5375c1ef1c (plain)
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
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#pragma once

#include "jsonkeys.h"
#include "jsonobject.h"
#include "jsonrpcmessages.h"
#include "languageserverprotocol_global.h"
#include "lsptypes.h"

namespace LanguageServerProtocol {

struct LANGUAGESERVERPROTOCOL_EXPORT SemanticToken
{
    int deltaLine = 0;
    int deltaStart = 0;
    int length = 0;
    int tokenIndex = 0;
    int tokenType = 0;
    int rawTokenModifiers = 0;
    int tokenModifiers = 0;
};

class LANGUAGESERVERPROTOCOL_EXPORT SemanticTokensLegend : public JsonObject
{
public:
    using JsonObject::JsonObject;

    // The token types a server uses.
    QList<QString> tokenTypes() const { return array<QString>(tokenTypesKey); }
    void setTokenTypes(const QList<QString> &tokenTypes) { insertArray(tokenTypesKey, tokenTypes); }

    // The token modifiers a server uses.
    QList<QString> tokenModifiers() const { return array<QString>(tokenModifiersKey); }
    void setTokenModifiers(const QList<QString> &value) { insertArray(tokenModifiersKey, value); }

    bool isValid() const override;
};

enum SemanticTokenTypes {
    namespaceToken,
    typeToken,
    classToken,
    enumToken,
    interfaceToken,
    structToken,
    typeParameterToken,
    parameterToken,
    variableToken,
    propertyToken,
    enumMemberToken,
    eventToken,
    functionToken,
    methodToken,
    macroToken,
    keywordToken,
    modifierToken,
    commentToken,
    stringToken,
    numberToken,
    regexpToken,
    operatorToken
};

enum SemanticTokenModifiers {
    declarationModifier = 0x1,
    definitionModifier = 0x2,
    readonlyModifier = 0x4,
    staticModifier = 0x8,
    deprecatedModifier = 0x10,
    abstractModifier = 0x20,
    asyncModifier = 0x40,
    modificationModifier = 0x80,
    documentationModifier = 0x100,
    defaultLibraryModifier = 0x200
};

class LANGUAGESERVERPROTOCOL_EXPORT SemanticTokensParams : public JsonObject
{
public:
    using JsonObject::JsonObject;

    TextDocumentIdentifier textDocument() const
    { return typedValue<TextDocumentIdentifier>(textDocumentKey); }
    void setTextDocument(const TextDocumentIdentifier &textDocument)
    { insert(textDocumentKey, textDocument); }

    bool isValid() const override { return contains(textDocumentKey); }
};

class LANGUAGESERVERPROTOCOL_EXPORT SemanticTokensDeltaParams : public SemanticTokensParams
{
public:
    using SemanticTokensParams::SemanticTokensParams;

    QString previousResultId() const { return typedValue<QString>(previousResultIdKey); }
    void setPreviousResultId(const QString &previousResultId)
    {
        insert(previousResultIdKey, previousResultId);
    }

    bool isValid() const override;
};

class LANGUAGESERVERPROTOCOL_EXPORT SemanticTokensRangeParams : public SemanticTokensParams
{
public:
    using SemanticTokensParams::SemanticTokensParams;

    Range range() const { return typedValue<Range>(rangeKey); }
    void setRange(const Range &range) { insert(rangeKey, range); }

    bool isValid() const override;
};

class LANGUAGESERVERPROTOCOL_EXPORT SemanticTokens : public JsonObject
{
public:
    using JsonObject::JsonObject;

    /**
     * An optional result id. If provided and clients support delta updating
     * the client will include the result id in the next semantic token request.
     * A server can then instead of computing all semantic tokens again simply
     * send a delta.
     */
    Utils::optional<QString> resultId() const { return optionalValue<QString>(resultIdKey); }
    void setResultId(const QString &resultId) { insert(resultIdKey, resultId); }
    void clearResultId() { remove(resultIdKey); }

    /// The actual tokens.
    QList<int> data() const { return array<int>(dataKey); }
    void setData(const QList<int> &value) { insertArray(dataKey, value); }

    bool isValid() const override { return contains(dataKey); }

    QList<SemanticToken> toTokens(const QList<int> &tokenTypes,
                                  const QList<int> &tokenModifiers) const;
    static QMap<QString, int> defaultTokenTypesMap();
    static QMap<QString, int> defaultTokenModifiersMap();
};

class LANGUAGESERVERPROTOCOL_EXPORT SemanticTokensResult
    : public std::variant<SemanticTokens, std::nullptr_t>
{
public:
    using variant::variant;
    explicit SemanticTokensResult(const QJsonValue &value);
};

class LANGUAGESERVERPROTOCOL_EXPORT SemanticTokensFullRequest
    : public Request<SemanticTokensResult, std::nullptr_t, SemanticTokensParams>
{
public:
    explicit SemanticTokensFullRequest(const SemanticTokensParams &params);
    using Request::Request;
    constexpr static const char methodName[] = "textDocument/semanticTokens/full";
};

class SemanticTokensEdit : public JsonObject
{
public:
    using JsonObject::JsonObject;

    int start() const { return typedValue<int>(startKey); }
    void setStart(int start) { insert(startKey, start); }

    int deleteCount() const { return typedValue<int>(deleteCountKey); }
    void setDeleteCount(int deleteCount) { insert(deleteCountKey, deleteCount); }

    Utils::optional<QList<int>> data() const { return optionalArray<int>(dataKey); }
    void setData(const QList<int> &value) { insertArray(dataKey, value); }
    void clearData() { remove(dataKey); }

    int dataSize() const { return contains(dataKey) ? value(dataKey).toArray().size() : 0; }

    bool isValid() const override { return contains(dataKey) && contains(deleteCountKey); }
};

class SemanticTokensDelta : public JsonObject
{
public:
    using JsonObject::JsonObject;

    QString resultId() const { return typedValue<QString>(resultIdKey); }
    void setResultId(const QString &resultId) { insert(resultIdKey, resultId); }

    QList<SemanticTokensEdit> edits() const { return array<SemanticTokensEdit>(editsKey); }
    void setEdits(const QList<SemanticTokensEdit> &edits) { insertArray(editsKey, edits); }

    bool isValid() const override { return contains(resultIdKey) && contains(editsKey); }
};

class LANGUAGESERVERPROTOCOL_EXPORT SemanticTokensDeltaResult
    : public std::variant<SemanticTokens, SemanticTokensDelta, std::nullptr_t>
{
public:
    using variant::variant;
    explicit SemanticTokensDeltaResult(const QJsonValue &value);
};

class LANGUAGESERVERPROTOCOL_EXPORT SemanticTokensFullDeltaRequest
    : public Request<SemanticTokensDeltaResult, std::nullptr_t, SemanticTokensDeltaParams>
{
public:
    explicit SemanticTokensFullDeltaRequest(const SemanticTokensDeltaParams &params);
    using Request::Request;
    constexpr static const char methodName[] = "textDocument/semanticTokens/full/delta";
};

class LANGUAGESERVERPROTOCOL_EXPORT SemanticTokensRangeRequest
    : public Request<SemanticTokensResult, std::nullptr_t, SemanticTokensRangeParams>
{
public:
    explicit SemanticTokensRangeRequest(const SemanticTokensRangeParams &params);
    using Request::Request;
    constexpr static const char methodName[] = "textDocument/semanticTokens/range";
};

class LANGUAGESERVERPROTOCOL_EXPORT SemanticTokensRefreshRequest
    : public Request<std::nullptr_t, std::nullptr_t, std::nullptr_t>
{
public:
    explicit SemanticTokensRefreshRequest();
    using Request::Request;
    constexpr static const char methodName[] = "workspace/semanticTokens/refresh";
};

} // namespace LanguageServerProtocol