summaryrefslogtreecommitdiffstatshomepage
path: root/test/functional/plugin/lsp/inline_completion_spec.lua
blob: a5ecf5fdd5aa5be07aca9f5ac81fa086df8b6d7d (plain)
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
local t = require('test.testutil')
local n = require('test.functional.testnvim')()
local t_lsp = require('test.functional.plugin.lsp.testutil')
local Screen = require('test.functional.ui.screen')

local dedent = t.dedent
local eq = t.eq

local api = n.api
local exec_lua = n.exec_lua
local insert = n.insert
local feed = n.feed

local clear_notrace = t_lsp.clear_notrace
local create_server_definition = t_lsp.create_server_definition

describe('vim.lsp.inline_completion', function()
  local text = dedent([[
    function fibonacci()
  ]])

  local grid_without_candidates = dedent([[
    function fibonacci()                                 |
    ^                                                     |
    {1:~                                                    }|*11
                                                         |
  ]])

  local grid_with_candidates = dedent([[
    function fibonacci({1:n) {}                              |
    {1:  if (n <= 0) return 0;}                              |
    {1:  if (n === 1) return 1;}                             |
                                                         |
    {1:  let a = 0, b = 1, c;}                               |
    {1:  for (let i = 2; i <= n; i++) {}                     |
    {1:    c = a + b;}                                       |
    {1:    a = b;}                                           |
    {1:    b = c;}                                           |
    {1:  }}                                                  |
    {1:  return b;}                                          |
    {1:}}                                                    |
    ^                                                     |
    {3:-- INSERT --}                                         |
  ]])

  local grid_applied_candidates = dedent([[
    function fibonacci(n) {                              |
      if (n <= 0) return 0;                              |
      if (n === 1) return 1;                             |
                                                         |
      let a = 0, b = 1, c;                               |
      for (let i = 2; i <= n; i++) {                     |
        c = a + b;                                       |
        a = b;                                           |
        b = c;                                           |
      }                                                  |
      return b;                                          |
    ^}                                                    |
                                                         |*2
  ]])

  --- @type test.functional.ui.screen
  local screen

  --- @type integer
  local client_id

  before_each(function()
    clear_notrace()
    exec_lua(create_server_definition)

    screen = Screen.new()
    screen:set_default_attr_ids({
      [1] = { bold = true, foreground = Screen.colors.Blue1 },
      [2] = { bold = true, foreground = Screen.colors.SeaGreen4 },
      [3] = { bold = true },
    })

    client_id = exec_lua(function()
      _G.server = _G._create_server({
        capabilities = {
          inlineCompletionProvider = true,
        },
        handlers = {
          ['textDocument/inlineCompletion'] = function(_, _, callback)
            if _G.empty then
              callback(nil, {
                items = {
                  {
                    insertText = 'foobar',
                    range = {
                      start = {
                        line = 0,
                        character = 19,
                      },
                      ['end'] = {
                        line = 0,
                        character = 19,
                      },
                    },
                  },
                },
              })
              return
            end

            callback(nil, {
              items = {
                {
                  command = {
                    command = 'dummy',
                    title = 'Completion Accepted',
                  },
                  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}',
                  range = {
                    ['end'] = {
                      character = 20,
                      line = 0,
                    },
                    start = {
                      character = 0,
                      line = 0,
                    },
                  },
                },
                {
                  command = {
                    command = 'dummy',
                    title = 'Completion Accepted',
                  },
                  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}',
                  range = {
                    ['end'] = {
                      character = 20,
                      line = 0,
                    },
                    start = {
                      character = 0,
                      line = 0,
                    },
                  },
                },
                {
                  command = {
                    command = 'dummy',
                    title = 'Completion Accepted',
                  },
                  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}',
                  range = {
                    ['end'] = {
                      character = 20,
                      line = 0,
                    },
                    start = {
                      character = 0,
                      line = 0,
                    },
                  },
                },
              },
            })
          end,
        },
      })

      return vim.lsp.start({ name = 'dummy', cmd = _G.server.cmd })
    end)

    exec_lua(function()
      local client = assert(vim.lsp.get_client_by_id(client_id))
      _G.called = false
      client.commands.dummy = function()
        _G.called = true
      end
    end)

    insert(text)
    feed('$')
    exec_lua(function()
      vim.lsp.inline_completion.enable()
    end)
  end)

  after_each(function()
    api.nvim_exec_autocmds('VimLeavePre', { modeline = false })
  end)

  describe('enable()', function()
    it('requests or abort when entered/left insert mode', function()
      screen:expect({ grid = grid_without_candidates })
      feed('i')
      screen:expect({ grid = grid_with_candidates })
      feed('<Esc>')
      screen:expect({ grid = grid_without_candidates })
    end)

    it('no request when leaving insert mode immediately after typing', function()
      screen:expect({ grid = grid_without_candidates })
      feed('ifoobar<Esc>')
      screen:expect([[
        function fibonacci()                                 |
        fooba^r                                               |
        {1:~                                                    }|*11
                                                             |
      ]])
      screen:expect_unchanged(false, 500)
    end)
  end)

  describe('get()', function()
    it('applies the current candidate', function()
      feed('i')
      screen:expect({ grid = grid_with_candidates })
      exec_lua(function()
        vim.lsp.inline_completion.get()
      end)
      n.poke_eventloop()
      feed('<Esc>')
      screen:expect({ grid = grid_applied_candidates })
    end)

    it('correctly displays with absent/empty range', function()
      exec_lua(function()
        _G.empty = true
      end)
      feed('I')
      screen:expect([[
        function fibonacci({1:foobar})                           |
        ^                                                     |
        {1:~                                                    }|*11
        {3:-- INSERT --}                                         |
      ]])
    end)

    it('accepts on_accept callback', function()
      feed('i')
      screen:expect({ grid = grid_with_candidates })
      local result = exec_lua(function()
        ---@type vim.lsp.inline_completion.Item
        local result
        vim.lsp.inline_completion.get({
          on_accept = function(item)
            result = item
          end,
        })
        vim.wait(1000, function()
          return result ~= nil
        end) -- Wait for async callback.
        return result
      end)
      feed('<Esc>')
      screen:expect({ grid = grid_without_candidates })
      eq({
        _index = 1,
        client_id = 1,
        command = {
          command = 'dummy',
          title = 'Completion Accepted',
        },
        insert_text = dedent([[
        function fibonacci(n) {
          if (n <= 0) return 0;
          if (n === 1) return 1;

          let a = 0, b = 1, c;
          for (let i = 2; i <= n; i++) {
            c = a + b;
            a = b;
            b = c;
          }
          return b;
        }]]),
        range = {
          0,
          0,
          0,
          20,
          1,
        },
      }, result)
    end)
  end)

  describe('select()', function()
    it('selects the next candidate', function()
      feed('i')
      screen:expect({ grid = grid_with_candidates })

      exec_lua(function()
        vim.lsp.inline_completion.select()
      end)

      screen:expect([[
        function fibonacci({1:n) {}                              |
        {1:  if (n <= 0) return 0;}                              |
        {1:  if (n === 1) return 1;}                             |
                                                             |
        {1:  let a = 0, b = 1, c;}                               |
        {1:  for (let i = 2; i <= n; i++) {}                     |
        {1:    c = a + b;}                                       |
        {1:    a = b;}                                           |
        {1:    b = c;}                                           |
        {1:  }}                                                  |
        {1:  return c;}                                          |
        {1:}}{2: (2/3)}                                              |
        ^                                                     |
        {3:-- INSERT --}                                         |
      ]])
      exec_lua(function()
        vim.lsp.inline_completion.get()
      end)
      n.poke_eventloop()
      feed('<Esc>')
      screen:expect([[
        function fibonacci(n) {                              |
          if (n <= 0) return 0;                              |
          if (n === 1) return 1;                             |
                                                             |
          let a = 0, b = 1, c;                               |
          for (let i = 2; i <= n; i++) {                     |
            c = a + b;                                       |
            a = b;                                           |
            b = c;                                           |
          }                                                  |
          return c;                                          |
        ^}                                                    |
                                                             |*2
      ]])
    end)
  end)
end)