robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , before_each =t . describe , t . it , t . before_each 5local clear =n . clear 6local eq =t . eq 7local exec_lua =n . exec_lua 8 9--- @return { [1]: string[], [2]: integer } 10local get_completions =function ( input , env ) 11return exec_lua ( 'return { vim._expand_pat(...) }' , input , env ) 12end 13 14--- @return { [1]: string[], [2]: integer } 15local get_compl_parts =function ( parts ) 16return exec_lua ( 'return { vim._expand_pat_get_parts(...) }' , parts ) 17end 18 19before_each ( clear ) 20 21describe ( 'nlua_expand_pat' , function () 22it ( 'completes exact matches' , function () 23eq ( { { 'exact' } , 0 } , get_completions ( 'exact' , { exact =true } )) 24end ) 25 26it ( 'returns empty table when nothing matches' , function () 27eq ( { {} , 0 } , get_completions ( 'foo' , { bar =true } )) 28 29-- can access non-exist field 30for _ , m in ipairs { 31'vim.' , 32'vim.lsp.' , 33'vim.treesitter.' , 34'vim.deepcopy.' , 35'vim.fn.' , 36'vim.api.' , 37'vim.o.' , 38'vim.b.' , 39} do 40eq ( { {} , m : len () } , get_completions ( m .. 'foo' )) 41eq ( { {} , 0 } , get_completions ( m .. 'foo.' )) 42eq ( { {} , 0 } , get_completions ( m .. 'foo.bar' )) 43eq ( { {} , 0 } , get_completions ( m .. 'foo.bar.' )) 44end 45end ) 46 47it ( 'returns nice completions with function call prefix' , function () 48eq ( { { 'FOO' } , 6 } , get_completions ( 'print(F' , { FOO =true , bawr =true } )) 49end ) 50 51it ( 'returns keys for nested dicts' , function () 52eq ( 53{ { 54'nvim_buf_set_lines' , 55} , 8 } , 56get_completions ( 'vim.api.nvim_buf_' , { 57vim ={ 58api ={ 59nvim_buf_set_lines =true , 60nvim_win_doesnt_match =true , 61} , 62other_key =true , 63} , 64} ) 65) 66end ) 67 68it ( 'with colons' , function () 69eq ( 70{ { 71'bawr' , 72'baz' , 73} , 8 } , 74get_completions ( 'MyClass:b' , { 75MyClass ={ 76baz =true , 77bawr =true , 78foo =false , 79} , 80} ) 81) 82end ) 83 84it ( 'returns keys after string key' , function () 85eq ( 86{ { 87'nvim_buf_set_lines' , 88} , 11 } , 89get_completions ( 'vim["api"].nvim_buf_' , { 90vim ={ 91api ={ 92nvim_buf_set_lines =true , 93nvim_win_doesnt_match =true , 94} , 95other_key =true , 96} , 97} ) 98) 99 100eq ( 101{ { 102'nvim_buf_set_lines' , 103} , 21 } , 104get_completions ( 'vim["nested"]["api"].nvim_buf_' , { 105vim ={ 106nested ={ 107api ={ 108nvim_buf_set_lines =true , 109nvim_win_doesnt_match =true , 110} , 111} , 112other_key =true , 113} , 114} ) 115) 116end ) 117 118it ( 'with lazy submodules of "vim" global' , function () 119eq ( { { 'inspect' , 'inspect_pos' } , 4 } , get_completions ( 'vim.inspec' )) 120eq ( { { 'treesitter' } , 4 } , get_completions ( 'vim.treesi' )) 121eq ( { { 'dev' } , 15 } , get_completions ( 'vim.treesitter.de' )) 122eq ( { { 'edit_query' } , 19 } , get_completions ( 'vim.treesitter.dev.edit_' )) 123eq ( { { 'set' } , 11 } , get_completions ( 'vim.keymap.se' )) 124end ) 125 126it ( 'include keys in mt.__index and ._submodules' , function () 127eq ( 128{ { 'bar1' , 'bar2' , 'bar3' } , 4 } , 129exec_lua ( function () -- metatable cannot be serialized 130return { 131vim . _expand_pat ( 'foo.' , { 132foo =setmetatable ( 133{ bar1 =true , _submodules ={ bar2 =true } } , 134{ __index ={ bar3 =true } } 135), 136} ), 137} 138end ) 139) 140end ) 141 142it ( 'excludes private fields after "."' , function () 143eq ( 144{ { 'bar' } , 4 } , 145get_completions ( 'foo.' , { 146foo ={ 147_bar =true , 148bar =true , 149} , 150} ) 151) 152end ) 153 154it ( 'includes private fields after "._"' , function () 155eq ( 156{ { '_bar' } , 4 } , 157get_completions ( 'foo._' , { 158foo ={ 159_bar =true , 160bar =true , 161} , 162} ) 163) 164end ) 165 166it ( 'can interpolate globals' , function () 167eq ( 168{ { 169'nvim_buf_set_lines' , 170} , 12 } , 171get_completions ( 'vim[MY_VAR].nvim_buf_' , { 172MY_VAR ='api' , 173vim ={ 174api ={ 175nvim_buf_set_lines =true , 176nvim_win_doesnt_match =true , 177} , 178other_key =true , 179} , 180} ) 181) 182end ) 183 184describe ( 'vim.fn' , function () 185it ( 'simple completion' , function () 186local actual =get_completions ( 'vim.fn.did' ) 187local expected ={ 188{ 'did_filetype' } , 189# 'vim.fn.' , 190} 191eq ( expected , actual ) 192end ) 193it ( 'does not suggest "#" items' , function () 194exec_lua [[ 195-- ensure remote#host#... functions exist 196vim.cmd [=[ 197runtime! autoload/remote/host.vim 198]=] 199-- make a dummy call to ensure vim.fn contains an entry: remote#host#... 200vim.fn['remote#host#IsRunning']('python3') 201]] 202local actual =get_completions ( 'vim.fn.remo' ) 203local expected ={ 204{ 'remove' } , -- there should be no completion "remote#host#..." 205# 'vim.fn.' , 206} 207eq ( expected , actual ) 208end ) 209end ) 210 211describe ( 'completes' , function () 212it ( 'vim.v' , function () 213local actual =get_completions ( 'vim.v.t_' ) 214local expected ={ 215{ 't_blob' , 't_bool' , 't_dict' , 't_float' , 't_func' , 't_list' , 't_number' , 't_string' } , 216# 'vim.v.' , 217} 218eq ( expected , actual ) 219end ) 220 221it ( 'vim.g' , function () 222exec_lua [[ 223vim.cmd [=[ 224let g:nlua_foo = 'completion' 225let g:nlua_foo_bar = 'completion' 226let g:nlua_foo#bar = 'nocompletion' " should be excluded from lua completion 227]=] 228]] 229local actual =get_completions ( 'vim.g.nlua' ) 230local expected ={ 231{ 'nlua_foo' , 'nlua_foo_bar' } , 232# 'vim.g.' , 233} 234eq ( expected , actual ) 235end ) 236 237it ( 'vim.b' , function () 238exec_lua [[ 239vim.b.nlua_foo_buf = 'bar' 240vim.b.some_other_vars = 'bar' 241]] 242local actual =get_completions ( 'vim.b.nlua' ) 243local expected ={ 244{ 'nlua_foo_buf' } , 245# 'vim.b.' , 246} 247eq ( expected , actual ) 248end ) 249 250it ( 'vim.w' , function () 251exec_lua [[ 252vim.w.nlua_win_var = 42 253]] 254local actual =get_completions ( 'vim.w.nlua' ) 255local expected ={ 256{ 'nlua_win_var' } , 257# 'vim.w.' , 258} 259eq ( expected , actual ) 260end ) 261 262it ( 'vim.t' , function () 263exec_lua [[ 264vim.t.nlua_tab_var = 42 265]] 266local actual =get_completions ( 'vim.t.' ) 267local expected ={ 268{ 'nlua_tab_var' } , 269# 'vim.t.' , 270} 271eq ( expected , actual ) 272end ) 273 274it ( 'vim.env' , function () 275exec_lua [[ 276vim.env.NLUA_ENV_VAR = 'foo' 277]] 278local actual =get_completions ( 'vim.env.NLUA' ) 279local expected ={ 280{ 'NLUA_ENV_VAR' } , 281# 'vim.env.' , 282} 283eq ( expected , actual ) 284end ) 285end ) 286 287describe ( 'completes' , function () 288-- for { vim.o, vim.go, vim.opt, vim.opt_local, vim.opt_global } 289local test_opt =function ( accessor ) 290it ( accessor , function () 291do 292local actual =get_completions ( accessor .. '.file' ) 293local expected ={ 294'fileencoding' , 295'fileencodings' , 296'fileformat' , 297'fileformats' , 298'fileignorecase' , 299'filetype' , 300} 301eq ( { expected , # accessor + 1 } , actual , accessor .. '.file' ) 302end 303do 304local actual =get_completions ( accessor .. '.winh' ) 305local expected ={ 306'winheight' , 307'winhighlight' , 308} 309eq ( { expected , # accessor + 1 } , actual , accessor .. '.winh' ) 310end 311end ) 312end 313 314test_opt ( 'vim.o' ) 315test_opt ( 'vim.go' ) 316test_opt ( 'vim.opt' ) 317test_opt ( 'vim.opt_local' ) 318test_opt ( 'vim.opt_global' ) 319 320it ( 'vim.o, suggesting all known options' , function () 321local completions =get_completions ( 'vim.o.' )[ 1 ] ---@type string[] 322eq ( 323exec_lua [[ 324return vim.tbl_count(vim.api.nvim_get_all_options_info()) 325]] , 326# completions 327) 328end ) 329 330it ( 'vim.bo' , function () 331do 332local actual =get_completions ( 'vim.bo.file' ) 333local compls ={ 334-- should contain buffer options only 335'fileencoding' , 336'fileformat' , 337'filetype' , 338} 339eq ( { compls , # 'vim.bo.' } , actual ) 340end 341do 342local actual =get_completions ( 'vim.bo.winh' ) 343local compls ={} 344eq ( { compls , # 'vim.bo.' } , actual ) 345end 346end ) 347 348it ( 'vim.wo' , function () 349do 350local actual =get_completions ( 'vim.wo.file' ) 351local compls ={} 352eq ( { compls , # 'vim.wo.' } , actual ) 353end 354do 355local actual =get_completions ( 'vim.wo.winh' ) 356-- should contain window options only 357local compls ={ 'winhighlight' } 358eq ( { compls , # 'vim.wo.' } , actual ) 359end 360end ) 361end ) 362 363it ( 'returns everything if input is empty' , function () 364eq ( { { 'other' , 'vim' } , 0 } , get_completions ( '' , { vim =true , other =true } )) 365end ) 366 367it ( 'get_parts' , function () 368eq ( { {} , 1 } , get_compl_parts ( 'vim' )) 369eq ( { { 'vim' } , 5 } , get_compl_parts ( 'vim.ap' )) 370eq ( { { 'vim' , 'api' } , 9 } , get_compl_parts ( 'vim.api.nvim_buf' )) 371eq ( { { 'vim' , 'api' } , 9 } , get_compl_parts ( 'vim:api.nvim_buf' )) 372eq ( { { 'vim' , 'api' } , 12 } , get_compl_parts ( "vim['api'].nvim_buf" )) 373eq ( { { 'vim' , 'api' } , 12 } , get_compl_parts ( 'vim["api"].nvim_buf' )) 374eq ( { { 'vim' , 'nested' , 'api' } , 22 } , get_compl_parts ( 'vim["nested"]["api"].nvim_buf' )) 375eq ( { { 'vim' , 'nested' , 'api' } , 25 } , get_compl_parts ( 'vim[ "nested" ]["api"].nvim_buf' )) 376eq ( { { 'vim' , { 'NESTED' } , 'api' } , 20 } , get_compl_parts ( 'vim[NESTED]["api"].nvim_buf' )) 377end ) 378end )