robin/laufey

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

robinmerge: 1dc9728daa1d8a2702

master
40.0 KiB835 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 dedent = t.dedent
8local eq = t.eq
9local retry = t.retry
10
11local clear_notrace = t_lsp.clear_notrace
12local create_server_definition = t_lsp.create_server_definition
13
14local api = n.api
15local exec_lua = n.exec_lua
16local insert = n.insert
17local command = n.command
18local feed = n.feed
19
20describe('vim.lsp.folding_range', function()
21  local text = [[// foldLevel() {{{2
22/// @return  fold level at line number "lnum" in the current window.
23static int foldLevel(linenr_T lnum)
24{
25  // While updating the folds lines between invalid_top and invalid_bot have
26  // an undefined fold level.  Otherwise update the folds first.
27  if (invalid_top == 0) {
28    checkupdate(curwin);
29  } else if (lnum == prev_lnum && prev_lnum_lvl >= 0) {
30    return prev_lnum_lvl;
31  } else if (lnum >= invalid_top && lnum <= invalid_bot) {
32    return -1;
33  }
34
35  // Return quickly when there is no folding at all in this window.
36  if (!hasAnyFolding(curwin)) {
37    return 0;
38  }
39
40  return foldLevelWin(curwin, lnum);
41}]]
42
43  local result = {
44    {
45      endLine = 19,
46      kind = 'region',
47      startCharacter = 1,
48      startLine = 3,
49    },
50    {
51      endCharacter = 2,
52      endLine = 7,
53      kind = 'region',
54      startCharacter = 25,
55      startLine = 6,
56    },
57    {
58      endCharacter = 2,
59      endLine = 9,
60      kind = 'region',
61      startCharacter = 55,
62      startLine = 8,
63    },
64    {
65      endCharacter = 2,
66      endLine = 11,
67      kind = 'region',
68      startCharacter = 58,
69      startLine = 10,
70    },
71    {
72      endCharacter = 2,
73      endLine = 16,
74      kind = 'region',
75      startCharacter = 31,
76      startLine = 15,
77    },
78    {
79      endCharacter = 68,
80      endLine = 1,
81      kind = 'comment',
82      startCharacter = 2,
83      startLine = 0,
84    },
85    {
86      endCharacter = 64,
87      endLine = 5,
88      kind = 'comment',
89      startCharacter = 4,
90      startLine = 4,
91    },
92  }
93
94  local bufnr ---@type integer
95  local client_id ---@type integer
96
97  clear_notrace()
98  before_each(function()
99    clear_notrace()
100
101    exec_lua(create_server_definition)
102    bufnr = n.api.nvim_get_current_buf()
103    client_id = exec_lua(function()
104      _G.folding_ranges = result
105      _G.server = _G._create_server({
106        capabilities = {
107          foldingRangeProvider = true,
108        },
109        handlers = {
110          ['textDocument/foldingRange'] = function(_, _, callback)
111            callback(nil, _G.folding_ranges)
112          end,
113        },
114      })
115
116      vim.api.nvim_win_set_buf(0, bufnr)
117
118      return vim.lsp.start({ name = 'dummy', cmd = _G.server.cmd })
119    end)
120    command('set foldmethod=expr foldcolumn=1 foldlevel=999')
121    insert(text)
122  end)
123  after_each(function()
124    api.nvim_exec_autocmds('VimLeavePre', { modeline = false })
125  end)
126
127  describe('expr()', function()
128    --- @type test.functional.ui.screen
129    local screen
130    before_each(function()
131      screen = Screen.new(80, 45)
132      command([[set foldexpr=v:lua.vim.lsp.foldexpr()]])
133      command([[split]])
134    end)
135
136    it('controls whether folding range is enabled', function()
137      eq(
138        true,
139        exec_lua(function()
140          return vim.lsp._capability.is_enabled('folding_range', { bufnr = 0 })
141        end)
142      )
143      command [[setlocal foldexpr=]]
144      eq(
145        false,
146        exec_lua(function()
147          return vim.lsp._capability.is_enabled('folding_range', { bufnr = 0 })
148        end)
149      )
150      command([[set foldexpr=v:lua.vim.lsp.foldexpr()]])
151      eq(
152        true,
153        exec_lua(function()
154          return vim.lsp._capability.is_enabled('folding_range', { bufnr = 0 })
155        end)
156      )
157    end)
158
159    it('can compute fold levels', function()
160      ---@type table<integer, string>
161      local foldlevels = {}
162      for i = 1, 21 do
163        foldlevels[i] = exec_lua('return vim.lsp.foldexpr(' .. i .. ')')
164      end
165      eq({
166        [1] = '>1',
167        [2] = '<1',
168        [3] = '0',
169        [4] = '>1',
170        [5] = '>2',
171        [6] = '<2',
172        [7] = '>2',
173        [8] = '<2',
174        [9] = '>2',
175        [10] = '<2',
176        [11] = '>2',
177        [12] = '<2',
178        [13] = '1',
179        [14] = '1',
180        [15] = '1',
181        [16] = '>2',
182        [17] = '<2',
183        [18] = '1',
184        [19] = '1',
185        [20] = '<1',
186        [21] = '0',
187      }, foldlevels)
188    end)
189
190    it('treats unknown folding range kinds like an absent kind #41765', function()
191      exec_lua(function()
192        _G.folding_ranges = {
193          { startLine = 0, endLine = 1, kind = 'function' },
194          { startLine = 3, endLine = 4 },
195          { startLine = 6, endLine = 7, kind = '' },
196          { startLine = 9, endLine = 10, kind = 'comment' },
197          { startLine = 12, endLine = 13, kind = 'imports' },
198          { startLine = 15, endLine = 16, kind = 'region' },
199        }
200        vim.lsp._folding_range.on_refresh(
201          nil,
202          nil,
203          { method = 'workspace/foldingRange/refresh', client_id = client_id }
204        )
205      end)
206
207      retry(nil, nil, function()
208        eq(
209          {
210            '>1',
211            '<1',
212            '0',
213            '>1',
214            '<1',
215            '0',
216            '>1',
217            '<1',
218            '0',
219            '>1',
220            '<1',
221            '0',
222            '>1',
223            '<1',
224            '0',
225            '>1',
226            '<1',
227          },
228          exec_lua(function()
229            local levels = {}
230            for lnum = 1, 17 do
231              levels[lnum] = vim.lsp.foldexpr(lnum)
232            end
233            return levels
234          end)
235        )
236      end)
237
238      exec_lua(function()
239        for _, kind in ipairs({ 'comment', 'imports', 'region', 'function', '' }) do
240          vim.lsp.foldclose(kind)
241        end
242      end)
243      -- Unknown and absent kinds are not selected by kind-specific folding.
244      eq(
245        { -1, -1, -1, 10, 13, 16 },
246        exec_lua(function()
247          return vim.tbl_map(vim.fn.foldclosed, { 1, 4, 7, 10, 13, 16 })
248        end)
249      )
250
251      command('normal! zM')
252      eq(
253        { 1, 4, 7, 10, 13, 16 },
254        exec_lua(function()
255          return vim.tbl_map(vim.fn.foldclosed, { 1, 4, 7, 10, 13, 16 })
256        end)
257      )
258    end)
259
260    it('refreshes folding ranges on request', function()
261      local function foldlevels()
262        return exec_lua(function()
263          return { vim.lsp.foldexpr(1), vim.lsp.foldexpr(2), vim.lsp.foldexpr(3) }
264        end)
265      end
266
267      retry(nil, nil, function()
268        eq({ '>1', '<1', '0' }, foldlevels())
269      end)
270
271      exec_lua(function()
272        _G.folding_ranges = { { startLine = 1, endLine = 2 } }
273        vim.lsp._folding_range.on_refresh(
274          nil,
275          nil,
276          { method = 'workspace/foldingRange/refresh', client_id = client_id }
277        )
278      end)
279
280      retry(nil, nil, function()
281        eq({ '0', '>1', '<1' }, foldlevels())
282      end)
283    end)
284
285    it('updates folds in all windows', function()
286      screen:expect({
287        grid = [[
288  {7:-}// foldLevel() {{{2                                                            |
289  {7:│}/// @return  fold level at line number "lnum" in the current window.           |
290  {7: }static int foldLevel(linenr_T lnum)                                            |
291  {7:-}{                                                                              |
292  {7:-}  // While updating the folds lines between invalid_top and invalid_bot have   |
293  {7:2}  // an undefined fold level.  Otherwise update the folds first.               |
294  {7:-}  if (invalid_top == 0) {                                                      |
295  {7:2}    checkupdate(curwin);                                                       |
296  {7:-}  } else if (lnum == prev_lnum && prev_lnum_lvl >= 0) {                        |
297  {7:2}    return prev_lnum_lvl;                                                      |
298  {7:-}  } else if (lnum >= invalid_top && lnum <= invalid_bot) {                     |
299  {7:2}    return -1;                                                                 |
300  {7:│}  }                                                                            |
301  {7:│}                                                                               |
302  {7:│}  // Return quickly when there is no folding at all in this window.            |
303  {7:-}  if (!hasAnyFolding(curwin)) {                                                |
304  {7:2}    return 0;                                                                  |
305  {7:│}  }                                                                            |
306  {7:│}                                                                               |
307  {7:│}  return foldLevelWin(curwin, lnum);                                           |
308  {7: }^}                                                                              |
309  {3:[No Name] [+]                                                                   }|
310  {7:-}// foldLevel() {{{2                                                            |
311  {7:│}/// @return  fold level at line number "lnum" in the current window.           |
312  {7: }static int foldLevel(linenr_T lnum)                                            |
313  {7:-}{                                                                              |
314  {7:-}  // While updating the folds lines between invalid_top and invalid_bot have   |
315  {7:2}  // an undefined fold level.  Otherwise update the folds first.               |
316  {7:-}  if (invalid_top == 0) {                                                      |
317  {7:2}    checkupdate(curwin);                                                       |
318  {7:-}  } else if (lnum == prev_lnum && prev_lnum_lvl >= 0) {                        |
319  {7:2}    return prev_lnum_lvl;                                                      |
320  {7:-}  } else if (lnum >= invalid_top && lnum <= invalid_bot) {                     |
321  {7:2}    return -1;                                                                 |
322  {7:│}  }                                                                            |
323  {7:│}                                                                               |
324  {7:│}  // Return quickly when there is no folding at all in this window.            |
325  {7:-}  if (!hasAnyFolding(curwin)) {                                                |
326  {7:2}    return 0;                                                                  |
327  {7:│}  }                                                                            |
328  {7:│}                                                                               |
329  {7:│}  return foldLevelWin(curwin, lnum);                                           |
330  {7: }}                                                                              |
331  {2:[No Name] [+]                                                                   }|
332                                                                                  |
333  ]],
334      })
335    end)
336
337    it('persists wherever foldexpr is set', function()
338      command([[setlocal foldexpr=]])
339      feed('<C-w><C-w>zx')
340      screen:expect({
341        grid = [[
342  {7: }// foldLevel() {{{2                                                            |
343  {7: }/// @return  fold level at line number "lnum" in the current window.           |
344  {7: }static int foldLevel(linenr_T lnum)                                            |
345  {7: }{                                                                              |
346  {7: }  // While updating the folds lines between invalid_top and invalid_bot have   |
347  {7: }  // an undefined fold level.  Otherwise update the folds first.               |
348  {7: }  if (invalid_top == 0) {                                                      |
349  {7: }    checkupdate(curwin);                                                       |
350  {7: }  } else if (lnum == prev_lnum && prev_lnum_lvl >= 0) {                        |
351  {7: }    return prev_lnum_lvl;                                                      |
352  {7: }  } else if (lnum >= invalid_top && lnum <= invalid_bot) {                     |
353  {7: }    return -1;                                                                 |
354  {7: }  }                                                                            |
355  {7: }                                                                               |
356  {7: }  // Return quickly when there is no folding at all in this window.            |
357  {7: }  if (!hasAnyFolding(curwin)) {                                                |
358  {7: }    return 0;                                                                  |
359  {7: }  }                                                                            |
360  {7: }                                                                               |
361  {7: }  return foldLevelWin(curwin, lnum);                                           |
362  {7: }}                                                                              |
363  {2:[No Name] [+]                                                                   }|
364  {7:-}// foldLevel() {{{2                                                            |
365  {7:│}/// @return  fold level at line number "lnum" in the current window.           |
366  {7: }static int foldLevel(linenr_T lnum)                                            |
367  {7:-}{                                                                              |
368  {7:-}  // While updating the folds lines between invalid_top and invalid_bot have   |
369  {7:2}  // an undefined fold level.  Otherwise update the folds first.               |
370  {7:-}  if (invalid_top == 0) {                                                      |
371  {7:2}    checkupdate(curwin);                                                       |
372  {7:-}  } else if (lnum == prev_lnum && prev_lnum_lvl >= 0) {                        |
373  {7:2}    return prev_lnum_lvl;                                                      |
374  {7:-}  } else if (lnum >= invalid_top && lnum <= invalid_bot) {                     |
375  {7:2}    return -1;                                                                 |
376  {7:│}  }                                                                            |
377  {7:│}                                                                               |
378  {7:│}  // Return quickly when there is no folding at all in this window.            |
379  {7:-}  if (!hasAnyFolding(curwin)) {                                                |
380  {7:2}    return 0;                                                                  |
381  {7:│}  }                                                                            |
382  {7:│}                                                                               |
383  {7:│}  return foldLevelWin(curwin, lnum);                                           |
384  {7: }^}                                                                              |
385  {3:[No Name] [+]                                                                   }|
386                                                                                  |
387  ]],
388      })
389    end)
390
391    it('synchronizes changed rows with their previous foldlevels', function()
392      command('1,2d')
393      screen:expect({
394        grid = [[
395  {7: }^static int foldLevel(linenr_T lnum)                                            |
396  {7:-}{                                                                              |
397  {7:-}  // While updating the folds lines between invalid_top and invalid_bot have   |
398  {7:2}  // an undefined fold level.  Otherwise update the folds first.               |
399  {7:-}  if (invalid_top == 0) {                                                      |
400  {7:2}    checkupdate(curwin);                                                       |
401  {7:-}  } else if (lnum == prev_lnum && prev_lnum_lvl >= 0) {                        |
402  {7:2}    return prev_lnum_lvl;                                                      |
403  {7:-}  } else if (lnum >= invalid_top && lnum <= invalid_bot) {                     |
404  {7:2}    return -1;                                                                 |
405  {7:│}  }                                                                            |
406  {7:│}                                                                               |
407  {7:│}  // Return quickly when there is no folding at all in this window.            |
408  {7:-}  if (!hasAnyFolding(curwin)) {                                                |
409  {7:2}    return 0;                                                                  |
410  {7:│}  }                                                                            |
411  {7:│}                                                                               |
412  {7:│}  return foldLevelWin(curwin, lnum);                                           |
413  {7: }}                                                                              |
414  {1:~                                                                               }|*2
415  {3:[No Name] [+]                                                                   }|
416  {7: }static int foldLevel(linenr_T lnum)                                            |
417  {7:-}{                                                                              |
418  {7:-}  // While updating the folds lines between invalid_top and invalid_bot have   |
419  {7:2}  // an undefined fold level.  Otherwise update the folds first.               |
420  {7:-}  if (invalid_top == 0) {                                                      |
421  {7:2}    checkupdate(curwin);                                                       |
422  {7:-}  } else if (lnum == prev_lnum && prev_lnum_lvl >= 0) {                        |
423  {7:2}    return prev_lnum_lvl;                                                      |
424  {7:-}  } else if (lnum >= invalid_top && lnum <= invalid_bot) {                     |
425  {7:2}    return -1;                                                                 |
426  {7:│}  }                                                                            |
427  {7:│}                                                                               |
428  {7:│}  // Return quickly when there is no folding at all in this window.            |
429  {7:-}  if (!hasAnyFolding(curwin)) {                                                |
430  {7:2}    return 0;                                                                  |
431  {7:│}  }                                                                            |
432  {7:│}                                                                               |
433  {7:│}  return foldLevelWin(curwin, lnum);                                           |
434  {7: }}                                                                              |
435  {1:~                                                                               }|*2
436  {2:[No Name] [+]                                                                   }|
437                                                                                  |
438]],
439      })
440    end)
441
442    it('clears folds when sole client detaches', function()
443      exec_lua(function()
444        vim.lsp.buf_detach_client(bufnr, client_id)
445      end)
446      screen:expect({
447        grid = [[
448  {7: }// foldLevel() {{{2                                                            |
449  {7: }/// @return  fold level at line number "lnum" in the current window.           |
450  {7: }static int foldLevel(linenr_T lnum)                                            |
451  {7: }{                                                                              |
452  {7: }  // While updating the folds lines between invalid_top and invalid_bot have   |
453  {7: }  // an undefined fold level.  Otherwise update the folds first.               |
454  {7: }  if (invalid_top == 0) {                                                      |
455  {7: }    checkupdate(curwin);                                                       |
456  {7: }  } else if (lnum == prev_lnum && prev_lnum_lvl >= 0) {                        |
457  {7: }    return prev_lnum_lvl;                                                      |
458  {7: }  } else if (lnum >= invalid_top && lnum <= invalid_bot) {                     |
459  {7: }    return -1;                                                                 |
460  {7: }  }                                                                            |
461  {7: }                                                                               |
462  {7: }  // Return quickly when there is no folding at all in this window.            |
463  {7: }  if (!hasAnyFolding(curwin)) {                                                |
464  {7: }    return 0;                                                                  |
465  {7: }  }                                                                            |
466  {7: }                                                                               |
467  {7: }  return foldLevelWin(curwin, lnum);                                           |
468  {7: }^}                                                                              |
469  {3:[No Name] [+]                                                                   }|
470  {7: }// foldLevel() {{{2                                                            |
471  {7: }/// @return  fold level at line number "lnum" in the current window.           |
472  {7: }static int foldLevel(linenr_T lnum)                                            |
473  {7: }{                                                                              |
474  {7: }  // While updating the folds lines between invalid_top and invalid_bot have   |
475  {7: }  // an undefined fold level.  Otherwise update the folds first.               |
476  {7: }  if (invalid_top == 0) {                                                      |
477  {7: }    checkupdate(curwin);                                                       |
478  {7: }  } else if (lnum == prev_lnum && prev_lnum_lvl >= 0) {                        |
479  {7: }    return prev_lnum_lvl;                                                      |
480  {7: }  } else if (lnum >= invalid_top && lnum <= invalid_bot) {                     |
481  {7: }    return -1;                                                                 |
482  {7: }  }                                                                            |
483  {7: }                                                                               |
484  {7: }  // Return quickly when there is no folding at all in this window.            |
485  {7: }  if (!hasAnyFolding(curwin)) {                                                |
486  {7: }    return 0;                                                                  |
487  {7: }  }                                                                            |
488  {7: }                                                                               |
489  {7: }  return foldLevelWin(curwin, lnum);                                           |
490  {7: }}                                                                              |
491  {2:[No Name] [+]                                                                   }|
492                                                                                  |
493  ]],
494      })
495    end)
496
497    it('remains valid after the client re-attaches.', function()
498      exec_lua(function()
499        vim.lsp.buf_detach_client(bufnr, client_id)
500        vim.lsp.buf_attach_client(bufnr, client_id)
501      end)
502      screen:expect({
503        grid = [[
504  {7:-}// foldLevel() {{{2                                                            |
505  {7:│}/// @return  fold level at line number "lnum" in the current window.           |
506  {7: }static int foldLevel(linenr_T lnum)                                            |
507  {7:-}{                                                                              |
508  {7:-}  // While updating the folds lines between invalid_top and invalid_bot have   |
509  {7:2}  // an undefined fold level.  Otherwise update the folds first.               |
510  {7:-}  if (invalid_top == 0) {                                                      |
511  {7:2}    checkupdate(curwin);                                                       |
512  {7:-}  } else if (lnum == prev_lnum && prev_lnum_lvl >= 0) {                        |
513  {7:2}    return prev_lnum_lvl;                                                      |
514  {7:-}  } else if (lnum >= invalid_top && lnum <= invalid_bot) {                     |
515  {7:2}    return -1;                                                                 |
516  {7:│}  }                                                                            |
517  {7:│}                                                                               |
518  {7:│}  // Return quickly when there is no folding at all in this window.            |
519  {7:-}  if (!hasAnyFolding(curwin)) {                                                |
520  {7:2}    return 0;                                                                  |
521  {7:│}  }                                                                            |
522  {7:│}                                                                               |
523  {7:│}  return foldLevelWin(curwin, lnum);                                           |
524  {7: }^}                                                                              |
525  {3:[No Name] [+]                                                                   }|
526  {7:-}// foldLevel() {{{2                                                            |
527  {7:│}/// @return  fold level at line number "lnum" in the current window.           |
528  {7: }static int foldLevel(linenr_T lnum)                                            |
529  {7:-}{                                                                              |
530  {7:-}  // While updating the folds lines between invalid_top and invalid_bot have   |
531  {7:2}  // an undefined fold level.  Otherwise update the folds first.               |
532  {7:-}  if (invalid_top == 0) {                                                      |
533  {7:2}    checkupdate(curwin);                                                       |
534  {7:-}  } else if (lnum == prev_lnum && prev_lnum_lvl >= 0) {                        |
535  {7:2}    return prev_lnum_lvl;                                                      |
536  {7:-}  } else if (lnum >= invalid_top && lnum <= invalid_bot) {                     |
537  {7:2}    return -1;                                                                 |
538  {7:│}  }                                                                            |
539  {7:│}                                                                               |
540  {7:│}  // Return quickly when there is no folding at all in this window.            |
541  {7:-}  if (!hasAnyFolding(curwin)) {                                                |
542  {7:2}    return 0;                                                                  |
543  {7:│}  }                                                                            |
544  {7:│}                                                                               |
545  {7:│}  return foldLevelWin(curwin, lnum);                                           |
546  {7: }}                                                                              |
547  {2:[No Name] [+]                                                                   }|
548                                                                                  |
549  ]],
550      })
551    end)
552  end)
553
554  describe('foldtext()', function()
555    --- @type test.functional.ui.screen
556    local screen
557    before_each(function()
558      screen = Screen.new(80, 23)
559      command(
560        [[set foldexpr=v:lua.vim.lsp.foldexpr() foldtext=v:lua.vim.lsp.foldtext() foldlevel=1]]
561      )
562    end)
563
564    it('shows the first folded line if `collapsedText` does not exist', function()
565      screen:expect({
566        grid = [[
567  {7:-}// foldLevel() {{{2                                                            |
568  {7:│}/// @return  fold level at line number "lnum" in the current window.           |
569  {7: }static int foldLevel(linenr_T lnum)                                            |
570  {7:-}{                                                                              |
571  {7:+}{13:  // While updating the folds lines between invalid_top and invalid_bot have···}|
572  {7:+}{13:  if (invalid_top == 0) {······················································}|
573  {7:+}{13:  } else if (lnum == prev_lnum && prev_lnum_lvl >= 0) {························}|
574  {7:+}{13:  } else if (lnum >= invalid_top && lnum <= invalid_bot) {·····················}|
575  {7:│}  }                                                                            |
576  {7:│}                                                                               |
577  {7:│}  // Return quickly when there is no folding at all in this window.            |
578  {7:+}{13:  if (!hasAnyFolding(curwin)) {················································}|
579  {7:│}  }                                                                            |
580  {7:│}                                                                               |
581  {7:│}  return foldLevelWin(curwin, lnum);                                           |
582  {7: }^}                                                                              |
583  {1:~                                                                               }|*6
584                                                                                  |
585  ]],
586      })
587    end)
588
589    it('shows the foldtext by virt line', function()
590      command([[set filetype=c]])
591      eq(
592        {
593          { '  ' },
594          { 'if', { '@keyword.conditional.c' } },
595          { ' ' },
596          { '(', { '@punctuation.bracket.c' } },
597          { '!', { '@operator.c' } },
598          { 'hasAnyFolding', { '@variable.c', '@function.call.c' } },
599          { '(', { '@punctuation.bracket.c' } },
600          { 'curwin', { '@variable.c' } },
601          { ')', { '@punctuation.bracket.c' } },
602          { ')', { '@punctuation.bracket.c' } },
603          { ' ' },
604          { '{', { '@punctuation.bracket.c' } },
605        },
606        exec_lua(function()
607          return vim.lsp.foldtext(16)
608        end)
609      )
610    end)
611  end)
612
613  describe('foldclose()', function()
614    --- @type test.functional.ui.screen
615    local screen
616    before_each(function()
617      screen = Screen.new(80, 23)
618      command([[set foldexpr=v:lua.vim.lsp.foldexpr()]])
619    end)
620
621    it('closes all folds of one kind immediately', function()
622      exec_lua(function()
623        vim.lsp.foldclose('comment')
624      end)
625      screen:expect({
626        grid = [[
627  {7:+}{13:+--  2 lines: foldLevel()······················································}|
628  {7: }static int foldLevel(linenr_T lnum)                                            |
629  {7:-}{                                                                              |
630  {7:+}{13:+---  2 lines: While updating the folds lines between invalid_top and invalid_b}|
631  {7:-}  if (invalid_top == 0) {                                                      |
632  {7:2}    checkupdate(curwin);                                                       |
633  {7:-}  } else if (lnum == prev_lnum && prev_lnum_lvl >= 0) {                        |
634  {7:2}    return prev_lnum_lvl;                                                      |
635  {7:-}  } else if (lnum >= invalid_top && lnum <= invalid_bot) {                     |
636  {7:2}    return -1;                                                                 |
637  {7:│}  }                                                                            |
638  {7:│}                                                                               |
639  {7:│}  // Return quickly when there is no folding at all in this window.            |
640  {7:-}  if (!hasAnyFolding(curwin)) {                                                |
641  {7:2}    return 0;                                                                  |
642  {7:│}  }                                                                            |
643  {7:│}                                                                               |
644  {7:│}  return foldLevelWin(curwin, lnum);                                           |
645  {7: }^}                                                                              |
646  {1:~                                                                               }|*3
647                                                                                  |
648  ]],
649      })
650    end)
651
652    it('closes the smallest fold first', function()
653      exec_lua(function()
654        vim.lsp.foldclose('region')
655      end)
656      screen:expect({
657        grid = [[
658  {7:-}// foldLevel() {{{2                                                            |
659  {7:│}/// @return  fold level at line number "lnum" in the current window.           |
660  {7: }static int foldLevel(linenr_T lnum)                                            |
661  {7:+}{13:+-- 17 lines: {································································}|
662  {7: }^}                                                                              |
663  {1:~                                                                               }|*17
664                                                                                  |
665  ]],
666      })
667      command('4foldopen')
668      screen:expect({
669        grid = [[
670  {7:-}// foldLevel() {{{2                                                            |
671  {7:│}/// @return  fold level at line number "lnum" in the current window.           |
672  {7: }static int foldLevel(linenr_T lnum)                                            |
673  {7:-}{                                                                              |
674  {7:-}  // While updating the folds lines between invalid_top and invalid_bot have   |
675  {7:2}  // an undefined fold level.  Otherwise update the folds first.               |
676  {7:+}{13:+---  2 lines: if (invalid_top == 0) {·········································}|
677  {7:+}{13:+---  2 lines: } else if (lnum == prev_lnum && prev_lnum_lvl >= 0) {···········}|
678  {7:+}{13:+---  2 lines: } else if (lnum >= invalid_top && lnum <= invalid_bot) {········}|
679  {7:│}  }                                                                            |
680  {7:│}                                                                               |
681  {7:│}  // Return quickly when there is no folding at all in this window.            |
682  {7:+}{13:+---  2 lines: if (!hasAnyFolding(curwin)) {···································}|
683  {7:│}  }                                                                            |
684  {7:│}                                                                               |
685  {7:│}  return foldLevelWin(curwin, lnum);                                           |
686  {7: }^}                                                                              |
687  {1:~                                                                               }|*5
688                                                                                  |
689  ]],
690      })
691    end)
692
693    it('is deferred when the buffer is not up-to-date', function()
694      exec_lua(function()
695        vim.lsp.foldclose('comment')
696        vim.lsp.util.buf_versions[bufnr] = 0
697      end)
698      screen:expect({
699        grid = [[
700  {7:+}{13:+--  2 lines: foldLevel()······················································}|
701  {7: }static int foldLevel(linenr_T lnum)                                            |
702  {7:-}{                                                                              |
703  {7:+}{13:+---  2 lines: While updating the folds lines between invalid_top and invalid_b}|
704  {7:-}  if (invalid_top == 0) {                                                      |
705  {7:2}    checkupdate(curwin);                                                       |
706  {7:-}  } else if (lnum == prev_lnum && prev_lnum_lvl >= 0) {                        |
707  {7:2}    return prev_lnum_lvl;                                                      |
708  {7:-}  } else if (lnum >= invalid_top && lnum <= invalid_bot) {                     |
709  {7:2}    return -1;                                                                 |
710  {7:│}  }                                                                            |
711  {7:│}                                                                               |
712  {7:│}  // Return quickly when there is no folding at all in this window.            |
713  {7:-}  if (!hasAnyFolding(curwin)) {                                                |
714  {7:2}    return 0;                                                                  |
715  {7:│}  }                                                                            |
716  {7:│}                                                                               |
717  {7:│}  return foldLevelWin(curwin, lnum);                                           |
718  {7: }^}                                                                              |
719  {1:~                                                                               }|*3
720                                                                                  |
721  ]],
722      })
723    end)
724  end)
725end)
726
727describe('vim.lsp nested folding ranges', function()
728  local bufnr ---@type integer
729
730  local function start_server(text, ranges)
731    insert(text)
732    exec_lua(function(ranges)
733      _G.server = _G._create_server({
734        capabilities = {
735          foldingRangeProvider = true,
736        },
737        handlers = {
738          ['textDocument/foldingRange'] = function(_, _, callback)
739            callback(nil, ranges)
740          end,
741        },
742      })
743
744      vim.api.nvim_win_set_buf(0, bufnr)
745      vim.lsp.start({ name = 'dummy', cmd = _G.server.cmd })
746    end, ranges)
747    command(
748      [[set foldmethod=expr foldexpr=v:lua.vim.lsp.foldexpr() foldtext=v:lua.vim.lsp.foldtext() foldminlines=0]]
749    )
750  end
751
752  before_each(function()
753    clear_notrace()
754    exec_lua(create_server_definition)
755    bufnr = api.nvim_get_current_buf()
756  end)
757  after_each(function()
758    api.nvim_exec_autocmds('VimLeavePre', { modeline = false })
759  end)
760
761  it('uses the outermost level when nested ranges end on the same row', function()
762    start_server(
763      dedent([=[
764        local function first()
765          return {
766            child = {
767              value = true,
768            } } end; local function second() return {
769          child = {
770            value = false,
771          },
772        }
773        end
774      ]=]),
775      {
776        { startLine = 0, endLine = 3, kind = 'region' },
777        { startLine = 4, endLine = 8, kind = 'region' },
778        { startLine = 1, endLine = 3, kind = 'region' },
779        { startLine = 4, endLine = 7, kind = 'region' },
780        { startLine = 2, endLine = 3, kind = 'region' },
781        { startLine = 5, endLine = 6, kind = 'region' },
782      }
783    )
784
785    retry(nil, nil, function()
786      eq(
787        { '>1', '>2', '>3', '<1', '>2', '>3', '<3', '<2', '<1', '0' },
788        exec_lua(function()
789          local levels = {}
790          for lnum = 1, 10 do
791            levels[lnum] = vim.lsp.foldexpr(lnum)
792          end
793          return levels
794        end)
795      )
796    end)
797
798    exec_lua(function()
799      vim._foldupdate(vim.api.nvim_get_current_win(), 0, vim.api.nvim_buf_line_count(0))
800    end)
801    command('normal! zM')
802    eq(
803      { 1, 4, 5, 9 },
804      exec_lua(function()
805        return {
806          vim.fn.foldclosed(1),
807          vim.fn.foldclosedend(1),
808          vim.fn.foldclosed(5),
809          vim.fn.foldclosedend(5),
810        }
811      end)
812    )
813  end)
814
815  it('prefers a fold start when ranges end and start on the same row', function()
816    start_server('one\ntwo\nthree\nfour\nfive', {
817      { startLine = 0, endLine = 4 },
818      { startLine = 2, endLine = 4 },
819      { startLine = 0, endLine = 2 },
820    })
821
822    retry(nil, nil, function()
823      eq(
824        { '>2', '2', '>3', '2', '<1' },
825        exec_lua(function()
826          local levels = {}
827          for lnum = 1, 5 do
828            levels[lnum] = vim.lsp.foldexpr(lnum)
829          end
830          return levels
831        end)
832      )
833    end)
834  end)
835end)