forked from MaskRay/ccls
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add textDocument/codeAction for clang FixIt
What do you think of the challenge ccls-fringe in Real World CTF?
- Loading branch information
Showing
6 changed files
with
103 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#include "message_handler.h" | ||
#include "pipeline.hh" | ||
#include "working_files.h" | ||
using namespace ccls; | ||
|
||
struct CommandArgs { | ||
lsDocumentUri textDocumentUri; | ||
std::vector<lsTextEdit> edits; | ||
}; | ||
MAKE_REFLECT_STRUCT_WRITER_AS_ARRAY(CommandArgs, textDocumentUri, edits); | ||
|
||
namespace { | ||
MethodType kMethodType = "textDocument/codeAction"; | ||
|
||
struct In_TextDocumentCodeAction : public RequestInMessage { | ||
MethodType GetMethodType() const override { return kMethodType; } | ||
|
||
// Contains additional diagnostic information about the context in which | ||
// a code action is run. | ||
struct lsCodeActionContext { | ||
// An array of diagnostics. | ||
std::vector<lsDiagnostic> diagnostics; | ||
}; | ||
// Params for the CodeActionRequest | ||
struct lsCodeActionParams { | ||
// The document in which the command was invoked. | ||
lsTextDocumentIdentifier textDocument; | ||
// The range for which the command was invoked. | ||
lsRange range; | ||
// Context carrying additional information. | ||
lsCodeActionContext context; | ||
} params; | ||
}; | ||
MAKE_REFLECT_STRUCT(In_TextDocumentCodeAction::lsCodeActionContext, | ||
diagnostics); | ||
MAKE_REFLECT_STRUCT(In_TextDocumentCodeAction::lsCodeActionParams, | ||
textDocument, | ||
range, | ||
context); | ||
MAKE_REFLECT_STRUCT(In_TextDocumentCodeAction, id, params); | ||
REGISTER_IN_MESSAGE(In_TextDocumentCodeAction); | ||
|
||
struct Out_TextDocumentCodeAction | ||
: public lsOutMessage<Out_TextDocumentCodeAction> { | ||
lsRequestId id; | ||
std::vector<lsCommand<CommandArgs>> result; | ||
}; | ||
MAKE_REFLECT_STRUCT(Out_TextDocumentCodeAction, jsonrpc, id, result); | ||
|
||
struct Handler_TextDocumentCodeAction | ||
: BaseMessageHandler<In_TextDocumentCodeAction> { | ||
MethodType GetMethodType() const override { return kMethodType; } | ||
|
||
void Run(In_TextDocumentCodeAction* request) override { | ||
const auto ¶ms = request->params; | ||
WorkingFile *wfile = | ||
working_files->GetFileByFilename(params.textDocument.uri.GetPath()); | ||
if (!wfile) | ||
return; | ||
Out_TextDocumentCodeAction out; | ||
out.id = request->id; | ||
std::vector<lsDiagnostic> diagnostics; | ||
working_files->DoAction([&]() { diagnostics = wfile->diagnostics_; }); | ||
for (lsDiagnostic &diag : diagnostics) | ||
if (diag.fixits_.size()) { | ||
lsCommand<CommandArgs> command; | ||
command.title = "FixIt: " + diag.message; | ||
command.command = "ccls._applyFixIt"; | ||
command.arguments.textDocumentUri = params.textDocument.uri; | ||
command.arguments.edits = diag.fixits_; | ||
out.result.push_back(command); | ||
} | ||
pipeline::WriteStdout(kMethodType, out); | ||
} | ||
}; | ||
REGISTER_MESSAGE_HANDLER(Handler_TextDocumentCodeAction); | ||
} |