aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmlls/qqmllintsuggestions_p.h
blob: 28ab3ccb5db3707b111f4bf607d6629cd51d606b (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
// Copyright (C) 2021 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 QMLLINTSUGGESTIONS_P_H
#define QMLLINTSUGGESTIONS_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 <chrono>
#include <optional>

QT_BEGIN_NAMESPACE
namespace QmlLsp {
struct LastLintUpdate
{
    std::optional<int> version;
    std::optional<std::chrono::steady_clock::time_point> invalidUpdatesSince;
};

class QmlLintSuggestions : public QLanguageServerModule
{
    Q_OBJECT
public:
    QmlLintSuggestions(QLanguageServer *server, QmlLsp::QQmlCodeModel *codeModel);

    QString name() const override { return QLatin1StringView("QmlLint Suggestions"); }
public Q_SLOTS:
    void diagnose(const QByteArray &uri);
    void registerHandlers(QLanguageServer *server, QLanguageServerProtocol *protocol) override;
    void setupCapabilities(const QLspSpecification::InitializeParams &clientInfo,
                           QLspSpecification::InitializeResult &) override;

private:
    struct VersionedDocument
    {
        std::optional<int> version;
        QQmlJS::Dom::DomItem item;
    };
    struct TryAgainLater
    {
        std::chrono::milliseconds time;
    };
    struct NoDocumentAvailable
    {
    };

    using VersionToDiagnose = std::variant<VersionedDocument, TryAgainLater, NoDocumentAvailable>;

    VersionToDiagnose chooseVersionToDiagnose(const QByteArray &url);
    VersionToDiagnose chooseVersionToDiagnoseHelper(const QByteArray &url);
    void diagnoseHelper(const QByteArray &uri, const VersionedDocument &document);

    QMutex m_mutex;
    QHash<QByteArray, LastLintUpdate> m_lastUpdate;
    QLanguageServer *m_server;
    QmlLsp::QQmlCodeModel *m_codeModel;
};
} // namespace QmlLsp
QT_END_NAMESPACE
#endif // QMLLINTSUGGESTIONS_P_H