Skip to content

Commit

Permalink
fix(api): not throw for unload buffer
Browse files Browse the repository at this point in the history
buf_line_count return 0 for buffer not loaded, same as neovim.
  • Loading branch information
chemzqm committed Feb 24, 2025
1 parent a6aa8d0 commit 2315321
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
22 changes: 9 additions & 13 deletions autoload/coc/api.vim
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function! s:create_popup(bufnr) abort
endfunction

function! s:check_bufnr(bufnr) abort
if !bufloaded(a:bufnr)
if a:bufnr != 0 && !bufloaded(a:bufnr)
throw 'Invalid buffer id: '.a:bufnr
endif
endfunction
Expand Down Expand Up @@ -122,20 +122,17 @@ function! s:win_tabnr(winid) abort
endfunction

function! s:buf_line_count(bufnr) abort
if bufnr('%') == a:bufnr
if a:bufnr == 0
return line('$')
endif
if exists('*getbufinfo')
let info = getbufinfo(a:bufnr)
if empty(info)
return 0
endif
" vim 8.1 has getbufinfo but no linecount
if has_key(info[0], 'linecount')
return info[0]['linecount']
endif
let info = getbufinfo(a:bufnr)
if empty(info)
throw "Invalid buffer id: ".a:bufnr
endif
return len(getbufline(a:bufnr, 1, '$'))
if info[0]['loaded'] == 0
return 0
endif
return info[0]['linecount']
endfunction

function! s:execute(cmd)
Expand Down Expand Up @@ -594,7 +591,6 @@ function! s:funcs.buf_clear_namespace(bufnr, srcId, startLine, endLine) abort
endfunction

function! s:funcs.buf_line_count(bufnr) abort
call s:check_bufnr(a:bufnr)
return s:buf_line_count(a:bufnr)
endfunction

Expand Down
4 changes: 4 additions & 0 deletions src/__tests__/vim.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,10 @@ describe('Buffer API', () => {
let n = await buffer.length
expect(n).toBe(5)
await nvim.command('silent! %bwipeout!')
await expect(async () => {
let buf = nvim.createBuffer(-1)
await buf.length
}).rejects.toThrow(/Invalid buffer/)
})

it('should get lines', async () => {
Expand Down

0 comments on commit 2315321

Please sign in to comment.