From 8002030de508837dbbf2b5bd5e5c3b091c29baef Mon Sep 17 00:00:00 2001 From: Briano Goestiawan Date: Sun, 30 May 2021 08:46:51 +1000 Subject: [PATCH] Add option to trim trailing newline in basic_format --- README.md | 3 +++ autoload/neoformat.vim | 14 ++++++++++++++ doc/neoformat.txt | 3 +++ 3 files changed, 20 insertions(+) diff --git a/README.md b/README.md index 09794f1c..98d0937a 100644 --- a/README.md +++ b/README.md @@ -124,6 +124,9 @@ let g:neoformat_basic_format_retab = 1 " Enable trimmming of trailing whitespace let g:neoformat_basic_format_trim = 1 + +" Enable trimmming of trailing newline +let g:neoformat_basic_format_trim_newline = 1 ``` Run all enabled formatters (by default Neoformat stops after the first formatter diff --git a/autoload/neoformat.vim b/autoload/neoformat.vim index d1cff939..c96e4212 100644 --- a/autoload/neoformat.vim +++ b/autoload/neoformat.vim @@ -312,6 +312,10 @@ function! s:basic_format() abort let g:neoformat_basic_format_trim = 0 endif + if !exists('g:neoformat_basic_format_trim_newline') + let g:neoformat_basic_format_trim_newline = 0 + endif + if neoformat#utils#var('neoformat_basic_format_align') call neoformat#utils#log('aligning with basic formatter') let v = winsaveview() @@ -333,4 +337,14 @@ function! s:basic_format() abort let @/=search call winrestview(view) endif + if neoformat#utils#var('neoformat_basic_format_trim_newline') + call neoformat#utils#log('trimming newline with basic formatter') + let search = @/ + let view = winsaveview() + " vint: -ProhibitCommandRelyOnUser -ProhibitCommandWithUnintendedSideEffect + silent! %s/\n\+\%$//e + " vint: +ProhibitCommandRelyOnUser +ProhibitCommandWithUnintendedSideEffect + let @/=search + call winrestview(view) + endif endfunction diff --git a/doc/neoformat.txt b/doc/neoformat.txt index b7115552..b5bba1ae 100644 --- a/doc/neoformat.txt +++ b/doc/neoformat.txt @@ -129,6 +129,9 @@ Enable basic formatting when a filetype is not found. Disabled by default. " Enable trimmming of trailing whitespace globally let g:neoformat_basic_format_trim = 1 + " Enable trimmming of trailing newline globally + let g:neoformat_basic_format_trim_newline = 1 + Run all enabled formatters (by default Neoformat stops after the first formatter succeeds)