robin/laufey

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

robinmerge: 9a29622b3b00ddb601

master
13.0 KiB382 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3local t_lsp = require('test.functional.plugin.lsp.testutil')
4local Screen = require('test.functional.ui.screen')
5
6local describe, it, before_each, after_each = t.describe, t.it, t.before_each, t.after_each
7local dedent = t.dedent
8local eq = t.eq
9
10local api = n.api
11local exec_lua = n.exec_lua
12local insert = n.insert
13local feed = n.feed
14
15local clear_notrace = t_lsp.clear_notrace
16local create_server_definition = t_lsp.create_server_definition
17
18describe('vim.lsp.inline_completion', function()
19  local text = dedent([[
20    function fibonacci()
21  ]])
22
23  local grid_without_candidates = dedent([[
24    function fibonacci()                                 |
25    ^                                                     |
26    {1:~                                                    }|*11
27                                                         |
28  ]])
29
30  local grid_with_candidates = dedent([[
31    function fibonacci({1:n) {}                              |
32    {1:  if (n <= 0) return 0;}                              |
33    {1:  if (n === 1) return 1;}                             |
34                                                         |
35    {1:  let a = 0, b = 1, c;}                               |
36    {1:  for (let i = 2; i <= n; i++) {}                     |
37    {1:    c = a + b;}                                       |
38    {1:    a = b;}                                           |
39    {1:    b = c;}                                           |
40    {1:  }}                                                  |
41    {1:  return b;}                                          |
42    {1:}}                                                    |
43    ^                                                     |
44    {3:-- INSERT --}                                         |
45  ]])
46
47  local grid_applied_candidates = dedent([[
48    function fibonacci(n) {                              |
49      if (n <= 0) return 0;                              |
50      if (n === 1) return 1;                             |
51                                                         |
52      let a = 0, b = 1, c;                               |
53      for (let i = 2; i <= n; i++) {                     |
54        c = a + b;                                       |
55        a = b;                                           |
56        b = c;                                           |
57      }                                                  |
58      return b;                                          |
59    ^}                                                    |
60                                                         |*2
61  ]])
62
63  --- @type test.functional.ui.screen
64  local screen
65
66  --- @type integer
67  local client_id
68
69  before_each(function()
70    clear_notrace()
71    exec_lua(create_server_definition)
72
73    screen = Screen.new()
74    screen:set_default_attr_ids({
75      [1] = { bold = true, foreground = Screen.colors.Blue1 },
76      [2] = { bold = true, foreground = Screen.colors.SeaGreen4 },
77      [3] = { bold = true },
78    })
79
80    client_id = exec_lua(function()
81      _G.server = _G._create_server({
82        capabilities = {
83          inlineCompletionProvider = true,
84        },
85        handlers = {
86          ['textDocument/inlineCompletion'] = function(_, _, callback)
87            if _G.items then
88              callback(nil, { items = _G.items })
89              return
90            end
91
92            if _G.empty then
93              callback(nil, {
94                items = {
95                  {
96                    insertText = 'foobar',
97                    range = {
98                      start = {
99                        line = 0,
100                        character = 19,
101                      },
102                      ['end'] = {
103                        line = 0,
104                        character = 19,
105                      },
106                    },
107                  },
108                },
109              })
110              return
111            end
112
113            callback(nil, {
114              items = {
115                {
116                  command = {
117                    command = 'dummy',
118                    title = 'Completion Accepted',
119                  },
120                  insertText = 'function fibonacci(n) {\n  if (n <= 0) return 0;\n  if (n === 1) return 1;\n\n  let a = 0, b = 1, c;\n  for (let i = 2; i <= n; i++) {\n    c = a + b;\n    a = b;\n    b = c;\n  }\n  return b;\n}',
121                  range = {
122                    ['end'] = {
123                      character = 20,
124                      line = 0,
125                    },
126                    start = {
127                      character = 0,
128                      line = 0,
129                    },
130                  },
131                },
132                {
133                  command = {
134                    command = 'dummy',
135                    title = 'Completion Accepted',
136                  },
137                  insertText = 'function fibonacci(n) {\n  if (n <= 0) return 0;\n  if (n === 1) return 1;\n\n  let a = 0, b = 1, c;\n  for (let i = 2; i <= n; i++) {\n    c = a + b;\n    a = b;\n    b = c;\n  }\n  return c;\n}',
138                  range = {
139                    ['end'] = {
140                      character = 20,
141                      line = 0,
142                    },
143                    start = {
144                      character = 0,
145                      line = 0,
146                    },
147                  },
148                },
149                {
150                  command = {
151                    command = 'dummy',
152                    title = 'Completion Accepted',
153                  },
154                  insertText = 'function fibonacci(n) {\n  if (n < 0) {\n    throw new Error("Input must be a non-negative integer.");\n  }\n  if (n === 0) return 0;\n  if (n === 1) return 1;\n\n  let a = 0, b = 1, c;\n  for (let i = 2; i <= n; i++) {\n    c = a + b;\n    a = b;\n    b = c;\n  }\n  return b;\n}',
155                  range = {
156                    ['end'] = {
157                      character = 20,
158                      line = 0,
159                    },
160                    start = {
161                      character = 0,
162                      line = 0,
163                    },
164                  },
165                },
166              },
167            })
168          end,
169        },
170      })
171
172      return vim.lsp.start({ name = 'dummy', cmd = _G.server.cmd })
173    end)
174
175    exec_lua(function()
176      local client = assert(vim.lsp.get_client_by_id(client_id))
177      _G.called = false
178      client.commands.dummy = function()
179        _G.called = true
180      end
181    end)
182
183    insert(text)
184    feed('$')
185    exec_lua(function()
186      vim.lsp.inline_completion.enable()
187    end)
188  end)
189
190  after_each(function()
191    api.nvim_exec_autocmds('VimLeavePre', { modeline = false })
192  end)
193
194  describe('enable()', function()
195    it('requests or abort when entered/left insert mode', function()
196      screen:expect({ grid = grid_without_candidates })
197      feed('i')
198      screen:expect({ grid = grid_with_candidates })
199      feed('<Esc>')
200      screen:expect({ grid = grid_without_candidates })
201    end)
202
203    it('no request when leaving insert mode immediately after typing', function()
204      screen:expect({ grid = grid_without_candidates })
205      feed('ifoobar<Esc>')
206      screen:expect([[
207        function fibonacci()                                 |
208        fooba^r                                               |
209        {1:~                                                    }|*11
210                                                             |
211      ]])
212      screen:expect_unchanged(false, 500)
213    end)
214  end)
215
216  describe('get()', function()
217    it('applies the current candidate', function()
218      feed('i')
219      screen:expect({ grid = grid_with_candidates })
220      exec_lua(function()
221        vim.lsp.inline_completion.get()
222      end)
223      n.poke_eventloop()
224      feed('<Esc>')
225      screen:expect({ grid = grid_applied_candidates })
226    end)
227
228    it('correctly displays with absent/empty range', function()
229      exec_lua(function()
230        _G.empty = true
231      end)
232      feed('I')
233      screen:expect([[
234        function fibonacci({1:foobar})                           |
235        ^                                                     |
236        {1:~                                                    }|*11
237        {3:-- INSERT --}                                         |
238      ]])
239    end)
240
241    it('accepts on_accept callback', function()
242      feed('i')
243      screen:expect({ grid = grid_with_candidates })
244      local result = exec_lua(function()
245        ---@type vim.lsp.inline_completion.Item
246        local result
247        vim.lsp.inline_completion.get({
248          on_accept = function(item)
249            result = item
250          end,
251        })
252        vim.wait(1000, function()
253          return result ~= nil
254        end) -- Wait for async callback.
255        return result
256      end)
257      feed('<Esc>')
258      screen:expect({ grid = grid_without_candidates })
259      eq({
260        _index = 1,
261        client_id = 1,
262        command = {
263          command = 'dummy',
264          title = 'Completion Accepted',
265        },
266        insert_text = dedent([[
267        function fibonacci(n) {
268          if (n <= 0) return 0;
269          if (n === 1) return 1;
270
271          let a = 0, b = 1, c;
272          for (let i = 2; i <= n; i++) {
273            c = a + b;
274            a = b;
275            b = c;
276          }
277          return b;
278        }]]),
279        range = {
280          0,
281          0,
282          0,
283          20,
284          1,
285        },
286      }, result)
287    end)
288
289    it('accepts an item after the line shrank past its range', function()
290      feed('i')
291      screen:expect({ grid = grid_with_candidates })
292      exec_lua(function()
293        -- Shrink the line the item's range covers, as backspacing would.
294        vim.api.nvim_buf_set_text(0, 0, 18, 0, 20, {})
295        vim.lsp.inline_completion.get()
296      end)
297      n.poke_eventloop()
298      feed('<Esc>')
299      screen:expect({ grid = grid_applied_candidates })
300    end)
301
302    it('does not leave behind text typed since the request', function()
303      exec_lua(function()
304        _G.items = {
305          {
306            insertText = 'foobar',
307            range = {
308              start = { line = 1, character = 0 },
309              ['end'] = { line = 1, character = 1 },
310            },
311          },
312        }
313      end)
314      feed('ifo')
315      screen:expect([[
316        function fibonacci()                                 |
317        fo{1:^obar}                                               |
318        {1:~                                                    }|*11
319        {3:-- INSERT --}                                         |
320      ]])
321      exec_lua(function()
322        vim.lsp.inline_completion.get()
323      end)
324      n.poke_eventloop()
325      feed('<Esc>')
326      screen:expect([[
327        function fibonacci()                                 |
328        fooba^r                                               |
329        {1:~                                                    }|*11
330                                                             |
331      ]])
332    end)
333  end)
334
335  describe('select()', function()
336    it('selects the next candidate', function()
337      feed('i')
338      screen:expect({ grid = grid_with_candidates })
339
340      exec_lua(function()
341        vim.lsp.inline_completion.select()
342      end)
343
344      screen:expect([[
345        function fibonacci({1:n) {}                              |
346        {1:  if (n <= 0) return 0;}                              |
347        {1:  if (n === 1) return 1;}                             |
348                                                             |
349        {1:  let a = 0, b = 1, c;}                               |
350        {1:  for (let i = 2; i <= n; i++) {}                     |
351        {1:    c = a + b;}                                       |
352        {1:    a = b;}                                           |
353        {1:    b = c;}                                           |
354        {1:  }}                                                  |
355        {1:  return c;}                                          |
356        {1:}}{2: (2/3)}                                              |
357        ^                                                     |
358        {3:-- INSERT --}                                         |
359      ]])
360      exec_lua(function()
361        vim.lsp.inline_completion.get()
362      end)
363      n.poke_eventloop()
364      feed('<Esc>')
365      screen:expect([[
366        function fibonacci(n) {                              |
367          if (n <= 0) return 0;                              |
368          if (n === 1) return 1;                             |
369                                                             |
370          let a = 0, b = 1, c;                               |
371          for (let i = 2; i <= n; i++) {                     |
372            c = a + b;                                       |
373            a = b;                                           |
374            b = c;                                           |
375          }                                                  |
376          return c;                                          |
377        ^}                                                    |
378                                                             |*2
379      ]])
380    end)
381  end)
382end)