Skip to content

Commit

Permalink
fix(completion): use args table instead of args string to parse snippet
Browse files Browse the repository at this point in the history
For now, we parse the `@param f fun(a: any, b: any)` as two placeholders instead of one.
BTW, use table have a slightly better performance than spliting args string.
  • Loading branch information
kevinhwang91 committed Apr 4, 2022
1 parent 9d59546 commit e56600b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 40 deletions.
45 changes: 25 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,35 @@ 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
if len > 0 and args[len]:match('^%s*%.%.%.:') then
table.remove(args)
end
local truncated = false
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

0 comments on commit e56600b

Please sign in to comment.