Line breaks between function definitions

11.3k Views Asked by At

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.

4

There are 4 best solutions below

0
On

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.

4
On

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.

0
On

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

1
On
SeparateDefinitionBlocks: Always
EmptyLineBeforeAccessModifier: LogicalBlock

above two options will solve your question