diff options
author | fesily <fesil@foxmail.com> | 2024-01-10 11:02:43 +0800 |
---|---|---|
committer | fesily <fesil@foxmail.com> | 2024-01-10 11:02:43 +0800 |
commit | bb6e172d6166190bd4edd3bb56230a7d60ebcb93 (patch) | |
tree | 935bd5608abf778319521a51216d22f5051c26ab | |
parent | 94a1cef1100621082b7f56d0a0fdd46c0662a79f (diff) | |
download | lua-language-server-bb6e172d6166190bd4edd3bb56230a7d60ebcb93.zip |
astHelper: addParamTypeDoc
-rw-r--r-- | script/parser/guide.lua | 19 | ||||
-rw-r--r-- | script/parser/luadoc.lua | 2 | ||||
-rw-r--r-- | script/plugins/astHelper.lua | 40 | ||||
-rw-r--r-- | test.lua | 7 | ||||
-rw-r--r-- | test/plugins/ast/helper.lua | 64 | ||||
-rw-r--r-- | test/plugins/ast/test.lua (renamed from test/plugins/ast/init.lua) | 7 | ||||
-rw-r--r-- | test/plugins/test.lua | 2 |
7 files changed, 115 insertions, 26 deletions
diff --git a/script/parser/guide.lua b/script/parser/guide.lua index 103a8cd5..0d190ed5 100644 --- a/script/parser/guide.lua +++ b/script/parser/guide.lua @@ -1306,21 +1306,28 @@ function m.isParam(source) end ---@param source parser.object ----@param index integer ----@return parser.object? -function m.getParam(source, index) +---@return parser.object[]? +function m.getParams(source) if source.type == 'call' then local args = source.args assert(args.type == 'callargs', 'call.args type is\'t callargs') - return args[index] + return args elseif source.type == 'callargs' then - return source[index] + return source elseif source.type == 'function' then local args = source.args assert(args.type == 'funcargs', 'function.args type is\'t callargs') - return args[index] + return args end return nil end +---@param source parser.object +---@param index integer +---@return parser.object? +function m.getParam(source, index) + local args = m.getParams(source) + return args and args[index] or nil +end + return m diff --git a/script/parser/luadoc.lua b/script/parser/luadoc.lua index df47fd94..9f3b8fd5 100644 --- a/script/parser/luadoc.lua +++ b/script/parser/luadoc.lua @@ -2071,6 +2071,7 @@ local function luadoc(state) table.sort(ast.docs, function (a, b) return a.start < b.start end) + ast.state.pluginDocs = nil end ast.docs.start = ast.start @@ -2091,6 +2092,7 @@ return { pluginDocs[#pluginDocs+1] = doc doc.special = src doc.originalComment = comment + doc.virtual = true ast.state.pluginDocs = pluginDocs return true end diff --git a/script/plugins/astHelper.lua b/script/plugins/astHelper.lua index 6f303c79..b12d62c6 100644 --- a/script/plugins/astHelper.lua +++ b/script/plugins/astHelper.lua @@ -3,12 +3,13 @@ local ssub = require 'core.substring' local guide = require 'parser.guide' local _M = {} -function _M.buildComment(t, value) +function _M.buildComment(t, value, pos) return { - type = 'comment.short', - start = 1, - finish = 1, - text = "-@" .. t .. " " .. value, + type = 'comment.short', + start = pos, + finish = pos, + text = "-@" .. t .. " " .. value, + virtual = true } end @@ -28,10 +29,7 @@ function _M.addClassDoc(ast, source, classname) end --TODO fileds --TODO callers - local comment = _M.buildComment("class", classname) - comment.start = source.start - 1 - comment.finish = comment.start - + local comment = _M.buildComment("class", classname, source.start - 1) return luadoc.buildAndBindDoc(ast, source, comment) end @@ -51,16 +49,36 @@ function _M.removeArg(source, index) return nil end ---- 把特定函数当成构造函数,`index` 参数是self +---把特定函数当成构造函数,`index` 参数是self ---@param classname string ---@param source parser.object function node ---@param index integer +---@return boolean, parser.object? function _M.addClassDocAtParam(ast, classname, source, index) local arg = _M.removeArg(source, index) if arg then - return _M.addClassDoc(ast, arg, classname) + return _M.addClassDoc(ast, arg, classname), arg end return false end +---把函数参数绑定类型 +---@param ast parser.object +---@param typename string +---@param source parser.object +function _M.addParamTypeDoc(ast, typename, source) + if not guide.isParam(source) then + return false + end + local paramname = guide.getKeyName(source) + if not paramname then + return false + end + local comment = _M.buildComment("param", + ('%s %s'):format(paramname, typename), + source.start - 1) + + return luadoc.buildAndBindDoc(ast, source, comment) +end + return _M @@ -102,12 +102,11 @@ local function main() testAll() end) - + test 'tclient' test 'full' - test 'plugins.ffi.test' - test 'plugins.ast' - end + test 'plugins.test' +end loadAllLibs() main() diff --git a/test/plugins/ast/helper.lua b/test/plugins/ast/helper.lua new file mode 100644 index 00000000..f892ea1b --- /dev/null +++ b/test/plugins/ast/helper.lua @@ -0,0 +1,64 @@ +local helper = require 'plugins.astHelper' +local parser = require 'parser' + +function Run(script, plugin) + local state = parser.compile(script, "Lua", "Lua 5.4") + plugin(state) + parser.luadoc(state) + return state +end + +local function TestInsertDoc(script) + local state = Run(script, function (state) + local comment = assert(helper.buildComment("class", "AA", state.ast[1].start)) + helper.InsertDoc(state.ast, comment) + end) + assert(state.ast[1].bindDocs) +end + +TestInsertDoc("A={}") + +local function TestaddClassDoc(script) + local state = Run(script, function (state) + assert(helper.addClassDoc(state.ast, state.ast[1], "AA")) + end) + assert(state.ast[1].bindDocs) +end + +TestaddClassDoc [[a={}]] + +TestaddClassDoc [[local a={}]] + +local function TestaddClassDocAtParam(script, index) + index = index or 1 + local arg + local state = Run(script, function (state) + local func = state.ast[1].value + local ok + ok, arg = helper.addClassDocAtParam(state.ast, "AA", func, index) + assert(ok) + end) + assert(arg.bindDocs) +end + +TestaddClassDocAtParam [[ + function a(b) end +]] + +local function TestaddParamTypeDoc(script, index) + index = index or 1 + local func + Run(script, function (state) + func = state.ast[1].value + assert(helper.addParamTypeDoc(state.ast, "string", func.args[index])) + end) + assert(func.args[index].bindDocs) +end + +TestaddParamTypeDoc [[ + local function t(a)end +]] + +TestaddParamTypeDoc([[ + local function t(a,b,c,d)end +]], 4) diff --git a/test/plugins/ast/init.lua b/test/plugins/ast/test.lua index ac2f7079..f13b33a2 100644 --- a/test/plugins/ast/init.lua +++ b/test/plugins/ast/test.lua @@ -1,14 +1,9 @@ -local config = require 'config' -local utility = require 'utility' local parser = require 'parser' -local luadoc = require 'parser.luadoc' local guide = require 'parser.guide' -local vm = require 'vm' local helper = require 'plugins.astHelper' ---@diagnostic disable: await-in-sync local function TestPlugin(script, plugin, checker) - config.set(TESTURI, 'Lua.workspace.preloadFileSize', 1000000000) local state = parser.compile(script, "Lua", "Lua 5.4") state.ast = plugin(TESTURI, state.ast) or state.ast parser.luadoc(state) @@ -90,3 +85,5 @@ TestPlugin2 [[ TestPlugin2 [[ function ctor(self) end ]] + +require 'plugins.ast.helper'
\ No newline at end of file diff --git a/test/plugins/test.lua b/test/plugins/test.lua new file mode 100644 index 00000000..d73bfac2 --- /dev/null +++ b/test/plugins/test.lua @@ -0,0 +1,2 @@ +require 'plugins.ast.test' +require 'plugins.ffi.test'
\ No newline at end of file |