Skip to content

Commit

Permalink
Trim trailing whitespace only on modified lines
Browse files Browse the repository at this point in the history
Fixes: #106
  • Loading branch information
Felix Weilbach committed Jul 27, 2022
1 parent d354117 commit 9c1bf44
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions plugin/editorconfig.vim
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,8 @@ function! s:ApplyConfig(config) abort " Set the buffer options {{{1
autocmd! BufWritePre <buffer>
if s:IsRuleActive('trim_trailing_whitespace', a:config) &&
\ get(a:config, 'trim_trailing_whitespace', 'false') ==# 'true'
autocmd BufWritePre <buffer> call s:TrimTrailingWhitespace()
autocmd InsertEnter <buffer> let s:pos_on_insert_enter = getpos('.')
autocmd InsertLeave <buffer> call s:TrimTrailingWhitespace()
endif
augroup END

Expand Down Expand Up @@ -500,10 +501,11 @@ endfunction

function! s:TrimTrailingWhitespace() " {{{1
if &l:modifiable
let pos = getpos('.')
" don't lose user position when trimming trailing whitespace
let s:view = winsaveview()
try
silent! keeppatterns keepjumps %s/\s\+$//e
execute 'silent! keeppatterns keepjumps :' . s:pos_on_insert_enter[1] . ',' . pos[1] . 's/\s\+$//e'
finally
call winrestview(s:view)
endtry
Expand Down

0 comments on commit 9c1bf44

Please sign in to comment.