robin/laufey

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

robinmerge: 1dc9728daa1d8a2702

master
8.8 KiB384 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each = t.describe, t.it, t.before_each
5local api = n.api
6local fn = n.fn
7local clear = n.clear
8local eq = t.eq
9local exec_lua = n.exec_lua
10local feed = n.feed
11
12local function get_selected()
13  return table.concat(fn.getregion(fn.getpos('v'), fn.getpos('.')), '\n')
14end
15
16local function set_lines(lines)
17  if type(lines) == 'string' then
18    lines = vim.split(lines, '\n')
19  end
20  api.nvim_buf_set_lines(0, 0, -1, true, lines)
21end
22
23local function set_filetype(ft)
24  api.nvim_set_option_value('filetype', ft, { buf = 0 })
25end
26
27local function treeselect(cmd_, count_)
28  exec_lua(function(cmd, count)
29    vim.treesitter.select(cmd, count)
30  end, cmd_, count_)
31end
32
33describe('treesitter incremental-selection', function()
34  before_each(function()
35    clear()
36
37    local code = {
38      '',
39      'foo(1)',
40      'bar(2)',
41      '',
42    }
43
44    set_lines(code)
45    set_filetype('lua')
46    feed('G')
47  end)
48
49  it('works', function()
50    treeselect('child')
51    eq('foo(1)\nbar(2)\n', get_selected())
52
53    treeselect('child')
54    eq('foo(1)', get_selected())
55
56    treeselect('next')
57    eq('bar(2)', get_selected())
58
59    treeselect('prev')
60    eq('foo(1)', get_selected())
61
62    treeselect('parent')
63    eq('foo(1)\nbar(2)\n', get_selected())
64
65    set_lines('quux(1,foo,bar,baz,qux,2)')
66    feed('<esc>fbve')
67    eq('bar', get_selected())
68
69    treeselect('extend_next')
70    eq('bar,baz', get_selected())
71
72    treeselect('extend_prev')
73    eq('foo,bar,baz', get_selected())
74
75    -- Operator-pending (|omap-info| textobj) does not clobber '< '>. #40949
76    exec_lua(function()
77      vim.keymap.set('o', 'an', function()
78        vim.treesitter.select('parent', vim.v.count1)
79      end)
80    end)
81    feed('<Esc>0v<Esc>')
82    local vmarks = { fn.getpos("'<"), fn.getpos("'>"), 'v' }
83    feed('fbyan')
84    eq('bar', fn.getreg('"'))
85    eq(vmarks, { fn.getpos("'<"), fn.getpos("'>"), fn.visualmode() }) -- '< '> are unchanged.
86
87    -- selection=exclusive: the cursor char is the start, like "inclusive".
88    api.nvim_set_option_value('selection', 'exclusive', {})
89    feed('f2yan')
90    eq('(1,foo,bar,baz,qux,2)', fn.getreg('"'))
91  end)
92
93  it('repeat', function()
94    set_lines('foo(1,2,3,4)')
95    treeselect('child')
96    eq('foo', get_selected())
97    treeselect('next')
98    eq('(1,2,3,4)', get_selected())
99    treeselect('parent')
100    eq('foo(1,2,3,4)', get_selected())
101
102    treeselect('child', 2)
103    eq('1', get_selected())
104
105    treeselect('next', 3)
106    eq('4', get_selected())
107
108    treeselect('prev', 2)
109    eq('2', get_selected())
110
111    treeselect('parent', 2)
112    eq('foo(1,2,3,4)', get_selected())
113
114    treeselect('child', 2)
115    eq('2', get_selected())
116
117    feed('<esc>F1')
118    treeselect('extend_next', 2)
119    eq('1,2,3', get_selected())
120
121    feed('<esc>f4v')
122    treeselect('extend_prev', 2)
123    eq('2,3,4', get_selected())
124  end)
125
126  it('history', function()
127    treeselect('child')
128    treeselect('child')
129    treeselect('next')
130
131    eq('bar(2)', get_selected())
132    treeselect('parent')
133    eq('foo(1)\nbar(2)\n', get_selected())
134    treeselect('child')
135    eq('bar(2)', get_selected())
136
137    treeselect('prev')
138
139    eq('foo(1)', get_selected())
140    treeselect('parent')
141    eq('foo(1)\nbar(2)\n', get_selected())
142    treeselect('child')
143    eq('foo(1)', get_selected())
144  end)
145
146  it('selects node as parent when node half-selected', function()
147    feed('kkl', 'v', 'l')
148    eq('oo', get_selected())
149
150    treeselect('parent')
151    eq('foo', get_selected())
152  end)
153
154  it('selects node as child when node half-selected', function()
155    feed('kkl', 'v', 'l')
156    eq('oo', get_selected())
157
158    treeselect('child')
159    eq('foo', get_selected())
160  end)
161
162  it('finds child node when node half-selected', function()
163    feed('kkl', 'v', 'j')
164    eq('oo(1)\nba', get_selected())
165
166    treeselect('child')
167    eq('(1)', get_selected())
168  end)
169
170  it('maintains cursor selection-end-pos', function()
171    feed('kk')
172    treeselect('child')
173    eq('foo', get_selected())
174
175    treeselect('parent')
176    feed('h')
177    eq('foo(1', get_selected())
178
179    treeselect('child')
180    eq('foo', get_selected())
181
182    feed('o')
183    treeselect('parent')
184    feed('l')
185    eq('oo(1)', get_selected())
186  end)
187
188  it('handles outside root node', function()
189    feed('gg', 'v')
190    eq('', get_selected())
191
192    treeselect('child')
193    eq('foo(1)\nbar(2)\n', get_selected())
194
195    feed('<esc>gg', 'v')
196    eq('', get_selected())
197
198    treeselect('child')
199    eq('foo(1)\nbar(2)\n', get_selected())
200
201    feed('<esc>gg', 'v')
202    eq('', get_selected())
203
204    treeselect('parent')
205    eq('foo(1)\nbar(2)\n', get_selected())
206  end)
207
208  it('handles unicode', function()
209    set_lines {
210      '',
211      'foo("abö")',
212      '',
213    }
214
215    feed('gg', 'jfb', 'v')
216    treeselect('child')
217    eq('abö', get_selected())
218
219    treeselect('parent')
220    eq('"abö"', get_selected())
221  end)
222end)
223
224describe('treesitter incremental-selection with injections', function()
225  before_each(function()
226    clear({ args_rm = { '--cmd' }, args = { '--clean', '--cmd', n.runtime_set } })
227  end)
228
229  it('works', function()
230    set_lines('```lua\ndo foo() end\n```')
231    set_filetype('markdown')
232    feed('gg0')
233    treeselect('child')
234    treeselect('parent')
235    eq('```lua\ndo foo() end\n```', get_selected())
236
237    treeselect('child')
238    treeselect('next')
239    treeselect('next')
240    treeselect('child')
241    treeselect('child')
242    treeselect('child')
243
244    eq('foo', get_selected())
245
246    treeselect('parent')
247    treeselect('parent')
248    treeselect('parent')
249    treeselect('prev')
250
251    eq('lua', get_selected())
252
253    treeselect('next')
254    treeselect('next')
255
256    eq('```', get_selected())
257  end)
258
259  it('ignores overlapping nodes', function()
260    do
261      -- Check that, if injections are disabled, there are nodes overlapping the injection
262      exec_lua(function()
263        vim.treesitter.query.set('vimdoc', 'injections', '')
264        vim.cmd.enew()
265      end)
266
267      set_filetype('help')
268      set_lines('>lua\n \n foo(\n )')
269
270      feed('G0')
271      treeselect('child')
272      eq(' )', get_selected())
273      treeselect('prev')
274      eq(' foo(', get_selected())
275
276      exec_lua(function()
277        vim.treesitter.query.set('vimdoc', 'injections', ';; extends')
278        vim.cmd.enew()
279      end)
280    end
281
282    set_filetype('help')
283    set_lines('>lua\n \n foo(\n )')
284
285    feed('G0')
286    treeselect('child')
287    eq('(\n )', get_selected())
288    treeselect('parent')
289    treeselect('parent')
290    eq('foo(\n )', get_selected())
291
292    -- There will be one out of the siblings that wont be covered:
293    treeselect('prev')
294    eq(' ', get_selected())
295  end)
296
297  it('ignores overlapping injections', function()
298    exec_lua(function()
299      vim.treesitter.query.set(
300        'lua',
301        'injections',
302        [[
303      (comment
304        content: (_) @injection.content
305        (#set! injection.language "vim")
306        (#offset! @injection.content 0 1 0 -3))
307      (comment
308        content: (_) @injection.content
309        (#set! injection.language "c")
310        (#offset! @injection.content 0 2 0 0))
311      ]]
312      )
313      vim.cmd.enew()
314    end)
315
316    -- What the above query does is create the injections as follows (v=vim, c=c):
317    --             vvvv
318    --              cccccc
319    --          -- edit();
320
321    set_filetype('lua')
322    set_lines({ '-- edit();' })
323    feed('gg0lll')
324    treeselect('child')
325    if get_selected() == 'edit' then
326      -- It is random which injection gets higher priority,
327      --   as the priority uses the treesitter-node's id as a priority
328      --  So reverse the priority if not favorable
329      exec_lua("require'vim.treesitter._select'.TEST_SWITCH_PRIORITY=true")
330    end
331
332    feed('<esc>gg0lll')
333    treeselect('child')
334    eq(' edit();', get_selected())
335    treeselect('child')
336    eq('dit();', get_selected())
337    treeselect('prev') -- should do nothing
338    eq('dit();', get_selected())
339
340    exec_lua(
341      "require'vim.treesitter._select'.TEST_SWITCH_PRIORITY=not require'vim.treesitter._select'.TEST_SWITCH_PRIORITY"
342    )
343    feed('<esc>gg0lll')
344    treeselect('child')
345    eq('edit', get_selected())
346    treeselect('next') -- should do nothing
347    eq('edit', get_selected())
348  end)
349
350  it('handles disjointed trees', function()
351    exec_lua(function()
352      vim.treesitter.query.set(
353        'lua',
354        'injections',
355        [[
356      (comment
357        content: (_) @injection.content
358        (#set! injection.language "c")
359        (#set! injection.combined))
360      ]]
361      )
362      vim.cmd.enew()
363    end)
364
365    set_filetype('lua')
366    set_lines({ '--int foo={', '--1};' })
367    feed('gg$')
368
369    treeselect('child')
370    eq('{', get_selected())
371    treeselect('parent')
372    treeselect('parent')
373    treeselect('parent')
374    eq('--int foo={', get_selected())
375
376    treeselect('next')
377    eq('--1};', get_selected())
378    treeselect('child')
379    treeselect('child')
380    eq('1}', get_selected())
381    treeselect('prev') -- should do nothing
382    eq('1}', get_selected())
383  end)
384end)