From 2315321681e90635f138dc1f18fc0e6523bd3bf5 Mon Sep 17 00:00:00 2001 From: Qiming Zhao Date: Mon, 24 Feb 2025 14:24:58 +0800 Subject: [PATCH] fix(api): not throw for unload buffer buf_line_count return 0 for buffer not loaded, same as neovim. --- autoload/coc/api.vim | 22 +++++++++------------- src/__tests__/vim.test.ts | 4 ++++ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/autoload/coc/api.vim b/autoload/coc/api.vim index 634be87b240..208106ba72d 100644 --- a/autoload/coc/api.vim +++ b/autoload/coc/api.vim @@ -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 @@ -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) @@ -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 diff --git a/src/__tests__/vim.test.ts b/src/__tests__/vim.test.ts index e1ffbc55db7..2db3fc36936 100644 --- a/src/__tests__/vim.test.ts +++ b/src/__tests__/vim.test.ts @@ -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 () => {