52

Is there any way to to automatically insert spaces between function definitions. E.g. my initial sources are:

void
func1()
{
    // func1 body.
}
void
func2()
{
    // func2 body.
}

I would like it to be reformatted to:

void
func1()
{
    // func1 body.
}


void
func2()
{
    // func2 body.
}

And if there are more line breaks, fixed number of them should be kept.

PovilasB
  • 1,488
  • 1
  • 15
  • 25
  • 2
    They really need to add this and the ability to insert blank lines in other obvious spots too (e.g. - after variable definitions). – jschultz410 Mar 05 '21 at 22:46
  • Possible in clang-format 14 - see: https://stackoverflow.com/questions/70732683/how-to-add-blank-lines-between-definitions/70732858 – dluco Nov 03 '22 at 02:33

4 Answers4

41

As far as I can tell, there's currently no way to force clang-format to insert blank lines between consecutive functions where there currently are none. IMHO this is a huge missing feature.

Keith F. Kelly
  • 418
  • 4
  • 3
21

Your best bet is to set 'MaxEmptyLinesToKeep: 2' inside .clang-format file to let clang-format keep 2 lines intact.

Nuray Altin
  • 1,294
  • 12
  • 19
15

As mentioned in this answer with clang-format 14, you can use the following in your config file:

SeparateDefinitionBlocks: Always

The other possible values are Leave, to leave the spacing of definition blocks as-is, or Never, to remove empty lines between definition blocks.

dluco
  • 113
  • 4
ejalaa12
  • 375
  • 3
  • 12
0
SeparateDefinitionBlocks: Always
EmptyLineBeforeAccessModifier: LogicalBlock

above two options will solve your question

George Ma
  • 11
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 27 '23 at 17:04