This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Abort rename when given the same name
ClosedPublic

Authored by kbobyrev on Nov 10 2020, 1:28 AM.

Details

Summary

When user wants to rename the symbol to the same name we shouldn't do any work.
Bail out early and return error to save compute.

Resolves: https://github.com/clangd/clangd/issues/580

Diff Detail

Event Timeline

kbobyrev created this revision.Nov 10 2020, 1:28 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 10 2020, 1:28 AM
kbobyrev requested review of this revision.Nov 10 2020, 1:28 AM
hokein added inline comments.Nov 10 2020, 6:36 AM
clang-tools-extra/clangd/refactor/Rename.cpp
218

returning an error seems to be an interesting idea, thinking more about that, it might be better than just returning an empty workspaceEdit silently without noticing user.

clang-tools-extra/clangd/unittests/RenameTests.cpp
907

this test is for prepareRename, I think we should put it to TEST(RenameTest, Renameable).

sammccall added inline comments.Nov 10 2020, 9:55 AM
clang-tools-extra/clangd/refactor/Rename.cpp
218

We don't know what the user's intent was here - it's at least somewhat likely they changed their mind about the operation but hit "enter" instead of "escape". So a message describing the situation "new name is the same as the old name" would be more appropriate than suggesting a correction.

But I'm wondering whether this error message actually helps (vs "succeeding" with no edits). Is it actionable? What's the scenario where the user:
a) doesn't already know that the name is the same, and
b) will take some action as a result (rather than leave the name unchanged)

hokein added inline comments.Nov 10 2020, 10:57 AM
clang-tools-extra/clangd/refactor/Rename.cpp
218

a message describing the situation "new name is the same as the old name" would be more appropriate than suggesting a correction.

SG.

In an ideal world, I'd expect we emit a non-error message to inform that there is nothing happen for the rename, but LSP doesn't have such option :(

The behavior of editors are varied on this case, e.g.

  • VSCode just succeeds with no edits;
  • our internal editor emits a message like "nothing to rename";
kbobyrev updated this revision to Diff 304423.Nov 11 2020, 12:56 AM
kbobyrev marked an inline comment as done.
  • Change error message to description rather than suggestion.
  • Move test to more appropriate location.
kbobyrev marked an inline comment as not done.Nov 11 2020, 1:01 AM
kbobyrev added inline comments.
clang-tools-extra/clangd/refactor/Rename.cpp
218

But I'm wondering whether this error message actually helps (vs "succeeding" with no edits). Is it actionable? What's the scenario where the user:
a) doesn't already know that the name is the same, and
b) will take some action as a result (rather than leave the name unchanged)

I can understand this point of view, but I'm a bit sceptical of the "fail" silently approach. The "actionable" for user would be to not type in the same name as before :)

Also, user might wanted to rename the symbol to something slightly different (e.g. fix typo) and then have a typo in new name, too. I think there is value in telling the user that there was no rename because the name is the same - I don't think it hurts them in any way. We're just being explicit in what happened/didn't happen. I think "new name = old name" can be a reasonable "error" scenario and I think there is a definite value in being explicit about what we do or decide not to do.

My point is that I can't see a scenario when user actually wants to rename the symbol deliberately using the same name and I think this should, in fact, be an error to do so.

hokein accepted this revision.Nov 11 2020, 1:11 AM
hokein added inline comments.
clang-tools-extra/clangd/unittests/RenameTests.cpp
780

Add a comment or rename Var => SameName, to make the "new_name == old_name" more explicitly.

This revision is now accepted and ready to land.Nov 11 2020, 1:11 AM
kbobyrev updated this revision to Diff 304434.Nov 11 2020, 2:13 AM

Add a comment to the test to make it more explicit.

This revision was landed with ongoing or failed builds.Nov 11 2020, 2:14 AM
This revision was automatically updated to reflect the committed changes.
kbobyrev marked an inline comment as done.Nov 11 2020, 2:15 AM