Skip to content

Commit

Permalink
Detect whitelines on save if specified (#145)
Browse files Browse the repository at this point in the history
Modify DetectWhitespace to detect final whitelines as well, when options say we should also remove it.
  • Loading branch information
Farzat07 authored Sep 18, 2021
1 parent 844ceba commit c5afbe9
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions plugin/better-whitespace.vim
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ if g:better_whitespace_skip_empty_lines == 1
endif

let s:strip_whitespace_pattern = s:eol_whitespace_pattern
let s:strip_whitelines_pattern = '\(\n\)\+\%$'
if g:show_spaces_that_precede_tabs == 1
let s:eol_whitespace_pattern .= '\|[' . s:whitespace_chars . ']\+\ze[\u0009]'
endif
Expand Down Expand Up @@ -195,7 +196,11 @@ endif
" WARNING: moves the cursor.
function! s:DetectWhitespace(line1, line2)
call cursor(a:line1, 1)
return search(s:strip_whitespace_pattern, 'cn', a:line2)
let found = search(s:strip_whitespace_pattern, 'cn', a:line2)
if g:strip_whitelines_at_eof == 1 && a:line2 >= line('$')
let found += search(s:strip_whitelines_pattern, 'cn')
endif
return found
endfunction

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

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

" Restore the saved search and cursor position
Expand Down

0 comments on commit c5afbe9

Please sign in to comment.