Skip to content

Commit

Permalink
fix(api): Fix Buffer.getVar() for non-current buffer (#4569)
Browse files Browse the repository at this point in the history
* test(api): Add non-current buffer test for buffer variable

* fix: vim api Buffer.getVar()

* fix: remove buffer variable
  • Loading branch information
weirongxu committed Mar 15, 2023
1 parent b28b8dc commit 94dc105
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
9 changes: 3 additions & 6 deletions autoload/coc/api.vim
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ endfunction

function! s:funcs.buf_get_var(bufnr, name)
call s:check_bufnr(a:bufnr)
if !has_key(b:, a:name)
if !has_key(getbufvar(a:bufnr, ''), a:name)
throw 'Key not found: '.a:name
endif
return getbufvar(a:bufnr, a:name)
Expand All @@ -616,11 +616,8 @@ endfunction

function! s:funcs.buf_del_var(bufnr, name)
call s:check_bufnr(a:bufnr)
if a:bufnr == bufnr('%')
execute 'unlet! b:'.a:name
else
call s:buf_execute(a:bufnr, ['unlet! b:'.a:name])
endif
let bufvars = getbufvar(a:bufnr, '')
call remove(bufvars, a:name)
return v:null
endfunction

Expand Down
9 changes: 9 additions & 0 deletions src/__tests__/vim.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,15 @@ describe('Buffer API', () => {
buffer.deleteVar('foo')
curr = await buffer.getVar('foo')
expect(curr).toBeNull()

// another non-current buffer
const buf2 = await nvim.createNewBuffer()
await buf2.setVar('foo', 'qux', false)
let curr2 = await buf2.getVar('foo')
expect(curr2).toBe('qux')
buf2.deleteVar('foo')
curr = await buf2.getVar('foo')
expect(curr).toBeNull()
})
})

Expand Down

0 comments on commit 94dc105

Please sign in to comment.