@@ -157,34 +157,37 @@ let s:is_stripping = 0
157157function 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
189192endfunction
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' )
0 commit comments