Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trim trailing whitespace only on modified lines #195

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 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,14 @@ endfunction

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