aboutsummaryrefslogtreecommitdiff
path: root/test/functional/lua/command_line_completion_spec.lua
blob: f8786a45bb09a27f707394f3a9aaf49fcc13b975 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
local t = require('test.testutil')
local n = require('test.functional.testnvim')()

local clear = n.clear
local eq = t.eq
local exec_lua = n.exec_lua

--- @return { [1]: string[], [2]: integer }
local get_completions = function(input, env)
  return exec_lua('return { vim._expand_pat(...) }', input, env)
end

--- @return { [1]: string[], [2]: integer }
local get_compl_parts = function(parts)
  return exec_lua('return { vim._expand_pat_get_parts(...) }', parts)
end

before_each(clear)

describe('nlua_expand_pat', function()
  it('should complete exact matches', function()
    eq({ { 'exact' }, 0 }, get_completions('exact', { exact = true }))
  end)

  it('should return empty table when nothing matches', function()
    eq({ {}, 0 }, get_completions('foo', { bar = true }))
  end)

  it('should return nice completions with function call prefix', function()
    eq({ { 'FOO' }, 6 }, get_completions('print(F', { FOO = true, bawr = true }))
  end)

  it('should return keys for nested dictionaries', function()
    eq(
      { {
        'nvim_buf_set_lines',
      }, 8 },
      get_completions('vim.api.nvim_buf_', {
        vim = {
          api = {
            nvim_buf_set_lines = true,
            nvim_win_doesnt_match = true,
          },
          other_key = true,
        },
      })
    )
  end)

  it('it should work with colons', function()
    eq(
      { {
        'bawr',
        'baz',
      }, 8 },
      get_completions('MyClass:b', {
        MyClass = {
          baz = true,
          bawr = true,
          foo = false,
        },
      })
    )
  end)

  it('should return keys for string reffed dictionaries', function()
    eq(
      { {
        'nvim_buf_set_lines',
      }, 11 },
      get_completions('vim["api"].nvim_buf_', {
        vim = {
          api = {
            nvim_buf_set_lines = true,
            nvim_win_doesnt_match = true,
          },
          other_key = true,
        },
      })
    )
  end)

  it('should return keys for string reffed dictionaries', function()
    eq(
      { {
        'nvim_buf_set_lines',
      }, 21 },
      get_completions('vim["nested"]["api"].nvim_buf_', {
        vim = {
          nested = {
            api = {
              nvim_buf_set_lines = true,
              nvim_win_doesnt_match = true,
            },
          },
          other_key = true,
        },
      })
    )
  end)

  it('should work with lazy submodules of "vim" global', function()
    eq({ { 'inspect', 'inspect_pos' }, 4 }, get_completions('vim.inspec'))

    eq({ { 'treesitter' }, 4 }, get_completions('vim.treesi'))

    eq({ { 'set' }, 11 }, get_completions('vim.keymap.se'))
  end)

  it('should be able to interpolate globals', function()
    eq(
      { {
        'nvim_buf_set_lines',
      }, 12 },
      get_completions('vim[MY_VAR].nvim_buf_', {
        MY_VAR = 'api',
        vim = {
          api = {
            nvim_buf_set_lines = true,
            nvim_win_doesnt_match = true,
          },
          other_key = true,
        },
      })
    )
  end)

  describe('should complete vim.fn', function()
    it('correctly works for simple completion', function()
      local actual = get_completions('vim.fn.did')
      local expected = {
        { 'did_filetype' },
        #'vim.fn.',
      }
      eq(expected, actual)
    end)
    it('should not suggest items with #', function()
      exec_lua [[
        -- ensure remote#host#... functions exist
        vim.cmd [=[
          runtime! autoload/remote/host.vim
        ]=]
        -- make a dummy call to ensure vim.fn contains an entry: remote#host#...
        vim.fn['remote#host#IsRunning']('python3')
      ]]
      local actual = get_completions('vim.fn.remo')
      local expected = {
        { 'remove' }, -- there should be no completion "remote#host#..."
        #'vim.fn.',
      }
      eq(expected, actual)
    end)
  end)

  describe('should complete for variable accessors for', function()
    it('vim.v', function()
      local actual = get_completions('vim.v.t_')
      local expected = {
        { 't_blob', 't_bool', 't_dict', 't_float', 't_func', 't_list', 't_number', 't_string' },
        #'vim.v.',
      }
      eq(expected, actual)
    end)

    it('vim.g', function()
      exec_lua [[
        vim.cmd [=[
          let g:nlua_foo = 'completion'
          let g:nlua_foo_bar = 'completion'
          let g:nlua_foo#bar = 'nocompletion'  " should be excluded from lua completion
        ]=]
      ]]
      local actual = get_completions('vim.g.nlua')
      local expected = {
        { 'nlua_foo', 'nlua_foo_bar' },
        #'vim.g.',
      }
      eq(expected, actual)
    end)

    it('vim.b', function()
      exec_lua [[
        vim.b.nlua_foo_buf = 'bar'
        vim.b.some_other_vars = 'bar'
      ]]
      local actual = get_completions('vim.b.nlua')
      local expected = {
        { 'nlua_foo_buf' },
        #'vim.b.',
      }
      eq(expected, actual)
    end)

    it('vim.w', function()
      exec_lua [[
        vim.w.nlua_win_var = 42
      ]]
      local actual = get_completions('vim.w.nlua')
      local expected = {
        { 'nlua_win_var' },
        #'vim.w.',
      }
      eq(expected, actual)
    end)

    it('vim.t', function()
      exec_lua [[
        vim.t.nlua_tab_var = 42
      ]]
      local actual = get_completions('vim.t.')
      local expected = {
        { 'nlua_tab_var' },
        #'vim.t.',
      }
      eq(expected, actual)
    end)
  end)

  describe('should complete for option accessors for', function()
    -- for { vim.o, vim.go, vim.opt, vim.opt_local, vim.opt_global }
    local test_opt = function(accessor)
      do
        local actual = get_completions(accessor .. '.file')
        local expected = {
          'fileencoding',
          'fileencodings',
          'fileformat',
          'fileformats',
          'fileignorecase',
          'filetype',
        }
        eq({ expected, #accessor + 1 }, actual, accessor .. '.file')
      end
      do
        local actual = get_completions(accessor .. '.winh')
        local expected = {
          'winheight',
          'winhighlight',
        }
        eq({ expected, #accessor + 1 }, actual, accessor .. '.winh')
      end
    end

    test_opt('vim.o')
    test_opt('vim.go')
    test_opt('vim.opt')
    test_opt('vim.opt_local')
    test_opt('vim.opt_global')

    it('vim.o, suggesting all the known options', function()
      local completions = get_completions('vim.o.')[1] ---@type string[]
      eq(
        exec_lua [[
        return vim.tbl_count(vim.api.nvim_get_all_options_info())
      ]],
        #completions
      )
    end)

    it('vim.bo', function()
      do
        local actual = get_completions('vim.bo.file')
        local compls = {
          -- should contain buffer options only
          'fileencoding',
          'fileformat',
          'filetype',
        }
        eq({ compls, #'vim.bo.' }, actual)
      end
      do
        local actual = get_completions('vim.bo.winh')
        local compls = {}
        eq({ compls, #'vim.bo.' }, actual)
      end
    end)

    it('vim.wo', function()
      do
        local actual = get_completions('vim.wo.file')
        local compls = {}
        eq({ compls, #'vim.wo.' }, actual)
      end
      do
        local actual = get_completions('vim.wo.winh')
        -- should contain window options only
        local compls = { 'winhighlight' }
        eq({ compls, #'vim.wo.' }, actual)
      end
    end)
  end)

  it('should return everything if the input is of length 0', function()
    eq({ { 'other', 'vim' }, 0 }, get_completions('', { vim = true, other = true }))
  end)

  describe('get_parts', function()
    it('should return an empty list for no separators', function()
      eq({ {}, 1 }, get_compl_parts('vim'))
    end)

    it('just the first item before a period', function()
      eq({ { 'vim' }, 5 }, get_compl_parts('vim.ap'))
    end)

    it('should return multiple parts just for period', function()
      eq({ { 'vim', 'api' }, 9 }, get_compl_parts('vim.api.nvim_buf'))
    end)

    it('should be OK with colons', function()
      eq({ { 'vim', 'api' }, 9 }, get_compl_parts('vim:api.nvim_buf'))
    end)

    it('should work for just one string ref', function()
      eq({ { 'vim', 'api' }, 12 }, get_compl_parts("vim['api'].nvim_buf"))
    end)

    it('should work for just one string ref, with double quote', function()
      eq({ { 'vim', 'api' }, 12 }, get_compl_parts('vim["api"].nvim_buf'))
    end)

    it('should allows back-to-back string ref', function()
      eq({ { 'vim', 'nested', 'api' }, 22 }, get_compl_parts('vim["nested"]["api"].nvim_buf'))
    end)

    it('should allows back-to-back string ref with spaces before and after', function()
      eq({ { 'vim', 'nested', 'api' }, 25 }, get_compl_parts('vim[ "nested"  ]["api"].nvim_buf'))
    end)

    it('should allow VAR style loolup', function()
      eq({ { 'vim', { 'NESTED' }, 'api' }, 20 }, get_compl_parts('vim[NESTED]["api"].nvim_buf'))
    end)
  end)
end)