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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
|
---@diagnostic disable: no-unknown
local t = require('test.testutil')
local t_lsp = require('test.functional.plugin.lsp.testutil')
local n = require('test.functional.testnvim')()
local clear = n.clear
local eq = t.eq
local neq = t.neq
local exec_lua = n.exec_lua
local feed = n.feed
local retry = t.retry
local create_server_definition = t_lsp.create_server_definition
--- Convert completion results.
---
---@param line string line contents. Mark cursor position with `|`
---@param candidates lsp.CompletionList|lsp.CompletionItem[]
---@param lnum? integer 0-based, defaults to 0
---@return {items: table[], server_start_boundary: integer?}
local function complete(line, candidates, lnum, server_boundary)
lnum = lnum or 0
-- nvim_win_get_cursor returns 0 based column, line:find returns 1 based
local cursor_col = line:find('|') - 1
line = line:gsub('|', '')
return exec_lua(function(result)
local line_to_cursor = line:sub(1, cursor_col)
local client_start_boundary = vim.fn.match(line_to_cursor, '\\k*$')
local items, new_server_boundary = require('vim.lsp.completion')._convert_results(
line,
lnum,
cursor_col,
1,
client_start_boundary,
server_boundary,
result,
'utf-16'
)
return {
items = items,
server_start_boundary = new_server_boundary,
}
end, candidates)
end
describe('vim.lsp.completion: item conversion', function()
before_each(n.clear)
-- https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_completion
it('prefers textEdit over label as word', function()
local range0 = {
start = { line = 0, character = 0 },
['end'] = { line = 0, character = 0 },
}
local completion_list = {
-- resolves into label
{ label = 'foobar', sortText = 'a', documentation = 'documentation' },
{
label = 'foobar',
sortText = 'b',
documentation = { value = 'documentation' },
},
-- resolves into insertText
{ label = 'foocar', sortText = 'c', insertText = 'foobar' },
{ label = 'foocar', sortText = 'd', insertText = 'foobar' },
-- resolves into textEdit.newText
{
label = 'foocar',
sortText = 'e',
insertText = 'foodar',
textEdit = { newText = 'foobar', range = range0 },
},
{ label = 'foocar', sortText = 'f', textEdit = { newText = 'foobar', range = range0 } },
-- plain text
{
label = 'foocar',
sortText = 'g',
insertText = 'foodar(${1:var1})',
insertTextFormat = 1,
},
{
label = '•INT16_C(c)',
insertText = 'INT16_C(${1:c})',
insertTextFormat = 2,
filterText = 'INT16_C',
sortText = 'h',
textEdit = {
newText = 'INT16_C(${1:c})',
range = range0,
},
},
}
local expected = {
{
abbr = 'foobar',
word = 'foobar',
},
{
abbr = 'foobar',
word = 'foobar',
},
{
abbr = 'foocar',
word = 'foobar',
},
{
abbr = 'foocar',
word = 'foobar',
},
{
abbr = 'foocar',
word = 'foobar',
},
{
abbr = 'foocar',
word = 'foobar',
},
{
abbr = 'foocar',
word = 'foodar(${1:var1})', -- marked as PlainText, text is used as is
},
{
abbr = '•INT16_C(c)',
word = 'INT16_C',
},
}
local result = complete('|', completion_list)
result = vim.tbl_map(function(x)
return {
abbr = x.abbr,
word = x.word,
}
end, result.items)
eq(expected, result)
end)
it('filters on label if filterText is missing', function()
local completion_list = {
{ label = 'foo' },
{ label = 'bar' },
}
local result = complete('fo|', completion_list)
local expected = {
{
abbr = 'foo',
word = 'foo',
},
}
result = vim.tbl_map(function(x)
return {
abbr = x.abbr,
word = x.word,
}
end, result.items)
eq(expected, result)
end)
it('works on non word prefix', function()
local completion_list = {
{ label = ' foo', insertText = '->foo' },
}
local result = complete('wp.|', completion_list, 0, 2)
local expected = {
{
abbr = ' foo',
word = '->foo',
},
}
result = vim.tbl_map(function(x)
return {
abbr = x.abbr,
word = x.word,
}
end, result.items)
eq(expected, result)
end)
it('trims trailing newline or tab from textEdit', function()
local range0 = {
start = { line = 0, character = 0 },
['end'] = { line = 0, character = 0 },
}
local items = {
{
detail = 'ansible.builtin',
filterText = 'lineinfile ansible.builtin.lineinfile builtin ansible',
kind = 7,
label = 'ansible.builtin.lineinfile',
sortText = '2_ansible.builtin.lineinfile',
textEdit = {
newText = 'ansible.builtin.lineinfile:\n ',
range = range0,
},
},
}
local result = complete('|', items)
result = vim.tbl_map(function(x)
return {
abbr = x.abbr,
word = x.word,
}
end, result.items)
local expected = {
{
abbr = 'ansible.builtin.lineinfile',
word = 'ansible.builtin.lineinfile:',
},
}
eq(expected, result)
end)
it('prefers wordlike components for snippets', function()
-- There are two goals here:
--
-- 1. The `word` should match what the user started typing, so that vim.fn.complete() doesn't
-- filter it away, preventing snippet expansion
--
-- For example, if they type `items@ins`, luals returns `table.insert(items, $0)` as
-- textEdit.newText and `insert` as label.
-- There would be no prefix match if textEdit.newText is used as `word`
--
-- 2. If users do not expand a snippet, but continue typing, they should see a somewhat reasonable
-- `word` getting inserted.
--
-- For example in:
--
-- insertText: "testSuites ${1:Env}"
-- label: "testSuites"
--
-- "testSuites" should have priority as `word`, as long as the full snippet gets expanded on accept (<c-y>)
local range0 = {
start = { line = 0, character = 0 },
['end'] = { line = 0, character = 0 },
}
local completion_list = {
-- luals postfix snippet (typed text: items@ins|)
{
label = 'insert',
insertTextFormat = 2,
textEdit = {
newText = 'table.insert(items, $0)',
range = range0,
},
},
-- eclipse.jdt.ls `new` snippet
{
label = 'new',
insertTextFormat = 2,
textEdit = {
newText = '${1:Object} ${2:foo} = new ${1}(${3});\n${0}',
range = range0,
},
textEditText = '${1:Object} ${2:foo} = new ${1}(${3});\n${0}',
},
-- eclipse.jdt.ls `List.copyO` function call completion
{
label = 'copyOf(Collection<? extends E> coll) : List<E>',
insertTextFormat = 2,
insertText = 'copyOf',
textEdit = {
newText = 'copyOf(${1:coll})',
range = range0,
},
},
}
local expected = {
{
abbr = 'copyOf(Collection<? extends E> coll) : List<E>',
word = 'copyOf',
},
{
abbr = 'insert',
word = 'insert',
},
{
abbr = 'new',
word = 'new',
},
}
local result = complete('|', completion_list)
result = vim.tbl_map(function(x)
return {
abbr = x.abbr,
word = x.word,
}
end, result.items)
eq(expected, result)
end)
it('uses correct start boundary', function()
local completion_list = {
isIncomplete = false,
items = {
{
filterText = 'this_thread',
insertText = 'this_thread',
insertTextFormat = 1,
kind = 9,
label = ' this_thread',
score = 1.3205767869949,
sortText = '4056f757this_thread',
textEdit = {
newText = 'this_thread',
range = {
start = { line = 0, character = 7 },
['end'] = { line = 0, character = 11 },
},
},
},
},
}
local expected = {
abbr = ' this_thread',
dup = 1,
empty = 1,
icase = 1,
info = '',
kind = 'Module',
menu = '',
hl_group = '',
word = 'this_thread',
}
local result = complete(' std::this|', completion_list)
eq(7, result.server_start_boundary)
local item = result.items[1]
item.user_data = nil
eq(expected, item)
end)
it('should search from start boundary to cursor position', function()
local completion_list = {
isIncomplete = false,
items = {
{
filterText = 'this_thread',
insertText = 'this_thread',
insertTextFormat = 1,
kind = 9,
label = ' this_thread',
score = 1.3205767869949,
sortText = '4056f757this_thread',
textEdit = {
newText = 'this_thread',
range = {
start = { line = 0, character = 7 },
['end'] = { line = 0, character = 11 },
},
},
},
{
filterText = 'no_match',
insertText = 'notthis_thread',
insertTextFormat = 1,
kind = 9,
label = ' notthis_thread',
score = 1.3205767869949,
sortText = '4056f757this_thread',
textEdit = {
newText = 'notthis_thread',
range = {
start = { line = 0, character = 7 },
['end'] = { line = 0, character = 11 },
},
},
},
},
}
local expected = {
abbr = ' this_thread',
dup = 1,
empty = 1,
icase = 1,
info = '',
kind = 'Module',
menu = '',
hl_group = '',
word = 'this_thread',
}
local result = complete(' std::this|is', completion_list)
eq(1, #result.items)
local item = result.items[1]
item.user_data = nil
eq(expected, item)
end)
it('uses defaults from itemDefaults', function()
--- @type lsp.CompletionList
local completion_list = {
isIncomplete = false,
itemDefaults = {
editRange = {
start = { line = 1, character = 1 },
['end'] = { line = 1, character = 4 },
},
insertTextFormat = 2,
data = 'foobar',
},
items = {
{
label = 'hello',
data = 'item-property-has-priority',
textEditText = 'hello',
},
},
}
local result = complete('|', completion_list)
eq(1, #result.items)
local item = result.items[1].user_data.nvim.lsp.completion_item --- @type lsp.CompletionItem
eq(2, item.insertTextFormat)
eq('item-property-has-priority', item.data)
eq({ line = 1, character = 1 }, item.textEdit.range.start)
end)
it(
'uses insertText as textEdit.newText if there are editRange defaults but no textEditText',
function()
--- @type lsp.CompletionList
local completion_list = {
isIncomplete = false,
itemDefaults = {
editRange = {
start = { line = 1, character = 1 },
['end'] = { line = 1, character = 4 },
},
insertTextFormat = 2,
data = 'foobar',
},
items = {
{
insertText = 'the-insertText',
label = 'hello',
data = 'item-property-has-priority',
},
},
}
local result = complete('|', completion_list)
eq(1, #result.items)
local text = result.items[1].user_data.nvim.lsp.completion_item.textEdit.newText
eq('the-insertText', text)
end
)
it(
'defaults to label as textEdit.newText if insertText or textEditText are not present',
function()
local completion_list = {
isIncomplete = false,
itemDefaults = {
editRange = {
start = { line = 1, character = 1 },
['end'] = { line = 1, character = 4 },
},
insertTextFormat = 2,
data = 'foobar',
},
items = {
{
label = 'hello',
data = 'item-property-has-priority',
},
},
}
local result = complete('|', completion_list)
eq(1, #result.items)
local text = result.items[1].user_data.nvim.lsp.completion_item.textEdit.newText
eq('hello', text)
end
)
end)
describe('vim.lsp.completion: protocol', function()
before_each(function()
clear()
exec_lua(create_server_definition)
exec_lua(function()
_G.capture = {}
--- @diagnostic disable-next-line:duplicate-set-field
vim.fn.complete = function(col, matches)
_G.capture.col = col
_G.capture.matches = matches
end
end)
end)
after_each(clear)
--- @param completion_result lsp.CompletionList
--- @return integer
local function create_server(completion_result)
return exec_lua(function()
local server = _G._create_server({
capabilities = {
completionProvider = {
triggerCharacters = { '.' },
},
},
handlers = {
['textDocument/completion'] = function(_, _, callback)
callback(nil, completion_result)
end,
},
})
local bufnr = vim.api.nvim_get_current_buf()
vim.api.nvim_win_set_buf(0, bufnr)
return vim.lsp.start({
name = 'dummy',
cmd = server.cmd,
on_attach = function(client, bufnr0)
vim.lsp.completion.enable(true, client.id, bufnr0, {
convert = function(item)
return { abbr = item.label:gsub('%b()', '') }
end,
})
end,
})
end)
end
local function assert_matches(fn)
retry(nil, nil, function()
fn(exec_lua('return _G.capture.matches'))
end)
end
--- @param pos [integer, integer]
local function trigger_at_pos(pos)
exec_lua(function()
local win = vim.api.nvim_get_current_win()
vim.api.nvim_win_set_cursor(win, pos)
vim.lsp.completion.trigger()
end)
retry(nil, nil, function()
neq(nil, exec_lua('return _G.capture.col'))
end)
end
it('fetches completions and shows them using complete on trigger', function()
create_server({
isIncomplete = false,
items = {
{
label = 'hello',
},
{
label = 'hercules',
tags = { 1 }, -- 1 represents Deprecated tag
},
{
label = 'hero',
deprecated = true,
},
},
})
feed('ih')
trigger_at_pos({ 1, 1 })
assert_matches(function(matches)
eq({
{
abbr = 'hello',
dup = 1,
empty = 1,
icase = 1,
info = '',
kind = 'Unknown',
menu = '',
hl_group = '',
user_data = {
nvim = {
lsp = {
client_id = 1,
completion_item = {
label = 'hello',
},
},
},
},
word = 'hello',
},
{
abbr = 'hercules',
dup = 1,
empty = 1,
icase = 1,
info = '',
kind = 'Unknown',
menu = '',
hl_group = 'DiagnosticDeprecated',
user_data = {
nvim = {
lsp = {
client_id = 1,
completion_item = {
label = 'hercules',
tags = { 1 },
},
},
},
},
word = 'hercules',
},
{
abbr = 'hero',
dup = 1,
empty = 1,
icase = 1,
info = '',
kind = 'Unknown',
menu = '',
hl_group = 'DiagnosticDeprecated',
user_data = {
nvim = {
lsp = {
client_id = 1,
completion_item = {
label = 'hero',
deprecated = true,
},
},
},
},
word = 'hero',
},
}, matches)
end)
end)
it('merges results from multiple clients', function()
create_server({
isIncomplete = false,
items = {
{
label = 'hello',
},
},
})
create_server({
isIncomplete = false,
items = {
{
label = 'hallo',
},
},
})
feed('ih')
trigger_at_pos({ 1, 1 })
assert_matches(function(matches)
eq(2, #matches)
eq('hello', matches[1].word)
eq('hallo', matches[2].word)
end)
end)
it('executes commands', function()
local completion_list = {
isIncomplete = false,
items = {
{
label = 'hello',
command = {
arguments = { '1', '0' },
command = 'dummy',
title = '',
},
},
},
}
local client_id = create_server(completion_list)
exec_lua(function()
_G.called = false
local client = assert(vim.lsp.get_client_by_id(client_id))
client.commands.dummy = function()
_G.called = true
end
end)
feed('ih')
trigger_at_pos({ 1, 1 })
local item = completion_list.items[1]
exec_lua(function()
vim.v.completed_item = {
user_data = {
nvim = {
lsp = {
client_id = client_id,
completion_item = item,
},
},
},
}
end)
feed('<C-x><C-o><C-y>')
assert_matches(function(matches)
eq(1, #matches)
eq('hello', matches[1].word)
eq(true, exec_lua('return _G.called'))
end)
end)
it('enable(…,{convert=fn}) custom word/abbr format', function()
create_server({
isIncomplete = false,
items = {
{
label = 'foo(bar)',
},
},
})
feed('ifo')
trigger_at_pos({ 1, 1 })
assert_matches(function(matches)
eq('foo', matches[1].abbr)
end)
end)
end)
|