[clang-tools-extra] [clangd] forward clang-tidy's readability-identifier-naming fix to textDocument/rename (PR #78454)

2024-02-19 Thread Tom Praschan via cfe-commits

https://github.com/tom-anders closed 
https://github.com/llvm/llvm-project/pull/78454
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] forward clang-tidy's readability-identifier-naming fix to textDocument/rename (PR #78454)

2024-02-19 Thread Nathan Ridge via cfe-commits

https://github.com/HighCommander4 updated 
https://github.com/llvm/llvm-project/pull/78454

>From 3a1ef6006764bd4d307ceec74199ed81a18aba2d Mon Sep 17 00:00:00 2001
From: tom-anders <13141438+tom-and...@users.noreply.github.com>
Date: Wed, 17 Jan 2024 15:36:02 +0100
Subject: [PATCH] [clangd] forward clang-tidy's readability-identifier-naming
 fix to textDocument/rename

---
 clang-tools-extra/clangd/ClangdLSPServer.cpp  | 32 ++
 clang-tools-extra/clangd/ClangdLSPServer.h|  1 +
 clang-tools-extra/clangd/ClangdServer.cpp | 44 ++---
 clang-tools-extra/clangd/ClangdServer.h   |  6 ++
 clang-tools-extra/clangd/Protocol.cpp |  8 +++
 clang-tools-extra/clangd/Protocol.h   |  1 +
 .../clangd/test/initialize-params.test|  1 +
 .../clangd/unittests/ClangdLSPServerTests.cpp | 61 +++
 .../clangd/unittests/LSPClient.cpp| 31 +-
 .../clangd/unittests/LSPClient.h  |  6 ++
 10 files changed, 182 insertions(+), 9 deletions(-)

diff --git a/clang-tools-extra/clangd/ClangdLSPServer.cpp 
b/clang-tools-extra/clangd/ClangdLSPServer.cpp
index a87da252b7a7e9..81c434bc4d987d 100644
--- a/clang-tools-extra/clangd/ClangdLSPServer.cpp
+++ b/clang-tools-extra/clangd/ClangdLSPServer.cpp
@@ -73,6 +73,23 @@ std::optional decodeVersion(llvm::StringRef 
Encoded) {
 
 const llvm::StringLiteral ApplyFixCommand = "clangd.applyFix";
 const llvm::StringLiteral ApplyTweakCommand = "clangd.applyTweak";
+const llvm::StringLiteral ApplyRenameCommand = "clangd.applyRename";
+
+CodeAction toCodeAction(const ClangdServer::CodeActionResult::Rename &R,
+const URIForFile &File) {
+  CodeAction CA;
+  CA.title = R.FixMessage;
+  CA.kind = std::string(CodeAction::REFACTOR_KIND);
+  CA.command.emplace();
+  CA.command->title = R.FixMessage;
+  CA.command->command = std::string(ApplyRenameCommand);
+  RenameParams Params;
+  Params.textDocument = TextDocumentIdentifier{File};
+  Params.position = R.Diag.Range.start;
+  Params.newName = R.NewName;
+  CA.command->argument = Params;
+  return CA;
+}
 
 /// Transforms a tweak into a code action that would apply it if executed.
 /// EXPECTS: T.prepare() was called and returned true.
@@ -808,6 +825,16 @@ void ClangdLSPServer::onCommandApplyTweak(const TweakArgs 
&Args,
  std::move(Action));
 }
 
+void ClangdLSPServer::onCommandApplyRename(const RenameParams &R,
+   Callback Reply) {
+  onRename(R, [this, Reply = std::move(Reply)](
+  llvm::Expected Edit) mutable {
+if (!Edit)
+  Reply(Edit.takeError());
+applyEdit(std::move(*Edit), "Rename applied.", std::move(Reply));
+  });
+}
+
 void ClangdLSPServer::applyEdit(WorkspaceEdit WE, llvm::json::Value Success,
 Callback Reply) {
   ApplyWorkspaceEditParams Edit;
@@ -1043,6 +1070,10 @@ void ClangdLSPServer::onCodeAction(const 
CodeActionParams &Params,
 CAs.back().diagnostics = {It->second};
   }
 }
+
+for (const auto &R : Fixits->Renames)
+  CAs.push_back(toCodeAction(R, File));
+
 for (const auto &TR : Fixits->TweakRefs)
   CAs.push_back(toCodeAction(TR, File, Selection));
 
@@ -1664,6 +1695,7 @@ void ClangdLSPServer::bindMethods(LSPBinder &Bind,
   Bind.method("textDocument/foldingRange", this, 
&ClangdLSPServer::onFoldingRange);
   Bind.command(ApplyFixCommand, this, &ClangdLSPServer::onCommandApplyEdit);
   Bind.command(ApplyTweakCommand, this, &ClangdLSPServer::onCommandApplyTweak);
+  Bind.command(ApplyRenameCommand, this, 
&ClangdLSPServer::onCommandApplyRename);
 
   ApplyWorkspaceEdit = Bind.outgoingMethod("workspace/applyEdit");
   PublishDiagnostics = 
Bind.outgoingNotification("textDocument/publishDiagnostics");
diff --git a/clang-tools-extra/clangd/ClangdLSPServer.h 
b/clang-tools-extra/clangd/ClangdLSPServer.h
index 79579c22b788a9..69a349c6a5e087 100644
--- a/clang-tools-extra/clangd/ClangdLSPServer.h
+++ b/clang-tools-extra/clangd/ClangdLSPServer.h
@@ -174,6 +174,7 @@ class ClangdLSPServer : private ClangdServer::Callbacks,
   /// Implement commands.
   void onCommandApplyEdit(const WorkspaceEdit &, Callback);
   void onCommandApplyTweak(const TweakArgs &, Callback);
+  void onCommandApplyRename(const RenameParams &, Callback);
 
   /// Outgoing LSP calls.
   LSPBinder::OutgoingMethodrunWithAST("Rename", File, std::move(Action));
 }
 
