Skip to content
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

fix(completion): use args table instead of args string to parse snippet #1016

Merged
merged 1 commit 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
46 changes: 26 additions & 20 deletions script/core/completion/completion.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
local sp = require 'bee.subprocess'
local define = require 'proto.define'
local files = require 'files'
local searcher = require 'core.searcher'
local matchKey = require 'core.matchkey'
local vm = require 'vm'
local getName = require 'core.hover.name'
local getArg = require 'core.hover.arg'
local getArgs = require 'core.hover.args'
local getHover = require 'core.hover'
local config = require 'config'
local util = require 'utility'
Expand Down Expand Up @@ -155,29 +154,36 @@ end

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
local args = getArgs(value)
if oop then
table.remove(args, 1)
end
local len = #args
local truncated = false
if len > 0 and args[len]:match('^%s*%.%.%.:') then
table.remove(args)
truncated = true
end
for i = #args, 1, -1 do
if args[i]:match('^%s*[^?]+%?:') then
table.remove(args)
truncated = true
else
needTruncateId = 0
break
end
return arg:gsub('^(%s*)(.+)', function (sp, word)
end

local snipArgs = {}
for id, arg in ipairs(args) do
local str = 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
table.insert(snipArgs, str)
end
if truncated and #snipArgs == 0 then
snipArgs = {'$1'}
end
return ('%s(%s)'):format(name, args)
return ('%s(%s)'):format(name, table.concat(snipArgs, ', '))
end

local function buildDetail(source)
Expand Down
25 changes: 8 additions & 17 deletions script/core/hover/arg.lua → script/core/hover/args.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local guide = require 'parser.guide'
local infer = require 'core.infer'
local vm = require 'vm'

local function optionalArg(arg)
if not arg.bindDocs then
Expand All @@ -14,7 +13,7 @@ local function optionalArg(arg)
end
end

local function asFunction(source, oop)
local function asFunction(source)
local args = {}
local methodDef
local parent = source.parent
Expand Down Expand Up @@ -48,14 +47,10 @@ local function asFunction(source, oop)
::CONTINUE::
end
end
if oop then
return table.concat(args, ', ', 2)
else
return table.concat(args, ', ')
end
return args
end

local function asDocFunction(source, oop)
local function asDocFunction(source)
if not source.args then
return ''
end
Expand All @@ -69,19 +64,15 @@ local function asDocFunction(source, oop)
arg.extends and infer.searchAndViewInfers(arg.extends) or 'any'
)
end
if oop then
return table.concat(args, ', ', 2)
else
return table.concat(args, ', ')
end
return args
end

return function (source, oop)
return function (source)
if source.type == 'function' then
return asFunction(source, oop)
return asFunction(source)
end
if source.type == 'doc.type.function' then
return asDocFunction(source, oop)
return asDocFunction(source)
end
return ''
return {}
end
6 changes: 3 additions & 3 deletions script/core/hover/label.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local buildName = require 'core.hover.name'
local buildArg = require 'core.hover.arg'
local buildArgs = require 'core.hover.args'
local buildReturn = require 'core.hover.return'
local buildTable = require 'core.hover.table'
local infer = require 'core.infer'
Expand All @@ -12,15 +12,15 @@ local guide = require 'parser.guide'

local function asFunction(source, oop)
local name = buildName(source, oop)
local arg = buildArg(source, oop)
local args = buildArgs(source)
local rtn = buildReturn(source)
local lines = {}

lines[1] = string.format('%s%s %s(%s)'
, vm.isAsync(source) and 'async ' or ''
, oop and 'method' or 'function'
, name or ''
, arg
, oop and table.concat(args, ', ', 2) or table.concat(args, ', ')
)
lines[2] = rtn

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

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

TEST [[
Expand Down