Skip to content

feat(completion): truncate arguments for callSnippet #1014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions script/core/completion/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,26 @@ local function buildFunctionSnip(source, value, oop)
local name = (getName(source) or ''):gsub('^.+[$.:]', '')
local args = getArg(value, oop)
local id = 0
local needTruncateId = 0
args = args:gsub('[^,]+', function (arg)
id = id + 1
if arg:match('^%s*[^?]+%?:') or arg:match('^%s*%.%.%.:') then
if needTruncateId == 0 then
needTruncateId = id
end
else
needTruncateId = 0
end
return arg:gsub('^(%s*)(.+)', function (sp, word)
return ('%s${%d:%s}'):format(sp, id, word)
end)
end)
if needTruncateId > 0 then
local start = args:find(',?%s*%${' .. needTruncateId)
if start then
args = start == 1 and '$1' or args:sub(1, start - 1)
end
end
return ('%s(%s)'):format(name, args)
end

Expand Down
84 changes: 84 additions & 0 deletions test/completion/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2373,6 +2373,90 @@ zzzz<??>
insertText = 'zzzz(${1:a: any}, ${2:b: any})',
},
}

TEST [[
---@param a any
---@param b? any
---@param c? any
---@vararg any
local function foo(a, b, c, ...) end
foo<??>
]]
{
{
label = 'foo(a, b, c, ...)',
kind = define.CompletionItemKind.Function,
insertText = 'foo',
},
{
label = 'foo(a, b, c, ...)',
kind = define.CompletionItemKind.Snippet,
insertText = 'foo(${1:a: any})',
},
}

TEST [[
---@param a any
---@param b? any
---@param c? any
---@vararg any
local function foo(a, b, c, ...) end
foo<??>
]]
{
{
label = 'foo(a, b, c, ...)',
kind = define.CompletionItemKind.Function,
insertText = 'foo',
},
{
label = 'foo(a, b, c, ...)',
kind = define.CompletionItemKind.Snippet,
insertText = 'foo(${1:a: any})',
},
}

TEST [[
---@param a? any
---@param b? any
---@param c? any
---@vararg any
local function foo(a, b, c, ...) end
foo<??>
]]
{
{
label = 'foo(a, b, c, ...)',
kind = define.CompletionItemKind.Function,
insertText = 'foo',
},
{
label = 'foo(a, b, c, ...)',
kind = define.CompletionItemKind.Snippet,
insertText = 'foo($1)',
},
}

TEST [[
---@param a? any
---@param b any
---@param c? any
---@vararg any
local function foo(a, b, c, ...) end
foo<??>
]]
{
{
label = 'foo(a, b, c, ...)',
kind = define.CompletionItemKind.Function,
insertText = 'foo',
},
{
label = 'foo(a, b, c, ...)',
kind = define.CompletionItemKind.Snippet,
insertText = 'foo(${1:a?: any}, ${2:b: any})',
},
}
Cared['insertText'] = false

TEST [[
Expand Down