1
0
forked from aniani/vim

patch 8.2.3288: cannot easily access namespace dictionaries from Lua

Problem:    Cannot easily access namespace dictionaries from Lua.
Solution:   Add vim.g, vim.b, etc. (Yegappan Lakshmanan, closes #8693,
            from NeoVim)
This commit is contained in:
Yegappan Lakshmanan
2021-08-04 21:12:52 +02:00
committed by Bram Moolenaar
parent 57942237c1
commit 9dc4bef897
4 changed files with 414 additions and 0 deletions

View File

@@ -916,4 +916,246 @@ vim.command('let s ..= "B"')
call assert_equal('ABCDE', s)
endfunc
" Test for adding, accessing and removing global variables using the vim.g
" Lua table
func Test_lua_global_var_table()
" Access global variables with different types of values
let g:Var1 = 10
let g:Var2 = 'Hello'
let g:Var3 = ['a', 'b']
let g:Var4 = #{x: 'edit', y: 'run'}
let g:Var5 = function('min')
call assert_equal(10, luaeval('vim.g.Var1'))
call assert_equal('Hello', luaeval('vim.g.Var2'))
call assert_equal(['a', 'b'], luaeval('vim.g.Var3'))
call assert_equal(#{x: 'edit', y: 'run'}, luaeval('vim.g.Var4'))
call assert_equal(2, luaeval('vim.g.Var5')([5, 9, 2]))
" Access list of dictionaries and dictionary of lists
let g:Var1 = [#{a: 10}, #{b: 20}]
let g:Var2 = #{p: [5, 6], q: [1.1, 2.2]}
call assert_equal([#{a: 10}, #{b: 20}], luaeval('vim.g.Var1'))
call assert_equal(#{p: [5, 6], q: [1.1, 2.2]}, luaeval('vim.g.Var2'))
" Create new global variables with different types of values
unlet g:Var1 g:Var2 g:Var3 g:Var4 g:Var5
lua << trim END
vim.g.Var1 = 34
vim.g.Var2 = 'World'
vim.g.Var3 = vim.list({'#', '$'})
vim.g.Var4 = vim.dict({model='honda', year=2020})
vim.g.Var5 = vim.funcref('max')
END
call assert_equal(34, g:Var1)
call assert_equal('World', g:Var2)
call assert_equal(['#', '$'], g:Var3)
call assert_equal(#{model: 'honda', year: 2020}, g:Var4)
call assert_equal(10, g:Var5([5, 10, 9]))
" Create list of dictionaries and dictionary of lists
unlet g:Var1 g:Var2
lua << trim END
vim.g.Var1 = vim.list({vim.dict({a=10}), vim.dict({b=20})})
vim.g.Var2 = vim.dict({p=vim.list({5, 6}), q=vim.list({1.1, 2.2})})
END
call assert_equal([#{a: 10}, #{b: 20}], luaeval('vim.g.Var1'))
call assert_equal(#{p: [5, 6], q: [1.1, 2.2]}, luaeval('vim.g.Var2'))
" Modify a global variable with a list value or a dictionary value
let g:Var1 = [10, 20]
let g:Var2 = #{one: 'mercury', two: 'mars'}
lua << trim END
vim.g.Var1[2] = Nil
vim.g.Var1[3] = 15
vim.g.Var2['two'] = Nil
vim.g.Var2['three'] = 'earth'
END
call assert_equal([10, 15], g:Var1)
call assert_equal(#{one: 'mercury', three: 'earth'}, g:Var2)
" Remove global variables with different types of values
let g:Var1 = 10
let g:Var2 = 'Hello'
let g:Var3 = ['a', 'b']
let g:Var4 = #{x: 'edit', y: 'run'}
let g:Var5 = function('min')
lua << trim END
vim.g.Var1 = Nil
vim.g.Var2 = Nil
vim.g.Var3 = Nil
vim.g.Var4 = Nil
vim.g.Var5 = Nil
END
call assert_false(exists('g:Var1'))
call assert_false(exists('g:Var2'))
call assert_false(exists('g:Var3'))
call assert_false(exists('g:Var4'))
call assert_false(exists('g:Var5'))
" Try to modify and remove a locked global variable
let g:Var1 = 10
lockvar g:Var1
call assert_fails('lua vim.g.Var1 = 20', 'variable is locked')
call assert_fails('lua vim.g.Var1 = Nil', 'variable is locked')
unlockvar g:Var1
let g:Var2 = [7, 14]
lockvar 0 g:Var2
lua vim.g.Var2[2] = Nil
lua vim.g.Var2[3] = 21
call assert_fails('lua vim.g.Var2 = Nil', 'variable is locked')
call assert_equal([7, 21], g:Var2)
lockvar 1 g:Var2
call assert_fails('lua vim.g.Var2[2] = Nil', 'list is locked')
call assert_fails('lua vim.g.Var2[3] = 21', 'list is locked')
unlockvar g:Var2
" Attempt to access a non-existing global variable
call assert_equal(v:null, luaeval('vim.g.NonExistingVar'))
lua vim.g.NonExisting = Nil
unlet! g:Var1 g:Var2 g:Var3 g:Var4 g:Var5
endfunc
" Test for accessing and modifying predefined vim variables using the vim.v
" Lua table
func Test_lua_predefined_var_table()
call assert_equal(v:progpath, luaeval('vim.v.progpath'))
let v:errmsg = 'SomeError'
call assert_equal('SomeError', luaeval('vim.v.errmsg'))
lua vim.v.errmsg = 'OtherError'
call assert_equal('OtherError', v:errmsg)
call assert_fails('lua vim.v.errmsg = Nil', 'variable is fixed')
let v:oldfiles = ['one', 'two']
call assert_equal(['one', 'two'], luaeval('vim.v.oldfiles'))
lua vim.v.oldfiles = vim.list({})
call assert_equal([], v:oldfiles)
call assert_equal(v:null, luaeval('vim.v.null'))
call assert_fails('lua vim.v.argv[1] = Nil', 'list is locked')
call assert_fails('lua vim.v.newvar = 1', 'Dictionary is locked')
endfunc
" Test for adding, accessing and modifying window-local variables using the
" vim.w Lua table
func Test_lua_window_var_table()
" Access window variables with different types of values
new
let w:wvar1 = 10
let w:wvar2 = 'edit'
let w:wvar3 = 3.14
let w:wvar4 = 0zdeadbeef
let w:wvar5 = ['a', 'b']
let w:wvar6 = #{action: 'run'}
call assert_equal(10, luaeval('vim.w.wvar1'))
call assert_equal('edit', luaeval('vim.w.wvar2'))
call assert_equal(3.14, luaeval('vim.w.wvar3'))
call assert_equal(0zdeadbeef, luaeval('vim.w.wvar4'))
call assert_equal(['a', 'b'], luaeval('vim.w.wvar5'))
call assert_equal(#{action: 'run'}, luaeval('vim.w.wvar6'))
call assert_equal(v:null, luaeval('vim.w.NonExisting'))
" modify a window variable
lua vim.w.wvar2 = 'paste'
call assert_equal('paste', w:wvar2)
" change the type stored in a variable
let w:wvar2 = [1, 2]
lua vim.w.wvar2 = vim.dict({a=10, b=20})
call assert_equal(#{a: 10, b: 20}, w:wvar2)
" create a new window variable
lua vim.w.wvar7 = vim.dict({a=vim.list({1, 2}), b=20})
call assert_equal(#{a: [1, 2], b: 20}, w:wvar7)
" delete a window variable
lua vim.w.wvar2 = Nil
call assert_false(exists('w:wvar2'))
new
call assert_equal(v:null, luaeval('vim.w.wvar1'))
call assert_equal(v:null, luaeval('vim.w.wvar2'))
%bw!
endfunc
" Test for adding, accessing and modifying buffer-local variables using the
" vim.b Lua table
func Test_lua_buffer_var_table()
" Access buffer variables with different types of values
let b:bvar1 = 10
let b:bvar2 = 'edit'
let b:bvar3 = 3.14
let b:bvar4 = 0zdeadbeef
let b:bvar5 = ['a', 'b']
let b:bvar6 = #{action: 'run'}
call assert_equal(10, luaeval('vim.b.bvar1'))
call assert_equal('edit', luaeval('vim.b.bvar2'))
call assert_equal(3.14, luaeval('vim.b.bvar3'))
call assert_equal(0zdeadbeef, luaeval('vim.b.bvar4'))
call assert_equal(['a', 'b'], luaeval('vim.b.bvar5'))
call assert_equal(#{action: 'run'}, luaeval('vim.b.bvar6'))
call assert_equal(v:null, luaeval('vim.b.NonExisting'))
" modify a buffer variable
lua vim.b.bvar2 = 'paste'
call assert_equal('paste', b:bvar2)
" change the type stored in a variable
let b:bvar2 = [1, 2]
lua vim.b.bvar2 = vim.dict({a=10, b=20})
call assert_equal(#{a: 10, b: 20}, b:bvar2)
" create a new buffer variable
lua vim.b.bvar7 = vim.dict({a=vim.list({1, 2}), b=20})
call assert_equal(#{a: [1, 2], b: 20}, b:bvar7)
" delete a buffer variable
lua vim.b.bvar2 = Nil
call assert_false(exists('b:bvar2'))
new
call assert_equal(v:null, luaeval('vim.b.bvar1'))
call assert_equal(v:null, luaeval('vim.b.bvar2'))
%bw!
endfunc
" Test for adding, accessing and modifying tabpage-local variables using the
" vim.t Lua table
func Test_lua_tabpage_var_table()
" Access tabpage variables with different types of values
let t:tvar1 = 10
let t:tvar2 = 'edit'
let t:tvar3 = 3.14
let t:tvar4 = 0zdeadbeef
let t:tvar5 = ['a', 'b']
let t:tvar6 = #{action: 'run'}
call assert_equal(10, luaeval('vim.t.tvar1'))
call assert_equal('edit', luaeval('vim.t.tvar2'))
call assert_equal(3.14, luaeval('vim.t.tvar3'))
call assert_equal(0zdeadbeef, luaeval('vim.t.tvar4'))
call assert_equal(['a', 'b'], luaeval('vim.t.tvar5'))
call assert_equal(#{action: 'run'}, luaeval('vim.t.tvar6'))
call assert_equal(v:null, luaeval('vim.t.NonExisting'))
" modify a tabpage variable
lua vim.t.tvar2 = 'paste'
call assert_equal('paste', t:tvar2)
" change the type stored in a variable
let t:tvar2 = [1, 2]
lua vim.t.tvar2 = vim.dict({a=10, b=20})
call assert_equal(#{a: 10, b: 20}, t:tvar2)
" create a new tabpage variable
lua vim.t.tvar7 = vim.dict({a=vim.list({1, 2}), b=20})
call assert_equal(#{a: [1, 2], b: 20}, t:tvar7)
" delete a tabpage variable
lua vim.t.tvar2 = Nil
call assert_false(exists('t:tvar2'))
tabnew
call assert_equal(v:null, luaeval('vim.t.tvar1'))
call assert_equal(v:null, luaeval('vim.t.tvar2'))
%bw!
endfunc
" vim: shiftwidth=2 sts=2 expandtab