From 9ffbf3c52e0e928efab20cdc0b4f2397d6ab8da5 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sun, 25 Nov 2018 14:55:33 -0800 Subject: [PATCH] codeAction: use codeActionProvider: CodeActionOptions and respect CodeActionParams::range --- src/lsp.hh | 6 ++++++ src/messages/initialize.cc | 17 ++++++++++------- src/messages/textDocument_code.cc | 15 +++++++++------ src/messages/textDocument_document.cc | 3 +-- 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/lsp.hh b/src/lsp.hh index 2c4c735f..9677aea5 100644 --- a/src/lsp.hh +++ b/src/lsp.hh @@ -109,6 +109,12 @@ struct lsRange { bool operator<(const lsRange &o) const { return !(start == o.start) ? start < o.start : end < o.end; } + bool Includes(const lsRange &o) const { + return start <= o.start && o.end <= end; + } + bool Intersects(const lsRange &o) const { + return start < o.end && o.start < end; + } }; struct Location { diff --git a/src/messages/initialize.cc b/src/messages/initialize.cc index cfea3b3e..4582e4ac 100644 --- a/src/messages/initialize.cc +++ b/src/messages/initialize.cc @@ -63,11 +63,11 @@ struct ServerCap { // for // '::' and '>' for '->'. See // https://github.com/Microsoft/language-server-protocol/issues/138. - std::vector triggerCharacters = {".", ":", ">", "#", - "<", "\"", "/"}; + std::vector triggerCharacters = {".", ":", ">", "#", + "<", "\"", "/"}; } completionProvider; struct SignatureHelpOptions { - std::vector triggerCharacters = {"(", ","}; + std::vector triggerCharacters = {"(", ","}; } signatureHelpProvider; bool definitionProvider = true; bool typeDefinitionProvider = true; @@ -76,7 +76,9 @@ struct ServerCap { bool documentHighlightProvider = true; bool documentSymbolProvider = true; bool workspaceSymbolProvider = true; - bool codeActionProvider = true; + struct CodeActionOptions { + std::vector codeActionKinds = {"quickfix"}; + } codeActionProvider; struct CodeLensOptions { bool resolveProvider = false; } codeLensProvider; @@ -84,7 +86,7 @@ struct ServerCap { bool documentRangeFormattingProvider = true; struct DocumentOnTypeFormattingOptions { std::string firstTriggerCharacter = "}"; - std::vector moreTriggerCharacter; + std::vector moreTriggerCharacter; } documentOnTypeFormattingProvider; bool renameProvider = true; struct DocumentLinkOptions { @@ -93,7 +95,7 @@ struct ServerCap { bool foldingRangeProvider = true; // The server provides execute command support. struct ExecuteCommandOptions { - std::vector commands{std::string(ccls_xref)}; + std::vector commands = {ccls_xref}; } executeCommandProvider; struct Workspace { struct WorkspaceFolders { @@ -102,9 +104,10 @@ struct ServerCap { } workspaceFolders; } workspace; }; +MAKE_REFLECT_STRUCT(ServerCap::CodeActionOptions, codeActionKinds); +MAKE_REFLECT_STRUCT(ServerCap::CodeLensOptions, resolveProvider); MAKE_REFLECT_STRUCT(ServerCap::CompletionOptions, resolveProvider, triggerCharacters); -MAKE_REFLECT_STRUCT(ServerCap::CodeLensOptions, resolveProvider); MAKE_REFLECT_STRUCT(ServerCap::DocumentLinkOptions, resolveProvider); MAKE_REFLECT_STRUCT(ServerCap::DocumentOnTypeFormattingOptions, firstTriggerCharacter, moreTriggerCharacter); diff --git a/src/messages/textDocument_code.cc b/src/messages/textDocument_code.cc index 3f645c26..4adcf0cb 100644 --- a/src/messages/textDocument_code.cc +++ b/src/messages/textDocument_code.cc @@ -33,21 +33,24 @@ MAKE_REFLECT_STRUCT(CodeAction, title, kind, edit); } void MessageHandler::textDocument_codeAction(CodeActionParam ¶m, ReplyOnce &reply) { - WorkingFile *wfile = + WorkingFile *wf = wfiles->GetFileByFilename(param.textDocument.uri.GetPath()); - if (!wfile) { + if (!wf) return; - } std::vector result; std::vector diagnostics; - wfiles->DoAction([&]() { diagnostics = wfile->diagnostics_; }); + wfiles->DoAction([&]() { diagnostics = wf->diagnostics_; }); for (Diagnostic &diag : diagnostics) - if (diag.fixits_.size()) { + if (diag.fixits_.size() && + (param.range.Intersects(diag.range) || + llvm::any_of(diag.fixits_, [&](const TextEdit &edit) { + return param.range.Intersects(edit.range); + }))) { CodeAction &cmd = result.emplace_back(); cmd.title = "FixIt: " + diag.message; auto &edit = cmd.edit.documentChanges.emplace_back(); edit.textDocument.uri = param.textDocument.uri; - edit.textDocument.version = wfile->version; + edit.textDocument.version = wf->version; edit.edits = diag.fixits_; } reply(result); diff --git a/src/messages/textDocument_document.cc b/src/messages/textDocument_document.cc index 48734552..a02005a7 100644 --- a/src/messages/textDocument_document.cc +++ b/src/messages/textDocument_document.cc @@ -201,8 +201,7 @@ void MessageHandler::textDocument_documentSymbol(Reader &reader, // range. if (sym.extent.Valid()) if (auto range1 = GetLsRange(wfile, sym.extent); - range1 && range1->start <= range->start && - range->end <= range1->end) + range1 && range1->Includes(*range)) ds->range = *range1; } std::vector def_ptrs;