forked from qt-creator/qt-creator
LanguageClient: Add option to always send hover requests
Even if we already have a diangostic visible at this location. This is used in the coco server to display additional information inside the coverage browser. Change-Id: I0d75f2e9f469ebdf6df7d6948a8e3e6b0cc58969 Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
@@ -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{"*"};
|
||||
|
@@ -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<Diagnostic> &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<Diagnostic> &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<Utils::variant<bool, WorkDoneProgressOptions>> &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<HoverRequest::Response::Error> 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());
|
||||
|
@@ -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<Client> m_client;
|
||||
Utils::optional<LanguageServerProtocol::MessageId> 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
|
||||
|
Reference in New Issue
Block a user