Skip to content

Commit aaa35b1

Browse files
committed
Switch to v:null
Previously used a bespoke null sentinel value of type Dictionary instead of the standard `v:null`, which essentially is an empty String. This had the advantage that `==` would work between null and dictionaries, however this was never utilised.
1 parent 020b2f7 commit aaa35b1

File tree

1 file changed

+34
-36
lines changed

1 file changed

+34
-36
lines changed

plugin/strip_trailing_whitespace.vim

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ let g:loaded_strip_trailing_whitespace = 1
1010
" Strip trailing whitespace
1111
command -bar -range=% StripTrailingWhitespace keeppatterns <line1>,<line2>substitute/\s\+$//e
1212

13-
const s:null = {} " Sentinel value indicating null.
14-
1513
if !exists('g:strip_trailing_whitespace_max_lines')
1614
" The maximum number of modified lines with trailing whitespace to keep
1715
" track of before falling back to stripping the entire file.
@@ -24,7 +22,7 @@ function s:RotateRight(n) abort
2422
let x.right = a:n
2523
let x.key += a:n.key
2624
let a:n.key -= x.key
27-
if a:n.left isnot s:null | let a:n.left.key -= a:n.key | endif
25+
if a:n.left isnot v:null | let a:n.left.key -= a:n.key | endif
2826
return x
2927
endfunction
3028

@@ -34,12 +32,12 @@ function s:RotateLeft(n) abort
3432
let x.left = a:n
3533
let x.key += a:n.key
3634
let a:n.key -= x.key
37-
if a:n.right isnot s:null | let a:n.right.key -= a:n.key | endif
35+
if a:n.right isnot v:null | let a:n.right.key -= a:n.key | endif
3836
return x
3937
endfunction
4038

4139
" Return the number of nodes in tree {n}.
42-
let s:NodeCount = {n -> n is s:null ? 0 : 1 + s:NodeCount(n.left) + s:NodeCount(n.right)}
40+
let s:NodeCount = {n -> n is v:null ? 0 : 1 + s:NodeCount(n.left) + s:NodeCount(n.right)}
4341

