LSP: simplify request response type

Instead of always defining the templates of the response type by hand
add a using construct in the Request where all the template types are
known.

Change-Id: I0dc00bd9aef9c37c9454e35aca200a30fcf2ebda
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
David Schulz
2018-11-20 07:45:22 +01:00
parent f73ed6bb54
commit e52ea7771f
8 changed files with 17 additions and 22 deletions

View File

@@ -176,6 +176,4 @@ public:
constexpr static const char methodName[] = "initialize"; constexpr static const char methodName[] = "initialize";
}; };
using InitializeResponse = Response<InitializeResult, InitializeError>;
} // namespace LanguageClient } // namespace LanguageClient

View File

@@ -245,7 +245,8 @@ public:
void setId(const MessageId &id) void setId(const MessageId &id)
{ JsonRpcMessage::m_jsonObject.insert(idKey, id.toJson()); } { JsonRpcMessage::m_jsonObject.insert(idKey, id.toJson()); }
using ResponseCallback = std::function<void(Response<Result, Error>)>; using Response = LanguageServerProtocol::Response<Result, Error>;
using ResponseCallback = std::function<void(Response)>;
void setResponseCallback(const ResponseCallback &callback) void setResponseCallback(const ResponseCallback &callback)
{ m_callBack = callback; } { m_callBack = callback; }
@@ -258,13 +259,13 @@ public:
QString parseError; QString parseError;
const QJsonObject &object = const QJsonObject &object =
JsonRpcMessageHandler::toJsonObject(content, codec, parseError); JsonRpcMessageHandler::toJsonObject(content, codec, parseError);
Response<Result, Error> response(object); Response response(object);
if (object.isEmpty()) { if (object.isEmpty()) {
ResponseError<Error> error; ResponseError<Error> error;
error.setMessage(parseError); error.setMessage(parseError);
response.setError(ResponseError<Error>()); response.setError(ResponseError<Error>());
} }
callback(Response<Result, Error>(object)); callback(Response(object));
}); });
} }

View File

@@ -95,8 +95,6 @@ public:
constexpr static const char methodName[] = "window/showMessageRequest"; constexpr static const char methodName[] = "window/showMessageRequest";
}; };
using ShowMessageResponse = Response<LanguageClientValue<MessageActionItem>, LanguageClientNull>;
using LogMessageParams = ShowMessageParams; using LogMessageParams = ShowMessageParams;
class LANGUAGESERVERPROTOCOL_EXPORT LogMessageNotification : public Notification<LogMessageParams> class LANGUAGESERVERPROTOCOL_EXPORT LogMessageNotification : public Notification<LogMessageParams>

View File

@@ -29,8 +29,6 @@
namespace LanguageServerProtocol { namespace LanguageServerProtocol {
using ShutdownResponse = Response<LanguageClientNull, LanguageClientNull>;
class LANGUAGESERVERPROTOCOL_EXPORT ShutdownRequest : public Request< class LANGUAGESERVERPROTOCOL_EXPORT ShutdownRequest : public Request<
LanguageClientNull, LanguageClientNull, LanguageClientNull> LanguageClientNull, LanguageClientNull, LanguageClientNull>
{ {

View File

@@ -92,7 +92,7 @@ void BaseClient::initialize()
return WorkSpaceFolder(pro->projectDirectory().toString(), pro->displayName()); return WorkSpaceFolder(pro->projectDirectory().toString(), pro->displayName());
})); }));
} }
initRequest->setResponseCallback([this](const InitializeResponse &initResponse){ initRequest->setResponseCallback([this](const InitializeRequest::Response &initResponse){
intializeCallback(initResponse); intializeCallback(initResponse);
}); });
// directly send data otherwise the state check would fail; // directly send data otherwise the state check would fail;
@@ -106,7 +106,7 @@ void BaseClient::shutdown()
QTC_ASSERT(m_state == Initialized, emit finished(); return); QTC_ASSERT(m_state == Initialized, emit finished(); return);
qCDebug(LOGLSPCLIENT) << "shutdown language server " << m_displayName; qCDebug(LOGLSPCLIENT) << "shutdown language server " << m_displayName;
ShutdownRequest shutdown; ShutdownRequest shutdown;
shutdown.setResponseCallback([this](const ShutdownResponse &shutdownResponse){ shutdown.setResponseCallback([this](const ShutdownRequest::Response &shutdownResponse){
shutDownCallback(shutdownResponse); shutDownCallback(shutdownResponse);
}); });
sendContent(shutdown); sendContent(shutdown);
@@ -348,7 +348,7 @@ void BaseClient::requestDocumentSymbols(TextEditor::TextDocument *document)
DocumentSymbolParams(TextDocumentIdentifier(DocumentUri::fromFileName(filePath)))); DocumentSymbolParams(TextDocumentIdentifier(DocumentUri::fromFileName(filePath))));
request.setResponseCallback( request.setResponseCallback(
[doc = QPointer<TextEditor::TextDocument>(document)] [doc = QPointer<TextEditor::TextDocument>(document)]
(Response<DocumentSymbolsResult, LanguageClientNull> response){ (DocumentSymbolsRequest::Response response){
if (!doc) if (!doc)
return; return;
const DocumentSymbolsResult result = response.result().value_or(DocumentSymbolsResult()); const DocumentSymbolsResult result = response.result().value_or(DocumentSymbolsResult());
@@ -441,7 +441,7 @@ void BaseClient::cursorPositionChanged(TextEditor::TextEditorWidget *widget)
DocumentHighlightsRequest request(TextDocumentPositionParams(uri, widget->textCursor())); DocumentHighlightsRequest request(TextDocumentPositionParams(uri, widget->textCursor()));
request.setResponseCallback( request.setResponseCallback(
[widget = QPointer<TextEditor::TextEditorWidget>(widget), this, uri] [widget = QPointer<TextEditor::TextEditorWidget>(widget), this, uri]
(Response<DocumentHighlightsResult, LanguageClientNull> response) (DocumentHighlightsRequest::Response response)
{ {
m_highlightRequests.remove(uri); m_highlightRequests.remove(uri);
if (!widget) if (!widget)
@@ -573,7 +573,7 @@ void BaseClient::showMessageBox(const ShowMessageRequestParams &message, const M
} }
box->setModal(true); box->setModal(true);
connect(box, &QMessageBox::finished, this, [=]{ connect(box, &QMessageBox::finished, this, [=]{
Response<LanguageClientValue<MessageActionItem>, LanguageClientNull> response; ShowMessageRequest::Response response;
response.setId(id); response.setId(id);
const MessageActionItem &item = itemForButton.value(box->clickedButton()); const MessageActionItem &item = itemForButton.value(box->clickedButton());
response.setResult(item.isValid(nullptr) ? LanguageClientValue<MessageActionItem>(item) response.setResult(item.isValid(nullptr) ? LanguageClientValue<MessageActionItem>(item)
@@ -615,7 +615,7 @@ void BaseClient::handleMethod(const QString &method, MessageId id, const IConten
if (paramsValid) { if (paramsValid) {
showMessageBox(params, request->id()); showMessageBox(params, request->id());
} else { } else {
Response<LanguageClientValue<MessageActionItem>, LanguageClientNull> response; ShowMessageRequest::Response response;
response.setId(request->id()); response.setId(request->id());
ResponseError<LanguageClientNull> error; ResponseError<LanguageClientNull> error;
const QString errorMessage = const QString errorMessage =
@@ -652,7 +652,7 @@ void BaseClient::handleMethod(const QString &method, MessageId id, const IConten
delete content; delete content;
} }
void BaseClient::intializeCallback(const InitializeResponse &initResponse) void BaseClient::intializeCallback(const InitializeRequest::Response &initResponse)
{ {
QTC_ASSERT(m_state == InitializeRequested, return); QTC_ASSERT(m_state == InitializeRequested, return);
if (optional<ResponseError<InitializeError>> error = initResponse.error()) { if (optional<ResponseError<InitializeError>> error = initResponse.error()) {
@@ -693,7 +693,7 @@ void BaseClient::intializeCallback(const InitializeResponse &initResponse)
openDocument(openedDocument); openDocument(openedDocument);
} }
void BaseClient::shutDownCallback(const ShutdownResponse &shutdownResponse) void BaseClient::shutDownCallback(const ShutdownRequest::Response &shutdownResponse)
{ {
QTC_ASSERT(m_state == ShutdownRequested, return); QTC_ASSERT(m_state == ShutdownRequested, return);
optional<ResponseError<JsonObject>> errorValue = shutdownResponse.error(); optional<ResponseError<JsonObject>> errorValue = shutdownResponse.error();

View File

@@ -136,8 +136,8 @@ private:
void handleMethod(const QString &method, LanguageServerProtocol::MessageId id, void handleMethod(const QString &method, LanguageServerProtocol::MessageId id,
const LanguageServerProtocol::IContent *content); const LanguageServerProtocol::IContent *content);
void intializeCallback(const LanguageServerProtocol::InitializeResponse &initResponse); void intializeCallback(const LanguageServerProtocol::InitializeRequest::Response &initResponse);
void shutDownCallback(const LanguageServerProtocol::ShutdownResponse &shutdownResponse); void shutDownCallback(const LanguageServerProtocol::ShutdownRequest::Response &shutdownResponse);
bool sendWorkspceFolderChanges() const; bool sendWorkspceFolderChanges() const;
void log(const LanguageServerProtocol::ShowMessageParams &message, void log(const LanguageServerProtocol::ShowMessageParams &message,
Core::MessageManager::PrintToOutputPaneFlag flag = Core::MessageManager::NoModeSwitch); Core::MessageManager::PrintToOutputPaneFlag flag = Core::MessageManager::NoModeSwitch);

View File

@@ -273,7 +273,7 @@ public:
bool needsRestart() const override { return true; } bool needsRestart() const override { return true; }
private: private:
void handleCompletionResponse(const Response<CompletionResult, LanguageClientNull> &response); void handleCompletionResponse(const CompletionRequest::Response &response);
QPointer<QTextDocument> m_document; QPointer<QTextDocument> m_document;
QPointer<BaseClient> m_client; QPointer<BaseClient> m_client;
@@ -343,7 +343,7 @@ bool LanguageClientCompletionAssistProcessor::running()
} }
void LanguageClientCompletionAssistProcessor::handleCompletionResponse( void LanguageClientCompletionAssistProcessor::handleCompletionResponse(
const Response<CompletionResult, LanguageClientNull> &response) const CompletionRequest::Response &response)
{ {
using namespace TextEditor; using namespace TextEditor;
qCDebug(LOGLSPCOMPLETION) << QTime::currentTime() << " : got completions"; qCDebug(LOGLSPCOMPLETION) << QTime::currentTime() << " : got completions";

View File

@@ -324,7 +324,7 @@ void LanguageClientManager::findLinkAt(const Utils::FileName &filePath,
const Position pos(cursor); const Position pos(cursor);
TextDocumentPositionParams params(document, pos); TextDocumentPositionParams params(document, pos);
GotoDefinitionRequest request(params); GotoDefinitionRequest request(params);
request.setResponseCallback([callback](const Response<GotoResult, LanguageClientNull> &response){ request.setResponseCallback([callback](const GotoDefinitionRequest::Response &response){
if (Utils::optional<GotoResult> _result = response.result()) { if (Utils::optional<GotoResult> _result = response.result()) {
const GotoResult result = _result.value(); const GotoResult result = _result.value();
if (Utils::holds_alternative<std::nullptr_t>(result)) if (Utils::holds_alternative<std::nullptr_t>(result))