Skip to content

Commit 3905068

Browse files
authored
Merge pull request #1014 from kevinhwang91/truncate-opt-and-variable
feat(completion): truncate arguments for callSnippet
2 parents 7ca05ec + b4d112f commit 3905068

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

script/core/completion/completion.lua

+14
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,26 @@ local function buildFunctionSnip(source, value, oop)
157157
local name = (getName(source) or ''):gsub('^.+[$.:]', '')
158158
local args = getArg(value, oop)
159159
local id = 0
160+
local needTruncateId = 0
160161
args = args:gsub('[^,]+', function (arg)
161162
id = id + 1
163+
if arg:match('^%s*[^?]+%?:') or arg:match('^%s*%.%.%.:') then
164+
if needTruncateId == 0 then
165+
needTruncateId = id
166+
end
167+
else
168+
needTruncateId = 0
169+
end
162170
return arg:gsub('^(%s*)(.+)', function (sp, word)
163171
return ('%s${%d:%s}'):format(sp, id, word)
164172
end)
165173
end)
174+
if needTruncateId > 0 then
175+
local start = args:find(',?%s*%${' .. needTruncateId)
176+
if start then
177+
args = start == 1 and '$1' or args:sub(1, start - 1)
178+
end
179+
end
166180
return ('%s(%s)'):format(name, args)
167181
end
168182

test/completion/common.lua

+84
Original file line numberDiff line numberDiff line change
@@ -2373,6 +2373,90 @@ zzzz<??>
23732373
insertText = 'zzzz(${1:a: any}, ${2:b: any})',
23742374
},
23752375
}
2376+
2377+
TEST [[
2378+
---@param a any
2379+
---@param b? any
2380+
---@param c? any
2381+
---@vararg any
2382+
local function foo(a, b, c, ...) end
2383+
foo<??>
2384+
]]
2385+
{
2386+
{
2387+
label = 'foo(a, b, c, ...)',
2388+
kind = define.CompletionItemKind.Function,
2389+
insertText = 'foo',
2390+
},
2391+
{
2392+
label = 'foo(a, b, c, ...)',
2393+
kind = define.CompletionItemKind.Snippet,
2394+
insertText = 'foo(${1:a: any})',
2395+
},
2396+
}
2397+
2398+
TEST [[
2399+
---@param a any
2400+
---@param b? any
2401+
---@param c? any
2402+
---@vararg any
2403+
local function foo(a, b, c, ...) end
2404+
foo<??>
2405+
]]
2406+
{
2407+
{
2408+
label = 'foo(a, b, c, ...)',
2409+
kind = define.CompletionItemKind.Function,
2410+
insertText = 'foo',
2411+
},
2412+
{
2413+
label = 'foo(a, b, c, ...)',
2414+
kind = define.CompletionItemKind.Snippet,
2415+
insertText = 'foo(${1:a: any})',
2416+
},
2417+
}
2418+
2419+
TEST [[
2420+
---@param a? any
2421+
---@param b? any
2422+
---@param c? any
2423+
---@vararg any
2424+
local function foo(a, b, c, ...) end
2425+
foo<??>
2426+
]]
2427+
{
2428+
{
2429+
label = 'foo(a, b, c, ...)',
2430+
kind = define.CompletionItemKind.Function,
2431+
insertText = 'foo',
2432+
},
2433+
{
2434+
label = 'foo(a, b, c, ...)',
2435+
kind = define.CompletionItemKind.Snippet,
2436+
insertText = 'foo($1)',
2437+
},
2438+
}
2439+
2440+
TEST [[
2441+
---@param a? any
2442+
---@param b any
2443+
---@param c? any
2444+
---@vararg any
2445+
local function foo(a, b, c, ...) end
2446+
foo<??>
2447+
]]
2448+
{
2449+
{
2450+
label = 'foo(a, b, c, ...)',
2451+
kind = define.CompletionItemKind.Function,
2452+
insertText = 'foo',
2453+
},
2454+
{
2455+
label = 'foo(a, b, c, ...)',
2456+
kind = define.CompletionItemKind.Snippet,
2457+
insertText = 'foo(${1:a?: any}, ${2:b: any})',
2458+
},
2459+
}
23762460
Cared['insertText'] = false
23772461

23782462
TEST [[

0 commit comments

Comments
 (0)