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

This commit is contained in:
Fangrui Song 2018-11-25 14:55:33 -08:00
parent 19d38bc1d2
commit 7f8f68c0b2
4 changed files with 26 additions and 15 deletions

View File

@ -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 {

View File

@ -51,11 +51,11 @@ struct ServerCap {
// for
// '::' and '>' for '->'. See
// https://github.com/Microsoft/language-server-protocol/issues/138.
std::vector<std::string> triggerCharacters = {".", ":", ">", "#",
std::vector<const char *> triggerCharacters = {".", ":", ">", "#",
"<", "\"", "/"};
} completionProvider;
struct SignatureHelpOptions {
std::vector<std::string> triggerCharacters = {"(", ","};
std::vector<const char *> 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<const char *> 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<std::string> moreTriggerCharacter;
std::vector<const char *> 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<std::string> commands{std::string(ccls_xref)};
std::vector<const char *> 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);

View File

@ -21,21 +21,24 @@ MAKE_REFLECT_STRUCT(CodeAction, title, kind, edit);
}
void MessageHandler::textDocument_codeAction(CodeActionParam &param,
ReplyOnce &reply) {
WorkingFile *wfile =
WorkingFile *wf =
wfiles->GetFileByFilename(param.textDocument.uri.GetPath());
if (!wfile) {
if (!wf)
return;
}
std::vector<CodeAction> result;
std::vector<Diagnostic> 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);

View File

@ -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<const void *> def_ptrs;