ClangCodeModel: Provide tooltips via clangd

Note that we temporarily lose the ability to hover over an include and
get the full path of the header file. This is a valuable feature that we
need to restore, preferably by fixing clangd itself.
Fixing the remaining few test failures would likely require more
complicated code as well as additional LSP round-trips, and as of now
I'm not convinced it is worth the effort.

Change-Id: I08c72c4bd1268bbd67baeb57bbfd29d9b11303a5
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2021-06-04 12:40:26 +02:00
parent eef0769d7a
commit 673d596c84
14 changed files with 651 additions and 9 deletions

View File

@@ -51,6 +51,18 @@ void HoverHandler::abort()
if (m_client && m_client->reachable() && m_currentRequest.has_value())
m_client->cancelRequest(*m_currentRequest);
m_currentRequest.reset();
m_response = {};
}
void HoverHandler::setHelpItem(const LanguageServerProtocol::MessageId &msgId,
const Core::HelpItem &help)
{
if (msgId == m_response.id()) {
setContent(m_response.result().value().content());
m_response = {};
setLastHelpItemIdentified(help);
m_report(priority());
}
}
void HoverHandler::identifyMatch(TextEditor::TextEditorWidget *editorWidget,
@@ -64,10 +76,11 @@ void HoverHandler::identifyMatch(TextEditor::TextEditorWidget *editorWidget,
report(Priority_None);
return;
}
auto uri = DocumentUri::fromFilePath(editorWidget->textDocument()->filePath());
m_uri = DocumentUri::fromFilePath(editorWidget->textDocument()->filePath());
m_response = {};
QTextCursor tc = editorWidget->textCursor();
tc.setPosition(pos);
const QList<Diagnostic> &diagnostics = m_client->diagnosticsAt(uri, tc);
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'));
@@ -101,7 +114,7 @@ void HoverHandler::identifyMatch(TextEditor::TextEditorWidget *editorWidget,
m_report = report;
QTextCursor cursor = editorWidget->textCursor();
cursor.setPosition(pos);
HoverRequest request((TextDocumentPositionParams(TextDocumentIdentifier(uri), Position(cursor))));
HoverRequest request((TextDocumentPositionParams(TextDocumentIdentifier(m_uri), Position(cursor))));
m_currentRequest = request.id();
request.setResponseCallback(
[this](const HoverRequest::Response &response) { handleResponse(response); });
@@ -115,8 +128,14 @@ void HoverHandler::handleResponse(const HoverRequest::Response &response)
if (m_client)
m_client->log(error.value());
}
if (Utils::optional<Hover> result = response.result())
if (Utils::optional<Hover> result = response.result()) {
if (m_helpItemProvider) {
m_response = response;
m_helpItemProvider(response, m_uri);
return;
}
setContent(result.value().content());
}
m_report(priority());
}