1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-03 00:00:04 +08:00

fix(vim#buffer): fix vim#buffer api

close https://github.com/SpaceVim/SpaceVim/issues/4674
This commit is contained in:
wsdjeg 2022-09-10 21:49:19 +08:00
parent 2067108074
commit 6674432dc4

View File

@ -188,34 +188,43 @@ function! s:self.buf_set_lines(buffer, start, end, strict_indexing, replacement)
" patch-8.1.0037 appendbufline() " patch-8.1.0037 appendbufline()
" patch-8.0.1039 setbufline() " patch-8.0.1039 setbufline()
" patch-8.1.1610 bufadd() bufload() " patch-8.1.1610 bufadd() bufload()
" in vim, setbufline will not load buffer automatically
" but in neovim, nvim_buf_set_lines will do it.
" @fixme vim issue #5044
" https://github.com/vim/vim/issues/5044
if !bufloaded(a:buffer)
call bufload(a:buffer)
endif
let lct = self.line_count(a:buffer) let lct = self.line_count(a:buffer)
if a:start > lct let start = a:start + (a:start < 0 ? lct + 1 : 0)
return let end = a:end + (a:end < 0 ? lct + 1 : 0)
elseif a:start >= 0 && a:end > a:start
" in vim, setbufline will not load buffer automatically if start < 0 || end < 0 || start > lct || end > lct
" but in neovim, nvim_buf_set_lines will do it. if a:strict_indexing
" @fixme vim issue #5044 " @fixme raise an error
" https://github.com/vim/vim/issues/5044 return
if !bufloaded(a:buffer)
call bufload(a:buffer)
endif
" 0, 1 len = 1 a:replacement = 4
if len(a:replacement) == a:end - a:start
for i in range(a:start, a:end - 1)
call setbufline(a:buffer, i + 1, a:replacement[i - a:start])
endfor
else else
let endtext = a:end >= lct ? [] : getbufline(a:buffer, a:end + 1, '$') let start = start < 0 ? 0 : start > lct ? lct : start
let replacement = a:replacement + endtext let end = end < 0 ? 0 : end > lct ? lct : end
for i in range(a:start, len(replacement) + a:start - 1)
call setbufline(a:buffer, i + 1, replacement[i - a:start])
endfor
call deletebufline(a:buffer,len(replacement) + a:start + 1, '$')
endif endif
elseif a:start >= 0 && a:end < 0 && lct + a:end >= a:start endif
call self.buf_set_lines(a:buffer, a:start, lct + a:end + 1, a:strict_indexing, a:replacement) if start > end
elseif a:start <= 0 && a:end > a:start && a:end < 0 && lct + a:start >= 0 " @fixme raise an error
call self.buf_set_lines(a:buffer, lct + a:start + 1, lct + a:end + 2, a:strict_indexing, a:replacement) return
endif
" in neovim, indexing is zero-based, end-exclusive
" but in vim, indexing is one-based, end-inclusive
let tochange = min([len(a:replacement), end - start])
if tochange > 0
call setbufline(a:buffer, start + 1, a:replacement[:tochange-1])
endif
if end > start + tochange
call deletebufline(a:buffer, start + 1 + tochange, end)
else
call appendbufline(a:buffer, start + tochange, a:replacement[tochange:])
endif endif
elseif has('python') elseif has('python')
py << EOF py << EOF