diff --git a/src/lsp.hh b/src/lsp.hh index ce319eb9..42b7c2a9 100644 --- a/src/lsp.hh +++ b/src/lsp.hh @@ -97,6 +97,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 30648c82..a38903c8 100644 --- a/src/messages/initialize.cc +++ b/src/messages/initialize.cc @@ -51,11 +51,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; @@ -64,7 +64,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; @@ -72,7 +74,7 @@ struct ServerCap { bool documentRangeFormattingProvider = true; struct DocumentOnTypeFormattingOptions { std::string firstTriggerCharacter = "}"; - std::vector moreTriggerCharacter; + std::vector moreTriggerCharacter; } documentOnTypeFormattingProvider; bool renameProvider = true; struct DocumentLinkOptions { @@ -81,7 +83,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 { @@ -90,9 +92,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 0a780460..3d455abf 100644 --- a/src/messages/textDocument_code.cc +++ b/src/messages/textDocument_code.cc @@ -21,21 +21,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 532e8007..39cc69dd 100644 --- a/src/messages/textDocument_document.cc +++ b/src/messages/textDocument_document.cc @@ -189,8 +189,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;