diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2024-01-10 17:41:07 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-10 17:41:07 +0800 |
commit | 4432dfea0fe8390be166b832a1ac416c8228296f (patch) | |
tree | 303ab537a96981614a2106f9c8771f8501555cde | |
parent | 37779f9b2493e51e59e1e4366bf7dcb8350e69bd (diff) | |
parent | a11c3928feaac59b460203ea02cbc48139d2acb4 (diff) | |
download | lua-language-server-4432dfea0fe8390be166b832a1ac416c8228296f.zip |
Merge pull request #2474 from fesily/plugin-add-OnTransformAst
astHelper: addParamTypeDoc
-rw-r--r-- | script/files.lua | 2 | ||||
-rw-r--r-- | script/parser/guide.lua | 19 | ||||
-rw-r--r-- | script/parser/luadoc.lua | 6 | ||||
-rw-r--r-- | script/plugins/astHelper.lua | 42 | ||||
-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 |
8 files changed, 119 insertions, 30 deletions
diff --git a/script/files.lua b/script/files.lua index b7a20e8b..8cc7a5ab 100644 --- a/script/files.lua +++ b/script/files.lua @@ -662,7 +662,7 @@ local function pluginOnTransformAst(uri, state) if not suc then return state end - state.ast = result + state.ast = result or state.ast return state end diff --git a/script/parser/guide.lua b/script/parser/guide.lua index 4efe066b..4e71c832 100644 --- a/script/parser/guide.lua +++ b/script/parser/guide.lua @@ -1309,21 +1309,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 64394e92..b835fd42 100644 --- a/script/parser/luadoc.lua +++ b/script/parser/luadoc.lua @@ -2114,6 +2114,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 @@ -2134,10 +2135,11 @@ return { pluginDocs[#pluginDocs+1] = doc doc.special = src doc.originalComment = comment + doc.virtual = true ast.state.pluginDocs = pluginDocs - return true + return doc end - return false + return nil end, luadoc = luadoc } diff --git a/script/plugins/astHelper.lua b/script/plugins/astHelper.lua index 6f303c79..aba09478 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 @@ -40,7 +38,7 @@ end ---@param index integer ---@return parser.object? function _M.removeArg(source, index) - if source.type == 'function' then + if source.type == 'function' or source.type == 'call' then local arg = table.remove(source.args, index) if not arg then return nil @@ -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.parent.parent, comment) +end + return _M @@ -104,12 +104,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..95e87694 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' diff --git a/test/plugins/test.lua b/test/plugins/test.lua new file mode 100644 index 00000000..655d30b8 --- /dev/null +++ b/test/plugins/test.lua @@ -0,0 +1,2 @@ +require 'plugins.ast.test' +require 'plugins.ffi.test' |