Skip to content

Commit 090bd90

Browse files
committed
Use detailed changes in Vim listener
The collated values were too coarse, and made it impossible to - in some cases - distinguish adding a new line from also editing a different line. Should bring the Vim and Neovim versions closer together.
1 parent 36c314d commit 090bd90

File tree

2 files changed

+37
-33
lines changed

2 files changed

+37
-33
lines changed

plugin/strip_trailing_whitespace.vim

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -157,34 +157,37 @@ let s:is_stripping = 0
157157
function StripTrailingWhitespaceListener(bufnr, start, end, added, changes) abort
158158
if s:is_stripping || b:stw_count > g:strip_trailing_whitespace_max_lines | return | endif
159159

160-
" Remove existing in range
161-
if a:start < a:end | call s:RemoveRange(a:start, a:end) | endif
162-
163-
" Adjust line numbers
164-
if b:stw_root isnot v:null
165-
let b:stw_root = s:Splay(b:stw_root, a:end)
166-
if b:stw_root.key >= a:end
167-
let b:stw_root.key += a:added
168-
if b:stw_root.left isnot v:null | let b:stw_root.left.key -= a:added | endif
169-
elseif b:stw_root.right isnot v:null
170-
let b:stw_root.right.key += a:added
160+
for change in a:changes
161+
let [start, end, added] = [change.lnum, change.end, change.added]
162+
" Remove existing in range
163+
if start < end | call s:RemoveRange(start, end) | endif
164+
165+
" Adjust line numbers
166+
if b:stw_root isnot v:null
167+
let b:stw_root = s:Splay(b:stw_root, end)
168+
if b:stw_root.key >= end
169+
let b:stw_root.key += added
170+
if b:stw_root.left isnot v:null | let b:stw_root.left.key -= added | endif
171+
elseif b:stw_root.right isnot v:null
172+
let b:stw_root.right.key += added
173+
endif
171174
endif
172-
endif
173175

174-
" (Re-)Add lines in range with trailing whitespace
175-
for lnum in range(a:start, a:end + a:added - 1)
176-
let has_trailing_ws = getline(lnum) =~# '\s$'
177-
if has_trailing_ws
178-
call s:Put(lnum)
179-
180-
if b:stw_count > g:strip_trailing_whitespace_max_lines
181-
" Max count since unable to recommence (might have missed changes)
182-
let [b:stw_root, b:stw_count] = [v:null, 1 / 0]
183-
echohl WarningMsg | echo 'Falling back to stripping entire file: Too many TWS'
184-
\ '(use `:let b:strip_trailing_whitespace_enabled = 0` to skip)' | echohl None
185-
break
176+
" (Re-)Add lines in range with trailing whitespace
177+
for lnum in range(start, end + added - 1)
178+
let has_trailing_ws = getline(lnum) =~# '\s$'
179+
if has_trailing_ws
180+
call s:Put(lnum)
181+
182+
if b:stw_count > g:strip_trailing_whitespace_max_lines
183+
" Max count since unable to recommence (might have missed changes)
184+
let [b:stw_root, b:stw_count] = [v:null, 1 / 0]
185+
echohl WarningMsg | echo 'Falling back to stripping entire file: Too many TWS'
186+
\ '(use `:let b:strip_trailing_whitespace_enabled = 0` to skip)' | echohl None
187+
return
188+
endif
186189
endif
187-
endif
190+
endfor
188191
endfor
189192
endfunction
190193

@@ -194,7 +197,8 @@ function s:OnBufEnter() abort
194197
if has('nvim')
195198
lua vim.api.nvim_buf_attach(0, false, {
196199
\ on_lines = function(_, bufnr, _, firstline, lastline, new_lastline)
197-
\ vim.api.nvim_call_function("StripTrailingWhitespaceListener", {bufnr, firstline + 1, lastline + 1, new_lastline - lastline, {}})
200+
\ vim.api.nvim_call_function("StripTrailingWhitespaceListener", {bufnr, firstline + 1, lastline + 1, new_lastline - lastline,
201+
\ {{lnum = firstline + 1, ["end"] = lastline + 1, added = new_lastline - lastline, col = 1}}})
198202
\ end, })
199203
else
200204
call listener_add('StripTrailingWhitespaceListener')

test/test.vim

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ function s:TestEdits(original, EditCb, expected) abort
88
endfunction
99

1010
function Test_OneLineUnchanged() abort
11-
function! s:EditCb() abort
12-
endfunction
13-
call s:TestEdits(['foo '], function('s:EditCb'), ['foo '])
11+
call s:TestEdits(['foo '], {-> 0}, ['foo '])
1412
endfunction
1513

1614
function Test_OneLineChanged() abort
@@ -20,6 +18,10 @@ function Test_OneLineChanged() abort
2018
call s:TestEdits(['line1 '], function('s:EditCb'), ['fine1'])
2119
endfunction
2220

21+
function Test_AddAbove() abort
22+
call s:TestEdits(['line '], {-> execute('normal! O ')}, ['', 'line '])
23+
endfunction
24+
2325
function Test_AddLineAboveChange() abort
2426
function! s:EditCb() abort
2527
normal! rzO
@@ -29,9 +31,7 @@ endfunction
2931

3032
function Test_AddBelowBelowAbove() abort
3133
function! s:EditCb() abort
32-
execute 'normal! 2o '
33-
if !has('nvim') | call listener_flush() | endif
34-
execute 'normal! 1GA '
34+
execute "normal! 2o \<Esc>ggA "
3535
endfunction
3636
call s:TestEdits([], function('s:EditCb'), ['', '', ''])
3737
endfunction
@@ -72,7 +72,7 @@ function Test_HandleManyLinesWithTWS() abort
7272
function! s:EditCb() abort
7373
execute 'normal! 100o '
7474
endfunction
75-
call s:TestEdits(['line '], function('s:EditCb'), extend(['line'], repeat([''], 100)))
75+
call s:TestEdits(['line '], function('s:EditCb'), ['line'] + repeat([''], 100))
7676
endfunction
7777

7878
function Test_LoadScriptTwice() abort

0 commit comments

Comments
 (0)