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
|
---@diagnostic disable: no-unknown
local helpers = require('test.functional.helpers')(after_each)
local eq = helpers.eq
local exec_lua = helpers.exec_lua
--- 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)
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(
[[
local line, cursor_col, lnum, result = ...
local line_to_cursor = line:sub(1, cursor_col)
local client_start_boundary = vim.fn.match(line_to_cursor, '\\k*$')
local items, server_start_boundary = require("vim.lsp._completion")._convert_results(
line,
lnum,
cursor_col,
client_start_boundary,
nil,
result,
"utf-16"
)
return {
items = items,
server_start_boundary = server_start_boundary
}
]],
line,
cursor_col,
lnum,
candidates
)
end
describe('vim.lsp._completion', function()
before_each(helpers.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 } },
-- real-world snippet text
{
label = 'foocar',
sortText = 'g',
insertText = 'foodar',
insertTextFormat = 2,
textEdit = {
newText = 'foobar(${1:place holder}, ${2:more ...holder{\\}})',
range = range0,
},
},
{
label = 'foocar',
sortText = 'h',
insertText = 'foodar(${1:var1} typ1, ${2:var2} *typ2) {$0\\}',
insertTextFormat = 2,
},
-- nested snippet tokens
{
label = 'foocar',
sortText = 'i',
insertText = 'foodar(${1:${2|typ1,typ2|}}) {$0\\}',
insertTextFormat = 2,
},
-- braced tabstop
{ label = 'foocar', sortText = 'j', insertText = 'foodar()${0}', insertTextFormat = 2 },
-- plain text
{
label = 'foocar',
sortText = 'k',
insertText = 'foodar(${1:var1})',
insertTextFormat = 1,
},
}
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 = 'foobar(place holder, more ...holder{})',
},
{
abbr = 'foocar',
word = 'foodar(var1 typ1, var2 *typ2) {}',
},
{
abbr = 'foocar',
word = 'foodar(typ1) {}',
},
{
abbr = 'foocar',
word = 'foodar()',
},
{
abbr = 'foocar',
word = 'foodar(${1:var1})',
},
}
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,
kind = 'Module',
menu = '',
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 = 'notthis_thread',
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,
kind = 'Module',
menu = '',
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)
end)
|