4442
" Splay {key} in the tree rooted at the node {n}.
4543
"
@@ -48,47 +46,47 @@ let s:NodeCount = {n -> n is s:null ? 0 : 1 + s:NodeCount(n.left) + s:NodeCount(
4846
" root.
4947
function s:Splay(n, key) abort
5048
let [n, key] = [a:n, a:key]
51-
if n is s:null | return s:null | endif
49+
if n is v:null | return v:null | endif
5250

5351
let key -= n.key
5452
if key < 0
55-
if n.left is s:null | return n | endif " Key is not in tree, so we are done
53+
if n.left is v:null | return n | endif " Key is not in tree, so we are done
5654

5755
let key -= n.left.key
5856
if key < 0
5957
let n.left.left = s:Splay(n.left.left, key)
6058
let n = s:RotateRight(n)
6159
elseif key > 0
6260
let n.left.right = s:Splay(n.left.right, key)
63-
if n.left.right isnot s:null
61+
if n.left.right isnot v:null
6462
let n.left = s:RotateLeft(n.left)
6563
endif
6664
endif
6765

68-
return n.left is s:null ? n : s:RotateRight(n)
66+
return n.left is v:null ? n : s:RotateRight(n)
6967
elseif key > 0
70-
if n.right is s:null | return n | endif " Key is not in tree, so we are done
68+
if n.right is v:null | return n | endif " Key is not in tree, so we are done
7169

7270
let key -= n.right.key
7371
if key < 0
7472
let n.right.left = s:Splay(n.right.left, key)
75-
if n.right.left isnot s:null
73+
if n.right.left isnot v:null
7674
let n.right = s:RotateRight(n.right)
7775
endif
7876
elseif key > 0
7977
let n.right.right = s:Splay(n.right.right, key)
8078
let n = s:RotateLeft(n)
8179
endif
8280

83-
return n.right is s:null ? n : s:RotateLeft(n)
81+
return n.right is v:null ? n : s:RotateLeft(n)
8482
else
8583
return n
8684
endif
8785
endfunction
8886

8987
function s:Put(key) abort
90-
if b:stw_root is s:null
91-
let [b:stw_root, b:stw_count] = [{'key': a:key, 'left': s:null, 'right': s:null}, 1]
88+
if b:stw_root is v:null
89+
let [b:stw_root, b:stw_count] = [{'key': a:key, 'left': v:null, 'right': v:null}, 1]
9290
return
9391
endif
9492

@@ -97,15 +95,15 @@ function s:Put(key) abort
9795
let cmp = a:key - b:stw_root.key
9896
if cmp < 0
9997
let n = {'key': a:key, 'left': b:stw_root.left, 'right': b:stw_root}
100-
let b:stw_root.left = s:null
101-
if n.left isnot s:null | let n.left.key += b:stw_root.key - n.key | endif
98+
let b:stw_root.left = v:null
99+
if n.left isnot v:null | let n.left.key += b:stw_root.key - n.key | endif
102100
let n.right.key -= n.key
103101
let b:stw_root = n
104102
let b:stw_count += 1
105103
elseif cmp > 0
106104
let n = {'key': a:key, 'left': b:stw_root, 'right': b:stw_root.right}
107-
let b:stw_root.right = s:null
108-
if n.right isnot s:null | let n.right.key += b:stw_root.key - n.key | endif
105+
let b:stw_root.right = v:null
106+
if n.right isnot v:null | let n.right.key += b:stw_root.key - n.key | endif
109107
let n.left.key -= n.key
110108
let b:stw_root = n
111109
let b:stw_count += 1
@@ -116,34 +114,34 @@ endfunction
116114
"
117115
" Does modified Hibbard deletion.
118116
function s:RemoveRange(min, max) abort
119-
if b:stw_root is s:null | return | endif
117+
if b:stw_root is v:null | return | endif
120118
let b:stw_root = s:Splay(b:stw_root, a:min) " Splay around lower bound
121119
let right = b:stw_root.right
122-
if right isnot s:null | let right.key += b:stw_root.key | endif
123-
let b:stw_root.right = s:null
120+
if right isnot v:null | let right.key += b:stw_root.key | endif
121+
let b:stw_root.right = v:null
124122

125123
" Remove root if in range
126124
if a:min <= b:stw_root.key && b:stw_root.key < a:max
127-
if b:stw_root.left isnot s:null | let b:stw_root.left.key += b:stw_root.key | endif
125+
if b:stw_root.left isnot v:null | let b:stw_root.left.key += b:stw_root.key | endif
128126
let b:stw_root = b:stw_root.left
129127
let b:stw_count -= 1
130128
endif
131129

132-
if right isnot s:null
130+
if right isnot v:null
133131
let right = s:Splay(right, a:max) " Splay around upper bound
134132
let b:stw_count -= s:NodeCount(right.left)
135-
let right.left = s:null
133+
let right.left = v:null
136134

137135
" If root of right subtree is in range: Remove it
138136
if right.key < a:max
139-
if right.right isnot s:null | let right.right.key += right.key | endif
137+
if right.right isnot v:null | let right.right.key += right.key | endif
140138
let right = right.right
141139
let b:stw_count -= 1
142140
endif
143141

144-
if b:stw_root is s:null
142+
if b:stw_root is v:null
145143
let b:stw_root = right
146-
elseif right isnot s:null
144+
elseif right isnot v:null
147145
let b:stw_root = s:Splay(b:stw_root, 1 / 0) " Move rightmost to root
148146
let right.key -= b:stw_root.key
149147
let b:stw_root.right = right
@@ -163,12 +161,12 @@ function StripTrailingWhitespaceListener(bufnr, start, end, added, changes) abor
163161
if a:start < a:end | call s:RemoveRange(a:start, a:end) | endif
164162

165163
" Adjust line numbers
166-
if b:stw_root isnot s:null
164+
if b:stw_root isnot v:null
167165
let b:stw_root = s:Splay(b:stw_root, a:end)
168166
if b:stw_root.key >= a:end
169167
let b:stw_root.key += a:added
170-
if b:stw_root.left isnot s:null | let b:stw_root.left.key -= a:added | endif
171-
elseif b:stw_root.right isnot s:null
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
172170
let b:stw_root.right.key += a:added
173171
endif
174172
endif
@@ -181,7 +179,7 @@ function StripTrailingWhitespaceListener(bufnr, start, end, added, changes) abor
181179

182180
if b:stw_count > g:strip_trailing_whitespace_max_lines
183181
" Max count since unable to recommence (might have missed changes)
184-
let [b:stw_root, b:stw_count] = [s:null, 1 / 0]
182+
let [b:stw_root, b:stw_count] = [v:null, 1 / 0]
185183
echohl WarningMsg | echo 'Falling back to stripping entire file: Too many TWS'
186184
\ '(use `:let b:strip_trailing_whitespace_enabled = 0` to skip)' | echohl None
187185
break
@@ -192,7 +190,7 @@ endfunction
192190

193191
function s:OnBufEnter() abort
194192
if exists('b:stw_root') | return | endif
195-
let [b:stw_root, b:stw_count] = [s:null, 0]
193+
let [b:stw_root, b:stw_count] = [v:null, 0]
196194
if has('nvim')
197195
lua vim.api.nvim_buf_attach(0, false, {
198196
\ on_lines = function(_, bufnr, _, firstline, lastline, new_lastline)
@@ -206,8 +204,8 @@ endfunction
206204
" Recursively strip lines in the specified tree.
207205
function s:StripTree(n, offset) abort
208206
silent execute (a:n.key + a:offset) 'StripTrailingWhitespace'
209-
if a:n.left isnot s:null | call s:StripTree(a:n.left, a:offset + a:n.key) | endif
210-
if a:n.right isnot s:null | call s:StripTree(a:n.right, a:offset + a:n.key) | endif
207+
if a:n.left isnot v:null | call s:StripTree(a:n.left, a:offset + a:n.key) | endif
208+
if a:n.right isnot v:null | call s:StripTree(a:n.right, a:offset + a:n.key) | endif
211209
endfunction
212210

213211
function s:OnWrite() abort
@@ -220,8 +218,8 @@ function s:OnWrite() abort
220218
if b:stw_count > g:strip_trailing_whitespace_max_lines
221219
silent StripTrailingWhitespace
222220
else
223-
if b:stw_root isnot s:null | call s:StripTree(b:stw_root, 0) | endif
224-
let [b:stw_root, b:stw_count] = [s:null, 0]
221+
if b:stw_root isnot v:null | call s:StripTree(b:stw_root, 0) | endif
222+
let [b:stw_root, b:stw_count] = [v:null, 0]
225223
endif
226224
finally
227225
call setpos('.', save_cursor)

0 commit comments

Comments
 (0)