google / vim-codefmt

Vim plugin for syntax-aware code formatting
Apache License 2.0
1.11k stars 114 forks source link

FormatCode adds line filled with spaces when run on .cc file #217

Open jeroen-dhollander opened 1 year ago

jeroen-dhollander commented 1 year ago

Description

Running :FormatCode on a cpp source (not header) file causes a trailing line filled with whitespaces to appear at the end of the document. Running :FormatCode again and again removes and re-adds and removes and ... this line.

Steps to reproduce

1) Use this init.vim file:

call plug#begin('~/.local/share/nvim/plugged-minimal')
Plug 'google/vim-maktaba' " Needed for vim-codefmt
Plug 'google/vim-glaive' " Needed for vim-codefmt
Plug 'google/vim-codefmt' " Code formatting
call plug#end()

2) Open any .cc file (for example I use base/run_loop.cc from the chromium project

3) Run :FormatCode. Observe line with trailing whitespaces at the end of the file 4) Run :FormatCode. Observe line with trailing whitespaces at the end of the file is gone. 5) Repeat

My Setup

─❯ nvim --version
NVIM v0.7.2
╰─❯ clang-format --version
clang-format version google3-trunk (30af2fb33ed2f610abfa50e53df9712887b2bd25)

Gif of the bug

bug

Other things I tried

Please let me know if you need any more information.

Thanks!

dbarnett commented 1 year ago

Can you confirm if clang-format is the actual formatter being used here? Tab complete after :FormatCode to see what formatters are installed and apply to .cc files, and check if running the one for clang-format has the same issue.

jeroen-dhollander commented 1 year ago

Thanks for the response. I can confirm it is caused by clang-format (by running :FormatCode clang-format which reproduces the issue):

image

jeroen-dhollander commented 5 months ago

Update: It is related to the presence of InsertNewlineAtEOF in the chromium .clang-format file.

Setting that to false (or removing it) makes the bug go away.

kevinptt0323 commented 1 month ago

I have the same issue.

Root of cause: https://github.com/llvm/llvm-project/issues/108333

dbarnett commented 1 month ago

So I checked your repro, definitely a clang bug w/o any plausible way that could be "working as intended", but if they're neglecting to fix that it's possible codefmt could have a workaround in the meantime to detect wonky output with mystery trailing whitespace and strip it off. Or maybe always ensure a trailing newline on the input it sends.

On affected files, does formatting a specific range give weird trailing whitespace or just :FormatCode?

kevinptt0323 commented 4 weeks ago

Seems like they fixed the issue in https://github.com/llvm/llvm-project/pull/108513. Great thanks to @owenca!

Let's keep tracking the latest llvm release (maybe 19.1.1?) and update here when the change is landed.