Skip to content

Commit 101c8f2

Browse files
committed
Fix Neovim script incompatibilities
Apart from Neovim missing some newer Vim script features, the main source of incompatibility stemmed from the API:s for listening to buffer changes being different. The Neovim equivalence to `listener_add()` is `nvim_buf_attach()`. It is only available from Lua however, so to have it still call the same Vim script listener, without having to fumble around with "<SID>", the listener was made global. Neovim listener are not buffered, so there is no need for something like `listener_flush()`. Closes #1
1 parent 20d464c commit 101c8f2

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,3 @@ jobs:
2525
esac
2626
- name: Run tests
2727
run: make check VIM=${{ matrix.vim }}
28-
continue-on-error: ${{ matrix.vim == 'nvim' }}

plugin/strip_trailing_whitespace.vim

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
" Vim plugin that removes trailing whitespace from modified lines on save
2-
scriptversion 4
2+
let s:save_cpo = &cpo | set cpo&vim
33

44
" Strip trailing whitespace
55
command -bar -range=% StripTrailingWhitespace keeppatterns <line1>,<line2>substitute/\s\+$//e
@@ -74,7 +74,7 @@ endfunction
7474
function s:Put(key) abort
7575
if b:root is s:null
7676
" Splay key to root
77-
let b:root = #{key: a:key, left: s:null, right: s:null}
77+
let b:root = {'key': a:key, 'left': s:null, 'right': s:null}
7878
return
7979
endif
8080

@@ -83,13 +83,13 @@ function s:Put(key) abort
8383
" Insert new node at root
8484
let cmp = a:key - b:root.key
8585
if cmp < 0
86-
let n = #{key: a:key, left: b:root.left, right: b:root}
86+
let n = {'key': a:key, 'left': b:root.left, 'right': b:root}
8787
let b:root.left = s:null
8888
if n.left isnot s:null | let n.left.key += b:root.key - n.key | endif
8989
let n.right.key -= n.key
9090
let b:root = n
9191
elseif cmp > 0
92-
let n = #{key: a:key, left: b:root, right: b:root.right}
92+
let n = {'key': a:key, 'left': b:root, 'right': b:root.right}
9393
let b:root.right = s:null
9494
if n.right isnot s:null | let n.right.key += b:root.key - n.key | endif
9595
let n.left.key -= n.key
@@ -161,7 +161,7 @@ endfunction
161161
" Ignore changes while that is the case.
162162
let s:is_stripping = 0
163163

164-
function s:Listener(bufnr, start, end, added, changes) abort
164+
function StripTrailingWhitespaceListener(bufnr, start, end, added, changes) abort
165165
if s:is_stripping | return | endif
166166

167167
" Remove existing in range
@@ -193,7 +193,14 @@ function s:OnBufEnter() abort
193193
if exists('b:root') | return | endif
194194

195195
let b:root = s:null
196-
call listener_add(function('s:Listener'))
196+
if has('nvim')
197+
lua vim.api.nvim_buf_attach(0, false, {
198+
\ on_lines = function(_, bufnr, _, firstline, lastline, new_lastline)
199+
\ vim.api.nvim_call_function("StripTrailingWhitespaceListener", {bufnr, firstline + 1, lastline + 1, new_lastline - lastline, {}})
200+
\ end, })
201+
else
202+
call listener_add('StripTrailingWhitespaceListener')
203+
endif
197204
endfunction
198205

199206
" Recursively strips lines in the specified tree.
@@ -205,7 +212,7 @@ function s:StripTree(n, offset) abort
205212
endfunction
206213

207214
function s:OnWrite() abort
208-
call listener_flush()
215+
if !has('nvim') | call listener_flush() | endif
209216

210217
let s:is_stripping = 1
211218
let save_cursor = getcurpos()
@@ -223,3 +230,5 @@ augroup strip_trailing_whitespace
223230
autocmd BufEnter * call s:OnBufEnter()
224231
autocmd BufWritePre * call s:OnWrite()
225232
augroup END
233+
234+
let &cpo = s:save_cpo | unlet s:save_cpo

runtest.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ try
88
for s:testfile in s:testfiles
99
execute 'source' s:testfile
1010

11-
let s:tests = execute('function /^Test_')->split("\n")->map({_, v -> v->matchstr('function \zs\k\+\ze()')})
11+
let s:tests = map(split(execute('function /^Test_'), "\n"), {_, v -> matchstr(v, 'function \zs\k\+\ze()')})
1212
for s:test_function in s:tests
1313
let v:errors = []
1414
try

test/test.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function s:TestEdits(original, EditCb, expected) abort
22
let fname = tempname()
33
call writefile(a:original, fname)
4-
silent execute 'edit' fname
4+
silent execute 'edit!' fname
55
call a:EditCb()
66
silent write
77
call assert_equal(a:expected, readfile(fname))

0 commit comments

Comments
 (0)