From 2ff5db7d87e04901d1f3dff514e72b959c2e44a4 Mon Sep 17 00:00:00 2001 From: Felix Weilbach Date: Wed, 27 Jul 2022 23:09:34 +0200 Subject: [PATCH] Trim trailing whitespace only on modified lines Fixes: #106 --- plugin/editorconfig.vim | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/plugin/editorconfig.vim b/plugin/editorconfig.vim index d0deb265..7e5caa52 100644 --- a/plugin/editorconfig.vim +++ b/plugin/editorconfig.vim @@ -440,7 +440,8 @@ function! s:ApplyConfig(config) abort " Set the buffer options {{{1 autocmd! BufWritePre if s:IsRuleActive('trim_trailing_whitespace', a:config) && \ get(a:config, 'trim_trailing_whitespace', 'false') ==# 'true' - autocmd BufWritePre call s:TrimTrailingWhitespace() + autocmd InsertEnter let s:pos_on_insert_enter = getpos('.') + autocmd InsertLeave call s:TrimTrailingWhitespace() endif augroup END @@ -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