diff --git a/src/plugins/coco/cocolanguageclient.cpp b/src/plugins/coco/cocolanguageclient.cpp index 11f1372f95e..cf589139e98 100644 --- a/src/plugins/coco/cocolanguageclient.cpp +++ b/src/plugins/coco/cocolanguageclient.cpp @@ -47,6 +47,7 @@ CocoLanguageClient::CocoLanguageClient(const FilePath &coco, const FilePath &csm : Client(clientInterface(coco, csmes)) { setName("Coco"); + hoverHandler()->setPreferDiagnosticts(false); setActivateDocumentAutomatically(false); LanguageFilter allFiles; allFiles.filePattern = QStringList{"*"}; diff --git a/src/plugins/languageclient/languageclienthoverhandler.cpp b/src/plugins/languageclient/languageclienthoverhandler.cpp index 5f601a55c86..a326a689ae8 100644 --- a/src/plugins/languageclient/languageclienthoverhandler.cpp +++ b/src/plugins/languageclient/languageclienthoverhandler.cpp @@ -54,6 +54,11 @@ void HoverHandler::abort() m_response = {}; } +void HoverHandler::setPreferDiagnosticts(bool prefer) +{ + m_preferDiagnostics = prefer; +} + void HoverHandler::setHelpItem(const LanguageServerProtocol::MessageId &msgId, const Core::HelpItem &help) { @@ -68,6 +73,18 @@ void HoverHandler::setHelpItem(const LanguageServerProtocol::MessageId &msgId, } } +bool HoverHandler::reportDiagnostics(const QTextCursor &cursor) +{ + const QList &diagnostics = m_client->diagnosticsAt(m_uri, 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) @@ -81,15 +98,12 @@ void HoverHandler::identifyMatch(TextEditor::TextEditorWidget *editorWidget, } m_uri = DocumentUri::fromFilePath(editorWidget->textDocument()->filePath()); m_response = {}; - QTextCursor tc = editorWidget->textCursor(); - tc.setPosition(pos); - const QList &diagnostics = m_client->diagnosticsAt(m_uri, tc); - if (!diagnostics.isEmpty()) { - const QStringList messages = Utils::transform(diagnostics, &Diagnostic::message); - setToolTip(messages.join('\n')); - report(Priority_Diagnostic); + m_report = report; + + QTextCursor cursor = editorWidget->textCursor(); + cursor.setPosition(pos); + if (m_preferDiagnostics && reportDiagnostics(cursor)) return; - } const Utils::optional> &provider = m_client->capabilities().hoverProvider(); @@ -114,17 +128,15 @@ void HoverHandler::identifyMatch(TextEditor::TextEditorWidget *editorWidget, return; } - m_report = report; - QTextCursor cursor = editorWidget->textCursor(); - cursor.setPosition(pos); - HoverRequest request((TextDocumentPositionParams(TextDocumentIdentifier(m_uri), Position(cursor)))); + HoverRequest request{TextDocumentPositionParams(TextDocumentIdentifier(m_uri), + Position(cursor))}; m_currentRequest = request.id(); request.setResponseCallback( - [this](const HoverRequest::Response &response) { handleResponse(response); }); + [this, cursor](const HoverRequest::Response &response) { handleResponse(response, cursor); }); m_client->sendMessage(request); } -void HoverHandler::handleResponse(const HoverRequest::Response &response) +void HoverHandler::handleResponse(const HoverRequest::Response &response, const QTextCursor &cursor) { m_currentRequest.reset(); if (Utils::optional error = response.error()) { @@ -139,6 +151,8 @@ void HoverHandler::handleResponse(const HoverRequest::Response &response) return; } setContent(hover->content()); + } else if (!m_preferDiagnostics && reportDiagnostics(cursor)) { + return; } } m_report(priority()); diff --git a/src/plugins/languageclient/languageclienthoverhandler.h b/src/plugins/languageclient/languageclienthoverhandler.h index c8bcb759fc6..f204de7fd9c 100644 --- a/src/plugins/languageclient/languageclienthoverhandler.h +++ b/src/plugins/languageclient/languageclienthoverhandler.h @@ -48,6 +48,13 @@ public: void abort() override; + /// If prefer diagnostics is enabled the hover handler checks whether a diagnostics is at the + /// pos passed to identifyMatch _before_ sending hover request to the server. If a diagnostic + /// can be found it will be used as a tooltip and no hover request is sent to the server. + /// If prefer diagnostics is disabled the diagnostics are only checked if the response is empty. + /// Defaults to prefer diagnostics. + void setPreferDiagnosticts(bool prefer); + void setHelpItemProvider(const HelpItemProvider &provider) { m_helpItemProvider = provider; } void setHelpItem(const LanguageServerProtocol::MessageId &msgId, const Core::HelpItem &help); @@ -57,8 +64,10 @@ protected: ReportPriority report) override; private: - void handleResponse(const LanguageServerProtocol::HoverRequest::Response &response); + void handleResponse(const LanguageServerProtocol::HoverRequest::Response &response, + const QTextCursor &cursor); void setContent(const LanguageServerProtocol::HoverContent &content); + bool reportDiagnostics(const QTextCursor &cursor); QPointer m_client; Utils::optional m_currentRequest; @@ -66,6 +75,7 @@ private: LanguageServerProtocol::HoverRequest::Response m_response; TextEditor::BaseHoverHandler::ReportPriority m_report; HelpItemProvider m_helpItemProvider; + bool m_preferDiagnostics = true; }; } // namespace LanguageClient