This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Introduce source.organizeImports code action.
Needs ReviewPublic

Authored by hokein on Jan 31 2023, 2:15 AM.
This revision needs review, but all reviewers have resigned.

Details

Reviewers
kadircet
Summary

This would provide us a way to apply unused-includes fixts at once via
the right-click "Source Action" menu or "Organize imports" command
in VSCode.

Diff Detail

Event Timeline

hokein created this revision.Jan 31 2023, 2:15 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 31 2023, 2:15 AM
Herald added a subscriber: arphaman. · View Herald Transcript
hokein requested review of this revision.Jan 31 2023, 2:15 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 31 2023, 2:15 AM
nridge added a subscriber: nridge.Feb 4 2023, 7:05 PM
hokein updated this revision to Diff 517948.Apr 28 2023, 9:04 AM

rebase and polish the implementation.

kadircet added inline comments.May 1 2023, 7:02 AM
clang-tools-extra/clangd/ClangdLSPServer.cpp
1035

instead of doing this in here, what about introducing a new "tweak" that'll perform include-cleaner fixes?
Pros are:

  • We can use it in places that don't use LSPServer.
  • We can later on extend "organize imports" behaviour to do other things if needed.
  • Results will be always up-to-date, as we can use the AST and compute the fixes. rather than making use of the "latest" cached version.
  • Implementation would be a little bit neater (and contained), as we can just re-use the existing components in IncludeCleaner, rather than doing some ad-hoc matching & retrieval.

WDYT?

1072–1074

what does the UI look like when there are multiple code actions with "organize imports" kind?
e.g. 1&2 can actually be applied together, but 3rd one can't be combined with others, and in theory these can also be triggered on save (if the user opts in). so I am not sure if having multiple code actions, especially when they can't be merged together, really makes much sense on the editor side.

hokein added inline comments.May 2 2023, 1:22 AM
clang-tools-extra/clangd/ClangdLSPServer.cpp
1035

I think it is a matter of how we view it (whether it is a fix for all diagnostics or a tweak).

but +1, yeah, this looks like a good idea to me.

1072–1074

what does the UI look like when there are multiple code actions with "organize imports" kind?

Similar to the normal code action, if you trigger it via the right context menu (SourceAction), a window with 3 alternatives will pop up, so that you can select one of them (verified on VSCode).

e.g. 1&2 can actually be applied together, but 3rd one can't be combined with others, and in theory these can also be triggered on save (if the user opts in). so I am not sure if having multiple code actions, especially when they can't be merged together, really makes much sense on the editor side.

Good point. The VSocde codeActionsOnSave behavior is like what you said -- it applies each fix one by one (because we trigger preview window per fix, so 3 preview windows will be triggered in sequence).

I don't see the best way to fix it (from the codeAction requests are identical from the one sent via the context menu), one option is to send only a fixAll codeAction rather than 3.

kadircet resigned from this revision.Jul 19 2023, 6:52 AM

resigning in favor of https://reviews.llvm.org/D153769