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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
local t = require('test.testutil')
local n = require('test.functional.testnvim')()
local api = n.api
local fn = n.fn
local clear = n.clear
local eq = t.eq
local exec_lua = n.exec_lua
local feed = n.feed
local function get_selected()
return table.concat(fn.getregion(fn.getpos('v'), fn.getpos('.')), '\n')
end
local function set_lines(lines)
if type(lines) == 'string' then
lines = vim.split(lines, '\n')
end
api.nvim_buf_set_lines(0, 0, -1, true, lines)
end
local function set_filetype(ft)
api.nvim_set_option_value('filetype', ft, { buf = 0 })
end
local function treeselect(cmd_, ...)
if cmd_ == 'select_node' then
cmd_ = 'select_child'
end
exec_lua(function(cmd, ...)
require 'vim.treesitter._select'[cmd](...)
end, cmd_, ...)
end
describe('treesitter incremental-selection', function()
before_each(function()
clear()
local code = {
'',
'foo(1)',
'bar(2)',
'',
}
set_lines(code)
set_filetype('lua')
feed('G')
end)
it('works', function()
treeselect('select_node')
eq('foo(1)\nbar(2)\n', get_selected())
treeselect('select_child')
eq('foo(1)', get_selected())
treeselect('select_next')
eq('bar(2)', get_selected())
treeselect('select_prev')
eq('foo(1)', get_selected())
treeselect('select_parent')
eq('foo(1)\nbar(2)\n', get_selected())
end)
it('repeat', function()
set_lines('foo(1,2,3,4)')
treeselect('select_node')
eq('foo', get_selected())
treeselect('select_next')
eq('(1,2,3,4)', get_selected())
treeselect('select_parent')
eq('foo(1,2,3,4)', get_selected())
treeselect('select_child', 2)
eq('1', get_selected())
treeselect('select_next', 3)
eq('4', get_selected())
treeselect('select_prev', 2)
eq('2', get_selected())
treeselect('select_parent', 2)
eq('foo(1,2,3,4)', get_selected())
treeselect('select_child', 2)
eq('2', get_selected())
end)
it('history', function()
treeselect('select_node')
treeselect('select_child')
treeselect('select_next')
eq('bar(2)', get_selected())
treeselect('select_parent')
eq('foo(1)\nbar(2)\n', get_selected())
treeselect('select_child')
eq('bar(2)', get_selected())
treeselect('select_prev')
eq('foo(1)', get_selected())
treeselect('select_parent')
eq('foo(1)\nbar(2)\n', get_selected())
treeselect('select_child')
eq('foo(1)', get_selected())
end)
it('selects node as parent when node half-selected', function()
feed('kkl', 'v', 'l')
eq('oo', get_selected())
treeselect('select_parent')
eq('foo', get_selected())
end)
it('selects node as child when node half-selected', function()
feed('kkl', 'v', 'l')
eq('oo', get_selected())
treeselect('select_child')
eq('foo', get_selected())
end)
it('finds child node when node half-selected', function()
feed('kkl', 'v', 'j')
eq('oo(1)\nba', get_selected())
treeselect('select_child')
eq('(1)', get_selected())
end)
it('maintains cursor selection-end-pos', function()
feed('kk')
treeselect('select_node')
eq('foo', get_selected())
treeselect('select_parent')
feed('h')
eq('foo(1', get_selected())
treeselect('select_child')
eq('foo', get_selected())
feed('o')
treeselect('select_parent')
feed('l')
eq('oo(1)', get_selected())
end)
it('handles outside root node', function()
feed('gg', 'v')
eq('', get_selected())
treeselect('select_node')
eq('foo(1)\nbar(2)\n', get_selected())
feed('<esc>gg', 'v')
eq('', get_selected())
treeselect('select_child')
eq('foo(1)\nbar(2)\n', get_selected())
feed('<esc>gg', 'v')
eq('', get_selected())
treeselect('select_parent')
eq('foo(1)\nbar(2)\n', get_selected())
end)
it('handles unicode', function()
set_lines {
'',
'foo("abö")',
'',
}
feed('gg', 'jfb', 'v')
treeselect('select_node')
eq('abö', get_selected())
treeselect('select_parent')
eq('"abö"', get_selected())
end)
end)
describe('treesitter incremental-selection with injections', function()
before_each(function()
clear({ args_rm = { '--cmd' }, args = { '--clean', '--cmd', n.runtime_set } })
end)
it('works', function()
set_lines('```lua\ndo foo() end\n```')
set_filetype('markdown')
feed('gg0')
treeselect('select_node')
treeselect('select_parent')
eq('```lua\ndo foo() end\n```', get_selected())
treeselect('select_child')
treeselect('select_next')
treeselect('select_next')
treeselect('select_child')
treeselect('select_child')
treeselect('select_child')
eq('foo', get_selected())
treeselect('select_parent')
treeselect('select_parent')
treeselect('select_parent')
treeselect('select_prev')
eq('lua', get_selected())
treeselect('select_next')
treeselect('select_next')
eq('```', get_selected())
end)
it('ignores overlapping nodes', function()
do
-- Check that, if injections are disabled, there are nodes overlapping the injection
exec_lua(function()
vim.treesitter.query.set('vimdoc', 'injections', '')
vim.cmd.enew()
end)
set_filetype('help')
set_lines('>lua\n \n foo(\n )')
feed('G0')
treeselect('select_node')
eq(' )', get_selected())
treeselect('select_prev')
eq(' foo(', get_selected())
exec_lua(function()
vim.treesitter.query.set('vimdoc', 'injections', ';; extends')
vim.cmd.enew()
end)
end
set_filetype('help')
set_lines('>lua\n \n foo(\n )')
feed('G0')
treeselect('select_node')
eq('(\n )', get_selected())
treeselect('select_parent')
treeselect('select_parent')
eq('foo(\n )', get_selected())
-- There will be one out of the siblings that wont be covered:
treeselect('select_prev')
eq(' ', get_selected())
end)
it('ignores overlapping injections', function()
exec_lua(function()
vim.treesitter.query.set(
'lua',
'injections',
[[
(comment
content: (_) @injection.content
(#set! injection.language "vim")
(#offset! @injection.content 0 1 0 -3))
(comment
content: (_) @injection.content
(#set! injection.language "c")
(#offset! @injection.content 0 2 0 0))
]]
)
vim.cmd.enew()
end)
-- What the above query does is create the injections as follows (v=vim, c=c):
-- vvvv
-- cccccc
-- -- edit();
set_filetype('lua')
set_lines({ '-- edit();' })
feed('gg0lll')
treeselect('select_node')
if get_selected() == 'edit' then
-- It is random which injection gets higher priority,
-- as the priority uses the treesitter-node's id as a priority
-- So reverse the priority if not favorable
exec_lua("require'vim.treesitter._select'.TEST_SWITCH_PRIORITY=true")
end
feed('<esc>gg0lll')
treeselect('select_node')
eq(' edit();', get_selected())
treeselect('select_child')
eq('dit();', get_selected())
treeselect('select_prev') -- should do nothing
eq('dit();', get_selected())
exec_lua(
"require'vim.treesitter._select'.TEST_SWITCH_PRIORITY=not require'vim.treesitter._select'.TEST_SWITCH_PRIORITY"
)
feed('<esc>gg0lll')
treeselect('select_node')
eq('edit', get_selected())
treeselect('select_next') -- should do nothing
eq('edit', get_selected())
end)
it('handles disjointed trees', function()
exec_lua(function()
vim.treesitter.query.set(
'lua',
'injections',
[[
(comment
content: (_) @injection.content
(#set! injection.language "c")
(#set! injection.combined))
]]
)
vim.cmd.enew()
end)
set_filetype('lua')
set_lines({ '--int foo={', '--1};' })
feed('gg$')
treeselect('select_node')
eq('{', get_selected())
treeselect('select_parent')
treeselect('select_parent')
treeselect('select_parent')
eq('--int foo={', get_selected())
treeselect('select_next')
eq('--1};', get_selected())
treeselect('select_child')
treeselect('select_child')
eq('1}', get_selected())
treeselect('select_prev') -- should do nothing
eq('1}', get_selected())
end)
end)
|