robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
17.4 KiB544 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.codelens', function()
19  local text = dedent([[
20    struct S {
21        a: i32,
22        b: String,
23    }
24
25    impl S {
26        fn new(a: i32, b: String) -> Self {
27            S { a, b }
28        }
29    }
30
31    fn main() {
32        let s = S::new(42, String::from("Hello, world!"));
33        println!("S.a: {}, S.b: {}", s.a, s.b);
34    }
35  ]])
36
37  local grid_with_lenses = dedent([[
38    {1:       1 implementation}                              |
39    struct S {                                           |
40        a: i32,                                          |
41        b: String,                                       |
42    }                                                    |
43                                                         |
44    impl S {                                             |
45        fn new(a: i32, b: String) -> Self {              |
46            S { a, b }                                   |
47        }                                                |
48    }                                                    |
49                                                         |
50    {1:   ▶︎ Run }                                            |
51    fn main() {                                          |
52        let s = S::new(42, String::from("Hello, world!"))|
53    ;                                                    |
54        println!("S.a: {}, S.b: {}", s.a, s.b);          |
55    }                                                    |
56    ^                                                     |
57                                                         |
58  ]])
59
60  local grid_without_lenses = dedent([[
61    struct S {                                           |
62        a: i32,                                          |
63        b: String,                                       |
64    }                                                    |
65                                                         |
66    impl S {                                             |
67        fn new(a: i32, b: String) -> Self {              |
68            S { a, b }                                   |
69        }                                                |
70    }                                                    |
71                                                         |
72    fn main() {                                          |
73        let s = S::new(42, String::from("Hello, world!"))|
74    ;                                                    |
75        println!("S.a: {}, S.b: {}", s.a, s.b);          |
76    }                                                    |
77    ^                                                     |
78    {1:~                                                    }|*2
79                                                         |
80  ]])
81
82  --- @type test.functional.ui.screen
83  local screen
84
85  --- @type integer
86  local client_id
87
88  before_each(function()
89    clear_notrace()
90    exec_lua(create_server_definition)
91    exec_lua(function()
92      vim.lsp.config('*', {
93        flags = { debounce_text_changes = 0 },
94      })
95    end)
96
97    screen = Screen.new(nil, 20)
98
99    client_id = exec_lua(function()
100      _G.server = _G._create_server({
101        capabilities = {
102          textDocumentSync = vim.lsp.protocol.TextDocumentSyncKind.Full,
103          codeLensProvider = {
104            resolveProvider = true,
105          },
106        },
107        handlers = {
108          ['textDocument/codeLens'] = function(_, _, callback)
109            callback(nil, {
110              {
111                data = {
112                  kind = {
113                    impls = {
114                      position = {
115                        character = 7,
116                        line = 0,
117                      },
118                    },
119                  },
120                  version = 0,
121                },
122                range = {
123                  ['end'] = {
124                    character = 8,
125                    line = 0,
126                  },
127                  start = {
128                    character = 7,
129                    line = 0,
130                  },
131                },
132              },
133              {
134                command = {
135                  arguments = {},
136                  command = 'rust-analyzer.runSingle',
137                  title = '▶︎ Run ',
138                },
139                range = {
140                  ['end'] = {
141                    character = 7,
142                    line = 11,
143                  },
144                  start = {
145                    character = 3,
146                    line = 11,
147                  },
148                },
149              },
150            })
151          end,
152          ['codeLens/resolve'] = function(_, _, callback)
153            vim.schedule(function()
154              callback(nil, {
155                command = {
156                  arguments = {},
157                  command = 'rust-analyzer.showReferences',
158                  title = '1 implementation',
159                },
160                range = {
161                  ['end'] = {
162                    character = 8,
163                    line = 0,
164                  },
165                  start = {
166                    character = 7,
167                    line = 0,
168                  },
169                },
170              })
171            end)
172          end,
173        },
174      })
175
176      return vim.lsp.start({ name = 'dummy', cmd = _G.server.cmd })
177    end)
178
179    insert(text)
180
181    exec_lua(function()
182      vim.lsp.codelens.enable()
183    end)
184
185    screen:expect({ grid = grid_with_lenses })
186  end)
187
188  it('clears/shows code lenses when disabled/enabled', function()
189    exec_lua(function()
190      vim.lsp.codelens.enable(false)
191    end)
192
193    screen:expect({ grid = grid_without_lenses })
194
195    exec_lua(function()
196      vim.lsp.codelens.enable(true)
197    end)
198
199    screen:expect({ grid = grid_with_lenses })
200  end)
201
202  it('clears code lenses when sole client detaches', function()
203    exec_lua(function()
204      vim.lsp.get_client_by_id(client_id):stop()
205    end)
206
207    screen:expect({ grid = grid_without_lenses })
208  end)
209
210  it('get code lenses in the current buffer', function()
211    local result = exec_lua(function()
212      vim.api.nvim_win_set_cursor(0, { 12, 3 })
213      return vim.lsp.codelens.get()
214    end)
215
216    eq({
217      {
218        client_id = 1,
219        lens = {
220          command = {
221            arguments = {},
222            command = 'rust-analyzer.showReferences',
223            title = '1 implementation',
224          },
225          range = {
226            ['end'] = {
227              character = 8,
228              line = 0,
229            },
230            start = {
231              character = 7,
232              line = 0,
233            },
234          },
235        },
236      },
237      {
238        client_id = 1,
239        lens = {
240          command = {
241            arguments = {},
242            command = 'rust-analyzer.runSingle',
243            title = '▶︎ Run ',
244          },
245          range = {
246            ['end'] = {
247              character = 7,
248              line = 11,
249            },
250            start = {
251              character = 3,
252              line = 11,
253            },
254          },
255        },
256      },
257    }, result)
258  end)
259
260  it('refreshes code lenses on request', function()
261    local get_message_count = function()
262      local messages = exec_lua('return _G.server.messages')
263      local count = 0
264      for _, m in ipairs(messages) do
265        if m.method == 'textDocument/codeLens' then
266          count = count + 1
267        end
268      end
269      return count
270    end
271
272    eq(2, get_message_count())
273
274    feed('ggdd')
275
276    -- Second codelens (▶︎ Run) is not resolved immediately, so its previous extmark (same row) isn't
277    -- cleared and it stays anchored to where it was. The new one (which would effectively look like
278    -- it moved a row down) needs to be resolved first, which goes through another screen update.
279    -- This is a quirk of the test since it returns the exact same result no matter what the text in
280    -- the buffer actually is
281
282    screen:expect([[
283      {1:       1 implementation}                              |
284          ^a: i32,                                          |
285          b: String,                                       |
286      }                                                    |
287                                                           |
288      impl S {                                             |
289          fn new(a: i32, b: String) -> Self {              |
290              S { a, b }                                   |
291          }                                                |
292      }                                                    |
293                                                           |
294      {1:   ▶︎ Run }                                            |
295      fn main() {                                          |
296          let s = S::new(42, String::from("Hello, world!"))|
297      ;                                                    |
298          println!("S.a: {}, S.b: {}", s.a, s.b);          |
299      }                                                    |
300                                                           |
301      {1:~                                                    }|
302                                                           |
303    ]])
304
305    eq(2, get_message_count())
306
307    screen:expect([[
308      {1:       1 implementation}                              |
309          ^a: i32,                                          |
310          b: String,                                       |
311      }                                                    |
312                                                           |
313      impl S {                                             |
314          fn new(a: i32, b: String) -> Self {              |
315              S { a, b }                                   |
316          }                                                |
317      }                                                    |
318                                                           |
319      fn main() {                                          |
320      {1:   ▶︎ Run }                                            |
321          let s = S::new(42, String::from("Hello, world!"))|
322      ;                                                    |
323          println!("S.a: {}, S.b: {}", s.a, s.b);          |
324      }                                                    |
325                                                           |
326      {1:~                                                    }|
327                                                           |
328    ]])
329
330    eq(3, get_message_count())
331
332    exec_lua(function()
333      vim.lsp.codelens.on_refresh(
334        nil,
335        nil,
336        { method = 'workspace/codeLens/refresh', client_id = client_id }
337      )
338    end)
339
340    eq(4, get_message_count())
341  end)
342
343  it('ignores stale codeLens/resolve responses', function()
344    clear_notrace()
345    exec_lua(create_server_definition)
346
347    insert('line1\nline2\n')
348
349    exec_lua(function()
350      local codelens_request_count = 0
351      _G.stale_resolve_sent = false
352      _G.server = _G._create_server({
353        capabilities = {
354          textDocumentSync = vim.lsp.protocol.TextDocumentSyncKind.Full,
355          codeLensProvider = {
356            resolveProvider = true,
357          },
358        },
359        handlers = {
360          ['textDocument/codeLens'] = function(_, _, callback)
361            codelens_request_count = codelens_request_count + 1
362            if codelens_request_count == 1 then
363              callback(nil, {
364                {
365                  range = {
366                    ['end'] = {
367                      character = 1,
368                      line = 0,
369                    },
370                    start = {
371                      character = 0,
372                      line = 0,
373                    },
374                  },
375                },
376              })
377            else
378              callback(nil, {})
379            end
380          end,
381          ['codeLens/resolve'] = function(_, lens, callback)
382            vim.defer_fn(function()
383              _G.stale_resolve_sent = true
384              callback(nil, {
385                command = {
386                  arguments = {},
387                  command = 'dummy.command',
388                  title = 'resolved',
389                },
390                range = lens.range,
391              })
392            end, 100)
393          end,
394        },
395      })
396
397      local stale_client_id = vim.lsp.start({ name = 'dummy', cmd = _G.server.cmd })
398      vim.lsp.codelens.enable()
399      vim.wait(1000, function()
400        return #vim.lsp.codelens.get() > 0
401      end)
402
403      vim.api.nvim__redraw({ flush = true })
404
405      vim.lsp.codelens.on_refresh(nil, nil, {
406        method = 'workspace/codeLens/refresh',
407        client_id = stale_client_id,
408      })
409
410      assert(
411        vim.wait(1000, function()
412          return _G.stale_resolve_sent
413        end),
414        'timed out waiting for stale resolve response'
415      )
416    end)
417
418    eq('', api.nvim_get_vvar('errmsg'))
419  end)
420
421  it('ignores refresh responses for deleted buffer', function()
422    clear_notrace()
423    exec_lua(create_server_definition)
424
425    insert('line1\n')
426
427    exec_lua(function()
428      local codelens_request_count = 0
429      _G.refresh_response_sent = false
430      _G.server = _G._create_server({
431        capabilities = {
432          textDocumentSync = vim.lsp.protocol.TextDocumentSyncKind.Full,
433          codeLensProvider = {
434            resolveProvider = true,
435          },
436        },
437        handlers = {
438          ['textDocument/codeLens'] = function(_, _, callback)
439            codelens_request_count = codelens_request_count + 1
440
441            local lenses = {
442              {
443                command = {
444                  arguments = {},
445                  command = 'dummy.command',
446                  title = 'Lens',
447                },
448                range = {
449                  ['end'] = {
450                    character = 1,
451                    line = 0,
452                  },
453                  start = {
454                    character = 0,
455                    line = 0,
456                  },
457                },
458              },
459            }
460
461            if codelens_request_count == 1 then
462              callback(nil, lenses)
463            else
464              -- Delay the refresh response so the buffer is wiped before it arrives.
465              vim.schedule(function()
466                _G.refresh_response_sent = true
467                callback(nil, lenses)
468              end)
469            end
470          end,
471        },
472      })
473
474      local client_id = vim.lsp.start({ name = 'dummy', cmd = _G.server.cmd })
475      vim.lsp.codelens.enable()
476
477      assert(
478        vim.wait(1000, function()
479          return #vim.lsp.codelens.get() > 0
480        end),
481        'timed out waiting for initial codelens response'
482      )
483
484      vim.lsp.codelens.on_refresh(nil, nil, {
485        method = 'workspace/codeLens/refresh',
486        client_id = client_id,
487      })
488      vim.cmd.bwipeout({ bang = true })
489
490      assert(
491        vim.wait(1000, function()
492          return _G.refresh_response_sent
493        end),
494        'timed out waiting for refresh response'
495      )
496    end)
497
498    eq('', api.nvim_get_vvar('errmsg'))
499  end)
500
501  it('clears extmarks beyond the bottom of the buffer', function()
502    feed('12G4dd')
503    screen:expect([[
504      {1:       1 implementation}                              |
505      struct S {                                           |
506          a: i32,                                          |
507          b: String,                                       |
508      }                                                    |
509                                                           |
510      impl S {                                             |
511          fn new(a: i32, b: String) -> Self {              |
512              S { a, b }                                   |
513          }                                                |
514      }                                                    |
515                                                           |
516      {1:▶︎ Run }                                               |
517      ^                                                     |
518      {1:~                                                    }|*5
519      4 fewer lines                                        |
520    ]])
521
522    feed('dd')
523    screen:expect([[
524      {1:       1 implementation}                              |
525      struct S {                                           |
526          a: i32,                                          |
527          b: String,                                       |
528      }                                                    |
529                                                           |
530      impl S {                                             |
531          fn new(a: i32, b: String) -> Self {              |
532              S { a, b }                                   |
533          }                                                |
534      }                                                    |
535      ^                                                     |
536      {1:~                                                    }|*7
537      4 fewer lines                                        |
538    ]])
539  end)
540
541  after_each(function()
542    api.nvim_exec_autocmds('VimLeavePre', { modeline = false })
543  end)
544end)