codeAction: use codeActionProvider: CodeActionOptions and respect CodeActionParams::range

This commit is contained in:
Fangrui Song 2018-11-25 14:55:33 -08:00
parent 38227e2bd5
commit 385eb582c8
4 changed files with 26 additions and 15 deletions

View File

@ -97,6 +97,12 @@ struct lsRange {
bool operator<(const lsRange &o) const { bool operator<(const lsRange &o) const {
return !(start == o.start) ? start < o.start : end < o.end; 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 { struct Location {

View File

@ -51,11 +51,11 @@ struct ServerCap {
// for // for
// '::' and '>' for '->'. See // '::' and '>' for '->'. See
// https://github.com/Microsoft/language-server-protocol/issues/138. // https://github.com/Microsoft/language-server-protocol/issues/138.
std::vector<std::string> triggerCharacters = {".", ":", ">", "#", std::vector<const char *> triggerCharacters = {".", ":", ">", "#",
"<", "\"", "/"}; "<", "\"", "/"};
} completionProvider; } completionProvider;
struct SignatureHelpOptions { struct SignatureHelpOptions {
std::vector<std::string> triggerCharacters = {"(", ","}; std::vector<const char *> triggerCharacters = {"(", ","};
} signatureHelpProvider; } signatureHelpProvider;
bool definitionProvider = true; bool definitionProvider = true;
bool typeDefinitionProvider = true; bool typeDefinitionProvider = true;
@ -64,7 +64,9 @@ struct ServerCap {
bool documentHighlightProvider = true; bool documentHighlightProvider = true;
bool documentSymbolProvider = true; bool documentSymbolProvider = true;
bool workspaceSymbolProvider = true; bool workspaceSymbolProvider = true;
bool codeActionProvider = true; struct CodeActionOptions {
std::vector<const char *> codeActionKinds = {"quickfix"};
} codeActionProvider;
struct CodeLensOptions { struct CodeLensOptions {
bool resolveProvider = false; bool resolveProvider = false;
} codeLensProvider; } codeLensProvider;
@ -72,7 +74,7 @@ struct ServerCap {
bool documentRangeFormattingProvider = true; bool documentRangeFormattingProvider = true;
struct DocumentOnTypeFormattingOptions { struct DocumentOnTypeFormattingOptions {
std::string firstTriggerCharacter = "}"; std::string firstTriggerCharacter = "}";
std::vector<std::string> moreTriggerCharacter; std::vector<const char *> moreTriggerCharacter;
} documentOnTypeFormattingProvider; } documentOnTypeFormattingProvider;
bool renameProvider = true; bool renameProvider = true;
struct DocumentLinkOptions { struct DocumentLinkOptions {
@ -81,7 +83,7 @@ struct ServerCap {
bool foldingRangeProvider = true; bool foldingRangeProvider = true;
// The server provides execute command support. // The server provides execute command support.
struct ExecuteCommandOptions { struct ExecuteCommandOptions {
std::vector<std::string> commands{std::string(ccls_xref)}; std::vector<const char *> commands = {ccls_xref};
} executeCommandProvider; } executeCommandProvider;
struct Workspace { struct Workspace {
struct WorkspaceFolders { struct WorkspaceFolders {
@ -90,9 +92,10 @@ struct ServerCap {
} workspaceFolders; } workspaceFolders;
} workspace; } workspace;
}; };
MAKE_REFLECT_STRUCT(ServerCap::CodeActionOptions, codeActionKinds);
MAKE_REFLECT_STRUCT(ServerCap::CodeLensOptions, resolveProvider);
MAKE_REFLECT_STRUCT(ServerCap::CompletionOptions, resolveProvider, MAKE_REFLECT_STRUCT(ServerCap::CompletionOptions, resolveProvider,
triggerCharacters); triggerCharacters);
MAKE_REFLECT_STRUCT(ServerCap::CodeLensOptions, resolveProvider);
MAKE_REFLECT_STRUCT(ServerCap::DocumentLinkOptions, resolveProvider); MAKE_REFLECT_STRUCT(ServerCap::DocumentLinkOptions, resolveProvider);
MAKE_REFLECT_STRUCT(ServerCap::DocumentOnTypeFormattingOptions, MAKE_REFLECT_STRUCT(ServerCap::DocumentOnTypeFormattingOptions,
firstTriggerCharacter, moreTriggerCharacter); firstTriggerCharacter, moreTriggerCharacter);

View File

@ -33,21 +33,24 @@ MAKE_REFLECT_STRUCT(CodeAction, title, kind, edit);
} }
void MessageHandler::textDocument_codeAction(CodeActionParam &param, void MessageHandler::textDocument_codeAction(CodeActionParam &param,
ReplyOnce &reply) { ReplyOnce &reply) {
WorkingFile *wfile = WorkingFile *wf =
wfiles->GetFileByFilename(param.textDocument.uri.GetPath()); wfiles->GetFileByFilename(param.textDocument.uri.GetPath());
if (!wfile) { if (!wf)
return; return;
}
std::vector<CodeAction> result; std::vector<CodeAction> result;
std::vector<Diagnostic> diagnostics; std::vector<Diagnostic> diagnostics;
wfiles->DoAction([&]() { diagnostics = wfile->diagnostics_; }); wfiles->DoAction([&]() { diagnostics = wf->diagnostics_; });
for (Diagnostic &diag : 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(); CodeAction &cmd = result.emplace_back();
cmd.title = "FixIt: " + diag.message; cmd.title = "FixIt: " + diag.message;
auto &edit = cmd.edit.documentChanges.emplace_back(); auto &edit = cmd.edit.documentChanges.emplace_back();
edit.textDocument.uri = param.textDocument.uri; edit.textDocument.uri = param.textDocument.uri;
edit.textDocument.version = wfile->version; edit.textDocument.version = wf->version;
edit.edits = diag.fixits_; edit.edits = diag.fixits_;
} }
reply(result); reply(result);

View File

@ -189,8 +189,7 @@ void MessageHandler::textDocument_documentSymbol(Reader &reader,
// range. // range.
if (sym.extent.Valid()) if (sym.extent.Valid())
if (auto range1 = GetLsRange(wfile, sym.extent); if (auto range1 = GetLsRange(wfile, sym.extent);
range1 && range1->start <= range->start && range1 && range1->Includes(*range))
range->end <= range1->end)
ds->range = *range1; ds->range = *range1;
} }
std::vector<const void *> def_ptrs; std::vector<const void *> def_ptrs;