robin/laufey

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

not_compiledfix(spell): avoid invalid window state after async spell select (#41346)8c0bf18374

master
9.4 KiB321 linesraw
1-- Tests for vim.ui.select(), including integration with builtins (:tselect, z=).
2
3local t = require('test.testutil')
4local describe, it, before_each, finally = t.describe, t.it, t.before_each, t.finally
5local retry = t.retry
6local n = require('test.functional.testnvim')()
7local clear = n.clear
8local exec_lua = n.exec_lua
9local api = n.api
10local eq = t.eq
11local neq = t.neq
12local write_file = t.write_file
13
14before_each(clear)
15
16--- Mock async vim.ui.select impl. Imitates fzf-lua/telescope/snacks: opens a transient floating
17--- window, then schedules on_choice to fire on the next event-loop tick.
18---
19--- Sets `_G._captured` so tests can assert the user choice.
20--- @param pick integer|nil 1-based index to "pick" (nil cancels).
21local function setup_async_picker(pick)
22  exec_lua(function()
23    _G._captured = nil
24    --- @diagnostic disable-next-line: duplicate-set-field
25    vim.ui.select = function(items, opts, on_choice)
26      _G._captured = { items = items, opts = opts }
27      -- Open a floating window like a real picker would.
28      local buf = vim.api.nvim_create_buf(false, true)
29      local win = vim.api.nvim_open_win(buf, false, {
30        relative = 'editor',
31        row = 1,
32        col = 1,
33        width = 30,
34        height = math.min(#items, 5),
35      })
36      _G._captured.win = win
37      -- Defer the choice so the wait actually has to pump events.
38      vim.defer_fn(function()
39        if vim.api.nvim_win_is_valid(win) then
40          vim.api.nvim_win_close(win, true)
41        end
42        if pick then
43          on_choice(items[pick], pick)
44        else
45          on_choice(nil, nil)
46        end
47      end, 30)
48    end
49  end, pick)
50end
51
52--- Mock fzf-lua-style picker: opens a floating window with a *terminal* buffer running a small
53--- shell command. When the command exits we treat the user as having "picked" `pick`.
54local function setup_term_picker(pick)
55  exec_lua(function(pick_, prog)
56    _G._captured = nil
57    --- @diagnostic disable-next-line: duplicate-set-field
58    vim.ui.select = function(items, opts, on_choice)
59      _G._captured = { items = items, opts = opts }
60      local buf = vim.api.nvim_create_buf(false, true)
61      local win = vim.api.nvim_open_win(buf, true, {
62        relative = 'editor',
63        row = 1,
64        col = 1,
65        width = 30,
66        height = math.min(#items, 5),
67      })
68      vim.fn.jobstart({ prog }, {
69        term = true,
70        on_exit = function()
71          if vim.api.nvim_win_is_valid(win) then
72            vim.api.nvim_win_close(win, true)
73          end
74          if pick_ then
75            on_choice(items[pick_], pick_)
76          else
77            on_choice(nil, nil)
78          end
79        end,
80      })
81    end
82  end, pick, n.testprg('shell-test'))
83end
84
85describe('vim.ui.select()', function()
86  it('can select an item', function()
87    local result = exec_lua [[
88      local items = {
89        { name = 'Item 1' },
90        { name = 'Item 2' },
91      }
92      local opts = {
93        format_item = function(entry)
94          return entry.name
95        end
96      }
97      local selected
98      local cb = function(item)
99        selected = item
100      end
101      -- inputlist would require input and block the test;
102      local choices
103      vim.fn.inputlist = function(x)
104        choices = x
105        return 1
106      end
107      vim.ui.select(items, opts, cb)
108      vim.wait(100, function() return selected ~= nil end)
109      return {selected, choices}
110    ]]
111    eq({ name = 'Item 1' }, result[1])
112    eq({
113      'Select one of:',
114      '1: Item 1',
115      '2: Item 2',
116    }, result[2])
117  end)
118
119  describe('via :tselect', function()
120    local function prepare_test()
121      -- Create dummy source files so the jump succeeds.
122      write_file('XselTagA.c', 'int foo;\n')
123      write_file('XselTagB.c', 'int foo = 1;\n')
124      finally(function()
125        os.remove('XselTagA.c')
126        os.remove('XselTagB.c')
127        os.remove('XselTags')
128      end)
129      write_file(
130        'XselTags',
131        '!_TAG_FILE_FORMAT\t2\t/extended format/\n'
132          .. 'foo\tXselTagA.c\t/^int foo;$/;"\tv\n'
133          .. 'foo\tXselTagB.c\t/^int foo = 1;$/;"\tv\n'
134      )
135      api.nvim_set_option_value('tags', 'XselTags', {})
136    end
137
138    it('passes items, gets user choice', function()
139      prepare_test()
140
141      local got = exec_lua(function()
142        --- @diagnostic disable-next-line: duplicate-set-field
143        vim.ui.select = function(items, opts, on_choice)
144          _G._captured = { items = items, kind = opts.kind }
145          -- Pick the second match.
146          on_choice(items[2], 2)
147        end
148        vim.cmd('tselect foo')
149      end)
150      -- on_choice queues `:[idx]tag` via feedkeys; let typeahead drain.
151      retry(nil, 1000, function()
152        eq('XselTagB.c', api.nvim_eval('expand("%:t")'))
153      end)
154      got = exec_lua(function()
155        return _G._captured
156      end)
157
158      eq('tag', got.kind)
159      eq(2, #got.items)
160      eq('foo', got.items[1].tag)
161      eq('XselTagB.c', got.items[2].file)
162    end)
163
164    it('does nothing when the user cancels', function()
165      prepare_test()
166
167      local before = api.nvim_buf_get_name(0)
168      exec_lua(function()
169        vim.ui.select = function(_, _, on_choice)
170          on_choice(nil, nil)
171        end
172        vim.cmd('tselect foo')
173      end)
174
175      eq(before, api.nvim_buf_get_name(0))
176    end)
177
178    it('+ async picker', function()
179      prepare_test()
180
181      setup_async_picker(2)
182      exec_lua([[vim.cmd('tselect foo')]])
183      retry(nil, 1000, function()
184        eq('XselTagB.c', api.nvim_eval('expand("%:t")'))
185      end)
186      eq('tag', exec_lua([[return _G._captured and _G._captured.opts.kind]]))
187    end)
188
189    it('+ async terminal-based picker', function()
190      prepare_test()
191
192      setup_term_picker(2)
193      exec_lua([[vim.cmd('tselect foo')]])
194      retry(nil, 1000, function()
195        eq('XselTagB.c', api.nvim_eval('expand("%:t")'))
196      end)
197    end)
198  end)
199
200  describe('via z=', function()
201    local function prepare_test()
202      api.nvim_set_option_value('spell', true, {})
203      api.nvim_set_option_value('spelllang', 'en_us', {})
204      api.nvim_buf_set_lines(0, 0, -1, false, { 'helo' })
205    end
206
207    it('passes items, gets user choice', function()
208      prepare_test()
209
210      exec_lua(function()
211        vim.cmd('normal! gg0')
212        --- @diagnostic disable-next-line: duplicate-set-field
213        vim.ui.select = function(items, opts, on_choice)
214          _G._captured = { items = items, kind = opts.kind, prompt = opts.prompt }
215          -- Pick the first suggestion.
216          on_choice(items[1], 1)
217        end
218        vim.cmd('normal! z=')
219      end)
220      -- z= delegates to vim.ui.select, see `_core/spell:select_suggest`. on_choice queues
221      -- `:normal! [idx]z=` via feedkeys; let typeahead drain.
222      retry(nil, 1000, function()
223        t.neq('helo', api.nvim_buf_get_lines(0, 0, -1, false)[1])
224      end)
225      local got = exec_lua([[return _G._captured]])
226
227      eq('spell', got.kind)
228      t.matches('helo', got.prompt)
229      eq(got.items[1].word, api.nvim_buf_get_lines(0, 0, -1, false)[1])
230    end)
231
232    it('does not produce E315 when picker changes the current window', function()
233      prepare_test()
234
235      api.nvim_buf_set_lines(0, 0, -1, false, {
236        'Praesent enim diam,',
237        'Praesent enim diam,',
238        'Praesent enim diam,',
239        'Praesent enim diam,',
240        'Praesent enim diam,',
241      })
242      -- Keep the cursor beyond the popup's single line to reproduce E315.
243      api.nvim_win_set_cursor(0, { 5, 0 })
244
245      exec_lua(function()
246        vim.ui.select = function()
247          local buf = vim.api.nvim_create_buf(false, true)
248
249          vim.api.nvim_open_win(buf, true, {
250            row = 10,
251            col = 10,
252            height = 10,
253            width = 10,
254            relative = 'editor',
255            style = 'minimal',
256          })
257        end
258
259        vim.cmd('normal! wz=')
260      end)
261    end)
262
263    it('does nothing when the user cancels', function()
264      prepare_test()
265
266      exec_lua(function()
267        vim.cmd('normal! gg0')
268        vim.ui.select = function(_, _, on_choice)
269          on_choice(nil, nil)
270        end
271        vim.cmd('normal! z=')
272      end)
273
274      eq('helo', api.nvim_buf_get_lines(0, 0, -1, false)[1])
275    end)
276
277    it('+ async picker', function()
278      prepare_test()
279
280      setup_async_picker(1)
281      exec_lua([[vim.cmd('normal! gg0z=')]])
282      retry(nil, 1000, function()
283        neq('helo', api.nvim_buf_get_lines(0, 0, -1, false)[1])
284      end)
285      eq('spell', exec_lua([[return _G._captured and _G._captured.opts.kind]]))
286    end)
287
288    it('+ async terminal-based picker', function()
289      prepare_test()
290
291      setup_term_picker(1)
292      exec_lua([[vim.cmd('normal! gg0z=')]])
293      retry(nil, 1000, function()
294        neq('helo', api.nvim_buf_get_lines(0, 0, -1, false)[1])
295      end)
296    end)
297  end)
298
299  describe('via ":browse oldfiles"', function()
300    it('+ async picker', function()
301      finally(function()
302        os.remove('XselOldA')
303        os.remove('XselOldB')
304      end)
305      write_file('XselOldA', 'a\n')
306      write_file('XselOldB', 'b\n')
307      local cwd = exec_lua([[return vim.uv.cwd()]])
308
309      setup_async_picker(2)
310      exec_lua(function(cwd_)
311        -- v:oldfiles is normally populated via shada; inject directly for the test.
312        vim.v.oldfiles = { cwd_ .. '/XselOldA', cwd_ .. '/XselOldB' }
313        vim.cmd('browse oldfiles')
314      end, cwd)
315      retry(nil, 1000, function()
316        eq('XselOldB', api.nvim_eval('expand("%:t")'))
317      end)
318      eq('oldfiles', exec_lua([[return _G._captured and _G._captured.opts.kind]]))
319    end)
320  end)
321end)