aboutsummaryrefslogtreecommitdiffstats
path: root/src/shared/lsp/progresssupport.h
blob: 8ccc34c37954a7a54511d7ddd57f037f1aad8dfd (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
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#pragma once

#include "jsonrpcmessages.h"

#include <QJsonValue>

#include <variant>

namespace lsp {

class LANGUAGESERVERPROTOCOL_EXPORT ProgressToken : public std::variant<int, QString>
{
public:
    using variant::variant;
    explicit ProgressToken(const QJsonValue &value);

    bool isValid() { return true; }
    operator QJsonValue() const;
};

class LANGUAGESERVERPROTOCOL_EXPORT WorkDoneProgressReport : public JsonObject
{
public:
    using JsonObject::JsonObject;

    /**
     * Controls if a cancel button should be shown to allow the user to cancel the
     * long running operation.
     * Clients that don't support cancellation can ignore the setting.
     */
    std::optional<bool> cancellable() const { return optionalValue<bool>(cancellableKey); }
    void setCancellable(bool cancellable) { insert(cancellableKey, cancellable); }
    void clearCancellable() { remove(cancellableKey); }

    /**
     * Optional, more detailed associated progress message. Contains
     * complementary information to the `title`.
     *
     * Examples: "3/25 files", "project/src/module2", "node_modules/some_dep".
     * If unset, the previous progress message (if any) is still valid.
     */
    std::optional<QString> message() const { return optionalValue<QString>(messageKey); }
    void setMessage(const QString &message) { insert(messageKey, message); }
    void clearMessage() { remove(messageKey); }

    /**
     * Optional progress percentage to display (value 100 is considered 100%).
     * If not provided infinite progress is assumed and clients are allowed
     * to ignore the `percentage` value in subsequent in report notifications.
     *
     * The value should be steadily rising. Clients are free to ignore values
     * that are not following this rule.
     */

    // Allthough percentage is defined as an uint by the protocol some server
    // return a double here. Be less strict and also use a double.
    // CAUTION: the range is still 0 - 100 and not 0 - 1
    std::optional<double> percentage() const { return optionalValue<double>(percentageKey); }
    void setPercentage(double percentage) { insert(percentageKey, percentage); }
    void clearPercentage() { remove(percentageKey); }
};

class LANGUAGESERVERPROTOCOL_EXPORT WorkDoneProgressBegin : public WorkDoneProgressReport
{
public:
    using WorkDoneProgressReport::WorkDoneProgressReport;

    /**
     * Mandatory title of the progress operation. Used to briefly inform about
     * the kind of operation being performed.
     *
     * Examples: "Indexing" or "Linking dependencies".
     */

    QString title() const { return typedValue<QString>(titleKey); }
    void setTitle(const QString &title) { insert(titleKey, title); }
};

class LANGUAGESERVERPROTOCOL_EXPORT WorkDoneProgressEnd : public JsonObject
{
public:
    using JsonObject::JsonObject;

    /**
     * Optional, a final message indicating to for example indicate the outcome
     * of the operation.
     */
    std::optional<QString> message() const { return optionalValue<QString>(messageKey); }
    void setMessage(const QString &message) { insert(messageKey, message); }
    void clearMessage() { remove(messageKey); }
};

class LANGUAGESERVERPROTOCOL_EXPORT ProgressParams : public JsonObject
{
public:
    using JsonObject::JsonObject;

    ProgressToken token() const { return ProgressToken(JsonObject::value(tokenKey)); }
    void setToken(const ProgressToken &token) { insert(tokenKey, token); }

    using ProgressType
        = std::variant<WorkDoneProgressBegin, WorkDoneProgressReport, WorkDoneProgressEnd>;
    ProgressType value() const;
    void setValue(const ProgressType &value);

    bool isValid() const override { return contains(tokenKey) && contains(valueKey); }
};

class LANGUAGESERVERPROTOCOL_EXPORT ProgressNotification : public Notification<ProgressParams>
{
public:
    ProgressNotification(const ProgressParams &params);
    using Notification::Notification;
    constexpr static const char methodName[] = "$/progress";
};

class LANGUAGESERVERPROTOCOL_EXPORT ProgressTokenParams : public JsonObject
{
public:
    using JsonObject::JsonObject;

    // The token to be used to report progress.
    ProgressToken token() const { return typedValue<ProgressToken>(tokenKey); }
    void setToken(const ProgressToken &token) { insert(tokenKey, token); }
};

using WorkDoneProgressCreateParams = ProgressTokenParams;

class LANGUAGESERVERPROTOCOL_EXPORT WorkDoneProgressCreateRequest
    : public Request<std::nullptr_t, std::nullptr_t, WorkDoneProgressCreateParams>
{
public:
    WorkDoneProgressCreateRequest(const WorkDoneProgressCreateParams &params);
    using Request::Request;
    constexpr static const char methodName[] = "window/workDoneProgress/create";
};

using WorkDoneProgressCancelParams = ProgressTokenParams;

class LANGUAGESERVERPROTOCOL_EXPORT WorkDoneProgressCancelRequest
    : public Request<std::nullptr_t, std::nullptr_t, WorkDoneProgressCancelParams>
{
public:
    using Request::Request;
    constexpr static const char methodName[] = "window/workDoneProgress/cancel";
};


} // namespace lsp