Skip to content

[clang-format] Fix regressions in BAS_AlwaysBreak #107506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 12, 2024
Merged

Conversation

owenca
Copy link
Contributor

@owenca owenca commented Sep 6, 2024

Fixes #107401.
Fixes #107574.

@llvmbot
Copy link
Member

llvmbot commented Sep 6, 2024

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

Changes

Fixes #107401.


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

2 Files Affected:

  • (modified) clang/lib/Format/ContinuationIndenter.cpp (+1-1)
  • (modified) clang/unittests/Format/FormatTestJS.cpp (+8)
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 5843571718b3a2..f65c1640a8765a 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -861,7 +861,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
       //  or
       //  caaaaaaaaaaaaaaaaaaaaal(
       //       new SomethingElseeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee());
-      !IsSimpleFunction(Current)) {
+      Current.isNot(tok::comment) && !IsSimpleFunction(Current)) {
     CurrentState.NoLineBreak = true;
   }
 
diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp
index 4b29ba720f6823..a0b663170c7b33 100644
--- a/clang/unittests/Format/FormatTestJS.cpp
+++ b/clang/unittests/Format/FormatTestJS.cpp
@@ -2850,5 +2850,13 @@ TEST_F(FormatTestJS, DontBreakFieldsAsGoToLabels) {
                "};");
 }
 
+TEST_F(FormatTestJS, BreakAfterOpenBracket) {
+  auto Style = getGoogleStyle(FormatStyle::LK_JavaScript);
+  EXPECT_EQ(Style.AlignAfterOpenBracket, FormatStyle::BAS_AlwaysBreak);
+  verifyFormat("ctrl.onCopy(/** @type {!WizEvent}*/ (\n"
+               "    {event, targetElement: {el: () => selectedElement}}));",
+               Style);
+}
+
 } // namespace format
 } // end namespace clang

@@ -861,7 +861,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
// or
// caaaaaaaaaaaaaaaaaaaaal(
// new SomethingElseeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee());
!IsSimpleFunction(Current)) {
Current.isNot(tok::comment) && !IsSimpleFunction(Current)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it make sense to introduce this into IsSimpleFunction ? similar to handling of new keyword in there? possibly just skipping over all of the comments. because we can also have foo(/*comments*/ new X()), and i think we still want that inner expression to be treated simple

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it make sense to introduce this into IsSimpleFunction ? similar to handling of new keyword in there?

I did that at first but thought it would be confusing to say that a comment is a simple function (or function call).

@kadircet
Copy link
Member

kadircet commented Sep 6, 2024

i've also found 3 more cases that point back to same patch in #107574, in case you want to address all of them with this change

… next

token if the argument is a comment.
@owenca
Copy link
Contributor Author

owenca commented Sep 7, 2024

i've also found 3 more cases that point back to same patch in #107574, in case you want to address all of them with this change

I'd rather make incremental changes. Maybe you or @gedare can give them a try.

@owenca owenca changed the title [clang-format] Fix a regression on BAS_AlwaysBreak [clang-format] Fix regressions in BAS_AlwaysBreak Sep 8, 2024
@gedare
Copy link
Contributor

gedare commented Sep 9, 2024

LGTM, I like the renaming of the lambda. I think this better captures the original author's intent about "simple functions."

@owenca owenca merged commit 8168088 into llvm:main Sep 12, 2024
8 checks passed
@owenca owenca deleted the 107401 branch September 12, 2024 02:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Different spacing for nested function calls Different formatting around type annotations with JS
5 participants