Skip to content

Add option to trim last empty line#120

Open
jreybert wants to merge 1 commit into
rhysd:masterfrom
jreybert:dev/jreybert-trim/master
Open

Add option to trim last empty line#120
jreybert wants to merge 1 commit into
rhysd:masterfrom
jreybert:dev/jreybert-trim/master

Conversation

@jreybert

Copy link
Copy Markdown
Contributor

This new feature fixes a behavior difference between vim-clang-format
and clang-format.

If the 'to be formatted' buffer ends with one or several empty lines,
vim-clang-format keeps one final empty line. clang-format does not let
any empty line.

If the 'to be formatted' buffer ends with a non empty line, both
vim-clang-format and clang-format produce the same result, a code
finishing with the non empty line.

This problem seems to have been a problem for other clang plugin, in
other editors, eg in VisualCode:
https://stackoverflow.com/questions/55374304/add-a-single-empty-line-as-the-last-line-in-the-code-clang-format

The culprit in vim-clang-format is the split() function: as the
formatted string ends with a newline, it generates a List with a last
empty item.

This new feature fixes a behavior difference between vim-clang-format
and clang-format.

If the 'to be formatted' buffer ends with one or several empty lines,
vim-clang-format keeps one final empty line. clang-format does not let
any empty line.

If the 'to be formatted' buffer ends with a non empty line, both
vim-clang-format and clang-format produce the same result, a code
finishing with the non empty line.

This problem seems to have been a problem for other clang plugin, in
other editors, eg in VisualCode:
https://stackoverflow.com/questions/55374304/add-a-single-empty-line-as-the-last-line-in-the-code-clang-format

The culprit in vim-clang-format is the `split()` function: as the
`formatted` string ends with a newline, it generates a List with a last
empty item.
@rhysd

rhysd commented Feb 3, 2022

Copy link
Copy Markdown
Owner

Thank you for making this patch.

If the 'to be formatted' buffer ends with one or several empty lines,
vim-clang-format keeps one final empty line. clang-format does not let
any empty line.

It means, output of clang-format does not contain extra empty line at the end but vim-clang-format adds it, am I correct? It would be a bug of vim-clang-format. So fixing it rather than adding a new option would be better.

@jreybert

jreybert commented Feb 3, 2022

Copy link
Copy Markdown
Contributor Author

It means, output of clang-format does not contain extra empty line at the end but vim-clang-format adds it, am I correct? It would be a bug of vim-clang-format. So fixing it rather than adding a new option would be better.

You are correct. I made an option to not break existing behavior. But if you want to merge this fix without option, I totally agree too. Tell me what you prefer.

Anyway, if you're OK with this fix, I will take time to add one or few tests about empty lines.

@jreybert

jreybert commented Apr 4, 2022

Copy link
Copy Markdown
Contributor Author

Hi @rhysd , let me know what you want to do with this patch.

@jreybert

Copy link
Copy Markdown
Contributor Author

ping, any news?

@christf

christf commented Feb 3, 2026

Copy link
Copy Markdown

I just ran into this (ci breaking builds of my submitted code because of an extra empty line after using the plugin). Any chance in upstreaming one of the suggested changes either from here or #127?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants