16
votes

Is it possible to tell Clang-Format to ignore comments for line break operations? The idea is to follow the style "the code is well formatted, even if comments exceed the line break margin". Code should not be split in multiple lines, if it does not exceed the margin, but the comment does.

e.g.

//desired behaviour:
short code = shortCode + 
        longlonglongCode;
short code = shortCode; //long comment without a line break

//not desired behaviour:
short code =
    shortCode;  //long comment without a line break
1
Did you ever found a solution for this?XKpe
@XKpe unfortunately not. The only thing I found useful is to do clang-format off and on again in imortant sections, but it's quiet a bad workaroundyar

1 Answers

10
votes

ReflowComments: (bool)

If true, clang-format will attempt to re-flow comments.

false:
// veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of information
/* second veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of information */

true:
// veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of
// information
/* second veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of
 * information */

Source

Use ReflowComments: true, when enabled it will add 1 space after // and brake comments when reach limit.

You can use PenaltyBreakComment to adjust.
E.g. block braking lines PenaltyBreakComment: 1000000000