Skip to content

[clang-format] Feature request: ability to break after ( / before ) in control flow statements #67738

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

Open
emilyy-dev opened this issue Sep 28, 2023 · 3 comments · May be fixed by #108332
Open

Comments

@emilyy-dev
Copy link

After searching the clang-format style options webpage, I found no option to break after an opening parenthesis or before a closing one in control flow statements (if/for/while/do while), I was hoping to be able to have a format like this

for (
    someReallyLongInit;
    someReallyLongPredicate;
    someReallyLongStep
) {
  // ...
}

if (
    someReallyLongPredicate
) {
  // ...
}

// etc.

The closest setting I could find was AlignAfterOpenBracket with BlockIndent, but that only applies to function invocations, not control flow statements (the documentation does not mention anything about it being specific to function calls, it might be worth noting that).

This formatting setting is available in IntelliJ IDEs (which I have used, hence me hoping I could find it in clang-format!), under File | Settings | Editor | Code Style | [language] | Wrapping and Braces, it has separate settings for for/if/while/do while statements (and for Java also has one for the try-with-resources statement) allowing to "Do not wrap", "Wrap if long", "Chop down if long" and "Wrap always" which control the behaviour of the parentheses and also breaking on the init/cond/iter step in the case of for, or breaking on &&s and ||s in an if/while.

@rymiel rymiel added the enhancement Improving things as opposed to bug fixing, e.g. new or missing feature label Sep 28, 2023
@DoDoENT
Copy link
Contributor

DoDoENT commented Dec 21, 2023

I would also be interested in option that allows this style:

for 
(
    someReallyLongInit;
    someReallyLongPredicate;
    someReallyLongStep
) 
{
    // ...
}

if 
(
    someReallyLongPredicate
) 
{
    // ...
}

// etc.

I haven't seen any options for breaking the line before ( or ), neither for functions nor for control statements. Is such thing feasible to implement?

@gedare
Copy link
Contributor

gedare commented May 19, 2024

The closest setting I could find was AlignAfterOpenBracket with BlockIndent, but that only applies to function invocations, not control flow statements (the documentation does not mention anything about it being specific to function calls, it might be worth noting that).

if() statements are supposed to work, and now they should:
#77699

for() loops have not worked.

@gedare
Copy link
Contributor

gedare commented May 24, 2024

This behavior is kind of like #62963

It may be possible to add something like that for conditionals. Although that breaking of the function is not based on spilling over the line length.

gedare added a commit to gedare/llvm-project that referenced this issue Sep 23, 2024
Introduce new options to allow for control of AlwaysBreak and
BlockIndent selectively for If conditional statements (as currently
supported), other conditional statements (for/while/switch), and
other statements.

Fixes llvm#67738.
Fixes llvm#79176.
Fixes llvm#80123.
gedare added a commit to gedare/llvm-project that referenced this issue Oct 18, 2024
Introduce new options to allow for control of AlwaysBreak and
BlockIndent selectively for If conditional statements (as currently
supported), other conditional statements (for/while/switch), and
other statements.

Fixes llvm#67738.
Fixes llvm#79176.
Fixes llvm#80123.
gedare added a commit to gedare/llvm-project that referenced this issue Dec 2, 2024
Introduce new options to allow for control of AlwaysBreak and
BlockIndent selectively for If conditional statements (as currently
supported), other conditional statements (for/while/switch), and
other statements.

Fixes llvm#67738.
Fixes llvm#79176.
Fixes llvm#80123.
gedare added a commit to gedare/llvm-project that referenced this issue Jan 28, 2025
Introduce new options to allow for control of AlwaysBreak and
BlockIndent selectively for If conditional statements (as currently
supported), other conditional statements (for/while/switch), and
other statements.

Fixes llvm#67738.
Fixes llvm#79176.
Fixes llvm#80123.
gedare added a commit to gedare/llvm-project that referenced this issue Jan 29, 2025
Introduce new options to allow for control of AlwaysBreak and
BlockIndent selectively for If conditional statements (as currently
supported), other conditional statements (for/while/switch), and
other statements.

Fixes llvm#67738.
Fixes llvm#79176.
Fixes llvm#80123.
gedare added a commit to gedare/llvm-project that referenced this issue Feb 9, 2025
Introduce new options to allow for control of AlwaysBreak and
BlockIndent selectively for If conditional statements (as currently
supported), other conditional statements (for/while/switch), and
other statements.

Fixes llvm#67738.
Fixes llvm#79176.
Fixes llvm#80123.
@owenca owenca removed the enhancement Improving things as opposed to bug fixing, e.g. new or missing feature label Feb 16, 2025
gedare added a commit to gedare/llvm-project that referenced this issue Apr 30, 2025
Introduce new style option to allow overriding the breaking after the
opening parenthesis for control statements (if/for/while/switch).

Fixes llvm#67738.
Fixes llvm#79176.
Fixes llvm#80123.
gedare added a commit to gedare/llvm-project that referenced this issue May 1, 2025
Introduce new style option to allow overriding the breaking after the
opening parenthesis for control statements (if/for/while/switch).

Fixes llvm#67738.
Fixes llvm#79176.
Fixes llvm#80123.
gedare added a commit to gedare/llvm-project that referenced this issue May 12, 2025
Introduce new options to allow for control of AlwaysBreak and
BlockIndent selectively for If conditional statements (as currently
supported), other conditional statements (for/while/switch), and
other statements.

Fixes llvm#67738.
Fixes llvm#79176.
Fixes llvm#80123.
gedare added a commit to gedare/llvm-project that referenced this issue May 22, 2025
Introduce new style option to allow overriding the breaking after the
opening parenthesis for control statements (if/for/while/switch).

Fixes llvm#67738.
Fixes llvm#79176.
Fixes llvm#80123.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants