Skip to content

[clang-format][feature request] Ternary support? #156210

@DiogoHSS

Description

@DiogoHSS

When using clang-format I found that the lacking of ternary operator styling options kinda of a bummer.

Ternary operators just format as the following code:

bool test = 10 > 6 ? true : false;

Support for the following sub-options under a top level options like Break Ternary Operator would be appreaciated

Break Before Operators

bool test = 10 > 6
    ? true
    : false;

Break Before Condition Operator Only

bool test = 10 > 6
    ? true : false;

Break After Operators

bool test = 10 > 6 ?
    true :
    false;

Break After Condition Operator Only

bool test = 10 > 6 ?
    true : false;

New Option

Break Semicolon when Enable Ternary Operator Break is true

bool test = 10 > 6 ?
    true :
    false
;

New Option

Wrap Ternary In Parenthesis, no idea about the value names for the following options

bool test = (10 > 6 ?
    true :
    false);
bool test =
    (10 > 6
        ? true
        : false);
bool test = (
    10 > 6
        ? true
        : false
);

Also, create a AlignTernaryOperands aside from AlignOperands to achieve:

bool test =
    10 > 6
    ? true
    : false;
}

This is the styling I go for personally

bool test = (
    10 > 6
        ? (
            1 >= 0
                ? true
                : false
        )
        : false
);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions