Skip to content

Commit 4432dfe

Browse files
authored
Merge pull request #2474 from fesily/plugin-add-OnTransformAst
astHelper: addParamTypeDoc
2 parents 37779f9 + a11c392 commit 4432dfe

File tree

8 files changed

+119
-30
lines changed

8 files changed

+119
-30
lines changed

script/files.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ local function pluginOnTransformAst(uri, state)
662662
if not suc then
663663
return state
664664
end
665-
state.ast = result
665+
state.ast = result or state.ast
666666
return state
667667
end
668668

script/parser/guide.lua

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,21 +1309,28 @@ function m.isParam(source)
13091309
end
13101310

13111311
---@param source parser.object
1312-
---@param index integer
1313-
---@return parser.object?
1314-
function m.getParam(source, index)
1312+
---@return parser.object[]?
1313+
function m.getParams(source)
13151314
if source.type == 'call' then
13161315
local args = source.args
13171316
assert(args.type == 'callargs', 'call.args type is\'t callargs')
1318-
return args[index]
1317+
return args
13191318
elseif source.type == 'callargs' then
1320-
return source[index]
1319+
return source
13211320
elseif source.type == 'function' then
13221321
local args = source.args
13231322
assert(args.type == 'funcargs', 'function.args type is\'t callargs')
1324-
return args[index]
1323+
return args
13251324
end
13261325
return nil
13271326
end
13281327

1328+
---@param source parser.object
1329+
---@param index integer
1330+
---@return parser.object?
1331+
function m.getParam(source, index)
1332+
local args = m.getParams(source)
1333+
return args and args[index] or nil
1334+
end
1335+
13291336
return m

script/parser/luadoc.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,6 +2114,7 @@ local function luadoc(state)
21142114
table.sort(ast.docs, function (a, b)
21152115
return a.start < b.start
21162116
end)
2117+
ast.state.pluginDocs = nil
21172118
end
21182119

21192120
ast.docs.start = ast.start
@@ -2134,10 +2135,11 @@ return {
21342135
pluginDocs[#pluginDocs+1] = doc
21352136
doc.special = src
21362137
doc.originalComment = comment
2138+
doc.virtual = true
21372139
ast.state.pluginDocs = pluginDocs
2138-
return true
2140+
return doc
21392141
end
2140-
return false
2142+
return nil
21412143
end,
21422144
luadoc = luadoc
21432145
}

script/plugins/astHelper.lua

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ local ssub = require 'core.substring'
33
local guide = require 'parser.guide'
44
local _M = {}
55

6-
function _M.buildComment(t, value)
6+
function _M.buildComment(t, value, pos)
77
return {
8-
type = 'comment.short',
9-
start = 1,
10-
finish = 1,
11-
text = "-@" .. t .. " " .. value,
8+
type = 'comment.short',
9+
start = pos,
10+
finish = pos,
11+
text = "-@" .. t .. " " .. value,
12+
virtual = true
1213
}
1314
end
1415

@@ -28,10 +29,7 @@ function _M.addClassDoc(ast, source, classname)
2829
end
2930
--TODO fileds
3031
--TODO callers
31-
local comment = _M.buildComment("class", classname)
32-
comment.start = source.start - 1
33-
comment.finish = comment.start
34-
32+
local comment = _M.buildComment("class", classname, source.start - 1)
3533
return luadoc.buildAndBindDoc(ast, source, comment)
3634
end
3735

@@ -40,7 +38,7 @@ end
4038
---@param index integer
4139
---@return parser.object?
4240
function _M.removeArg(source, index)
43-
if source.type == 'function' then
41+
if source.type == 'function' or source.type == 'call' then
4442
local arg = table.remove(source.args, index)
4543
if not arg then
4644
return nil
@@ -51,16 +49,36 @@ function _M.removeArg(source, index)
5149
return nil
5250
end
5351

54-
--- 把特定函数当成构造函数,`index` 参数是self
52+
---把特定函数当成构造函数,`index` 参数是self
5553
---@param classname string
5654
---@param source parser.object function node
5755
---@param index integer
56+
---@return boolean, parser.object?
5857
function _M.addClassDocAtParam(ast, classname, source, index)
5958
local arg = _M.removeArg(source, index)
6059
if arg then
61-
return _M.addClassDoc(ast, arg, classname)
60+
return _M.addClassDoc(ast, arg, classname), arg
6261
end
6362
return false
6463
end
6564

65+
---把函数参数绑定类型
66+
---@param ast parser.object
67+
---@param typename string
68+
---@param source parser.object
69+
function _M.addParamTypeDoc(ast, typename, source)
70+
if not guide.isParam(source) then
71+
return false
72+
end
73+
local paramname = guide.getKeyName(source)
74+
if not paramname then
75+
return false
76+
end
77+
local comment = _M.buildComment("param",
78+
('%s %s'):format(paramname, typename),
79+
source.start - 1)
80+
81+
return luadoc.buildAndBindDoc(ast, source.parent.parent, comment)
82+
end
83+
6684
return _M

test.lua

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,11 @@ local function main()
104104

105105
testAll()
106106
end)
107-
107+
108108
test 'tclient'
109109
test 'full'
110-
test 'plugins.ffi.test'
111-
test 'plugins.ast'
112-
end
110+
test 'plugins.test'
111+
end
113112

