Add CodeActionLiteralSupport

This slightly optimize MessageHandler::textDocument_codeAction
This commit is contained in:
Felicián Németh 2019-11-15 16:05:38 +01:00 committed by Fangrui Song
parent c5acf62060
commit 8b4d0fc055
5 changed files with 41 additions and 2 deletions

View File

@ -121,6 +121,11 @@ struct Config {
// If false, disable snippets and complete just the identifier part.
// TextDocumentClientCapabilities.completion.completionItem.snippetSupport
bool snippetSupport = true;
// List of supported CodeActionKinds. If empty, client does not
// have CodeActionLiteralSupport.
// TextDocumentClientCapabilities.codeAction.codeActionLiteralSupport.codeActionKind.valueSet
std::vector<std::string> codeActionKind;
} client;
struct CodeLens {

View File

@ -19,7 +19,7 @@ using namespace clang;
MAKE_HASHABLE(ccls::SymbolIdx, t.usr, t.kind);
namespace ccls {
REFLECT_STRUCT(CodeActionParam::Context, diagnostics);
REFLECT_STRUCT(CodeActionParam::Context, diagnostics, only);
REFLECT_STRUCT(CodeActionParam, textDocument, range, context);
void reflect(JsonReader &, EmptyParam &) {}
REFLECT_STRUCT(TextDocumentParam, textDocument);

View File

@ -30,6 +30,7 @@ struct CodeActionParam {
lsRange range;
struct Context {
std::vector<Diagnostic> diagnostics;
std::vector<std::string> only;
} context;
};
struct EmptyParam {};

View File

@ -162,6 +162,15 @@ struct TextDocumentClientCap {
bool hierarchicalDocumentSymbolSupport = false;
} documentSymbol;
struct CodeAction {
bool dynamicRegistration = false;
struct CodeActionLiteralSupport {
struct CodeActionKind {
std::vector<std::string> valueSet;
} codeActionKind;
} codeActionLiteralSupport;
} codeAction;
struct PublishDiagnostics {
bool relatedInformation = false;
} publishDiagnostics;
@ -173,9 +182,14 @@ REFLECT_STRUCT(TextDocumentClientCap::Completion, completionItem);
REFLECT_STRUCT(TextDocumentClientCap::DocumentSymbol,
hierarchicalDocumentSymbolSupport);
REFLECT_STRUCT(TextDocumentClientCap::LinkSupport, linkSupport);
REFLECT_STRUCT(TextDocumentClientCap::CodeAction::CodeActionLiteralSupport::CodeActionKind,
valueSet);
REFLECT_STRUCT(TextDocumentClientCap::CodeAction::CodeActionLiteralSupport, codeActionKind);
REFLECT_STRUCT(TextDocumentClientCap::CodeAction,
dynamicRegistration, codeActionLiteralSupport);
REFLECT_STRUCT(TextDocumentClientCap::PublishDiagnostics, relatedInformation);
REFLECT_STRUCT(TextDocumentClientCap, completion, definition, documentSymbol,
publishDiagnostics);
codeAction, publishDiagnostics);
struct ClientCap {
WorkspaceClientCap workspace;
@ -322,6 +336,10 @@ void do_initialize(MessageHandler *m, InitializeParam &param,
if (!g_config->client.snippetSupport)
g_config->completion.duplicateOptional = false;
g_config->client.codeActionKind =
capabilities.textDocument
.codeAction.codeActionLiteralSupport.codeActionKind.valueSet;
// Ensure there is a resource directory.
if (g_config->clang.resourceDir.empty())
g_config->clang.resourceDir = getDefaultResourceDirectory();

View File

@ -21,12 +21,27 @@ struct CodeAction {
};
REFLECT_STRUCT(CodeAction, title, kind, edit);
} // namespace
template <typename T> bool vec_has(const std::vector<T> &vec, const T &key) {
return std::find(std::begin(vec), std::end(vec), key) != std::end(vec);
}
void MessageHandler::textDocument_codeAction(CodeActionParam &param,
ReplyOnce &reply) {
WorkingFile *wf = findOrFail(param.textDocument.uri.getPath(), reply).second;
if (!wf)
return;
std::vector<std::string> only = param.context.only;
std::vector<CodeAction> result;
if (!only.empty() && !vec_has(only, std::string("quickfix"))) {
reply(result);
return;
}
auto kinds = g_config->client.codeActionKind;
if (!kinds.empty() && !vec_has(kinds, std::string("quickfix"))) {
reply(result);
return;
}
std::vector<Diagnostic> diagnostics;
wfiles->withLock([&]() { diagnostics = wf->diagnostics; });
for (Diagnostic &diag : diagnostics)