robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
13.0 KiB457 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3local Screen = require('test.functional.ui.screen')
4local t_lsp = require('test.functional.plugin.lsp.testutil')
5
6local describe, it, before_each, after_each = t.describe, t.it, t.before_each, t.after_each
7local eq = t.eq
8local pcall_err = t.pcall_err
9local dedent = t.dedent
10local exec_lua = n.exec_lua
11local insert = n.insert
12local feed = n.feed
13local api = n.api
14
15local clear_notrace = t_lsp.clear_notrace
16local create_server_definition = t_lsp.create_server_definition
17
18describe('vim.lsp.inlay_hint', function()
19  local text = dedent([[
20auto add(int a, int b) { return a + b; }
21
22int main() {
23    int x = 1;
24    int y = 2;
25    return add(x,y);
26}
27}]])
28
29  ---@type lsp.InlayHint[]
30  local response = {
31    {
32      kind = 1,
33      paddingLeft = false,
34      paddingRight = false,
35      label = '-> int',
36      position = { character = 22, line = 0 },
37    },
38    {
39      kind = 2,
40      paddingLeft = false,
41      paddingRight = true,
42      label = 'a:',
43      position = { character = 15, line = 5 },
44    },
45    {
46      kind = 2,
47      paddingLeft = false,
48      paddingRight = true,
49      label = 'b:',
50      position = { character = 17, line = 5 },
51    },
52  }
53
54  local grid_without_inlay_hints = [[
55  auto add(int a, int b) { return a + b; }          |
56                                                    |
57  int main() {                                      |
58      int x = 1;                                    |
59      int y = 2;                                    |
60      return add(x,y);                              |
61  }                                                 |
62  ^}                                                 |
63                                                    |
64]]
65
66  local grid_with_inlay_hints = [[
67  auto add(int a, int b){1:-> int} { return a + b; }    |
68                                                    |
69  int main() {                                      |
70      int x = 1;                                    |
71      int y = 2;                                    |
72      return add({1:a:} x,{1:b:} y);                        |
73  }                                                 |
74  ^}                                                 |
75                                                    |
76]]
77
78  --- @type test.functional.ui.screen
79  local screen
80
81  --- @type integer
82  local client_id
83
84  --- @type integer
85  local bufnr
86
87  before_each(function()
88    clear_notrace()
89    screen = Screen.new(50, 9)
90
91    bufnr = n.api.nvim_get_current_buf()
92    exec_lua(create_server_definition)
93    client_id = exec_lua(function()
94      _G.server = _G._create_server({
95        capabilities = {
96          textDocumentSync = vim.lsp.protocol.TextDocumentSyncKind.Full,
97          inlayHintProvider = true,
98        },
99        handlers = {
100          ['textDocument/inlayHint'] = function(_, _, callback)
101            callback(nil, response)
102          end,
103        },
104      })
105
106      return vim.lsp.start({ name = 'dummy', cmd = _G.server.cmd })
107    end)
108
109    insert(text)
110    exec_lua(function()
111      vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })
112    end)
113    screen:expect({ grid = grid_with_inlay_hints })
114  end)
115
116  after_each(function()
117    api.nvim_exec_autocmds('VimLeavePre', { modeline = false })
118  end)
119
120  it('clears inlay hints when sole client detaches', function()
121    exec_lua(function()
122      vim.lsp.get_client_by_id(client_id):stop()
123    end)
124    screen:expect({ grid = grid_without_inlay_hints, unchanged = true })
125  end)
126
127  it('does not clear inlay hints when one of several clients detaches', function()
128    local client_id2 = exec_lua(function()
129      _G.server2 = _G._create_server({
130        capabilities = {
131          textDocumentSync = vim.lsp.protocol.TextDocumentSyncKind.Full,
132          inlayHintProvider = true,
133        },
134        handlers = {
135          ['textDocument/inlayHint'] = function(_, _, callback)
136            callback(nil, {})
137          end,
138        },
139      })
140      return vim.lsp.start({ name = 'dummy2', cmd = _G.server2.cmd })
141    end)
142
143    exec_lua(function()
144      vim.lsp.get_client_by_id(client_id2):stop()
145    end)
146    screen:expect({ grid = grid_with_inlay_hints, unchanged = true })
147  end)
148
149  describe('enable()', function()
150    it('validation', function()
151      t.matches(
152        'enable: expected boolean, got table',
153        t.pcall_err(exec_lua, function()
154          --- @diagnostic disable-next-line:param-type-mismatch
155          vim.lsp.inlay_hint.enable({}, { bufnr = bufnr })
156        end)
157      )
158      t.matches(
159        'enable: expected boolean, got number',
160        t.pcall_err(exec_lua, function()
161          --- @diagnostic disable-next-line:param-type-mismatch
162          vim.lsp.inlay_hint.enable(42)
163        end)
164      )
165      t.matches(
166        'filter: expected table, got number',
167        t.pcall_err(exec_lua, function()
168          --- @diagnostic disable-next-line:param-type-mismatch
169          vim.lsp.inlay_hint.enable(true, 42)
170        end)
171      )
172    end)
173  end)
174
175  describe('clears/applies inlay hints when passed false/true/nil', function()
176    local bufnr2 --- @type integer
177    before_each(function()
178      bufnr2 = exec_lua(function()
179        local bufnr2_0 = vim.api.nvim_create_buf(true, false)
180        vim.lsp.buf_attach_client(bufnr2_0, client_id)
181        vim.api.nvim_win_set_buf(0, bufnr2_0)
182        return bufnr2_0
183      end)
184      insert(text)
185      screen:expect({ grid = grid_without_inlay_hints })
186      exec_lua(function()
187        vim.lsp.inlay_hint.enable(true, { bufnr = bufnr2 })
188      end)
189      screen:expect({ grid = grid_with_inlay_hints })
190    end)
191
192    it('for one single buffer', function()
193      exec_lua(function()
194        vim.lsp.inlay_hint.enable(false, { bufnr = bufnr })
195        vim.api.nvim_win_set_buf(0, bufnr2)
196      end)
197      screen:expect({ grid = grid_with_inlay_hints, unchanged = true })
198      n.api.nvim_win_set_buf(0, bufnr)
199      screen:expect({ grid = grid_without_inlay_hints, unchanged = true })
200
201      exec_lua(function()
202        vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })
203      end)
204      screen:expect({ grid = grid_with_inlay_hints, unchanged = true })
205
206      exec_lua(function()
207        vim.lsp.inlay_hint.enable(
208          not vim.lsp.inlay_hint.is_enabled({ bufnr = bufnr }),
209          { bufnr = bufnr }
210        )
211      end)
212      screen:expect({ grid = grid_without_inlay_hints, unchanged = true })
213
214      exec_lua(function()
215        vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })
216      end)
217      screen:expect({ grid = grid_with_inlay_hints, unchanged = true })
218    end)
219
220    it('for all buffers', function()
221      exec_lua(function()
222        vim.lsp.inlay_hint.enable(false)
223      end)
224      screen:expect({ grid = grid_without_inlay_hints, unchanged = true })
225      n.api.nvim_win_set_buf(0, bufnr2)
226      screen:expect({ grid = grid_without_inlay_hints, unchanged = true })
227
228      exec_lua(function()
229        vim.lsp.inlay_hint.enable(true)
230      end)
231      screen:expect({ grid = grid_with_inlay_hints, unchanged = true })
232      n.api.nvim_win_set_buf(0, bufnr)
233      screen:expect({ grid = grid_with_inlay_hints, unchanged = true })
234    end)
235  end)
236
237  describe('get()', function()
238    it('returns filtered inlay hints', function()
239      local expected2 = {
240        kind = 1,
241        paddingLeft = false,
242        label = ': int',
243        position = {
244          character = 10,
245          line = 2,
246        },
247        paddingRight = false,
248      }
249
250      exec_lua(function()
251        _G.server2 = _G._create_server({
252          capabilities = {
253            textDocumentSync = vim.lsp.protocol.TextDocumentSyncKind.Full,
254            inlayHintProvider = true,
255          },
256          handlers = {
257            ['textDocument/inlayHint'] = function(_, _, callback)
258              callback(nil, { expected2 })
259            end,
260          },
261        })
262        _G.client2 = vim.lsp.start({ name = 'dummy2', cmd = _G.server2.cmd })
263      end)
264
265      --- @type vim.lsp.inlay_hint.get.ret
266      eq(
267        {
268          { bufnr = 1, client_id = 1, inlay_hint = response[1] },
269          { bufnr = 1, client_id = 1, inlay_hint = response[2] },
270          { bufnr = 1, client_id = 1, inlay_hint = response[3] },
271          { bufnr = 1, client_id = 2, inlay_hint = expected2 },
272        },
273        exec_lua(function()
274          return vim.lsp.inlay_hint.get()
275        end)
276      )
277
278      eq(
279        {
280          { bufnr = 1, client_id = 2, inlay_hint = expected2 },
281        },
282        exec_lua(function()
283          return vim.lsp.inlay_hint.get({
284            range = {
285              start = { line = 2, character = 10 },
286              ['end'] = { line = 2, character = 10 },
287            },
288          })
289        end)
290      )
291
292      eq(
293        {
294          { bufnr = 1, client_id = 1, inlay_hint = response[2] },
295          { bufnr = 1, client_id = 1, inlay_hint = response[3] },
296        },
297        exec_lua(function()
298          return vim.lsp.inlay_hint.get({
299            bufnr = vim.api.nvim_get_current_buf(),
300            range = {
301              start = { line = 4, character = 18 },
302              ['end'] = { line = 5, character = 17 },
303            },
304          })
305        end)
306      )
307
308      eq(
309        {},
310        exec_lua(function()
311          return vim.lsp.inlay_hint.get({
312            bufnr = vim.api.nvim_get_current_buf() + 1,
313          })
314        end)
315      )
316    end)
317  end)
318
319  it('does not request hints from lsp when disabled', function()
320    local client_id2 = exec_lua(function()
321      _G.server2 = _G._create_server({
322        capabilities = {
323          textDocumentSync = vim.lsp.protocol.TextDocumentSyncKind.Full,
324          inlayHintProvider = true,
325        },
326        handlers = {
327          ['textDocument/inlayHint'] = function(_, _, callback)
328            _G.got_inlay_hint_request = true
329            callback(nil, {})
330          end,
331        },
332      })
333      return vim.lsp.start({
334        name = 'dummy2',
335        cmd = _G.server2.cmd,
336        on_attach = function(client, _)
337          vim.lsp.inlay_hint.enable(false, { client_id = client.id })
338        end,
339      })
340    end)
341
342    local function was_request_sent()
343      return exec_lua(function()
344        return _G.got_inlay_hint_request or false
345      end)
346    end
347
348    eq(false, was_request_sent())
349
350    exec_lua(function()
351      vim.lsp.inlay_hint.get()
352    end)
353
354    eq(false, was_request_sent())
355
356    exec_lua(function()
357      vim.lsp.inlay_hint.enable(true, { client_id = client_id2 })
358    end)
359
360    eq(true, was_request_sent())
361  end)
362end)
363
364describe('Inlay hints handler', function()
365  local text = dedent([[
366test text
367  ]])
368
369  local response = {
370    { position = { line = 0, character = 0 }, label = '0' },
371    { position = { line = 0, character = 0 }, label = '1' },
372    { position = { line = 0, character = 0 }, label = '2' },
373    { position = { line = 0, character = 0 }, label = '3' },
374    { position = { line = 0, character = 0 }, label = '4' },
375  }
376
377  local grid_without_inlay_hints = [[
378  test text                                         |
379  ^                                                  |
380                                                    |
381]]
382
383  local grid_with_inlay_hints = [[
384  {1:01234}test text                                    |
385  ^                                                  |
386                                                    |
387]]
388
389  --- @type test.functional.ui.screen
390  local screen
391
392  --- @type integer
393  local client_id
394
395  --- @type integer
396  local bufnr
397
398  before_each(function()
399    clear_notrace()
400    screen = Screen.new(50, 3)
401
402    exec_lua(create_server_definition)
403    bufnr = n.api.nvim_get_current_buf()
404    client_id = exec_lua(function()
405      _G.server = _G._create_server({
406        capabilities = {
407          textDocumentSync = vim.lsp.protocol.TextDocumentSyncKind.Full,
408          inlayHintProvider = true,
409        },
410        handlers = {
411          ['textDocument/inlayHint'] = function(_, _, callback)
412            callback(nil, response)
413          end,
414        },
415      })
416
417      vim.api.nvim_win_set_buf(0, bufnr)
418
419      return vim.lsp.start({ name = 'dummy', cmd = _G.server.cmd })
420    end)
421    insert(text)
422  end)
423
424  it('renders hints with same position in received order', function()
425    exec_lua([[vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })]])
426    screen:expect({ grid = grid_with_inlay_hints })
427    exec_lua(function()
428      vim.lsp.get_client_by_id(client_id):stop()
429    end)
430    screen:expect({ grid = grid_without_inlay_hints, unchanged = true })
431  end)
432
433  it('refreshes hints on request', function()
434    exec_lua([[vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })]])
435    screen:expect({ grid = grid_with_inlay_hints })
436    feed('kibefore <Esc>')
437    screen:expect([[
438      before^ {1:01234}test text                             |
439                                                        |*2
440    ]])
441    exec_lua(function()
442      vim.lsp.inlay_hint.on_refresh(
443        nil,
444        nil,
445        { method = 'workspace/inlayHint/refresh', client_id = client_id }
446      )
447    end)
448    screen:expect([[
449      {1:01234}before^ test text                             |
450                                                        |*2
451    ]])
452  end)
453
454  after_each(function()
455    api.nvim_exec_autocmds('VimLeavePre', { modeline = false })
456  end)
457end)