aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/languageclient/languageclienthoverhandler.cpp
blob: 429c89e511ac8b229f30ae3b088b219b1da78b67 (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
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "languageclienthoverhandler.h"

#include "client.h"
#include "dynamiccapabilities.h"

#include <texteditor/textdocument.h>
#include <texteditor/texteditor.h>
#include <utils/mimeutils.h>
#include <utils/qtcassert.h>
#include <utils/tooltip/tooltip.h>

using namespace LanguageServerProtocol;

namespace LanguageClient {

HoverHandler::HoverHandler(Client *client)
    : m_client(client)
{}

HoverHandler::~HoverHandler()
{
    abort();
}

void HoverHandler::abort()
{
    if (m_client && m_currentRequest.has_value()) {
        m_client->cancelRequest(*m_currentRequest);
        m_currentRequest.reset();
    }
    m_response = {};
}

void HoverHandler::setPreferDiagnosticts(bool prefer)
{
    m_preferDiagnostics = prefer;
}

void HoverHandler::setHelpItem(const LanguageServerProtocol::MessageId &msgId,
                               const Core::HelpItem &help)
{
    if (msgId == m_response.id()) {
        if (std::optional<HoverResult> result = m_response.result()) {
            if (auto hover = std::get_if<Hover>(&(*result)))
                setContent(hover->content());
        }
        m_response = {};
        setLastHelpItemIdentified(help);
        m_report(priority());
    }
}

bool HoverHandler::reportDiagnostics(const QTextCursor &cursor)
{
    const QList<Diagnostic> &diagnostics = m_client->diagnosticsAt(m_filePath, cursor);
    if (diagnostics.isEmpty())
        return false;

    const QStringList messages = Utils::transform(diagnostics, &Diagnostic::message);
    setToolTip(messages.join('\n'));
    m_report(Priority_Diagnostic);
    return true;
}

void HoverHandler::identifyMatch(TextEditor::TextEditorWidget *editorWidget,
                                 int pos,
                                 TextEditor::BaseHoverHandler::ReportPriority report)
{
    if (m_currentRequest.has_value())
        abort();
    if (m_client.isNull() || !m_client->documentOpen(editorWidget->textDocument())
        || !m_client->reachable()) {
        report(Priority_None);
        return;
    }
    m_filePath = editorWidget->textDocument()->filePath();
    m_response = {};
    m_report = report;

    QTextCursor cursor = editorWidget->textCursor();
    cursor.setPosition(pos);
    if (m_preferDiagnostics && reportDiagnostics(cursor))
        return;

    const std::optional<std::variant<bool, WorkDoneProgressOptions>> &provider
        = m_client->capabilities().hoverProvider();
    const bool *boolvalue = provider.has_value() ? std::get_if<bool>(&*provider) : nullptr;
    bool sendMessage = provider.has_value() && (!boolvalue || *boolvalue);
    if (std::optional<bool> registered = m_client->dynamicCapabilities().isRegistered(
            HoverRequest::methodName)) {
        sendMessage = *registered;
        if (sendMessage) {
            const TextDocumentRegistrationOptions option(
                m_client->dynamicCapabilities().option(HoverRequest::methodName).toObject());
            if (option.isValid()) {
                sendMessage = option.filterApplies(editorWidget->textDocument()->filePath(),
                                                   Utils::mimeTypeForName(
                                                       editorWidget->textDocument()->mimeType()));
            }
        }
    }
    if (!sendMessage) {
        report(Priority_None);
        return;
    }

    HoverRequest request{
        TextDocumentPositionParams(TextDocumentIdentifier(m_client->hostPathToServerUri(m_filePath)),
                                   Position(cursor))};
    m_currentRequest = request.id();
    request.setResponseCallback(
        [this, cursor](const HoverRequest::Response &response) { handleResponse(response, cursor); });
    m_client->sendMessage(request);
}

void HoverHandler::handleResponse(const HoverRequest::Response &response, const QTextCursor &cursor)
{
    m_currentRequest.reset();
    if (std::optional<HoverRequest::Response::Error> error = response.error()) {
        if (m_client)
            m_client->log(*error);
    }
    if (std::optional<HoverResult> result = response.result()) {
        if (auto hover = std::get_if<Hover>(&(*result))) {
            if (m_helpItemProvider) {
                m_response = response;
                m_helpItemProvider(response, m_filePath);
                return;
            }
            setContent(hover->content());
        } else if (!m_preferDiagnostics && reportDiagnostics(cursor)) {
            return;
        }
    }
    m_report(priority());
}

static QString toolTipForMarkedStrings(const QList<MarkedString> &markedStrings)
{
    QString tooltip;
    for (const MarkedString &markedString : markedStrings) {
        if (!tooltip.isEmpty())
            tooltip += '\n';
        if (auto string = std::get_if<QString>(&markedString))
            tooltip += *string;
        else if (auto string = std::get_if<MarkedLanguageString>(&markedString))
            tooltip += string->value() + " [" + string->language() + ']';
    }
    return tooltip;
}

void HoverHandler::setContent(const HoverContent &hoverContent)
{
    if (auto markupContent = std::get_if<MarkupContent>(&hoverContent))
        setToolTip(markupContent->content(), markupContent->textFormat());
    else if (auto markedString = std::get_if<MarkedString>(&hoverContent))
        setToolTip(toolTipForMarkedStrings({*markedString}));
    else if (auto markedStrings = std::get_if<QList<MarkedString>>(&hoverContent))
        setToolTip(toolTipForMarkedStrings(*markedStrings));
}

} // namespace LanguageClient