LanguageClient: check correct provider for various goto targets

Change-Id: Ie0acf800fad46cc11f7bb5b5134fc5b70beeaddd
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
David Schulz
2023-10-26 13:02:56 +02:00
parent 6b831a3737
commit cb8d79bb2f

View File

@@ -78,8 +78,10 @@ SymbolSupport::SymbolSupport(Client *client)
: m_client(client) : m_client(client)
{} {}
template<typename Request> template<typename Request, typename R>
static MessageId sendTextDocumentPositionParamsRequest(Client *client, const Request &request) static MessageId sendTextDocumentPositionParamsRequest(Client *client,
const Request &request,
R ServerCapabilities::*member)
{ {
if (!request.isValid(nullptr)) if (!request.isValid(nullptr))
return {}; return {};
@@ -97,8 +99,7 @@ static MessageId sendTextDocumentPositionParamsRequest(Client *client, const Req
else else
sendMessage = supportedFile; sendMessage = supportedFile;
} else { } else {
const std::optional<std::variant<bool, WorkDoneProgressOptions>> &provider const auto provider = std::mem_fn(member)(serverCapability);
= serverCapability.referencesProvider();
sendMessage = provider.has_value(); sendMessage = provider.has_value();
if (sendMessage && std::holds_alternative<bool>(*provider)) if (sendMessage && std::holds_alternative<bool>(*provider))
sendMessage = std::get<bool>(*provider); sendMessage = std::get<bool>(*provider);
@@ -110,7 +111,8 @@ static MessageId sendTextDocumentPositionParamsRequest(Client *client, const Req
return {}; return {};
} }
static void handleGotoResponse(const GotoDefinitionRequest::Response &response, template<typename Request>
static void handleGotoResponse(const typename Request::Response &response,
Utils::LinkHandler callback, Utils::LinkHandler callback,
std::optional<Utils::Link> linkUnderCursor, std::optional<Utils::Link> linkUnderCursor,
const Client *client) const Client *client)
@@ -141,19 +143,20 @@ static TextDocumentPositionParams generateDocPosParams(TextEditor::TextDocument
return TextDocumentPositionParams(documentId, pos); return TextDocumentPositionParams(documentId, pos);
} }
template<typename Request> template<typename Request, typename R>
static MessageId sendGotoRequest(TextEditor::TextDocument *document, static MessageId sendGotoRequest(TextEditor::TextDocument *document,
const QTextCursor &cursor, const QTextCursor &cursor,
Utils::LinkHandler callback, Utils::LinkHandler callback,
Client *client, Client *client,
std::optional<Utils::Link> linkUnderCursor) std::optional<Utils::Link> linkUnderCursor,
R ServerCapabilities::*member)
{ {
Request request(generateDocPosParams(document, cursor, client)); Request request(generateDocPosParams(document, cursor, client));
request.setResponseCallback([callback, linkUnderCursor, client]( request.setResponseCallback([callback, linkUnderCursor, client](
const GotoDefinitionRequest::Response &response) { const typename Request::Response &response) {
handleGotoResponse(response, callback, linkUnderCursor, client); handleGotoResponse<Request>(response, callback, linkUnderCursor, client);
}); });
return sendTextDocumentPositionParamsRequest(client, request); return sendTextDocumentPositionParamsRequest(client, request, member);
return request.id(); return request.id();
} }
@@ -223,19 +226,22 @@ MessageId SymbolSupport::findLinkAt(TextEditor::TextDocument *document,
cursor, cursor,
callback, callback,
m_client, m_client,
linkUnderCursor); linkUnderCursor,
&ServerCapabilities::definitionProvider);
case LinkTarget::SymbolTypeDef: case LinkTarget::SymbolTypeDef:
return sendGotoRequest<GotoTypeDefinitionRequest>(document, return sendGotoRequest<GotoTypeDefinitionRequest>(document,
cursor, cursor,
callback, callback,
m_client, m_client,
linkUnderCursor); linkUnderCursor,
&ServerCapabilities::typeDefinitionProvider);
case LinkTarget::SymbolImplementation: case LinkTarget::SymbolImplementation:
return sendGotoRequest<GotoImplementationRequest>(document, return sendGotoRequest<GotoImplementationRequest>(document,
cursor, cursor,
callback, callback,
m_client, m_client,
linkUnderCursor); linkUnderCursor,
&ServerCapabilities::implementationProvider);
} }
return {}; return {};
} }
@@ -389,7 +395,7 @@ std::optional<MessageId> SymbolSupport::findUsages(TextEditor::TextDocument *doc
handleFindReferencesResponse(response, wordUnderCursor, handler); handleFindReferencesResponse(response, wordUnderCursor, handler);
}); });
sendTextDocumentPositionParamsRequest(m_client, request); sendTextDocumentPositionParamsRequest(m_client, request, &ServerCapabilities::referencesProvider);
return request.id(); return request.id();
} }