@@ -73,6 +73,23 @@ std::optional<int64_t> decodeVersion(llvm::StringRef Encoded) {
7373
7474const llvm::StringLiteral ApplyFixCommand = " clangd.applyFix" ;
7575const 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+
811838void 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" );
0 commit comments