Skip to content

Commit c5afbe9

Browse files
authored
Detect whitelines on save if specified (#145)
Modify DetectWhitespace to detect final whitelines as well, when options say we should also remove it.
1 parent 844ceba commit c5afbe9

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

plugin/better-whitespace.vim

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ if g:better_whitespace_skip_empty_lines == 1
8484
endif
8585

8686
let s:strip_whitespace_pattern = s:eol_whitespace_pattern
87+
let s:strip_whitelines_pattern = '\(\n\)\+\%$'
8788
if g:show_spaces_that_precede_tabs == 1
8889
let s:eol_whitespace_pattern .= '\|[' . s:whitespace_chars . ']\+\ze[\u0009]'
8990
endif
@@ -195,7 +196,11 @@ endif
195196
" WARNING: moves the cursor.
196197
function! s:DetectWhitespace(line1, line2)
197198
call cursor(a:line1, 1)
198-
return search(s:strip_whitespace_pattern, 'cn', a:line2)
199+
let found = search(s:strip_whitespace_pattern, 'cn', a:line2)
200+
if g:strip_whitelines_at_eof == 1 && a:line2 >= line('$')
201+
let found += search(s:strip_whitelines_pattern, 'cn')
202+
endif
203+
return found
199204
endfunction
200205

201206
" Removes all extraneous whitespace in the file
@@ -209,7 +214,7 @@ function! s:StripWhitespace(line1, line2)
209214

210215
" Strip empty lines at EOF
211216
if g:strip_whitelines_at_eof == 1 && a:line2 >= line('$')
212-
silent execute '%s/\(\n\)\+\%$//e'
217+
silent execute '%s/' . s:strip_whitelines_pattern . '//e'
213218
endif
214219

215220
" Restore the saved search and cursor position

0 commit comments

Comments
 (0)