Skip to content

Commit

Permalink
Add textDocument/codeAction for clang FixIt
Browse files Browse the repository at this point in the history
What do you think of the challenge ccls-fringe in Real World CTF?
  • Loading branch information
MaskRay committed Oct 24, 2019
1 parent bfb759f commit 1c0bf0a
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 17 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ target_sources(ccls PRIVATE
src/messages/exit.cc
src/messages/initialize.cc
src/messages/shutdown.cc
src/messages/textDocument_codeAction.cc
src/messages/textDocument_codeLens.cc
src/messages/textDocument_completion.cc
src/messages/textDocument_definition.cc
Expand Down
5 changes: 3 additions & 2 deletions src/clang_complete.cc
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,8 @@ void TryEnsureDocumentParsed(ClangCompleteManager *manager,
WorkingFiles::Snapshot snapshot = session->working_files->AsSnapshot(
{StripFileType(session->file.filename)});

LOG_S(INFO) << "create completion session for " << session->file.filename;
LOG_S(INFO) << "create " << (diagnostic ? "diagnostic" : "completion")
<< " TU for " << session->file.filename;
*tu = ClangTranslationUnit::Create(session->file.filename, args, snapshot,
diagnostic);
}
Expand Down Expand Up @@ -541,7 +542,7 @@ void DiagnosticQueryMain(ClangCompleteManager *manager) {
for (const FixItHint &FixIt : I->getFixIts()) {
lsTextEdit edit;
edit.newText = FixIt.CodeToInsert;
r = FromCharRange(SM, LangOpts, FixIt.RemoveRange.getAsRange());
r = FromCharSourceRange(SM, LangOpts, FixIt.RemoveRange);
edit.range =
lsRange{{r.start.line, r.start.column}, {r.end.line, r.end.column}};
ls_diag.fixits_.push_back(edit);
Expand Down
23 changes: 12 additions & 11 deletions src/clang_tu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ using namespace clang;
#include <assert.h>
#include <mutex>

Range FromSourceRange(const SourceManager &SM, const LangOptions &LangOpts,
SourceRange R, llvm::sys::fs::UniqueID *UniqueID,
bool token) {
Range FromCharSourceRange(const SourceManager &SM, const LangOptions &LangOpts,
CharSourceRange R,
llvm::sys::fs::UniqueID *UniqueID) {
SourceLocation BLoc = R.getBegin(), ELoc = R.getEnd();
std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(BLoc);
std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(ELoc);
if (token)
if (R.isTokenRange())
EInfo.second += Lexer::MeasureTokenLength(ELoc, SM, LangOpts);
unsigned l0 = SM.getLineNumber(BInfo.first, BInfo.second) - 1,
c0 = SM.getColumnNumber(BInfo.first, BInfo.second) - 1,
Expand All @@ -42,15 +42,15 @@ Range FromSourceRange(const SourceManager &SM, const LangOptions &LangOpts,
}

Range FromCharRange(const SourceManager &SM, const LangOptions &LangOpts,
SourceRange R,
llvm::sys::fs::UniqueID *UniqueID) {
return FromSourceRange(SM, LangOpts, R, UniqueID, false);
SourceRange R, llvm::sys::fs::UniqueID *UniqueID) {
return FromCharSourceRange(SM, LangOpts, CharSourceRange::getCharRange(R),
UniqueID);
}

Range FromTokenRange(const SourceManager &SM, const LangOptions &LangOpts,
SourceRange R,
llvm::sys::fs::UniqueID *UniqueID) {
return FromSourceRange(SM, LangOpts, R, UniqueID, true);
SourceRange R, llvm::sys::fs::UniqueID *UniqueID) {
return FromCharSourceRange(SM, LangOpts, CharSourceRange::getTokenRange(R),
UniqueID);
}

std::vector<ASTUnit::RemappedFile>
Expand All @@ -70,8 +70,9 @@ std::unique_ptr<ClangTranslationUnit> ClangTranslationUnit::Create(
std::vector<const char *> Args;
for (auto& arg : args)
Args.push_back(arg.c_str());
Args.push_back("-fno-spell-checking");
Args.push_back("-fallow-editor-placeholders");
if (!diagnostic)
Args.push_back("-fno-spell-checking");

auto ret = std::make_unique<ClangTranslationUnit>();
IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Expand Down
5 changes: 5 additions & 0 deletions src/clang_tu.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
std::vector<clang::ASTUnit::RemappedFile>
GetRemapped(const WorkingFiles::Snapshot &snapshot);

Range FromCharSourceRange(const clang::SourceManager &SM,
const clang::LangOptions &LangOpts,
clang::CharSourceRange R,
llvm::sys::fs::UniqueID *UniqueID = nullptr);

Range FromCharRange(const clang::SourceManager &SM, const clang::LangOptions &LangOpts,
clang::SourceRange R,
llvm::sys::fs::UniqueID *UniqueID = nullptr);
Expand Down
9 changes: 5 additions & 4 deletions src/indexer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ class IndexDataConsumer : public index::IndexDataConsumer {
auto R = SM.isMacroArgExpansion(Loc) ? CharSourceRange::getTokenRange(Spell)
: SM.getExpansionRange(Loc);
#endif
loc = FromTokenRange(SM, Lang, R.getAsRange());
loc = FromCharSourceRange(SM, Lang, R);
LocFID = SM.getFileID(R.getBegin());
FE = SM.getFileEntryForID(LocFID);
if (!FE)
Expand Down Expand Up @@ -992,9 +992,10 @@ class IndexPPCallbacks : public PPCallbacks {
if (!File)
return;
llvm::sys::fs::UniqueID UniqueID;
SourceRange R = FilenameRange.getAsRange();
auto spell = FromCharRange(SM, param.Ctx->getLangOpts(), R, &UniqueID);
const FileEntry *FE = SM.getFileEntryForID(SM.getFileID(R.getBegin()));
auto spell = FromCharSourceRange(SM, param.Ctx->getLangOpts(),
FilenameRange, &UniqueID);
const FileEntry *FE =
SM.getFileEntryForID(SM.getFileID(FilenameRange.getBegin()));
if (!FE)
return;
if (IndexFile *db = param.ConsumeFile(*FE)) {
Expand Down
77 changes: 77 additions & 0 deletions src/messages/textDocument_codeAction.cc
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 &params = 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);
}

0 comments on commit 1c0bf0a

Please sign in to comment.