From 7f9547be6881c16cdf6d0977acf4c726c6e15348 Mon Sep 17 00:00:00 2001 From: kevinhwang91 Date: Sun, 3 Apr 2022 22:24:29 +0800 Subject: [PATCH 1/3] feat(completion): truncate arguments for callSnippet The callSnippet always expands all the optional arguments and variable argument that is annoying. To truncate optional arguments and variable arguments help users more frequently use `callSnippet`. We already have the label and signature to get the total arguments. This behavior is like tsserver. --- script/core/completion/completion.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/script/core/completion/completion.lua b/script/core/completion/completion.lua index 30df047cd..05b035afe 100644 --- a/script/core/completion/completion.lua +++ b/script/core/completion/completion.lua @@ -158,11 +158,14 @@ local function buildFunctionSnip(source, value, oop) local args = getArg(value, oop) local id = 0 args = args:gsub('[^,]+', function (arg) + if arg:match('^%s+[^?]+%?:') or arg:match('^%s+%.%.%.:') then + return '' + end id = id + 1 return arg:gsub('^(%s*)(.+)', function (sp, word) return ('%s${%d:%s}'):format(sp, id, word) end) - end) + end):gsub(',+$', '') return ('%s(%s)'):format(name, args) end From bc8e292e3bedc423d31abdd4ce040fcf1143d46a Mon Sep 17 00:00:00 2001 From: kevinhwang91 Date: Mon, 4 Apr 2022 02:15:00 +0800 Subject: [PATCH 2/3] fix(completion): optional arguments may not continued If the arguments are all optional or variable until the end, we can truncate them directly. --- script/core/completion/completion.lua | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/script/core/completion/completion.lua b/script/core/completion/completion.lua index 05b035afe..d751b727e 100644 --- a/script/core/completion/completion.lua +++ b/script/core/completion/completion.lua @@ -157,15 +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) - if arg:match('^%s+[^?]+%?:') or arg:match('^%s+%.%.%.:') then - return '' - end 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):gsub(',+$', '') + 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 From b4d112f6140a454e856301b636f101d56820d088 Mon Sep 17 00:00:00 2001 From: kevinhwang91 Date: Mon, 4 Apr 2022 14:23:12 +0800 Subject: [PATCH 3/3] fix(test): add test cases for callSnippet --- test/completion/common.lua | 84 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/test/completion/common.lua b/test/completion/common.lua index 95903acbe..fa261a5f9 100644 --- a/test/completion/common.lua +++ b/test/completion/common.lua @@ -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 [[