robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

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