+namespace {
 // May generate several candidate selections, due to SelectionTree ambiguity.
 // vector of pointers because GCC doesn't like non-copyable Selection.
-static llvm::Expected>>
+llvm::Expected>>
 tweakSelection(const Range &Sel, const InputsAndAST &AST,
llvm::vfs::FileSystem *FS) {
   auto Begin = positionToOffset(AST.Inputs.Contents, Sel.start);
@@ -648,6 +649,27 @@ tweakSelection(const Range &Sel, const InputsAndAST &AST,
   return std::move(Result);
 }
 
+// Some fixes may perform local renaming, we want to convert those 

[clang-tools-extra] [clangd] forward clang-tidy's readability-identifier-naming fix to textDocument/rename (PR #78454)

2024-02-19 Thread Nathan Ridge via cfe-commits

https://github.com/HighCommander4 approved this pull request.

Thanks, LGTM!

https://github.com/llvm/llvm-project/pull/78454
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] forward clang-tidy's readability-identifier-naming fix to textDocument/rename (PR #78454)

2024-02-18 Thread Tom Praschan via cfe-commits

https://github.com/tom-anders updated 
https://github.com/llvm/llvm-project/pull/78454

>From 3a1ef6006764bd4d307ceec74199ed81a18aba2d Mon Sep 17 00:00:00 2001
From: tom-anders <13141438+tom-and...@users.noreply.github.com>
Date: Wed, 17 Jan 2024 15:36:02 +0100
Subject: [PATCH] [clangd] forward clang-tidy's readability-identifier-naming
 fix to textDocument/rename

---
 clang-tools-extra/clangd/ClangdLSPServer.cpp  | 32 ++
 clang-tools-extra/clangd/ClangdLSPServer.h|  1 +
 clang-tools-extra/clangd/ClangdServer.cpp | 44 ++---
 clang-tools-extra/clangd/ClangdServer.h   |  6 ++
 clang-tools-extra/clangd/Protocol.cpp |  8 +++
 clang-tools-extra/clangd/Protocol.h   |  1 +
 .../clangd/test/initialize-params.test|  1 +
 .../clangd/unittests/ClangdLSPServerTests.cpp | 61 +++
 .../clangd/unittests/LSPClient.cpp| 31 +-
 .../clangd/unittests/LSPClient.h  |  6 ++
 10 files changed, 182 insertions(+), 9 deletions(-)

diff --git a/clang-tools-extra/clangd/ClangdLSPServer.cpp 
b/clang-tools-extra/clangd/ClangdLSPServer.cpp
index a87da252b7a7e9..81c434bc4d987d 100644
--- a/clang-tools-extra/clangd/ClangdLSPServer.cpp
+++ b/clang-tools-extra/clangd/ClangdLSPServer.cpp
@@ -73,6 +73,23 @@ std::optional decodeVersion(llvm::StringRef 
Encoded) {
 
 const llvm::StringLiteral ApplyFixCommand = "clangd.applyFix";
 const llvm::StringLiteral ApplyTweakCommand = "clangd.applyTweak";
+const llvm::StringLiteral ApplyRenameCommand = "clangd.applyRename";
+
+CodeAction toCodeAction(const ClangdServer::CodeActionResult::Rename &R,
+const URIForFile &File) {
+  CodeAction CA;
+  CA.title = R.FixMessage;
+  CA.kind = std::string(CodeAction::REFACTOR_KIND);
+  CA.command.emplace();
+  CA.command->title = R.FixMessage;
+  CA.command->command = std::string(ApplyRenameCommand);
+  RenameParams Params;
+  Params.textDocument = TextDocumentIdentifier{File};
+  Params.position = R.Diag.Range.start;
+  Params.newName = R.NewName;
+  CA.command->argument = Params;
+  return CA;
+}
 
 /// Transforms a tweak into a code action that would apply it if executed.
 /// EXPECTS: T.prepare() was called and returned true.
@@ -808,6 +825,16 @@ void ClangdLSPServer::onCommandApplyTweak(const TweakArgs 
&Args,
  std::move(Action));
 }
 
+void ClangdLSPServer::onCommandApplyRename(const RenameParams &R,
+   Callback Reply) {
+  onRename(R, [this, Reply = std::move(Reply)](
+  llvm::Expected Edit) mutable {
+if (!Edit)
+  Reply(Edit.takeError());
+applyEdit(std::move(*Edit), "Rename applied.", std::move(Reply));
+  });
+}
+
 void ClangdLSPServer::applyEdit(WorkspaceEdit WE, llvm::json::Value Success,
 Callback Reply) {
   ApplyWorkspaceEditParams Edit;
@@ -1043,6 +1070,10 @@ void ClangdLSPServer::onCodeAction(const 
CodeActionParams &Params,
 CAs.back().diagnostics = {It->second};
   }
 }
+
+for (const auto &R : Fixits->Renames)
+  CAs.push_back(toCodeAction(R, File));
+
 for (const auto &TR : Fixits->TweakRefs)
   CAs.push_back(toCodeAction(TR, File, Selection));
 
@@ -1664,6 +1695,7 @@ void ClangdLSPServer::bindMethods(LSPBinder &Bind,
   Bind.method("textDocument/foldingRange", this, 
&ClangdLSPServer::onFoldingRange);
   Bind.command(ApplyFixCommand, this, &ClangdLSPServer::onCommandApplyEdit);
   Bind.command(ApplyTweakCommand, this, &ClangdLSPServer::onCommandApplyTweak);
+  Bind.command(ApplyRenameCommand, this, 
&ClangdLSPServer::onCommandApplyRename);
 
   ApplyWorkspaceEdit = Bind.outgoingMethod("workspace/applyEdit");
   PublishDiagnostics = 
Bind.outgoingNotification("textDocument/publishDiagnostics");
diff --git a/clang-tools-extra/clangd/ClangdLSPServer.h 
b/clang-tools-extra/clangd/ClangdLSPServer.h
index 79579c22b788a9..69a349c6a5e087 100644
--- a/clang-tools-extra/clangd/ClangdLSPServer.h
+++ b/clang-tools-extra/clangd/ClangdLSPServer.h
@@ -174,6 +174,7 @@ class ClangdLSPServer : private ClangdServer::Callbacks,
   /// Implement commands.
   void onCommandApplyEdit(const WorkspaceEdit &, Callback);
   void onCommandApplyTweak(const TweakArgs &, Callback);
+  void onCommandApplyRename(const RenameParams &, Callback);
 
   /// Outgoing LSP calls.
   LSPBinder::OutgoingMethodrunWithAST("Rename", File, std::move(Action));
 }
 
+namespace {
 // May generate several candidate selections, due to SelectionTree ambiguity.
 // vector of pointers because GCC doesn't like non-copyable Selection.
-static llvm::Expected>>
+llvm::Expected>>
 tweakSelection(const Range &Sel, const InputsAndAST &AST,
llvm::vfs::FileSystem *FS) {
   auto Begin = positionToOffset(AST.Inputs.Contents, Sel.start);
@@ -648,6 +649,27 @@ tweakSelection(const Range &Sel, const InputsAndAST &AST,
   return std::move(Result);
 }
 
+// Some fixes may perform local renaming, we want to convert those to c

[clang-tools-extra] [clangd] forward clang-tidy's readability-identifier-naming fix to textDocument/rename (PR #78454)

2024-02-18 Thread Tom Praschan via cfe-commits

tom-anders wrote:

> Something else I noticed while trying out the patch locally: before the 
> patch, the description of the code action in the editor is "change 'foo' to 
> 'Foo'", i.e. a description of what the code action will do.
> 
> After the patch, the description of the code action is now "invalid case 
> style for function 'foo'", i.e. it describes the problem, not the fix.
> 
> We should keep the description of the code action the same, i.e. describing 
> the fix.

Ah good find, I added a new `FixMessage` field for this (and also added this to 
the test)

https://github.com/llvm/llvm-project/pull/78454
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] forward clang-tidy's readability-identifier-naming fix to textDocument/rename (PR #78454)

2024-02-18 Thread Tom Praschan via cfe-commits


@@ -648,6 +649,27 @@ tweakSelection(const Range &Sel, const InputsAndAST &AST,
   return std::move(Result);
 }
 
+// Some fixes may perform local renaming, we want to convert those to clangd
+// rename commands, such that we can leverage the index for more accurate
+// results.
+std::optional
+TryConvertToRename(const Diag *Diag, const ClangdServer::DiagRef &DR,

tom-anders wrote:

hah :D 

At work we're using Google style, so functions are CamelCase and variables 
snake_case. Switching back and forth between Google and LLVM style is so 
confusing

https://github.com/llvm/llvm-project/pull/78454
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] forward clang-tidy's readability-identifier-naming fix to textDocument/rename (PR #78454)

2024-02-11 Thread Nathan Ridge via cfe-commits

HighCommander4 wrote:

(It's also no longer labelled as a "quick fix", likely due to the 
`CodeActionKind` changing from `quickfix` to `refactor`, but that's probably 
fine.)

https://github.com/llvm/llvm-project/pull/78454
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] forward clang-tidy's readability-identifier-naming fix to textDocument/rename (PR #78454)

2024-02-11 Thread Nathan Ridge via cfe-commits

HighCommander4 wrote:

Something else I noticed while trying out the patch locally: before the patch, 
the description of the quick fix in the editor is "change 'foo' to 'Foo'", i.e. 
a description of what the code action will do.

After the patch, the description of the quick fix is now "invalid case style 
for function 'foo'", i.e. it describes the problem, not the fix.

We should keep the description of the code action the same, i.e. describing the 
fix.

https://github.com/llvm/llvm-project/pull/78454
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] forward clang-tidy's readability-identifier-naming fix to textDocument/rename (PR #78454)

