Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion script/files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 13 additions & 6 deletions script/parser/guide.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 4 additions & 2 deletions script/parser/luadoc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
42 changes: 30 additions & 12 deletions script/plugins/astHelper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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
Expand All @@ -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
7 changes: 3 additions & 4 deletions test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
64 changes: 64 additions & 0 deletions test/plugins/ast/helper.lua
Original file line number Diff line number Diff line change
@@ -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)
7 changes: 2 additions & 5 deletions test/plugins/ast/init.lua → test/plugins/ast/test.lua
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -90,3 +85,5 @@ TestPlugin2 [[
TestPlugin2 [[
function ctor(self) end
]]

require 'plugins.ast.helper'
2 changes: 2 additions & 0 deletions test/plugins/test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require 'plugins.ast.test'
require 'plugins.ffi.test'