aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/languageserverprotocol/languagefeatures.cpp
blob: e960296dc803f81dd72759de5abd619ba441dd8a (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
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
/****************************************************************************
**
** Copyright (C) 2018 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.
**
****************************************************************************/

#include "languagefeatures.h"

#include <cstddef>

namespace LanguageServerProtocol {

constexpr const char HoverRequest::methodName[];
constexpr const char GotoDefinitionRequest::methodName[];
constexpr const char GotoTypeDefinitionRequest::methodName[];
constexpr const char GotoImplementationRequest::methodName[];
constexpr const char FindReferencesRequest::methodName[];
constexpr const char DocumentHighlightsRequest::methodName[];
constexpr const char DocumentSymbolsRequest::methodName[];
constexpr const char CodeActionRequest::methodName[];
constexpr const char CodeLensRequest::methodName[];
constexpr const char CodeLensResolveRequest::methodName[];
constexpr const char DocumentLinkRequest::methodName[];
constexpr const char DocumentLinkResolveRequest::methodName[];
constexpr const char DocumentColorRequest::methodName[];
constexpr const char ColorPresentationRequest::methodName[];
constexpr const char DocumentFormattingRequest::methodName[];
constexpr const char DocumentRangeFormattingRequest::methodName[];
constexpr const char DocumentOnTypeFormattingRequest::methodName[];
constexpr const char RenameRequest::methodName[];
constexpr const char SignatureHelpRequest::methodName[];
constexpr const char SemanticHighlightNotification::methodName[];
constexpr const char PrepareRenameRequest::methodName[];

HoverContent LanguageServerProtocol::Hover::content() const
{
    return HoverContent(value(contentsKey));
}

void Hover::setContent(const HoverContent &content)
{
    if (auto val = Utils::get_if<MarkedString>(&content))
        insert(contentsKey, *val);
    else if (auto val = Utils::get_if<MarkupContent>(&content))
        insert(contentsKey, *val);
    else if (auto val = Utils::get_if<QList<MarkedString>>(&content))
        insert(contentsKey, LanguageClientArray<MarkedString>(*val).toJson());
    else
        QTC_ASSERT_STRING("LanguageClient Using unknown type Hover::setContent");
}

HoverRequest::HoverRequest(const TextDocumentPositionParams &params)
    : Request(methodName, params)
{ }

Utils::optional<MarkupOrString> ParameterInformation::documentation() const
{
    QJsonValue documentation = value(documentationKey);
    if (documentation.isUndefined())
        return Utils::nullopt;
    return MarkupOrString(documentation);
}

bool SignatureHelp::isValid(ErrorHierarchy *error) const
{
    return checkArray<SignatureInformation>(error, signaturesKey);
}

GotoDefinitionRequest::GotoDefinitionRequest(const TextDocumentPositionParams &params)
    : Request(methodName, params)
{ }

GotoTypeDefinitionRequest::GotoTypeDefinitionRequest(const TextDocumentPositionParams &params)
    : Request(methodName, params)
{ }

GotoImplementationRequest::GotoImplementationRequest(const TextDocumentPositionParams &params)
    : Request(methodName, params)
{ }

FindReferencesRequest::FindReferencesRequest(const ReferenceParams &params)
    : Request(methodName, params)
{ }

DocumentHighlightsRequest::DocumentHighlightsRequest(const TextDocumentPositionParams &params)
    : Request(methodName, params)
{ }

DocumentSymbolsRequest::DocumentSymbolsRequest(const DocumentSymbolParams &params)
    : Request(methodName, params)
{ }

Utils::optional<QList<CodeActionKind> > CodeActionParams::CodeActionContext::only() const
{
    return optionalArray<CodeActionKind>(onlyKey);
}

void CodeActionParams::CodeActionContext::setOnly(const QList<CodeActionKind> &only)
{
    insertArray(onlyKey, only);
}

bool CodeActionParams::CodeActionContext::isValid(ErrorHierarchy *error) const
{
    return checkArray<Diagnostic>(error, diagnosticsKey);
}

bool CodeActionParams::isValid(ErrorHierarchy *error) const
{
    return check<TextDocumentIdentifier>(error, textDocumentKey)
            && check<Range>(error, rangeKey)
            && check<CodeActionContext>(error, contextKey);
}

CodeActionRequest::CodeActionRequest(const CodeActionParams &params)
    : Request(methodName, params)
{ }

CodeLensRequest::CodeLensRequest(const CodeLensParams &params)
    : Request(methodName, params)
{ }

CodeLensResolveRequest::CodeLensResolveRequest(const CodeLens &params)
    : Request(methodName, params)
{ }

DocumentLinkRequest::DocumentLinkRequest(const DocumentLinkParams &params)
    : Request(methodName, params)
{ }

DocumentLinkResolveRequest::DocumentLinkResolveRequest(const DocumentLink &params)
    : Request(methodName, params)
{ }

DocumentColorRequest::DocumentColorRequest(const DocumentColorParams &params)
    : Request(methodName, params)
{ }

bool Color::isValid(ErrorHierarchy *error) const
{
    return check<int>(error, redKey)
            && check<int>(error, greenKey)
            && check<int>(error, blueKey)
            && check<int>(error, alphaKey);
}

bool ColorPresentationParams::isValid(ErrorHierarchy *error) const
{
    return check<TextDocumentIdentifier>(error, textDocumentKey)
            && check<Color>(error, colorInfoKey)
            && check<Range>(error, rangeKey);
}

ColorPresentationRequest::ColorPresentationRequest(const ColorPresentationParams &params)
    : Request(methodName, params)
{ }

QHash<QString, DocumentFormattingProperty> FormattingOptions::properties() const
{
    QHash<QString, DocumentFormattingProperty> ret;
    for (const QString &key : keys()) {
        if (key == tabSizeKey || key == insertSpaceKey)
            continue;
        QJsonValue property = value(key);
        if (property.isBool())
            ret[key] = property.toBool();
        if (property.isDouble())
            ret[key] = property.toDouble();
        if (property.isString())
            ret[key] = property.toString();
    }
    return ret;
}

void FormattingOptions::setProperty(const QString &key, const DocumentFormattingProperty &property)
{
    using namespace Utils;
    if (auto val = get_if<double>(&property))
        insert(key, *val);
    else if (auto val = get_if<QString>(&property))
        insert(key, *val);
    else if (auto val = get_if<bool>(&property))
        insert(key, *val);
}

bool FormattingOptions::isValid(ErrorHierarchy *error) const
{
    return Utils::allOf(keys(), [this, &error](auto key){
        return (key == tabSizeKey && this->check<int>(error, key))
                || (key == insertSpaceKey && this->check<bool>(error, key))
                || this->check<DocumentFormattingProperty>(error, key);
    });
}

bool DocumentFormattingParams::isValid(ErrorHierarchy *error) const
{
    return check<TextDocumentIdentifier>(error, textDocumentKey)
            && check<FormattingOptions>(error, optionsKey);
}

DocumentFormattingRequest::DocumentFormattingRequest(const DocumentFormattingParams &params)
    : Request(methodName, params)
{ }

bool DocumentRangeFormattingParams::isValid(ErrorHierarchy *error) const
{
    return check<TextDocumentIdentifier>(error, textDocumentKey)
            && check<Range>(error, rangeKey)
            && check<FormattingOptions>(error, optionsKey);
}

DocumentRangeFormattingRequest::DocumentRangeFormattingRequest(
        const DocumentRangeFormattingParams &params)
    : Request(methodName, params)
{ }

bool DocumentOnTypeFormattingParams::isValid(ErrorHierarchy *error) const
{
    return check<TextDocumentIdentifier>(error, textDocumentKey)
            && check<Position>(error, positionKey)
            && check<QString>(error, chKey)
            && check<FormattingOptions>(error, optionsKey);
}

DocumentOnTypeFormattingRequest::DocumentOnTypeFormattingRequest(
        const DocumentFormattingParams &params)
    : Request(methodName, params)
{ }

PrepareRenameRequest::PrepareRenameRequest(const TextDocumentPositionParams &params)
    : Request(methodName, params)
{ }

bool RenameParams::isValid(ErrorHierarchy *error) const
{
    return check<TextDocumentIdentifier>(error, textDocumentKey)
            && check<Position>(error, positionKey)
            && check<QString>(error, newNameKey);
}

RenameRequest::RenameRequest(const RenameParams &params)
    : Request(methodName, params)
{ }

Utils::optional<DocumentUri> DocumentLink::target() const
{
    Utils::optional<QString> optionalTarget = optionalValue<QString>(targetKey);
    return optionalTarget.has_value()
            ? Utils::make_optional(DocumentUri::fromProtocol(optionalTarget.value()))
            : Utils::nullopt;
}

TextDocumentParams::TextDocumentParams()
    : TextDocumentParams(TextDocumentIdentifier())
{ }

TextDocumentParams::TextDocumentParams(const TextDocumentIdentifier &identifier)
    : JsonObject()
{
    setTextDocument(identifier);
}

GotoResult::GotoResult(const QJsonValue &value)
{
    if (value.isArray()) {
        QList<Location> locations;
        for (auto arrayValue : value.toArray()) {
            if (arrayValue.isObject())
                locations.append(Location(arrayValue.toObject()));
        }
        emplace<QList<Location>>(locations);
    } else if (value.isObject()) {
        emplace<Location>(value.toObject());
    } else {
        emplace<std::nullptr_t>(nullptr);
    }
}

template<typename Symbol>
QList<Symbol> documentSymbolsResultArray(const QJsonArray &array)
{
    QList<Symbol> ret;
    for (const auto &arrayValue : array) {
        if (arrayValue.isObject())
            ret << Symbol(arrayValue.toObject());
    }
    return ret;
}

DocumentSymbolsResult::DocumentSymbolsResult(const QJsonValue &value)
{
    if (value.isArray()) {
        QJsonArray array = value.toArray();
        if (array.isEmpty()) {
            *this = QList<SymbolInformation>();
        } else {
            QJsonObject arrayObject = array.first().toObject();
            if (arrayObject.contains(rangeKey))
                *this = documentSymbolsResultArray<DocumentSymbol>(array);
            else
                *this = documentSymbolsResultArray<SymbolInformation>(array);
        }
    } else {
        *this = nullptr;
    }
}

DocumentHighlightsResult::DocumentHighlightsResult(const QJsonValue &value)
{
    if (value.isArray()) {
        QList<DocumentHighlight> highlights;
        for (auto arrayValue : value.toArray()) {
            if (arrayValue.isObject())
                highlights.append(DocumentHighlight(arrayValue.toObject()));
        }
        *this = highlights;
    } else {
        *this = nullptr;
    }
}

MarkedString::MarkedString(const QJsonValue &value)
{
    if (value.isObject()) {
        MarkedLanguageString string(value.toObject());
        if (string.isValid(nullptr))
            emplace<MarkedLanguageString>(string);
    } else if (value.isString()) {
        emplace<QString>(value.toString());
    }
}

LanguageServerProtocol::MarkedString::operator QJsonValue() const
{
    if (auto val = Utils::get_if<QString>(this))
        return *val;
    if (auto val = Utils::get_if<MarkedLanguageString>(this))
        return QJsonValue(*val);
    return {};
}

HoverContent::HoverContent(const QJsonValue &value)
{
    if (value.isArray()) {
        emplace<QList<MarkedString>>(LanguageClientArray<MarkedString>(value).toList());
    } else if (value.isObject()) {
        const QJsonObject &object = value.toObject();
        MarkedLanguageString markedLanguageString(object);
        if (markedLanguageString.isValid(nullptr))
            emplace<MarkedString>(markedLanguageString);
        else
            emplace<MarkupContent>(MarkupContent(object));
    } else if (value.isString()) {
        emplace<MarkedString>(MarkedString(value.toString()));
    }
}

bool HoverContent::isValid(ErrorHierarchy *errorHierarchy) const
{
    if (Utils::holds_alternative<MarkedString>(*this)
            || Utils::holds_alternative<MarkupContent>(*this)
            || Utils::holds_alternative<QList<MarkedString>>(*this)) {
        return true;
    }
    if (errorHierarchy) {
        errorHierarchy->setError(
            QCoreApplication::translate("LanguageServerProtocol::HoverContent",
                                        "HoverContent should be either MarkedString, "
                                        "MarkupContent, or QList<MarkedString>."));
    }
    return false;
}

DocumentFormattingProperty::DocumentFormattingProperty(const QJsonValue &value)
{
    if (value.isBool())
        *this = value.toBool();
    if (value.isDouble())
        *this = value.toDouble();
    if (value.isString())
        *this = value.toString();
}

bool DocumentFormattingProperty::isValid(ErrorHierarchy *error) const
{
    if (Utils::holds_alternative<bool>(*this)
            || Utils::holds_alternative<double>(*this)
            || Utils::holds_alternative<QString>(*this)) {
        return true;
    }
    if (error) {
        error->setError(QCoreApplication::translate(
            "LanguageServerProtocol::MarkedString",
            "DocumentFormattingProperty should be either bool, double, or QString."));
    }
    return false;
}

SignatureHelpRequest::SignatureHelpRequest(const TextDocumentPositionParams &params)
    : Request(methodName, params)
{ }

CodeActionResult::CodeActionResult(const QJsonValue &val)
{
    using ResultArray = QList<Utils::variant<Command, CodeAction>>;
    if (val.isArray()) {
        const QJsonArray array = val.toArray();
        ResultArray result;
        for (const QJsonValue &val : array) {
            Command command(val);
            if (command.isValid(nullptr))
                result << command;
            else
                result << CodeAction(val);
        }
        emplace<ResultArray>(result);
        return;
    }
    emplace<std::nullptr_t>(nullptr);
}

bool CodeAction::isValid(ErrorHierarchy *error) const
{
    return check<QString>(error, titleKey)
           && checkOptional<CodeActionKind>(error, codeActionKindKey)
           && checkOptionalArray<Diagnostic>(error, diagnosticsKey)
           && checkOptional<WorkspaceEdit>(error, editKey)
           && checkOptional<Command>(error, commandKey);
}

Utils::optional<QList<SemanticHighlightToken>> SemanticHighlightingInformation::tokens() const
{
    QList<SemanticHighlightToken> resultTokens;

    const QByteArray tokensByteArray = QByteArray::fromBase64(
        typedValue<QString>(tokensKey).toLocal8Bit());
    constexpr int tokensByteSize = 8;
    int index = 0;
    while (index + tokensByteSize <= tokensByteArray.size()) {
        resultTokens << SemanticHighlightToken(tokensByteArray.mid(index, tokensByteSize));
        index += tokensByteSize;
    }
    return Utils::make_optional(resultTokens);
}

void SemanticHighlightingInformation::setTokens(const QList<SemanticHighlightToken> &tokens)
{
    QByteArray byteArray;
    byteArray.reserve(8 * tokens.size());
    for (const SemanticHighlightToken &token : tokens)
        token.appendToByteArray(byteArray);
    insert(tokensKey, QString::fromLocal8Bit(byteArray.toBase64()));
}

SemanticHighlightToken::SemanticHighlightToken(const QByteArray &token)
{
    QTC_ASSERT(token.size() == 8, return );
    character = ( quint32(token.at(0)) << 24
                | quint32(token.at(1)) << 16
                | quint32(token.at(2)) << 8
                | quint32(token.at(3)));

    length = quint16(token.at(4) << 8 | token.at(5));

    scope = quint16(token.at(6) << 8 | token.at(7));
}

void SemanticHighlightToken::appendToByteArray(QByteArray &byteArray) const
{
    byteArray.append(char((character & 0xff000000) >> 24));
    byteArray.append(char((character & 0x00ff0000) >> 16));
    byteArray.append(char((character & 0x0000ff00) >> 8));
    byteArray.append(char((character & 0x000000ff)));
    byteArray.append(char((length & 0xff00) >> 8));
    byteArray.append(char((length & 0x00ff)));
    byteArray.append(char((scope & 0xff00) >> 8));
    byteArray.append(char((scope & 0x00ff)));
}

Utils::variant<VersionedTextDocumentIdentifier, TextDocumentIdentifier>
SemanticHighlightingParams::textDocument() const
{
    VersionedTextDocumentIdentifier textDocument = fromJsonValue<VersionedTextDocumentIdentifier>(
        value(textDocumentKey));
    ErrorHierarchy error;
    if (!textDocument.isValid(&error)) {
        return TextDocumentIdentifier(textDocument);
    } else {
        return textDocument;
    }
}

bool SemanticHighlightingParams::isValid(ErrorHierarchy *error) const
{
    return checkVariant<VersionedTextDocumentIdentifier, TextDocumentIdentifier>(error,
                                                                                 textDocumentKey)
           && checkArray<SemanticHighlightingInformation>(error, linesKey);
}

PrepareRenameResult::PrepareRenameResult()
    : Utils::variant<PlaceHolderResult, Range, std::nullptr_t>(nullptr)
{}

PrepareRenameResult::PrepareRenameResult(
    const Utils::variant<PlaceHolderResult, Range, std::nullptr_t> &val)
    : Utils::variant<PlaceHolderResult, Range, std::nullptr_t>(val)
{}

PrepareRenameResult::PrepareRenameResult(const PlaceHolderResult &val)
    : Utils::variant<PlaceHolderResult, Range, std::nullptr_t>(val)

{}

PrepareRenameResult::PrepareRenameResult(const Range &val)
    : Utils::variant<PlaceHolderResult, Range, std::nullptr_t>(val)
{}

PrepareRenameResult::PrepareRenameResult(const QJsonValue &val)
{
    if (val.isNull()) {
        emplace<std::nullptr_t>(nullptr);
    } else if (val.isObject()) {
        const QJsonObject object = val.toObject();
        if (object.keys().contains(rangeKey))
            emplace<PlaceHolderResult>(PlaceHolderResult(object));
        else
            emplace<Range>(Range(object));
    }
}

} // namespace LanguageServerProtocol