114113
loadAllLibs()
115114
main()

test/plugins/ast/helper.lua

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
local helper = require 'plugins.astHelper'
2+
local parser = require 'parser'
3+
4+
function Run(script, plugin)
5+
local state = parser.compile(script, "Lua", "Lua 5.4")
6+
plugin(state)
7+
parser.luadoc(state)
8+
return state
9+
end
10+
11+
local function TestInsertDoc(script)
12+
local state = Run(script, function (state)
13+
local comment = assert(helper.buildComment("class", "AA", state.ast[1].start))
14+
helper.InsertDoc(state.ast, comment)
15+
end)
16+
assert(state.ast[1].bindDocs)
17+
end
18+
19+
TestInsertDoc("A={}")
20+
21+
local function TestaddClassDoc(script)
22+
local state = Run(script, function (state)
23+
assert(helper.addClassDoc(state.ast, state.ast[1], "AA"))
24+
end)
25+
assert(state.ast[1].bindDocs)
26+
end
27+
28+
TestaddClassDoc [[a={}]]
29+
30+
TestaddClassDoc [[local a={}]]
31+
32+
local function TestaddClassDocAtParam(script, index)
33+
index = index or 1
34+
local arg
35+
local state = Run(script, function (state)
36+
local func = state.ast[1].value
37+
local ok
38+
ok, arg = helper.addClassDocAtParam(state.ast, "AA", func, index)
39+
assert(ok)
40+
end)
41+
assert(arg.bindDocs)
42+
end
43+
44+
TestaddClassDocAtParam [[
45+
function a(b) end
46+
]]
47+
48+
local function TestaddParamTypeDoc(script, index)
49+
index = index or 1
50+
local func
51+
Run(script, function (state)
52+
func = state.ast[1].value
53+
assert(helper.addParamTypeDoc(state.ast, "string", func.args[index]))
54+
end)
55+
assert(func.args[index].bindDocs)
56+
end
57+
58+
TestaddParamTypeDoc [[
59+
local function t(a)end
60+
]]
61+
62+
TestaddParamTypeDoc([[
63+
local function t(a,b,c,d)end
64+
]], 4)

test/plugins/ast/init.lua renamed to test/plugins/ast/test.lua

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
local config = require 'config'
2-
local utility = require 'utility'
31
local parser = require 'parser'
4-
local luadoc = require 'parser.luadoc'
52
local guide = require 'parser.guide'
6-
local vm = require 'vm'
73
local helper = require 'plugins.astHelper'
84

95
---@diagnostic disable: await-in-sync
106
local function TestPlugin(script, plugin, checker)
11-
config.set(TESTURI, 'Lua.workspace.preloadFileSize', 1000000000)
127
local state = parser.compile(script, "Lua", "Lua 5.4")
138
state.ast = plugin(TESTURI, state.ast) or state.ast
149
parser.luadoc(state)
@@ -90,3 +85,5 @@ TestPlugin2 [[
9085
TestPlugin2 [[
9186
function ctor(self) end
9287
]]
88+
89+
require 'plugins.ast.helper'

test/plugins/test.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require 'plugins.ast.test'
2+
require 'plugins.ffi.test'

0 commit comments

Comments
 (0)