Skip to content

Commit 1fb6fb1

Browse files
committed
[clangd] forward clang-tidy's readability-identifier-naming fix to textDocument/rename
1 parent 837cde8 commit 1fb6fb1

File tree

7 files changed

+68
-7
lines changed

7 files changed

+68
-7
lines changed

clang-tools-extra/clangd/ClangdLSPServer.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,23 @@ std::optional<int64_t> decodeVersion(llvm::StringRef Encoded) {
7373

7474
const llvm::StringLiteral ApplyFixCommand = "clangd.applyFix";
7575
const llvm::StringLiteral ApplyTweakCommand = "clangd.applyTweak";
76+
const llvm::StringLiteral ApplyRenameCommand = "clangd.applyRename";
77+
78+
CodeAction toCodeAction(const ClangdServer::CodeActionResult::Rename &R,
79+
const URIForFile &File) {
80+
CodeAction CA;
81+
CA.title = R.Diag.Message;
82+
CA.kind = std::string(CodeAction::REFACTOR_KIND);
83+
CA.command.emplace();
84+
CA.command->title = R.Diag.Message;
85+
CA.command->command = std::string(ApplyRenameCommand);
86+
RenameParams Params;
87+
Params.textDocument = TextDocumentIdentifier{File};
88+
Params.position = R.Diag.Range.start;
89+
Params.newName = R.NewName;
90+
CA.command->argument = Params;
91+
return CA;
92+
}
7693

7794
/// Transforms a tweak into a code action that would apply it if executed.
7895
/// EXPECTS: T.prepare() was called and returned true.
@@ -808,6 +825,16 @@ void ClangdLSPServer::onCommandApplyTweak(const TweakArgs &Args,
808825
std::move(Action));
809826
}
810827

828+
void ClangdLSPServer::onCommandApplyRename(const RenameParams &R,
829+
Callback<llvm::json::Value> Reply) {
830+
onRename(R, [this, Reply = std::move(Reply)](
831+
llvm::Expected<WorkspaceEdit> Edit) mutable {
832+
if (!Edit)
833+
Reply(Edit.takeError());
834+
applyEdit(std::move(*Edit), "Rename applied.", std::move(Reply));
835+
});
836+
}
837+
811838
void ClangdLSPServer::applyEdit(WorkspaceEdit WE, llvm::json::Value Success,
812839
Callback<llvm::json::Value> Reply) {
813840
ApplyWorkspaceEditParams Edit;
@@ -1043,6 +1070,10 @@ void ClangdLSPServer::onCodeAction(const CodeActionParams &Params,
10431070
CAs.back().diagnostics = {It->second};
10441071
}
10451072
}
1073+
1074+
for (const auto &R : Fixits->Renames)
1075+
CAs.push_back(toCodeAction(R, File));
1076+
10461077
for (const auto &TR : Fixits->TweakRefs)
10471078
CAs.push_back(toCodeAction(TR, File, Selection));
10481079

