aboutsummaryrefslogtreecommitdiffstats
path: root/tools/qmlls/qmllintsuggestions.cpp
blob: 6f17f7a9ce08dd5117d7168d9c1643471f77fc48 (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
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "qmllintsuggestions.h"
#include <QtLanguageServer/private/qlanguageserverspec_p.h>
#include <QtQmlCompiler/private/qqmljslinter_p.h>
#include <QtQmlCompiler/private/qqmljslogger_p.h>
#include <QtCore/qlibraryinfo.h>
#include <QtCore/qtimer.h>
#include <QtCore/qdebug.h>

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

Q_LOGGING_CATEGORY(lintLog, "qt.languageserver.lint")

QT_BEGIN_NAMESPACE
namespace QmlLsp {

static DiagnosticSeverity severityFromMsgType(QtMsgType t)
{
    switch (t) {
    case QtDebugMsg:
        return DiagnosticSeverity::Hint;
    case QtInfoMsg:
        return DiagnosticSeverity::Information;
    case QtWarningMsg:
        return DiagnosticSeverity::Warning;
    case QtCriticalMsg:
    case QtFatalMsg:
        break;
    }
    return DiagnosticSeverity::Error;
}

static void codeActionHandler(
        const QByteArray &, const CodeActionParams &params,
        LSPPartialResponse<std::variant<QList<std::variant<Command, CodeAction>>, std::nullptr_t>,
                           QList<std::variant<Command, CodeAction>>> &&response)
{
    QList<std::variant<Command, CodeAction>> responseData;

    for (const Diagnostic &diagnostic : params.context.diagnostics) {
        if (!diagnostic.data.has_value())
            continue;

        const auto &data = diagnostic.data.value();

        int version = data[u"version"].toInt();
        QJsonArray suggestions = data[u"suggestions"].toArray();

        QList<TextDocumentEdit> edits;
        QString message;
        for (const QJsonValue &suggestion : suggestions) {
            QString replacement = suggestion[u"replacement"].toString();
            message += suggestion[u"message"].toString() + u"\n";

            TextEdit textEdit;
            textEdit.range = { Position { suggestion[u"lspBeginLine"].toInt(),
                                          suggestion[u"lspBeginCharacter"].toInt() },
                               Position { suggestion[u"lspEndLine"].toInt(),
                                          suggestion[u"lspEndCharacter"].toInt() } };
            textEdit.newText = replacement.toUtf8();

            TextDocumentEdit textDocEdit;
            textDocEdit.textDocument = { params.textDocument, version };
            textDocEdit.edits.append(textEdit);

            edits.append(textDocEdit);
        }
        message.chop(1);
        WorkspaceEdit edit;
        edit.documentChanges = edits;

        CodeAction action;
        action.kind = u"refactor.rewrite"_s.toUtf8();
        action.edit = edit;
        action.title = message.toUtf8();

        responseData.append(action);
    }

    response.sendResponse(responseData);
}

void QmlLintSuggestions::registerHandlers(QLanguageServer *, QLanguageServerProtocol *protocol)
{
    protocol->registerCodeActionRequestHandler(&codeActionHandler);
}

void QmlLintSuggestions::setupCapabilities(const QLspSpecification::InitializeParams &,
                                           QLspSpecification::InitializeResult &serverInfo)
{
    serverInfo.capabilities.codeActionProvider = true;
}

QmlLintSuggestions::QmlLintSuggestions(QLanguageServer *server, QmlLsp::QQmlCodeModel *codeModel)
    : m_server(server), m_codeModel(codeModel)
{
    QObject::connect(m_codeModel, &QmlLsp::QQmlCodeModel::updatedSnapshot, this,
                     &QmlLintSuggestions::diagnose, Qt::DirectConnection);
}

void QmlLintSuggestions::diagnose(const QByteArray &url)
{
    const int maxInvalidMsec = 4000;
    qCDebug(lintLog) << "diagnose start";
    QmlLsp::OpenDocumentSnapshot snapshot = m_codeModel->snapshotByUrl(url);
    QList<Diagnostic> diagnostics;
    std::optional<int> version;
    DomItem doc;
    {
        QMutexLocker l(&m_mutex);
        LastLintUpdate &lastUpdate = m_lastUpdate[url];
        if (lastUpdate.version && *lastUpdate.version == version) {
            qCDebug(lspServerLog) << "skipped update of " << url << "unchanged valid doc";
            return;
        }
        if (snapshot.validDocVersion
            && (!lastUpdate.version || *snapshot.validDocVersion > *lastUpdate.version)) {
            doc = snapshot.validDoc;
            version = snapshot.validDocVersion;
        } else if (snapshot.docVersion
                   && (!lastUpdate.version || *snapshot.docVersion > *lastUpdate.version)) {
            if (!lastUpdate.version || !snapshot.validDocVersion
                || (lastUpdate.invalidUpdatesSince
                    && lastUpdate.invalidUpdatesSince->msecsTo(QDateTime::currentDateTime())
                            > maxInvalidMsec)) {
                doc = snapshot.doc;
                version = snapshot.docVersion;
            } else if (!lastUpdate.invalidUpdatesSince) {
                lastUpdate.invalidUpdatesSince = QDateTime::currentDateTime();
                QTimer::singleShot(maxInvalidMsec, Qt::VeryCoarseTimer, this,
                                   [this, url]() { diagnose(url); });
            }
        }
        if (doc) {
            // update immediately, and do not keep track of sent version, thus in extreme cases sent
            // updates could be out of sync
            lastUpdate.version = version;
            lastUpdate.invalidUpdatesSince.reset();
        }
    }
    QString fileContents;
    if (doc) {
        qCDebug(lintLog) << "has doc, do real lint";
        QStringList imports = m_codeModel->buildPathsForFileUrl(url);
        imports.append(QLibraryInfo::path(QLibraryInfo::QmlImportsPath));
        // add m_server->clientInfo().rootUri & co?
        bool silent = true;
        QString filename = doc.canonicalFilePath();
        fileContents = doc.field(Fields::code).value().toString();
        QStringList qmltypesFiles;
        QStringList resourceFiles;
        QMap<QString, QQmlJSLogger::Option> options;

        QQmlJSLinter linter(imports);

        linter.lintFile(filename, &fileContents, silent, nullptr, imports, qmltypesFiles,
                        resourceFiles, options);
        auto addLength = [&fileContents](Position &position, int startOffset, int length) {
            int i = startOffset;
            int iEnd = i + length;
            if (iEnd > int(fileContents.size()))
                iEnd = fileContents.size();
            while (i < iEnd) {
                if (fileContents.at(i) == u'\n') {
                    ++position.line;
                    position.character = 0;
                    if (i + 1 < iEnd && fileContents.at(i) == u'\r')
                        ++i;
                } else {
                    ++position.character;
                }
                ++i;
            }
        };

        auto messageToDiagnostic = [&addLength, &version](const Message &message) {
            Diagnostic diagnostic;
            diagnostic.severity = severityFromMsgType(message.type);
            Range &range = diagnostic.range;
            Position &position = range.start;

            QQmlJS::SourceLocation srcLoc = message.loc;

            position.line = srcLoc.isValid() ? srcLoc.startLine - 1 : 0;
            position.character = srcLoc.isValid() ? srcLoc.startColumn - 1 : 0;
            range.end = position;
            addLength(range.end, srcLoc.isValid() ? message.loc.offset : 0, srcLoc.isValid() ? message.loc.length : 0);
            diagnostic.message = message.message.toUtf8();
            diagnostic.source = QByteArray("qmllint");

            auto suggestion = message.fixSuggestion;
            if (suggestion.has_value()) {
                // We need to interject the information about where the fix suggestions end
                // here since we don't have access to the textDocument to calculate it later.
                QJsonArray fixedSuggestions;
                for (const FixSuggestion::Fix &fix : suggestion->fixes) {
                    QQmlJS::SourceLocation cut = fix.cutLocation;

                    int line = cut.isValid() ? cut.startLine - 1 : 0;
                    int column = cut.isValid() ? cut.startColumn - 1 : 0;

                    QJsonObject object;
                    object[u"lspBeginLine"] = line;
                    object[u"lspBeginCharacter"] = column;

                    Position end = { line, column };

                    addLength(end, srcLoc.isValid() ? cut.offset : 0,
                              srcLoc.isValid() ? cut.length : 0);
                    object[u"lspEndLine"] = end.line;
                    object[u"lspEndCharacter"] = end.character;

                    object[u"message"] = fix.message;
                    object[u"replacement"] = fix.replacementString;

                    fixedSuggestions << object;
                }
                QJsonObject data;
                data[u"suggestions"] = fixedSuggestions;

                Q_ASSERT(version.has_value());
                data[u"version"] = version.value();

                diagnostic.data = data;
            }
            return diagnostic;
        };
        doc.iterateErrors(
                [&diagnostics, &addLength](DomItem, ErrorMessage msg) {
                    Diagnostic diagnostic;
                    diagnostic.severity = severityFromMsgType(QtMsgType(int(msg.level)));
                    // do something with msg.errorGroups ?
                    auto &location = msg.location;
                    Range &range = diagnostic.range;
                    range.start.line = location.startLine - 1;
                    range.start.character = location.startColumn - 1;
                    range.end = range.start;
                    addLength(range.end, location.offset, location.length);
                    diagnostic.code = QByteArray(msg.errorId.data(), msg.errorId.size());
                    diagnostic.source = "domParsing";
                    diagnostic.message = msg.message.toUtf8();
                    diagnostics.append(diagnostic);
                    return true;
                },
                true);

        if (const QQmlJSLogger *logger = linter.logger()) {
            for (const auto &messages : { logger->infos(), logger->warnings(), logger->errors() }) {
                for (const Message &message : messages) {
                    diagnostics.append(messageToDiagnostic(message));
                }
            }
        }
    }
    PublishDiagnosticsParams diagnosticParams;
    diagnosticParams.uri = url;
    diagnosticParams.diagnostics = diagnostics;
    diagnosticParams.version = version;

    m_server->protocol()->notifyPublishDiagnostics(diagnosticParams);
    qCDebug(lintLog) << "lint" << QString::fromUtf8(url) << "found"
                     << diagnosticParams.diagnostics.size() << "issues"
                     << QTypedJson::toJsonValue(diagnosticParams);
}

} // namespace QmlLsp
QT_END_NAMESPACE