@@ -9,6 +9,9 @@ local m = {}
9
9
--- @field classes meta.class[]
10
10
11
11
--- @class meta.class
12
+ --- @field name string
13
+ --- @field comment string
14
+ --- @field location string
12
15
--- @field namespace string
13
16
--- @field baseClass string
14
17
--- @field attribute string
@@ -30,14 +33,89 @@ local m = {}
30
33
--- @field returnTypeName string
31
34
--- @field params { name : string , typeName : string } []
32
35
33
- --- @param api meta
36
+ --- @param ... string
34
37
--- @return string
35
- local function buildText (api )
38
+ local function mergeString (...)
39
+ local buf = {}
40
+ for i = 1 , select (' #' , ... ) do
41
+ local str = select (i , ... )
42
+ if str ~= ' ' then
43
+ buf [# buf + 1 ] = str
44
+ end
45
+ end
46
+ return table.concat (buf , ' .' )
47
+ end
48
+
49
+ local function addComments (lines , comment )
50
+ if comment == ' ' then
51
+ return
52
+ end
53
+ lines [# lines + 1 ] = ' --'
54
+ lines [# lines + 1 ] = ' --' .. comment :gsub (' [\r\n ]+$' , ' ' ):gsub (' \n ' , ' \n --' )
55
+ lines [# lines + 1 ] = ' --'
56
+ end
57
+
58
+ --- @param lines string[]
59
+ --- @param name string
60
+ --- @param method meta.method
61
+ local function addMethod (lines , name , method )
62
+ if not method .name :match ' ^[%a_][%w_]*$' then
63
+ return
64
+ end
65
+ addComments (lines , method .comment )
66
+ local params = {}
67
+ for _ , param in ipairs (method .params ) do
68
+ lines [# lines + 1 ] = (' ---@param %s %s' ):format (param .name , param .typeName )
69
+ params [# params + 1 ] = param .name
70
+ end
71
+ if method .returnTypeName ~= ' '
72
+ and method .returnTypeName ~= ' Void' then
73
+ lines [# lines + 1 ] = (' ---@return %s' ):format (method .returnTypeName )
74
+ end
75
+ lines [# lines + 1 ] = (' function %s%s%s(%s) end' ):format (
76
+ name ,
77
+ method .isStatic and ' :' or ' .' ,
78
+ method .name ,
79
+ table.concat (params , ' , ' )
80
+ )
81
+ lines [# lines + 1 ] = ' '
82
+ end
83
+
84
+ --- @param root string
85
+ --- @param class meta.class
86
+ --- @return string
87
+ local function buildText (root , class )
36
88
local lines = {}
37
- for _ , class in ipairs (api .classes ) do
38
-
89
+
90
+ addComments (lines , class .comment )
91
+ if class .baseClass == ' ' then
92
+ lines [# lines + 1 ] = (' ---@class %s' ):format (mergeString (class .namespace , class .name ))
93
+ else
94
+ lines [# lines + 1 ] = (' ---@class %s: %s' ):format (mergeString (class .namespace , class .name ), class .baseClass )
39
95
end
40
96
97
+ for _ , field in ipairs (class .fields ) do
98
+ addComments (lines , field .comment )
99
+ lines [# lines + 1 ] = (' ---@source %s' ):format (field .location :gsub (' #' , ' :' ))
100
+ lines [# lines + 1 ] = (' ---@field %s %s' ):format (field .name , field .typeName )
101
+ end
102
+
103
+ local name = mergeString (root , class .namespace , class .name )
104
+ lines [# lines + 1 ] = (' %s = {}' ):format (name )
105
+ lines [# lines + 1 ] = ' '
106
+
107
+ for _ , method in ipairs (class .methods ) do
108
+ addMethod (lines , name , method )
109
+ end
110
+
111
+ return table.concat (lines , ' \n ' )
112
+ end
113
+
114
+ local function buildRootText (api )
115
+ local lines = {}
116
+
117
+ lines [# lines + 1 ] = (' ---@class %s' ):format (api .root )
118
+ lines [# lines + 1 ] = (' %s = {}' ):format (api .root )
41
119
lines [# lines + 1 ] = ' '
42
120
return table.concat (lines , ' \n ' )
43
121
end
46
124
--- @param api meta
47
125
function m .build (name , api )
48
126
local encoding = config .get (nil , ' Lua.runtime.fileEncoding' )
49
- local filePath = fs .path (METAPATH ) / (name .. ' ' .. encoding .. ' .lua' )
127
+ local fileDir = fs .path (METAPATH ) / (name .. ' ' .. encoding )
128
+ fs .create_directories (fileDir )
129
+
130
+ local files = util .multiTable (2 , function ()
131
+ return { ' ---@meta' }
132
+ end )
133
+
134
+ files [api .root ][# files [api .root ]+ 1 ] = buildRootText (api )
50
135
51
- local text = buildText (api )
136
+ for _ , class in ipairs (api .classes ) do
137
+ local space = class .namespace ~= ' ' and class .namespace or api .root
138
+ local text = buildText (api .root , class )
139
+ files [space ][# files [space ]+ 1 ] = text
140
+ end
141
+
142
+ for space , texts in pairs (files ) do
143
+ util .saveFile ((fileDir / (space .. ' .lua' )):string (), table.concat (texts , ' \n\n ' ))
144
+ end
52
145
53
- util .saveFile (filePath :string (), text )
54
146
end
55
147
56
148
return m
0 commit comments