Skip to content

vim adds extra newlines when option 'InsertNewlineAtEOF' is set in clang-format #127

@loebl

Description

@loebl
  • OS: Ubuntu 22.04
  • vim version: VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Sep 25 2024 05:30:01) (package vim-gtk3, version: 2:8.2.3995-1ubuntu2.21)
  • Included patches: 1-16, 647, 17-579, 1969, 580-647, 678, 648-1848, 4975, 5016, 5023, 5072, 2068, 1849-1854, 1857, 1855-1857, 1331, 1858, 1858-1859, 1873, 1860-1969, 1992, 1970-1992, 2010, 1993-2068, 2106, 2069-2106, 2108, 2107-2109, 2109
    -3995, 4563, 4646, 4774, 4895, 4899, 4901, 4919, 213, 1840, 1846-1847, 2110-2112, 2121
  • clang-format: v16.0.6

Observation

When using the the option 'InsertNewlineAtEOF: true' clang-format returns a string that will have a newline followed by a space, compared to no newline at then end when it is switched off. This newline will cause an empty line at the end of the file, which is unwanted.

Proposed fix

I am not well versed in Vimscript, so my current approach is to trim all outputs returned in the system function:

@@ -39,6 +39,9 @@ function! s:system(str, ...) abort
                     \ vimproc#system(command, input, a:2) : system(command, input)
     endif
 
+    " if clang-format option InsertNewlineAtEOF is set, there is a newline + space at the end
+    " of the output, which we don't want as this leads to additional newlines in vim
+    let output = trim(output, " \n", 2)
     return output
 endfunction

An alternative would be to only trim the output in the format_ranges function, as system is used in other places as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions