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";
};
using InitializeResponse = Response<InitializeResult, InitializeError>;
} // namespace LanguageClient

View File

@@ -245,7 +245,8 @@ public:
void setId(const MessageId &id)
{ 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)
{ m_callBack = callback; }
@@ -258,13 +259,13 @@ public:
QString parseError;
const QJsonObject &object =
JsonRpcMessageHandler::toJsonObject(content, codec, parseError);
Response<Result, Error> response(object);
Response response(object);
if (object.isEmpty()) {
ResponseError<Error> error;
error.setMessage(parseError);
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";
};
using ShowMessageResponse = Response<LanguageClientValue<MessageActionItem>, LanguageClientNull>;
using LogMessageParams = ShowMessageParams;
class LANGUAGESERVERPROTOCOL_EXPORT LogMessageNotification : public Notification<LogMessageParams>

View File

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

View File

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

View File

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

View File

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

View File

@@ -324,7 +324,7 @@ void LanguageClientManager::findLinkAt(const Utils::FileName &filePath,
const Position pos(cursor);
TextDocumentPositionParams params(document, pos);
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()) {
const GotoResult result = _result.value();
if (Utils::holds_alternative<std::nullptr_t>(result))