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 | |
parent | 36d1ee596527e2133c8681c3e4bf493a85357862 (diff) | |
download | lua-language-server-9140dfc8fcf4f9dd4b28a8928fcf2ddcf977f9c7.zip |
hint of param name
-rw-r--r-- | script/config.lua | 1 | ||||
-rw-r--r-- | script/core/hint.lua | 81 | ||||
-rw-r--r-- | script/provider/provider.lua | 23 |
3 files changed, 87 insertions, 18 deletions
diff --git a/script/config.lua b/script/config.lua index b39c70cc..96e25ea3 100644 --- a/script/config.lua +++ b/script/config.lua @@ -158,6 +158,7 @@ local ConfigTemplate = { enable = {true, Boolean}, paramType = {true, Boolean}, setType = {false, Boolean}, + paramName = {true, Boolean}, }, intelliSense = { searchDepth = {0, Integer}, 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 diff --git a/script/provider/provider.lua b/script/provider/provider.lua index ac259779..aceb0b6b 100644 --- a/script/provider/provider.lua +++ b/script/provider/provider.lua @@ -733,14 +733,21 @@ end) -- Hint do - local function updateTypeHint(uri, visibles, edits) + local function updateHint(uri) if not config.config.hint.enable then return end + local visibles = files.getVisibles(uri) + if not visibles then + return + end + await.close('hint') + await.setID('hint') + local edits = {} local hint = require 'core.hint' local _ <close> = progress.create(lang.script.WINDOW_PROCESSING_TYPE_HINT, 0.5) for _, visible in ipairs(visibles) do - local piece = hint.typeHint(uri, visible.start, visible.finish) + local piece = hint(uri, visible.start, visible.finish) if piece then for _, edit in ipairs(piece) do edits[#edits+1] = { @@ -750,15 +757,7 @@ do end end end - end - local function updateHint(uri) - local visibles = files.getVisibles(uri) - if not visibles then - return - end - local edits = {} - updateTypeHint(uri, visibles, edits) proto.notify('$/hint', { uri = uri, edits = edits, @@ -769,7 +768,9 @@ do if ev == 'create' or ev == 'update' or ev == 'updateVisible' then - updateHint(uri) + await.call(function () + updateHint(uri) + end) end end) end |