aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmlls/qqmlbasemodule_p.h
blob: 47596914cbb1ed9f2d810d1ca2f9f6a06107509f (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
// Copyright (C) 2023 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

#ifndef QQMLBASEMODULE_P_H
#define QQMLBASEMODULE_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include "qlanguageserver_p.h"
#include "qqmlcodemodel_p.h"
#include "qqmllsutils_p.h"
#include <QtQmlDom/private/qqmldom_utils_p.h>

#include <QObject>
#include <type_traits>
#include <unordered_map>

template<typename ParametersT, typename ResponseT>
struct BaseRequest
{
    // allow using Parameters and Response type aliases in the
    // implementations of the different requests.
    using Parameters = ParametersT;
    using Response = ResponseT;

    // The version of the code on which the typedefinition request was made.
    // Request is received: mark it with the current version of the textDocument.
    // Then, wait for the codemodel to finish creating a snapshot version that is newer or equal to
    // the textDocument version at request-received-time.
    int m_minVersion;
    Parameters m_parameters;
    Response m_response;

    bool fillFrom(QmlLsp::OpenDocument doc, const Parameters &params, Response &&response);
};

/*!
\internal
\brief This class sends a result or an error when going out of scope.

It has a helper method \c setErrorFrom that sets an error from variant and optionals.
*/

template<typename Result, typename ResponseCallback>
struct ResponseScopeGuard
{
    Q_DISABLE_COPY_MOVE(ResponseScopeGuard)

    std::variant<Result *, QQmlLSUtilsErrorMessage> m_response;
    ResponseCallback &m_callback;

    ResponseScopeGuard(Result &results, ResponseCallback &callback)
        : m_response(&results), m_callback(callback)
    {
    }

    // note: discards the current result or error message, if there is any
    void setError(const QQmlLSUtilsErrorMessage &error) { m_response = error; }

    template<typename... T>
    bool setErrorFrom(const std::variant<T...> &variant)
    {
        static_assert(std::disjunction_v<std::is_same<T, QQmlLSUtilsErrorMessage>...>,
                      "ResponseScopeGuard::setErrorFrom was passed a variant that never contains"
                      " an error message.");
        if (auto x = std::get_if<QQmlLSUtilsErrorMessage>(&variant)) {
            setError(*x);
            return true;
        }
        return false;
    }

    /*!
        \internal
        Note: use it as follows:
        \badcode
        if (scopeGuard.setErrorFrom(xxx)) {
            // do early exit
        }
        // xxx was not an error, continue
        \endcode
    */
    bool setErrorFrom(const std::optional<QQmlLSUtilsErrorMessage> &error)
    {
        if (error) {
            setError(*error);
            return true;
        }
        return false;
    }

    ~ResponseScopeGuard()
    {
        std::visit(qOverloadedVisitor{ [this](Result *result) { m_callback.sendResponse(*result); },
                                       [this](const QQmlLSUtilsErrorMessage &error) {
                                           m_callback.sendErrorResponse(error.code,
                                                                        error.message.toUtf8());
                                       } },
                   m_response);
    }
};

template<typename RequestType>
struct QQmlBaseModule : public QLanguageServerModule
{
    using RequestParameters = typename RequestType::Parameters;
    using RequestResponse = typename RequestType::Response;
    using RequestPointer = std::unique_ptr<RequestType>;
    using RequestPointerArgument = RequestPointer &&;
    using BaseT = QQmlBaseModule<RequestType>;

    QQmlBaseModule(QmlLsp::QQmlCodeModel *codeModel);
    ~QQmlBaseModule();

    void requestHandler(const RequestParameters &parameters, RequestResponse &&response);
    decltype(auto) getRequestHandler();
    // processes a request in a different thread.
    virtual void process(RequestPointerArgument toBeProcessed) = 0;
    std::variant<QList<QQmlLSUtilsItemLocation>, QQmlLSUtilsErrorMessage>
    itemsForRequest(const RequestPointer &request);

public Q_SLOTS:
    void updatedSnapshot(const QByteArray &uri);

protected:
    QMutex m_pending_mutex;
    std::unordered_multimap<QString, RequestPointer> m_pending;
    QmlLsp::QQmlCodeModel *m_codeModel;
};

template<typename Parameters, typename Response>
bool BaseRequest<Parameters, Response>::fillFrom(QmlLsp::OpenDocument doc, const Parameters &params,
                                                 Response &&response)
{
    Q_UNUSED(doc);
    m_parameters = params;
    m_response = std::move(response);

    if (!doc.textDocument) {
        qDebug() << "Cannot find document in qmlls's codemodel, did you open it before accessing "
                    "it?";
        return false;
    }

    {
        QMutexLocker l(doc.textDocument->mutex());
        m_minVersion = doc.textDocument->version().value_or(0);
    }
    return true;
}

template<typename RequestType>
QQmlBaseModule<RequestType>::QQmlBaseModule(QmlLsp::QQmlCodeModel *codeModel)
    : m_codeModel(codeModel)
{
    QObject::connect(m_codeModel, &QmlLsp::QQmlCodeModel::updatedSnapshot, this,
                     &QQmlBaseModule<RequestType>::updatedSnapshot);
}

template<typename RequestType>
QQmlBaseModule<RequestType>::~QQmlBaseModule()
{
    QMutexLocker l(&m_pending_mutex);
    m_pending.clear(); // empty the m_pending while the mutex is hold
}

template<typename RequestType>
decltype(auto) QQmlBaseModule<RequestType>::getRequestHandler()
{
    auto handler = [this](const QByteArray &, const RequestParameters &parameters,
                          RequestResponse &&response) {
        requestHandler(parameters, std::move(response));
    };
    return handler;
}

template<typename RequestType>
void QQmlBaseModule<RequestType>::requestHandler(const RequestParameters &parameters,
                                                 RequestResponse &&response)
{
    auto req = std::make_unique<RequestType>();
    QmlLsp::OpenDocument doc = m_codeModel->openDocumentByUrl(
            QQmlLSUtils::lspUriToQmlUrl(parameters.textDocument.uri));

    if (!req->fillFrom(doc, parameters, std::move(response))) {
        req->m_response.sendErrorResponse(0, "Received invalid request", parameters);
        return;
    }
    const int minVersion = req->m_minVersion;
    {
        QMutexLocker l(&m_pending_mutex);
        m_pending.insert({ QString::fromUtf8(req->m_parameters.textDocument.uri), std::move(req) });
    }

    if (doc.snapshot.docVersion && *doc.snapshot.docVersion >= minVersion)
        updatedSnapshot(QQmlLSUtils::lspUriToQmlUrl(parameters.textDocument.uri));
}

template<typename RequestType>
void QQmlBaseModule<RequestType>::updatedSnapshot(const QByteArray &url)
{
    QmlLsp::OpenDocumentSnapshot doc = m_codeModel->snapshotByUrl(url);
    std::vector<RequestPointer> toCompl;
    {
        QMutexLocker l(&m_pending_mutex);
        for (auto [it, end] = m_pending.equal_range(QString::fromUtf8(url)); it != end;) {
            if (auto &[key, value] = *it;
                doc.docVersion && value->m_minVersion <= *doc.docVersion) {
                toCompl.push_back(std::move(value));
                it = m_pending.erase(it);
            } else {
                ++it;
            }
        }
    }
    for (auto it = toCompl.rbegin(), end = toCompl.rend(); it != end; ++it) {
        process(std::move(*it));
    }
}

template<typename RequestType>
std::variant<QList<QQmlLSUtilsItemLocation>, QQmlLSUtilsErrorMessage>
QQmlBaseModule<RequestType>::itemsForRequest(const RequestPointer &request)
{

    QmlLsp::OpenDocument doc = m_codeModel->openDocumentByUrl(
            QQmlLSUtils::lspUriToQmlUrl(request->m_parameters.textDocument.uri));

    if (!doc.snapshot.validDocVersion || doc.snapshot.validDocVersion != doc.snapshot.docVersion) {
        return QQmlLSUtilsErrorMessage{ 0,
                                        u"Cannot proceed: current QML document is invalid! Fix"
                                        u" all the errors in your QML code and try again."_s };
    }

    QQmlJS::Dom::DomItem file = doc.snapshot.validDoc.fileObject(QQmlJS::Dom::GoTo::MostLikely);
    // clear reference cache to resolve latest versions (use a local env instead?)
    if (auto envPtr = file.environment().ownerAs<QQmlJS::Dom::DomEnvironment>())
        envPtr->clearReferenceCache();
    if (!file) {
        return QQmlLSUtilsErrorMessage{
            0,
            u"Could not find file %1 in project."_s.arg(doc.snapshot.doc.toString()),
        };
    }

    auto itemsFound = QQmlLSUtils::itemsFromTextLocation(file, request->m_parameters.position.line,
                                                         request->m_parameters.position.character);

    if (itemsFound.isEmpty()) {
        return QQmlLSUtilsErrorMessage{
            0,
            u"Could not find any items at given text location."_s,
        };
    }
    return itemsFound;
}

#endif // QQMLBASEMODULE_P_H