This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Add BlockEnd comments for control flow statements
ClosedPublic

Authored by sammccall on Jul 16 2023, 11:13 PM.

Details

Summary

These mark the end of CompoundStmts bodies of if/while/for/switch.
To identify which statement is being ended, we include abbreviated
text of the condition/loop variable.

Looks like this:

Diff Detail

Event Timeline

sammccall created this revision.Jul 16 2023, 11:13 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 16 2023, 11:13 PM
sammccall requested review of this revision.Jul 16 2023, 11:13 PM
sammccall edited the summary of this revision. (Show Details)Jul 16 2023, 11:14 PM
hokein accepted this revision.Jul 21 2023, 12:14 AM

Thanks, this looks like in a good shape. I left comments with some thoughts and nits, but they're not blockers, feel free to land it.

clang-tools-extra/clangd/InlayHints.cpp
252

This looks like a complicated implementation to get an abbreviated-form of cond expression for the inlay label text.

A different idea would be if the user can just click the inlay label text, then the editor jumps to the corresponding for statement (looks like it is supported with the InlayHintLabelPart::Location) with that we probably don't need to emit an abbreviated-form text (// for should be clear enough).

583

my opinion for this case would be (the marking result is also consistent with the brackets)

if (cond1) {
} // mark as cond1
else if (cond2) {
} // mark as cond2.
604

it looks like we will mark the block end for single-statement CompoundStmt without {}, it doesn't seem to add much value to this case (the body is short enough to spot the for statement).

for (int i = 0; i < 10; ++i)
  foo();
clang-tools-extra/clangd/unittests/InlayHintTests.cpp
1786

nit: add a case if (int i = 0; i > 10) { ... }.

1859

nit: I guess showing foo... is better.

This revision is now accepted and ready to land.Jul 21 2023, 12:14 AM
sammccall marked 3 inline comments as done.Jul 21 2023, 1:54 PM
sammccall added inline comments.
clang-tools-extra/clangd/InlayHints.cpp
252

A different idea would be if the user can just click the inlay label text, then the editor jumps to the corresponding for statement (looks like it is supported with the InlayHintLabelPart::Location) with that we probably don't need to emit an abbreviated-form text (// for should be clear enough).

Agree we should have this too (also for other kinds of hints).

But I don't think it's a replacement:

  • BlockEnd is most useful to connect blockend + blockstart context when the block start is far away, and navigating to it destroys your blockend context. (Hover is better in this regard but still isn't available *in-line*).
  • inlayhintlabelpart isn't available in most editors that support inlayhint, and realistically may never be
583

If the first if is } is on a separate line we could do this.

The styles we work the most place } else on one line though, so this isn't an option.
In those styles we see if/elseif/else as a single construct (and format it as such) - and marking as cond2 seems strange to me...

604

As discussed offline, CompoundStmt exists exactly where braces exist.

This revision was landed with ongoing or failed builds.Jul 21 2023, 1:57 PM
This revision was automatically updated to reflect the committed changes.
sammccall marked an inline comment as done.