diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2022-04-04 15:45:55 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-04 15:45:55 +0800 |
commit | 39050680587e9e98af9bba06a8925277dd28c13c (patch) | |
tree | 03ef985af269dd78064f9b60d029395c78de8d1f | |
parent | 7ca05ec6a4a86f650b1058afac5541de7c48e9af (diff) | |
parent | b4d112f6140a454e856301b636f101d56820d088 (diff) | |
download | lua-language-server-39050680587e9e98af9bba06a8925277dd28c13c.zip |
Merge pull request #1014 from kevinhwang91/truncate-opt-and-variable
feat(completion): truncate arguments for callSnippet
-rw-r--r-- | script/core/completion/completion.lua | 14 | ||||
-rw-r--r-- | test/completion/common.lua | 84 |
2 files changed, 98 insertions, 0 deletions
diff --git a/script/core/completion/completion.lua b/script/core/completion/completion.lua index 30df047c..d751b727 100644 --- a/script/core/completion/completion.lua +++ b/script/core/completion/completion.lua @@ -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 diff --git a/test/completion/common.lua b/test/completion/common.lua index 95903acb..fa261a5f 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 [[ |