Skip to content

clang-format option to indent empty lines to same level as surrounding lines #18815

@llvmbot

Description

@llvmbot
Bugzilla Link 18441
Version trunk
OS All
Reporter LLVM Bugzilla Contributor
CC @kevincox

Extended Description

I'd love to see an option to clang-format that would allow blank lines to be indented to the same level as the lines surrounding them (at the same scope). Currently clang-format will always remove all indentation from otherwise empty lines. This interferes with text editing by requiring manual insertion of tab/indentation characters when later editing the code to add additional lines.

Take this simple example:

int foo(int a)
{
    int b = a + 2;

    return b + 2;
}

When typing this code initially many editors will automatically add indentation characters on the empty line to match the indentation of the line above it. clang-format currently strips out this indentation. I later decide to change this function to:

int foo(int a)
{
    int b = a + 2;
    b++;
    return b + 2;
}

If the indentation of the blank line still matches the indentation of the line above it I can just click the mouse on the blank line and type the code. If the indentation is stripped I have to manually indent the new line of code. I'd really prefer to just keep the indentation characters automatically inserted by my text editor there always.

I'd like to see an option to clang-format to allow these blank lines to maintain their indentation. My team and I find indented blank lines more convenient to work with. Additionally, such an option would avoid thousands of unnecessary changes/diffs if we ran our entire (fairly large) source code base through clang-format.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillaclang-formatenhancementImproving things as opposed to bug fixing, e.g. new or missing feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions