diff options
author | kevinhwang91 <kevin.hwang@live.com> | 2022-04-04 19:42:26 +0800 |
---|---|---|
committer | kevinhwang91 <kevin.hwang@live.com> | 2022-04-04 19:51:57 +0800 |
commit | be2e0b5a9cfe94f81fd8f5f45b70d8b093c680fe (patch) | |
tree | 7bf1d0cd00b0c0fcd97bb25726e50fb2be925791 /script/core/completion/completion.lua | |
parent | 9d595463733c37c649b54d0f5b5db747a5eb7e5e (diff) | |
download | lua-language-server-be2e0b5a9cfe94f81fd8f5f45b70d8b093c680fe.zip |
fix(completion): use args table instead of args string to parse snippet
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.
Diffstat (limited to 'script/core/completion/completion.lua')
-rw-r--r-- | script/core/completion/completion.lua | 46 |
1 files changed, 26 insertions, 20 deletions
diff --git a/script/core/completion/completion.lua b/script/core/completion/completion.lua index 87542422..cf794433 100644 --- a/script/core/completion/completion.lua +++ b/script/core/completion/completion.lua @@ -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' @@ -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) |