@@ -1664,6 +1695,7 @@ void ClangdLSPServer::bindMethods(LSPBinder &Bind,
16641695
Bind.method("textDocument/foldingRange", this, &ClangdLSPServer::onFoldingRange);
16651696
Bind.command(ApplyFixCommand, this, &ClangdLSPServer::onCommandApplyEdit);
16661697
Bind.command(ApplyTweakCommand, this, &ClangdLSPServer::onCommandApplyTweak);
1698+
Bind.command(ApplyRenameCommand, this, &ClangdLSPServer::onCommandApplyRename);
16671699

16681700
ApplyWorkspaceEdit = Bind.outgoingMethod("workspace/applyEdit");
16691701
PublishDiagnostics = Bind.outgoingNotification("textDocument/publishDiagnostics");

clang-tools-extra/clangd/ClangdLSPServer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ class ClangdLSPServer : private ClangdServer::Callbacks,
174174
/// Implement commands.
175175
void onCommandApplyEdit(const WorkspaceEdit &, Callback<llvm::json::Value>);
176176
void onCommandApplyTweak(const TweakArgs &, Callback<llvm::json::Value>);
177+
void onCommandApplyRename(const RenameParams &, Callback<llvm::json::Value>);
177178

178179
/// Outgoing LSP calls.
179180
LSPBinder::OutgoingMethod<ApplyWorkspaceEditParams,

clang-tools-extra/clangd/ClangdServer.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -668,16 +668,29 @@ void ClangdServer::codeAction(const CodeActionInputs &Params,
668668
CodeActionResult Result;
669669
Result.Version = InpAST->AST.version().str();
670670
if (KindAllowed(CodeAction::QUICKFIX_KIND)) {
671-
auto FindMatchedFixes =
672-
[&InpAST](const DiagRef &DR) -> llvm::ArrayRef<Fix> {
671+
auto FindMatchedDiag = [&InpAST](const DiagRef &DR) -> const Diag * {
673672
for (const auto &Diag : InpAST->AST.getDiagnostics())
674673
if (Diag.Range == DR.Range && Diag.Message == DR.Message)
675-
return Diag.Fixes;
676-
return {};
674+
return &Diag;
675+
return nullptr;
677676
};
678-
for (const auto &Diag : Params.Diagnostics)
679-
for (const auto &Fix : FindMatchedFixes(Diag))
680-
Result.QuickFixes.push_back({Diag, Fix});
677+
for (const auto &DiagRef : Params.Diagnostics) {
678+
if (const auto *Diag = FindMatchedDiag(DiagRef))
679+
for (const auto &Fix : Diag->Fixes) {
680+
bool IsClangTidyRename =
681+
Diag->Source == Diag::ClangTidy &&
682+
Diag->Name == "readability-identifier-naming" &&
683+
!Fix.Edits.empty();
684+
if (IsClangTidyRename) {
685+
CodeActionResult::Rename R;
686+
R.NewName = Fix.Edits.front().newText;
687+
R.Diag = DiagRef;
688+
Result.Renames.emplace_back(std::move(R));
689+
} else {
690+
Result.QuickFixes.push_back({DiagRef, Fix});
691+
}
692+
}
693+
}
681694
}
682695

683696
// Collect Tweaks

clang-tools-extra/clangd/ClangdServer.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,11 @@ class ClangdServer {
380380
};
381381
std::vector<QuickFix> QuickFixes;
382382
std::vector<TweakRef> TweakRefs;
383+
struct Rename {
384+
DiagRef Diag;
385+
std::string NewName;
386+
};
387+
std::vector<Rename> Renames;
383388
};
384389
/// Surface code actions (quick-fixes for diagnostics, or available code
385390
/// tweaks) for a given range in a file.

clang-tools-extra/clangd/Protocol.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,6 +1187,14 @@ bool fromJSON(const llvm::json::Value &Params, RenameParams &R,
11871187
O.map("position", R.position) && O.map("newName", R.newName);
11881188
}
11891189

1190+
llvm::json::Value toJSON(const RenameParams &R) {
1191+
return llvm::json::Object{
1192+
{"textDocument", R.textDocument},
1193+
{"position", R.position},
1194+
{"newName", R.newName},
1195+
};
1196+
}
1197+
11901198
llvm::json::Value toJSON(const DocumentHighlight &DH) {
11911199
return llvm::json::Object{
11921200
{"range", toJSON(DH.range)},

clang-tools-extra/clangd/Protocol.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,7 @@ struct RenameParams {
14351435
std::string newName;
14361436
};
14371437
bool fromJSON(const llvm::json::Value &, RenameParams &, llvm::json::Path);
1438+
llvm::json::Value toJSON(const RenameParams &);
14381439

14391440
enum class DocumentHighlightKind { Text = 1, Read = 2, Write = 3 };
14401441

clang-tools-extra/clangd/test/initialize-params.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
# CHECK-NEXT: "executeCommandProvider": {
4141
# CHECK-NEXT: "commands": [
4242
# CHECK-NEXT: "clangd.applyFix",
43+
# CHECK-NEXT: "clangd.applyRename"
4344
# CHECK-NEXT: "clangd.applyTweak"
4445
# CHECK-NEXT: ]
4546
# CHECK-NEXT: },

0 commit comments

Comments
 (0)