aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmlls/qqmlhighlightsupport.cpp
blob: 55002804a0ade088790442775312e84643e9300c (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
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include <qqmlhighlightsupport_p.h>
#include <qqmlsemantictokens_p.h>

QT_BEGIN_NAMESPACE

using namespace Qt::StringLiterals;
using namespace QLspSpecification;
using namespace QQmlJS::Dom;

/*!
\internal
Make a list of enum names to register the supported token
types and modifiers. It is case-sensitive in the protocol
thus we need to lower the first characters of the enum names.
*/
template <typename EnumType>
static QList<QByteArray> enumToByteArray()
{
    QList<QByteArray> result;
    QMetaEnum metaEnum = QMetaEnum::fromType<EnumType>();
    for (auto i = 0; i < metaEnum.keyCount(); ++i) {
        auto &&enumName = QByteArray(metaEnum.key(i));
        enumName.front() = std::tolower(enumName.front());
        result.emplace_back(std::move(enumName));
    }

    return result;
}

static QList<QByteArray> tokenTypesList()
{
    return enumToByteArray<SemanticTokenTypes>();
}

static QList<QByteArray> tokenModifiersList()
{
    return enumToByteArray<SemanticTokenModifiers>();
}

/*!
\internal
A wrapper class that handles the semantic tokens request for a whole file as described in
https://microsoft.github.io/language-server-protocol/specifications/specification-3-16/#semanticTokens_fullRequest
Sends a QLspSpecification::SemanticTokens data as response that is generated for the entire file.
*/
SemanticTokenFullHandler::SemanticTokenFullHandler(QmlLsp::QQmlCodeModel *codeModel)
    : QQmlBaseModule(codeModel)
{
}

void SemanticTokenFullHandler::process(
        QQmlBaseModule<SemanticTokensRequest>::RequestPointerArgument request)
{
    Responses::SemanticTokensResultType result;
    ResponseScopeGuard guard(result, request->m_response);

    if (!request) {
        qCWarning(semanticTokens) << "No semantic token request is available!";
        return;
    }

    const auto doc = m_codeModel->openDocumentByUrl(
            QQmlLSUtils::lspUriToQmlUrl(request->m_parameters.textDocument.uri));
    DomItem file = doc.snapshot.doc.fileObject(GoTo::MostLikely);
    Highlights highlights;
    auto &&encoded = highlights.collectTokens(file, std::nullopt);
    auto &registeredTokens = m_codeModel->registeredTokens();
    if (!encoded.isEmpty()) {
        HighlightingUtils::updateResultID(registeredTokens.resultId);
        result = SemanticTokens{ registeredTokens.resultId, encoded };
        registeredTokens.lastTokens = std::move(encoded);
    } else {
        result = nullptr;
    }
}

void SemanticTokenFullHandler::registerHandlers(QLanguageServer *, QLanguageServerProtocol *protocol)
{
    protocol->registerSemanticTokensRequestHandler(getRequestHandler());
}

/*!
\internal
A wrapper class that handles the semantic tokens delta request for a file
https://microsoft.github.io/language-server-protocol/specifications/specification-3-16/#semanticTokens_deltaRequest
Sends either SemanticTokens or SemanticTokensDelta data as response.
This is generally requested when the text document is edited after receiving full highlighting data.
*/
SemanticTokenDeltaHandler::SemanticTokenDeltaHandler(QmlLsp::QQmlCodeModel *codeModel)
    : QQmlBaseModule(codeModel)
{
}

void SemanticTokenDeltaHandler::process(
        QQmlBaseModule<SemanticTokensDeltaRequest>::RequestPointerArgument request)
{
    Responses::SemanticTokensDeltaResultType result;
    ResponseScopeGuard guard(result, request->m_response);

    if (!request) {
        qCWarning(semanticTokens) << "No semantic token request is available!";
        return;
    }
    const auto doc = m_codeModel->openDocumentByUrl(
            QQmlLSUtils::lspUriToQmlUrl(request->m_parameters.textDocument.uri));
    DomItem file = doc.snapshot.validDoc.fileObject(GoTo::MostLikely);
    Highlights highlights;
    auto newEncoded = highlights.collectTokens(file, std::nullopt);
    auto &registeredTokens = m_codeModel->registeredTokens();
    const auto lastResultId = registeredTokens.resultId;
    HighlightingUtils::updateResultID(registeredTokens.resultId);

    // Return full token list if result ids not align
    // otherwise compute the delta.
    if (lastResultId == request->m_parameters.previousResultId) {
        auto &&oldEncoded = registeredTokens.lastTokens;
        QList<SemanticTokensEdit> edits = HighlightingUtils::computeDiff(oldEncoded, newEncoded);
        result = QLspSpecification::SemanticTokensDelta{ registeredTokens.resultId, edits };
    } else if (!newEncoded.isEmpty()){
        result = QLspSpecification::SemanticTokens{ registeredTokens.resultId, newEncoded };
    } else {
        result = nullptr;
    }
    registeredTokens.lastTokens = newEncoded;
}

void SemanticTokenDeltaHandler::registerHandlers(QLanguageServer *, QLanguageServerProtocol *protocol)
{
    protocol->registerSemanticTokensDeltaRequestHandler(getRequestHandler());
}

/*!
\internal
A wrapper class that handles the semantic tokens range request for a file
https://microsoft.github.io/language-server-protocol/specifications/specification-3-16/#semanticTokens_rangeRequest
Sends a QLspSpecification::SemanticTokens data as response that is generated for a range of file.
*/
SemanticTokenRangeHandler::SemanticTokenRangeHandler(QmlLsp::QQmlCodeModel *codeModel)
    : QQmlBaseModule(codeModel)
{
}

void SemanticTokenRangeHandler::process(
        QQmlBaseModule<SemanticTokensRangeRequest>::RequestPointerArgument request)
{
    Responses::SemanticTokensRangeResultType result;
    ResponseScopeGuard guard(result, request->m_response);

    if (!request) {
        qCWarning(semanticTokens) << "No semantic token request is available!";
        return;
    }
    const auto doc = m_codeModel->openDocumentByUrl(
            QQmlLSUtils::lspUriToQmlUrl(request->m_parameters.textDocument.uri));
    DomItem file = doc.snapshot.doc.fileObject(GoTo::MostLikely);
    const auto qmlFile = file.as<QmlFile>();
    if (!qmlFile)
        return;
    const auto code = qmlFile->code();
    const auto range = request->m_parameters.range;
    int startOffset = int(QQmlLSUtils::textOffsetFrom(code, range.start.line, range.end.character));
    int endOffset = int(QQmlLSUtils::textOffsetFrom(code, range.end.line, range.end.character));
    Highlights highlights;
    auto &&encoded = highlights.collectTokens(file, HighlightsRange{startOffset, endOffset});
    auto &registeredTokens = m_codeModel->registeredTokens();
    if (!encoded.isEmpty()) {
        HighlightingUtils::updateResultID(registeredTokens.resultId);
        result = SemanticTokens{ registeredTokens.resultId, encoded };
    } else {
        result = nullptr;
    }
}

void SemanticTokenRangeHandler::registerHandlers(QLanguageServer *, QLanguageServerProtocol *protocol)
{
    protocol->registerSemanticTokensRangeRequestHandler(getRequestHandler());
}

QQmlHighlightSupport::QQmlHighlightSupport(QmlLsp::QQmlCodeModel *codeModel)
    : m_full(codeModel), m_delta(codeModel), m_range(codeModel)
{
}

QString QQmlHighlightSupport::name() const
{
    return "QQmlHighlightSupport"_L1;
}

void QQmlHighlightSupport::registerHandlers(QLanguageServer *server, QLanguageServerProtocol *protocol)
{
    m_full.registerHandlers(server, protocol);
    m_delta.registerHandlers(server, protocol);
    m_range.registerHandlers(server, protocol);
}

void QQmlHighlightSupport::setupCapabilities(
        const QLspSpecification::InitializeParams &,
        QLspSpecification::InitializeResult &serverCapabilities)
{
    QLspSpecification::SemanticTokensOptions options;
    options.range = true;
    options.full = QJsonObject({ { u"delta"_s, true } });
    options.legend.tokenTypes = tokenTypesList();
    options.legend.tokenModifiers = tokenModifiersList();

    serverCapabilities.capabilities.semanticTokensProvider = options;
}

QT_END_NAMESPACE