diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2021-01-30 21:12:24 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2021-01-30 21:12:24 +0800 |
commit | 9140dfc8fcf4f9dd4b28a8928fcf2ddcf977f9c7 (patch) | |
tree | 68b38f2b74ed2eab0fc3bc614f8abb33982238db /script/core | |
parent | 36d1ee596527e2133c8681c3e4bf493a85357862 (diff) | |
download | lua-language-server-9140dfc8fcf4f9dd4b28a8928fcf2ddcf977f9c7.zip |
hint of param name
Diffstat (limited to 'script/core')
-rw-r--r-- | script/core/hint.lua | 81 |
1 files changed, 74 insertions, 7 deletions
diff --git a/script/core/hint.lua b/script/core/hint.lua index e58758a3..0504735a 100644 --- a/script/core/hint.lua +++ b/script/core/hint.lua @@ -3,12 +3,11 @@ local guide = require 'parser.guide' local vm = require 'vm' local config = require 'config' -local function typeHint(uri, start, finish) +local function typeHint(uri, edits, start, finish) local ast = files.getAst(uri) if not ast then - return nil + return end - local edits = {} guide.eachSourceBetween(ast.ast, start, finish, function (source) if source.type ~= 'local' and source.type ~= 'setglobal' @@ -47,9 +46,77 @@ local function typeHint(uri, start, finish) finish = src.finish, } end) - return edits end -return { - typeHint = typeHint, -} +local function getArgNames(func) + if not func.args or #func.args == 0 then + return nil + end + local names = {} + for _, arg in ipairs(func.args) do + if arg.type == '...' then + break + end + names[#names+1] = arg[1] or '' + end + if #names == 0 then + return nil + end + return names +end + +local function paramName(uri, edits, start, finish) + if not config.config.hint.paramName then + return + end + local ast = files.getAst(uri) + if not ast then + return + end + guide.eachSourceBetween(ast.ast, start, finish, function (source) + if source.type ~= 'call' then + return + end + if not source.args then + return + end + local defs = vm.getDefs(source.node, 0) + if not defs then + return + end + local args + for _, def in ipairs(defs) do + if def.value then + def = def.value + end + if def.type == 'function' then + args = getArgNames(def) + if args then + break + end + end + end + if not args then + return + end + if source.node and source.node.type == 'getmethod' then + table.remove(args, 1) + end + for i, arg in ipairs(source.args) do + if args[i] and args[i] ~= '' then + edits[#edits+1] = { + newText = ('%s:'):format(args[i]), + start = arg.start, + finish = arg.start - 1, + } + end + end + end) +end + +return function (uri, start, finish) + local edits = {} + typeHint(uri, edits, start, finish) + paramName(uri, edits, start, finish) + return edits +end |