2024-02-10 Thread Nathan Ridge via cfe-commits


@@ -648,6 +649,27 @@ tweakSelection(const Range &Sel, const InputsAndAST &AST,
   return std::move(Result);
 }
 
+// Some fixes may perform local renaming, we want to convert those to clangd
+// rename commands, such that we can leverage the index for more accurate
+// results.
+std::optional
+TryConvertToRename(const Diag *Diag, const ClangdServer::DiagRef &DR,

HighCommander4 wrote:

Amusingly, I get a `readability-identifier-naming` diagnostic for this (it 
wants `lowerCamelCase`; my bad for the original suggestion having the wrong 
case)

https://github.com/llvm/llvm-project/pull/78454
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] forward clang-tidy's readability-identifier-naming fix to textDocument/rename (PR #78454)

2024-02-10 Thread Nathan Ridge via cfe-commits


@@ -648,6 +649,27 @@ tweakSelection(const Range &Sel, const InputsAndAST &AST,
   return std::move(Result);
 }
 
+// Some fixes may perform local renaming, we want to convert those to clangd
+// rename commands, such that we can leverage the index for more accurate
+// results.
+std::optional
+TryConvertToRename(const Diag *Diag, const ClangdServer::DiagRef &DR,
+   const Fix &Fix) {
+  bool IsClangTidyRename = Diag->Source == Diag::ClangTidy &&
+   Diag->Name == "readability-identifier-naming" &&
+   !Fix.Edits.empty();
+  if (IsClangTidyRename) {
+ClangdServer::CodeActionResult::Rename R;
+R.NewName = Fix.Edits.front().newText;
+R.Diag = DR;

HighCommander4 wrote:

(for simplicity, consider omitting the `DR` parameter, and just construct a 
`DiagRef` here as `{Diag->Range, Diag->Message}`, which is how a `DiagRef` is 
constructed 
[elsewhere](https://searchfox.org/llvm/rev/33c6b77d2a18862fb5b16160ef9d600382e93f19/clang-tools-extra/clangd/ClangdLSPServer.cpp#1763))

https://github.com/llvm/llvm-project/pull/78454
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] forward clang-tidy's readability-identifier-naming fix to textDocument/rename (PR #78454)

2024-02-10 Thread Nathan Ridge via cfe-commits

https://github.com/HighCommander4 edited 
https://github.com/llvm/llvm-project/pull/78454
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] forward clang-tidy's readability-identifier-naming fix to textDocument/rename (PR #78454)

2024-02-10 Thread Nathan Ridge via cfe-commits


@@ -648,6 +649,27 @@ tweakSelection(const Range &Sel, const InputsAndAST &AST,
   return std::move(Result);
 }
 
+// Some fixes may perform local renaming, we want to convert those to clangd
+// rename commands, such that we can leverage the index for more accurate
+// results.
+std::optional
+TryConvertToRename(const Diag *Diag, const ClangdServer::DiagRef &DR,
+   const Fix &Fix) {
+  bool IsClangTidyRename = Diag->Source == Diag::ClangTidy &&
+   Diag->Name == "readability-identifier-naming" &&
+   !Fix.Edits.empty();
+  if (IsClangTidyRename) {

HighCommander4 wrote:

For good measure, we should check `Diag->InsideMainFile` as well. If that's 
false, then `Diag->Range` (and thus `DR.Range`) may not be a location in the 
main file, which would be a problem (since we propagate `DR.Range.start` into 
`RenameParams::position`, where it then gets interpreted as a position in the 
main file).

https://github.com/llvm/llvm-project/pull/78454
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] forward clang-tidy's readability-identifier-naming fix to textDocument/rename (PR #78454)

2024-02-10 Thread Nathan Ridge via cfe-commits

https://github.com/HighCommander4 requested changes to this pull request.

Ok, I had a more detailed look at the implementation. Still looks good overall, 
just have some minor comments.

https://github.com/llvm/llvm-project/pull/78454
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] forward clang-tidy's readability-identifier-naming fix to textDocument/rename (PR #78454)

2024-02-06 Thread Tom Praschan via cfe-commits

https://github.com/tom-anders updated 
https://github.com/llvm/llvm-project/pull/78454

>From 0f4be9fe945a23598c6eb49d733e0a709375669d Mon Sep 17 00:00:00 2001
From: tom-anders <13141438+tom-and...@users.noreply.github.com>
Date: Wed, 17 Jan 2024 15:36:02 +0100
Subject: [PATCH] [clangd] forward clang-tidy's readability-identifier-naming
 fix to textDocument/rename

---
 clang-tools-extra/clangd/ClangdLSPServer.cpp  | 32 ++
 clang-tools-extra/clangd/ClangdLSPServer.h|  1 +
 clang-tools-extra/clangd/ClangdServer.cpp | 44 +++---
 clang-tools-extra/clangd/ClangdServer.h   |  5 ++
 clang-tools-extra/clangd/Protocol.cpp |  8 +++
 clang-tools-extra/clangd/Protocol.h   |  1 +
 .../clangd/test/initialize-params.test|  1 +
 .../clangd/unittests/ClangdLSPServerTests.cpp | 59 +++
 .../clangd/unittests/LSPClient.cpp| 31 +-
 .../clangd/unittests/LSPClient.h  |  6 ++
 10 files changed, 179 insertions(+), 9 deletions(-)

diff --git a/clang-tools-extra/clangd/ClangdLSPServer.cpp 
b/clang-tools-extra/clangd/ClangdLSPServer.cpp
index a87da252b7a7e9..49d1eb0e8341c1 100644
--- a/clang-tools-extra/clangd/ClangdLSPServer.cpp
+++ b/clang-tools-extra/clangd/ClangdLSPServer.cpp
@@ -73,6 +73,23 @@ std::optional decodeVersion(llvm::StringRef 
Encoded) {
 
 const llvm::StringLiteral ApplyFixCommand = "clangd.applyFix";
 const llvm::StringLiteral ApplyTweakCommand = "clangd.applyTweak";
+const llvm::StringLiteral ApplyRenameCommand = "clangd.applyRename";
+
+CodeAction toCodeAction(const ClangdServer::CodeActionResult::Rename &R,
+const URIForFile &File) {
+  CodeAction CA;
+  CA.title = R.Diag.Message;
+  CA.kind = std::string(CodeAction::REFACTOR_KIND);
+  CA.command.emplace();
+  CA.command->title = R.Diag.Message;
+  CA.command->command = std::string(ApplyRenameCommand);
+  RenameParams Params;
+  Params.textDocument = TextDocumentIdentifier{File};
+  Params.position = R.Diag.Range.start;
+  Params.newName = R.NewName;
+  CA.command->argument = Params;
+  return CA;
+}
 
 /// Transforms a tweak into a code action that would apply it if executed.
 /// EXPECTS: T.prepare() was called and returned true.
@@ -808,6 +825,16 @@ void ClangdLSPServer::onCommandApplyTweak(const TweakArgs 
&Args,
  std::move(Action));
 }
 
+void ClangdLSPServer::onCommandApplyRename(const RenameParams &R,
+   Callback Reply) {
+  onRename(R, [this, Reply = std::move(Reply)](
+  llvm::Expected Edit) mutable {
+if (!Edit)
+  Reply(Edit.takeError());
+applyEdit(std::move(*Edit), "Rename applied.", std::move(Reply));
+  });
+}
+
 void ClangdLSPServer::applyEdit(WorkspaceEdit WE, llvm::json::Value Success,
 Callback Reply) {
   ApplyWorkspaceEditParams Edit;
@@ -1043,6 +1070,10 @@ void ClangdLSPServer::onCodeAction(const 
CodeActionParams &Params,
 CAs.back().diagnostics = {It->second};
   }
 }
+
+for (const auto &R : Fixits->Renames)
+  CAs.push_back(toCodeAction(R, File));
+
 for (const auto &TR : Fixits->TweakRefs)
   CAs.push_back(toCodeAction(TR, File, Selection));
 
@@ -1664,6 +1695,7 @@ void ClangdLSPServer::bindMethods(LSPBinder &Bind,
   Bind.method("textDocument/foldingRange", this, 
&ClangdLSPServer::onFoldingRange);
   Bind.command(ApplyFixCommand, this, &ClangdLSPServer::onCommandApplyEdit);
   Bind.command(ApplyTweakCommand, this, &ClangdLSPServer::onCommandApplyTweak);
+  Bind.command(ApplyRenameCommand, this, 
&ClangdLSPServer::onCommandApplyRename);
 
   ApplyWorkspaceEdit = Bind.outgoingMethod("workspace/applyEdit");
   PublishDiagnostics = 
Bind.outgoingNotification("textDocument/publishDiagnostics");
diff --git a/clang-tools-extra/clangd/ClangdLSPServer.h 
b/clang-tools-extra/clangd/ClangdLSPServer.h
index 79579c22b788a9..69a349c6a5e087 100644
--- a/clang-tools-extra/clangd/ClangdLSPServer.h
+++ b/clang-tools-extra/clangd/ClangdLSPServer.h
@@ -174,6 +174,7 @@ class ClangdLSPServer : private ClangdServer::Callbacks,
   /// Implement commands.
   void onCommandApplyEdit(const WorkspaceEdit &, Callback);
   void onCommandApplyTweak(const TweakArgs &, Callback);
+  void onCommandApplyRename(const RenameParams &, Callback);
 
   /// Outgoing LSP calls.
   LSPBinder::OutgoingMethodrunWithAST("Rename", File, std::move(Action));
 }
 
+namespace {
 // May generate several candidate selections, due to SelectionTree ambiguity.
 // vector of pointers because GCC doesn't like non-copyable Selection.
-static llvm::Expected>>
+llvm::Expected>>
 tweakSelection(const Range &Sel, const InputsAndAST &AST,
llvm::vfs::FileSystem *FS) {
   auto Begin = positionToOffset(AST.Inputs.Contents, Sel.start);
@@ -648,6 +649,27 @@ tweakSelection(const Range &Sel, const InputsAndAST &AST,
   return std::move(Result);
 }
 
+// Some fixes may perform local renaming, we want to convert those

[clang-tools-extra] [clangd] forward clang-tidy's readability-identifier-naming fix to textDocument/rename (PR #78454)

2024-02-06 Thread Tom Praschan via cfe-commits

tom-anders wrote:

(fixed formatting issues)

https://github.com/llvm/llvm-project/pull/78454
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] forward clang-tidy's readability-identifier-naming fix to textDocument/rename (PR #78454)

2024-02-06 Thread Tom Praschan via cfe-commits

tom-anders wrote:

> Looks pretty good!
> 
> Are there further changes you're planning to make, or is this ready to 
> graduate from "Draft" status?

Ah, thanks about the heads up, forgot about this 

https://github.com/llvm/llvm-project/pull/78454
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] forward clang-tidy's readability-identifier-naming fix to textDocument/rename (PR #78454)

2024-02-06 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clangd

Author: Tom Praschan (tom-anders)


Changes



---
Full diff: https://github.com/llvm/llvm-project/pull/78454.diff


10 Files Affected:

- (modified) clang-tools-extra/clangd/ClangdLSPServer.cpp (+32) 
- (modified) clang-tools-extra/clangd/ClangdLSPServer.h (+1) 
- (modified) clang-tools-extra/clangd/ClangdServer.cpp (+35-8) 
- (modified) clang-tools-extra/clangd/ClangdServer.h (+5) 
- (modified) clang-tools-extra/clangd/Protocol.cpp (+8) 
- (modified) clang-tools-extra/clangd/Protocol.h (+1) 
- (modified) clang-tools-extra/clangd/test/initialize-params.test (+1) 
- (modified) clang-tools-extra/clangd/unittests/ClangdLSPServerTests.cpp (+59) 
- (modified) clang-tools-extra/clangd/unittests/LSPClient.cpp (+30-1) 
- (modified) clang-tools-extra/clangd/unittests/LSPClient.h (+6) 


``diff
diff --git a/clang-tools-extra/clangd/ClangdLSPServer.cpp 
b/clang-tools-extra/clangd/ClangdLSPServer.cpp
index a87da252b7a7e9..49d1eb0e8341c1 100644
--- a/clang-tools-extra/clangd/ClangdLSPServer.cpp
+++ b/clang-tools-extra/clangd/ClangdLSPServer.cpp
@@ -73,6 +73,23 @@ std::optional decodeVersion(llvm::StringRef 
Encoded) {
 
 const llvm::StringLiteral ApplyFixCommand = "clangd.applyFix";
 const llvm::StringLiteral ApplyTweakCommand = "clangd.applyTweak";
+const llvm::StringLiteral ApplyRenameCommand = "clangd.applyRename";
+
+CodeAction toCodeAction(const ClangdServer::CodeActionResult::Rename &R,
+const URIForFile &File) {
+  CodeAction CA;
+  CA.title = R.Diag.Message;
+  CA.kind = std::string(CodeAction::REFACTOR_KIND);
+  CA.command.emplace();
+  CA.command->title = R.Diag.Message;
+  CA.command->command = std::string(ApplyRenameCommand);
+  RenameParams Params;
+  Params.textDocument = TextDocumentIdentifier{File};
+  Params.position = R.Diag.Range.start;
+  Params.newName = R.NewName;
+  CA.command->argument = Params;
+  return CA;
+}
 
 /// Transforms a tweak into a code action that would apply it if executed.
 /// EXPECTS: T.prepare() was called and returned true.
@@ -808,6 +825,16 @@ void ClangdLSPServer::onCommandApplyTweak(const TweakArgs 
&Args,
  std::move(Action));
 }
 
+void ClangdLSPServer::onCommandApplyRename(const RenameParams &R,
+   Callback Reply) {
+  onRename(R, [this, Reply = std::move(Reply)](
+  llvm::Expected Edit) mutable {
+if (!Edit)
+  Reply(Edit.takeError());
+applyEdit(std::move(*Edit), "Rename applied.", std::move(Reply));
+  });
+}
+
 void ClangdLSPServer::applyEdit(WorkspaceEdit WE, llvm::json::Value Success,
 Callback Reply) {
   ApplyWorkspaceEditParams Edit;
@@ -1043,6 +1070,10 @@ void ClangdLSPServer::onCodeAction(const 
CodeActionParams &Params,
 CAs.back().diagnostics = {It->second};
   }
 }
+
+for (const auto &R : Fixits->Renames)
+  CAs.push_back(toCodeAction(R, File));
+
 for (const auto &TR : Fixits->TweakRefs)
   CAs.push_back(toCodeAction(TR, File, Selection));
 
@@ -1664,6 +1695,7 @@ void ClangdLSPServer::bindMethods(LSPBinder &Bind,
   Bind.method("textDocument/foldingRange", this, 
&ClangdLSPServer::onFoldingRange);
   Bind.command(ApplyFixCommand, this, &ClangdLSPServer::onCommandApplyEdit);
   Bind.command(ApplyTweakCommand, this, &ClangdLSPServer::onCommandApplyTweak);
+  Bind.command(ApplyRenameCommand, this, 
&ClangdLSPServer::onCommandApplyRename);
 
   ApplyWorkspaceEdit = Bind.outgoingMethod("workspace/applyEdit");
   PublishDiagnostics = 
Bind.outgoingNotification("textDocument/publishDiagnostics");
diff --git a/clang-tools-extra/clangd/ClangdLSPServer.h 
b/clang-tools-extra/clangd/ClangdLSPServer.h
index 79579c22b788a9..69a349c6a5e087 100644
--- a/clang-tools-extra/clangd/ClangdLSPServer.h
+++ b/clang-tools-extra/clangd/ClangdLSPServer.h
@@ -174,6 +174,7 @@ class ClangdLSPServer : private ClangdServer::Callbacks,
   /// Implement commands.
   void onCommandApplyEdit(const WorkspaceEdit &, Callback);
   void onCommandApplyTweak(const TweakArgs &, Callback);
+  void onCommandApplyRename(const RenameParams &, Callback);
 
   /// Outgoing LSP calls.
   LSPBinder::OutgoingMethodrunWithAST("Rename", File, std::move(Action));
 }
 
+namespace {
 // May generate several candidate selections, due to SelectionTree ambiguity.
 // vector of pointers because GCC doesn't like non-copyable Selection.
-static llvm::Expected>>
+llvm::Expected>>
 tweakSelection(const Range &Sel, const InputsAndAST &AST,
llvm::vfs::FileSystem *FS) {
   auto Begin = positionToOffset(AST.Inputs.Contents, Sel.start);
@@ -648,6 +649,26 @@ tweakSelection(const Range &Sel, const InputsAndAST &AST,
   return std::move(Result);
 }
 
+// Some fixes may perform local renaming, we want to convert those to clangd
+// rename commands, such that we can leverage the index for more accurate
+// results.
+std::optional
+TryConvertToRename(const Diag *Diag, const ClangdSe

[clang-tools-extra] [clangd] forward clang-tidy's readability-identifier-naming fix to textDocument/rename (PR #78454)

2024-02-06 Thread Tom Praschan via cfe-commits

https://github.com/tom-anders ready_for_review 
https://github.com/llvm/llvm-project/pull/78454
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] forward clang-tidy's readability-identifier-naming fix to textDocument/rename (PR #78454)

2024-02-05 Thread Nathan Ridge via cfe-commits

HighCommander4 wrote:

Looks pretty good!

Are there further changes you're planning to make, or is this ready to graduate 
from "Draft" status?

https://github.com/llvm/llvm-project/pull/78454
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] forward clang-tidy's readability-identifier-naming fix to textDocument/rename (PR #78454)

2024-01-31 Thread Tom Praschan via cfe-commits

https://github.com/tom-anders updated 
https://github.com/llvm/llvm-project/pull/78454

>From 579d681323db5f92d494f0cd0aaa9158dc8c4e3b Mon Sep 17 00:00:00 2001
From: tom-anders <13141438+tom-and...@users.noreply.github.com>
Date: Wed, 17 Jan 2024 15:36:02 +0100
Subject: [PATCH] [clangd] forward clang-tidy's readability-identifier-naming
 fix to textDocument/rename

---
 clang-tools-extra/clangd/ClangdLSPServer.cpp  | 32 ++
 clang-tools-extra/clangd/ClangdLSPServer.h|  1 +
 clang-tools-extra/clangd/ClangdServer.cpp | 43 +++---
 clang-tools-extra/clangd/ClangdServer.h   |  5 ++
 clang-tools-extra/clangd/Protocol.cpp |  8 +++
 clang-tools-extra/clangd/Protocol.h   |  1 +
 .../clangd/test/initialize-params.test|  1 +
 .../clangd/unittests/ClangdLSPServerTests.cpp | 59 +++
 .../clangd/unittests/LSPClient.cpp| 31 +-
 .../clangd/unittests/LSPClient.h  |  6 ++
 10 files changed, 178 insertions(+), 9 deletions(-)

diff --git a/clang-tools-extra/clangd/ClangdLSPServer.cpp 
b/clang-tools-extra/clangd/ClangdLSPServer.cpp
index a87da252b7a7e..49d1eb0e8341c 100644
--- a/clang-tools-extra/clangd/ClangdLSPServer.cpp
+++ b/clang-tools-extra/clangd/ClangdLSPServer.cpp
@@ -73,6 +73,23 @@ std::optional decodeVersion(llvm::StringRef 
Encoded) {
 
 const llvm::StringLiteral ApplyFixCommand = "clangd.applyFix";
 const llvm::StringLiteral ApplyTweakCommand = "clangd.applyTweak";
+const llvm::StringLiteral ApplyRenameCommand = "clangd.applyRename";
+
+CodeAction toCodeAction(const ClangdServer::CodeActionResult::Rename &R,
+const URIForFile &File) {
+  CodeAction CA;
+  CA.title = R.Diag.Message;
+  CA.kind = std::string(CodeAction::REFACTOR_KIND);
+  CA.command.emplace();
+  CA.command->title = R.Diag.Message;
+  CA.command->command = std::string(ApplyRenameCommand);
+  RenameParams Params;
+  Params.textDocument = TextDocumentIdentifier{File};
+  Params.position = R.Diag.Range.start;
+  Params.newName = R.NewName;
+  CA.command->argument = Params;
+  return CA;
+}
 
 /// Transforms a tweak into a code action that would apply it if executed.
 /// EXPECTS: T.prepare() was called and returned true.
@@ -808,6 +825,16 @@ void ClangdLSPServer::onCommandApplyTweak(const TweakArgs 
&Args,
  std::move(Action));
 }
 
+void ClangdLSPServer::onCommandApplyRename(const RenameParams &R,
+   Callback Reply) {
+  onRename(R, [this, Reply = std::move(Reply)](
+  llvm::Expected Edit) mutable {
+if (!Edit)
+  Reply(Edit.takeError());
+applyEdit(std::move(*Edit), "Rename applied.", std::move(Reply));
+  });
+}
+
 void ClangdLSPServer::applyEdit(WorkspaceEdit WE, llvm::json::Value Success,
 Callback Reply) {
   ApplyWorkspaceEditParams Edit;
@@ -1043,6 +1070,10 @@ void ClangdLSPServer::onCodeAction(const 
CodeActionParams &Params,
 CAs.back().diagnostics = {It->second};
   }
 }
+
+for (const auto &R : Fixits->Renames)
+  CAs.push_back(toCodeAction(R, File));
+
 for (const auto &TR : Fixits->TweakRefs)
   CAs.push_back(toCodeAction(TR, File, Selection));
 
@@ -1664,6 +1695,7 @@ void ClangdLSPServer::bindMethods(LSPBinder &Bind,
   Bind.method("textDocument/foldingRange", this, 
&ClangdLSPServer::onFoldingRange);
   Bind.command(ApplyFixCommand, this, &ClangdLSPServer::onCommandApplyEdit);
   Bind.command(ApplyTweakCommand, this, &ClangdLSPServer::onCommandApplyTweak);
+  Bind.command(ApplyRenameCommand, this, 
&ClangdLSPServer::onCommandApplyRename);
 
   ApplyWorkspaceEdit = Bind.outgoingMethod("workspace/applyEdit");
   PublishDiagnostics = 
Bind.outgoingNotification("textDocument/publishDiagnostics");
diff --git a/clang-tools-extra/clangd/ClangdLSPServer.h 
b/clang-tools-extra/clangd/ClangdLSPServer.h
index 79579c22b788a..69a349c6a5e08 100644
--- a/clang-tools-extra/clangd/ClangdLSPServer.h
+++ b/clang-tools-extra/clangd/ClangdLSPServer.h
@@ -174,6 +174,7 @@ class ClangdLSPServer : private ClangdServer::Callbacks,
   /// Implement commands.
   void onCommandApplyEdit(const WorkspaceEdit &, Callback);
   void onCommandApplyTweak(const TweakArgs &, Callback);
+  void onCommandApplyRename(const RenameParams &, Callback);
 
   /// Outgoing LSP calls.
   LSPBinder::OutgoingMethodrunWithAST("Rename", File, std::move(Action));
 }
 
+namespace {
 // May generate several candidate selections, due to SelectionTree ambiguity.
 // vector of pointers because GCC doesn't like non-copyable Selection.
-static llvm::Expected>>
+llvm::Expected>>
 tweakSelection(const Range &Sel, const InputsAndAST &AST,
llvm::vfs::FileSystem *FS) {
   auto Begin = positionToOffset(AST.Inputs.Contents, Sel.start);
@@ -648,6 +649,26 @@ tweakSelection(const Range &Sel, const InputsAndAST &AST,
   return std::move(Result);
 }
 
+// Some fixes may perform local renaming, we want to convert those to 

[clang-tools-extra] [clangd] forward clang-tidy's readability-identifier-naming fix to textDocument/rename (PR #78454)

2024-01-31 Thread Tom Praschan via cfe-commits

tom-anders wrote:

Thanks for the feedback!

> Thanks, the approach in this patch looks pretty good to me.
> 
> My only feedback is to encapsulate the "hard coding" into a function like:
> 
> ```
> Option TryConvertToRename(const Diag *D, const Fix 
> *F)
> ```
> 
> because I can imagine in the future coming across other diagnostics that are 
> conceptually renames that we may want to handle this way.

:heavy_check_mark: 

> Regarding testing: I find lit-tests pretty hard to read and maintain; a nicer 
> alternative might be a gtest in `ClangdLSPServerTests` (e.g. see [this 
> one](https://searchfox.org/llvm/rev/23b233c8adad5b81e185e50d04356fab64c2f870/clang-tools-extra/clangd/unittests/ClangdLSPServerTests.cpp#198)
>  for testing call hierarchy incoming calls).

Perfect, thanks for the hint! I had to adapt `LSPClient` a bit, it usually 
would flag every server->client call as an error, but in this case we actually 
expect a `workspace/applyEdit` after we trigger the renaming.


https://github.com/llvm/llvm-project/pull/78454
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] forward clang-tidy's readability-identifier-naming fix to textDocument/rename (PR #78454)

2024-01-31 Thread Nathan Ridge via cfe-commits

HighCommander4 wrote:

> Duplicate issue: 
> [clangd/clangd#741](https://github.com/clangd/clangd/issues/741)

Nice find, I closed it as a duplicate.

https://github.com/llvm/llvm-project/pull/78454
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] forward clang-tidy's readability-identifier-naming fix to textDocument/rename (PR #78454)

2024-01-29 Thread Tom Praschan via cfe-commits

tom-anders wrote:

Duplicate issue: https://github.com/clangd/clangd/issues/741

https://github.com/llvm/llvm-project/pull/78454
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] forward clang-tidy's readability-identifier-naming fix to textDocument/rename (PR #78454)

2024-01-29 Thread Nathan Ridge via cfe-commits

https://github.com/HighCommander4 commented:

Thanks, the approach in this patch looks pretty good to me.

My only feedback is to encapsulate the "hard coding" into a function like:

```
Option TryConvertToRename(const Diag *D, const Fix *F)
```

because I can imagine in the future coming across other diagnostics that are 
conceptually renames that we may want to handle this way.

Regarding testing: I find lit-tests pretty hard to read and maintain; a nicer 
alternative might be a gtest in `ClangdLSPServerTests` (e.g. see [this 
one](https://searchfox.org/llvm/rev/23b233c8adad5b81e185e50d04356fab64c2f870/clang-tools-extra/clangd/unittests/ClangdLSPServerTests.cpp#198)
 for testing call hierarchy incoming calls).

https://github.com/llvm/llvm-project/pull/78454
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] forward clang-tidy's readability-identifier-naming fix to textDocument/rename (PR #78454)

2024-01-29 Thread Nathan Ridge via cfe-commits

HighCommander4 wrote:

Linking to the issue this is seeking to address for reference: 
https://github.com/clangd/clangd/issues/1589

https://github.com/llvm/llvm-project/pull/78454
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] forward clang-tidy's readability-identifier-naming fix to textDocument/rename (PR #78454)

2024-01-17 Thread Tom Praschan via cfe-commits

https://github.com/tom-anders updated 
https://github.com/llvm/llvm-project/pull/78454

>From 1fb6fb163bf2e63cb30571b179768bae2f43eb71 Mon Sep 17 00:00:00 2001
From: tom-anders <13141438+tom-and...@users.noreply.github.com>
Date: Wed, 17 Jan 2024 15:36:02 +0100
Subject: [PATCH] [clangd] forward clang-tidy's readability-identifier-naming
 fix to textDocument/rename

---
 clang-tools-extra/clangd/ClangdLSPServer.cpp  | 32 +++
 clang-tools-extra/clangd/ClangdLSPServer.h|  1 +
 clang-tools-extra/clangd/ClangdServer.cpp | 27 
 clang-tools-extra/clangd/ClangdServer.h   |  5 +++
 clang-tools-extra/clangd/Protocol.cpp |  8 +
 clang-tools-extra/clangd/Protocol.h   |  1 +
 .../clangd/test/initialize-params.test|  1 +
 7 files changed, 68 insertions(+), 7 deletions(-)

diff --git a/clang-tools-extra/clangd/ClangdLSPServer.cpp 
b/clang-tools-extra/clangd/ClangdLSPServer.cpp
index a87da252b7a7e9..49d1eb0e8341c1 100644
--- a/clang-tools-extra/clangd/ClangdLSPServer.cpp
+++ b/clang-tools-extra/clangd/ClangdLSPServer.cpp
@@ -73,6 +73,23 @@ std::optional decodeVersion(llvm::StringRef 
Encoded) {
 
 const llvm::StringLiteral ApplyFixCommand = "clangd.applyFix";
 const llvm::StringLiteral ApplyTweakCommand = "clangd.applyTweak";
+const llvm::StringLiteral ApplyRenameCommand = "clangd.applyRename";
+
+CodeAction toCodeAction(const ClangdServer::CodeActionResult::Rename &R,
+const URIForFile &File) {
+  CodeAction CA;
+  CA.title = R.Diag.Message;
+  CA.kind = std::string(CodeAction::REFACTOR_KIND);
+  CA.command.emplace();
+  CA.command->title = R.Diag.Message;
+  CA.command->command = std::string(ApplyRenameCommand);
+  RenameParams Params;
+  Params.textDocument = TextDocumentIdentifier{File};
+  Params.position = R.Diag.Range.start;
+  Params.newName = R.NewName;
+  CA.command->argument = Params;
+  return CA;
+}
 
 /// Transforms a tweak into a code action that would apply it if executed.
 /// EXPECTS: T.prepare() was called and returned true.
@@ -808,6 +825,16 @@ void ClangdLSPServer::onCommandApplyTweak(const TweakArgs 
&Args,
  std::move(Action));
 }
 
+void ClangdLSPServer::onCommandApplyRename(const RenameParams &R,
+   Callback Reply) {
+  onRename(R, [this, Reply = std::move(Reply)](
+  llvm::Expected Edit) mutable {
+if (!Edit)
+  Reply(Edit.takeError());
+applyEdit(std::move(*Edit), "Rename applied.", std::move(Reply));
+  });
+}
+
 void ClangdLSPServer::applyEdit(WorkspaceEdit WE, llvm::json::Value Success,
 Callback Reply) {
   ApplyWorkspaceEditParams Edit;
@@ -1043,6 +1070,10 @@ void ClangdLSPServer::onCodeAction(const 
CodeActionParams &Params,
 CAs.back().diagnostics = {It->second};
   }
 }
+
+for (const auto &R : Fixits->Renames)
+  CAs.push_back(toCodeAction(R, File));
+
 for (const auto &TR : Fixits->TweakRefs)
   CAs.push_back(toCodeAction(TR, File, Selection));
 
@@ -1664,6 +1695,7 @@ void ClangdLSPServer::bindMethods(LSPBinder &Bind,
   Bind.method("textDocument/foldingRange", this, 
&ClangdLSPServer::onFoldingRange);
   Bind.command(ApplyFixCommand, this, &ClangdLSPServer::onCommandApplyEdit);
   Bind.command(ApplyTweakCommand, this, &ClangdLSPServer::onCommandApplyTweak);
+  Bind.command(ApplyRenameCommand, this, 
&ClangdLSPServer::onCommandApplyRename);
 
   ApplyWorkspaceEdit = Bind.outgoingMethod("workspace/applyEdit");
   PublishDiagnostics = 
Bind.outgoingNotification("textDocument/publishDiagnostics");
diff --git a/clang-tools-extra/clangd/ClangdLSPServer.h 
b/clang-tools-extra/clangd/ClangdLSPServer.h
index 79579c22b788a9..69a349c6a5e087 100644
--- a/clang-tools-extra/clangd/ClangdLSPServer.h
+++ b/clang-tools-extra/clangd/ClangdLSPServer.h
@@ -174,6 +174,7 @@ class ClangdLSPServer : private ClangdServer::Callbacks,
   /// Implement commands.
   void onCommandApplyEdit(const WorkspaceEdit &, Callback);
   void onCommandApplyTweak(const TweakArgs &, Callback);
+  void onCommandApplyRename(const RenameParams &, Callback);
 
   /// Outgoing LSP calls.
   LSPBinder::OutgoingMethodAST.version().str();
 if (KindAllowed(CodeAction::QUICKFIX_KIND)) {
-  auto FindMatchedFixes =
-  [&InpAST](const DiagRef &DR) -> llvm::ArrayRef {
+  auto FindMatchedDiag = [&InpAST](const DiagRef &DR) -> const Diag * {
 for (const auto &Diag : InpAST->AST.getDiagnostics())
   if (Diag.Range == DR.Range && Diag.Message == DR.Message)
-return Diag.Fixes;
-return {};
+return &Diag;
+return nullptr;
   };
-  for (const auto &Diag : Params.Diagnostics)
-for (const auto &Fix : FindMatchedFixes(Diag))
-  Result.QuickFixes.push_back({Diag, Fix});
+  for (const auto &DiagRef : Params.Diagnostics) {
+if (const auto *Diag = FindMatchedDiag(DiagRef))
+  for (const auto 

[clang-tools-extra] [clangd] forward clang-tidy's readability-identifier-naming fix to textDocument/rename (PR #78454)

2024-01-17 Thread via cfe-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff 837cde8aa15b0eb17aefc24530e82e25000d9fe0 
71619f8dc07ca038b8e2e30d89f308c1e38a0726 -- 
clang-tools-extra/clangd/ClangdLSPServer.cpp 
clang-tools-extra/clangd/ClangdLSPServer.h 
clang-tools-extra/clangd/ClangdServer.cpp 
clang-tools-extra/clangd/ClangdServer.h clang-tools-extra/clangd/Protocol.cpp 
clang-tools-extra/clangd/Protocol.h
``





View the diff from clang-format here.


``diff
diff --git a/clang-tools-extra/clangd/ClangdLSPServer.cpp 
b/clang-tools-extra/clangd/ClangdLSPServer.cpp
index ffb241b02b..49d1eb0e83 100644
--- a/clang-tools-extra/clangd/ClangdLSPServer.cpp
+++ b/clang-tools-extra/clangd/ClangdLSPServer.cpp
@@ -825,12 +825,14 @@ void ClangdLSPServer::onCommandApplyTweak(const TweakArgs 
&Args,
  std::move(Action));
 }
 
-void ClangdLSPServer::onCommandApplyRename(const RenameParams &R, 
Callback Reply) {
-onRename(R, [this, Reply = std::move(Reply)](llvm::Expected 
Edit) mutable {
-if (!Edit)
-Reply(Edit.takeError());
-applyEdit(std::move(*Edit), "Rename applied.", std::move(Reply));
-});
+void ClangdLSPServer::onCommandApplyRename(const RenameParams &R,
+   Callback Reply) {
+  onRename(R, [this, Reply = std::move(Reply)](
+  llvm::Expected Edit) mutable {
+if (!Edit)
+  Reply(Edit.takeError());
+applyEdit(std::move(*Edit), "Rename applied.", std::move(Reply));
+  });
 }
 
 void ClangdLSPServer::applyEdit(WorkspaceEdit WE, llvm::json::Value Success,
diff --git a/clang-tools-extra/clangd/Protocol.cpp 
b/clang-tools-extra/clangd/Protocol.cpp
index 2b44f5c284..7cc248cd1d 100644
--- a/clang-tools-extra/clangd/Protocol.cpp
+++ b/clang-tools-extra/clangd/Protocol.cpp
@@ -1188,11 +1188,11 @@ bool fromJSON(const llvm::json::Value &Params, 
RenameParams &R,
 }
 
 llvm::json::Value toJSON(const RenameParams &R) {
-return llvm::json::Object{
-{"textDocument", R.textDocument},
-{"position", R.position},
-{"newName", R.newName},
-};
+  return llvm::json::Object{
+  {"textDocument", R.textDocument},
+  {"position", R.position},
+  {"newName", R.newName},
+  };
 }
 
 llvm::json::Value toJSON(const DocumentHighlight &DH) {

``




https://github.com/llvm/llvm-project/pull/78454
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] forward clang-tidy's readability-identifier-naming fix to textDocument/rename (PR #78454)

2024-01-17 Thread Tom Praschan via cfe-commits

https://github.com/tom-anders created 
https://github.com/llvm/llvm-project/pull/78454

None

>From 71619f8dc07ca038b8e2e30d89f308c1e38a0726 Mon Sep 17 00:00:00 2001
From: tom-anders <13141438+tom-and...@users.noreply.github.com>
Date: Wed, 17 Jan 2024 15:36:02 +0100
Subject: [PATCH] [clangd] forward clang-tidy's readability-identifier-naming
 fix to textDocument/rename

---
 clang-tools-extra/clangd/ClangdLSPServer.cpp  | 30 +++
 clang-tools-extra/clangd/ClangdLSPServer.h|  1 +
 clang-tools-extra/clangd/ClangdServer.cpp | 27 -
 clang-tools-extra/clangd/ClangdServer.h   |  5 
 clang-tools-extra/clangd/Protocol.cpp |  8 +
 clang-tools-extra/clangd/Protocol.h   |  1 +
 .../clangd/test/initialize-params.test|  1 +
 7 files changed, 66 insertions(+), 7 deletions(-)

diff --git a/clang-tools-extra/clangd/ClangdLSPServer.cpp 
b/clang-tools-extra/clangd/ClangdLSPServer.cpp
index a87da252b7a7e9b..ffb241b02b99673 100644
--- a/clang-tools-extra/clangd/ClangdLSPServer.cpp
+++ b/clang-tools-extra/clangd/ClangdLSPServer.cpp
@@ -73,6 +73,23 @@ std::optional decodeVersion(llvm::StringRef 
Encoded) {
 
 const llvm::StringLiteral ApplyFixCommand = "clangd.applyFix";
 const llvm::StringLiteral ApplyTweakCommand = "clangd.applyTweak";
+const llvm::StringLiteral ApplyRenameCommand = "clangd.applyRename";
+
+CodeAction toCodeAction(const ClangdServer::CodeActionResult::Rename &R,
+const URIForFile &File) {
+  CodeAction CA;
+  CA.title = R.Diag.Message;
+  CA.kind = std::string(CodeAction::REFACTOR_KIND);
+  CA.command.emplace();
+  CA.command->title = R.Diag.Message;
+  CA.command->command = std::string(ApplyRenameCommand);
+  RenameParams Params;
+  Params.textDocument = TextDocumentIdentifier{File};
+  Params.position = R.Diag.Range.start;
+  Params.newName = R.NewName;
+  CA.command->argument = Params;
+  return CA;
+}
 
 /// Transforms a tweak into a code action that would apply it if executed.
 /// EXPECTS: T.prepare() was called and returned true.
@@ -808,6 +825,14 @@ void ClangdLSPServer::onCommandApplyTweak(const TweakArgs 
&Args,
  std::move(Action));
 }
 
+void ClangdLSPServer::onCommandApplyRename(const RenameParams &R, 
Callback Reply) {
+onRename(R, [this, Reply = std::move(Reply)](llvm::Expected 
Edit) mutable {
+if (!Edit)
+Reply(Edit.takeError());
+applyEdit(std::move(*Edit), "Rename applied.", std::move(Reply));
+});
+}
+
 void ClangdLSPServer::applyEdit(WorkspaceEdit WE, llvm::json::Value Success,
 Callback Reply) {
   ApplyWorkspaceEditParams Edit;
@@ -1043,6 +1068,10 @@ void ClangdLSPServer::onCodeAction(const 
CodeActionParams &Params,
 CAs.back().diagnostics = {It->second};
   }
 }
+
+for (const auto &R : Fixits->Renames)
+  CAs.push_back(toCodeAction(R, File));
+
 for (const auto &TR : Fixits->TweakRefs)
   CAs.push_back(toCodeAction(TR, File, Selection));
 
@@ -1664,6 +1693,7 @@ void ClangdLSPServer::bindMethods(LSPBinder &Bind,
   Bind.method("textDocument/foldingRange", this, 
&ClangdLSPServer::onFoldingRange);
   Bind.command(ApplyFixCommand, this, &ClangdLSPServer::onCommandApplyEdit);
   Bind.command(ApplyTweakCommand, this, &ClangdLSPServer::onCommandApplyTweak);
+  Bind.command(ApplyRenameCommand, this, 
&ClangdLSPServer::onCommandApplyRename);
 
   ApplyWorkspaceEdit = Bind.outgoingMethod("workspace/applyEdit");
   PublishDiagnostics = 
Bind.outgoingNotification("textDocument/publishDiagnostics");
diff --git a/clang-tools-extra/clangd/ClangdLSPServer.h 
b/clang-tools-extra/clangd/ClangdLSPServer.h
index 79579c22b788a9c..69a349c6a5e087b 100644
--- a/clang-tools-extra/clangd/ClangdLSPServer.h
+++ b/clang-tools-extra/clangd/ClangdLSPServer.h
@@ -174,6 +174,7 @@ class ClangdLSPServer : private ClangdServer::Callbacks,
   /// Implement commands.
   void onCommandApplyEdit(const WorkspaceEdit &, Callback);
   void onCommandApplyTweak(const TweakArgs &, Callback);
+  void onCommandApplyRename(const RenameParams &, Callback);
 
   /// Outgoing LSP calls.
   LSPBinder::OutgoingMethodAST.version().str();
 if (KindAllowed(CodeAction::QUICKFIX_KIND)) {
-  auto FindMatchedFixes =
-  [&InpAST](const DiagRef &DR) -> llvm::ArrayRef {
+  auto FindMatchedDiag = [&InpAST](const DiagRef &DR) -> const Diag * {
 for (const auto &Diag : InpAST->AST.getDiagnostics())
   if (Diag.Range == DR.Range && Diag.Message == DR.Message)
-return Diag.Fixes;
-return {};
+return &Diag;
+return nullptr;
   };
-  for (const auto &Diag : Params.Diagnostics)
-for (const auto &Fix : FindMatchedFixes(Diag))
-  Result.QuickFixes.push_back({Diag, Fix});
+  for (const auto &DiagRef : Params.Diagnostics) {
+if (const auto *Diag = FindMatchedDiag(DiagRef))
+  for (const auto &Fix : Diag->Fixes) {
+