Skip to content

Commit 8707e65

Browse files
committed
Fix bug in s:RemoveRange()
1 parent dc896c3 commit 8707e65

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

plugin/strip_trailing_whitespace.vim

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@ endfunction
120120
function s:Remove(key) abort
121121
if b:stw_root is s:null | return | endif " Empty tree
122122
let b:stw_root = s:Splay(b:stw_root, a:key)
123-
" Check if key was in the tree
124-
if a:key != b:stw_root.key | return | endif
123+
if a:key != b:stw_root.key | return | endif " Not in tree
125124
let b:stw_count -= 1
126125

127126
if b:stw_root.left is s:null
@@ -145,14 +144,14 @@ function s:RemoveRange(min, max) abort
145144
let b:stw_root = s:Splay(b:stw_root, a:min)
146145

147146
if b:stw_root.right is s:null
148-
if b:stw_root.key >= a:min && b:stw_root.key <= a:max
147+
if a:min <= b:stw_root.key && b:stw_root.key <= a:max
149148
if b:stw_root.left isnot s:null | let b:stw_root.left.key += b:stw_root.key | endif
150149
let b:stw_root = b:stw_root.left
151150
let b:stw_count -= 1
152151
endif
153152
else
154153
" Do modified Hibbard deletion
155-
if b:stw_root.key >= a:min && b:stw_root.key <= a:max " Should remove root node but keep left subtree
154+
if a:min <= b:stw_root.key && b:stw_root.key <= a:max " Should remove root node but keep left subtree
156155
let rootkey = b:stw_root.key
157156
let x = b:stw_root.left
158157
let b:stw_root = s:Splay(b:stw_root.right, a:max - rootkey + 1)
@@ -166,14 +165,14 @@ function s:RemoveRange(min, max) abort
166165
else " Should keep root node and left subtree
167166
let b:stw_root.right = s:Splay(b:stw_root.right, a:max - b:stw_root.key + 1)
168167
let b:stw_count -= s:NodeCount(b:stw_root.right.left)
169-
if b:stw_root.right.key < a:max
170-
let b:stw_root.right.left = s:null
171-
else
168+
if b:stw_root.key + b:stw_root.right.key <= a:max
172169
if b:stw_root.right.right isnot s:null
173170
let b:stw_root.right.right.key += b:stw_root.right.key
174171
endif
175172
let b:stw_root.right = b:stw_root.right.right
176173
let b:stw_count -= 1
174+
else
175+
let b:stw_root.right.left = s:null
177176
endif
178177
endif
179178
endif

test/test.vim

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ function Test_AddLineAboveChange() abort
2727
call s:TestEdits(['foo '], function('s:EditCb'), ['', 'zoo'])
2828
endfunction
2929

30+
function Test_AddBelowBelowAbove() abort
31+
function! s:EditCb() abort
32+
execute 'normal! 2o '
33+
if !has('nvim') | call listener_flush() | endif
34+
execute 'normal! 1GA '
35+
endfunction
36+
call s:TestEdits([], function('s:EditCb'), ['', '', ''])
37+
endfunction
38+
3039
function Test_DeleteAndPut() abort
3140
function! s:EditCb() abort
3241
normal! j2ddP

0 commit comments

Comments
 (0)