-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGenerateEmmyLuaAnnotations.lua
More file actions
168 lines (125 loc) · 5.19 KB
/
GenerateEmmyLuaAnnotations.lua
File metadata and controls
168 lines (125 loc) · 5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
local sourcePath = debug.getinfo(1).source;
sourcePath = string.gsub(sourcePath, "\\", "/")
sourcePath = string.gsub(sourcePath, "@", "")
sourcePath = string.gsub(sourcePath, "[^/]+$", "ClassAndFunction.json")
local targetDir = string.gsub(sourcePath, "[^/]+$", "EmmyLuaAnnotations/")
local json = require("dkjson")
local fullString = io.open(sourcePath,"r"):read("*a")
local allData = json.decode(fullString)
function typesToStringArray(t)
local stringArray = {}
for i, singleType in ipairs(t) do
if type(singleType) == "table" then
if singleType.kind == "array" then
table.insert(stringArray, table.concat(typesToStringArray(singleType.types), "|") .. "[]");
elseif singleType.kind == "table" then
table.insert(stringArray, "table");
elseif singleType.kind == "function" then
table.insert(stringArray, "function");
elseif singleType.kind == "literal" then
table.insert(stringArray, "number");
else
fff =abc+def+sss
end
elseif type(singleType)=="string" then
table.insert(stringArray, singleType);
end
end
return stringArray;
end
local ReservedWords = {
}
ReservedWords['function'] = true;
ReservedWords['end'] = true;
ReservedWords['for'] = true;
ReservedWords['while'] = true;
ReservedWords['local'] = true;
ReservedWords['and'] = true;
ReservedWords['or'] = true;
function fixReservedWord(name)
if ReservedWords[name] then
return "_"..name;
end
return name
end
function saveClass(data)
local savePath = targetDir .. "/" .. data.name .. ".lua"
local fieldsStrings = {}
local functionStrings = {}
if data.members then
for i,v in ipairs(data.members) do
if v.kind == "field" then
local string = "--"..v.description.."\n"
string = string .. "\t---@type " .. table.concat( typesToStringArray(v.types), "|") .. "\n";
string = string .. "\t" .. v.name .. " = nil;\n\n"
table.insert(fieldsStrings, string)
elseif v.kind == "function" then
local string = "--"..(v.description or "").."\n"
string = string .. "--available:" .. v.available .. "\n";
local argNames = {}
for i, arg in ipairs(v.args) do
arg.name = fixReservedWord(arg.name);
string = string .. "---@param " .. arg.name .. " " .. table.concat(typesToStringArray(arg.types),"|") .. "\n"
table.insert(argNames, arg.name)
end
string = string .. "---@return " .. table.concat(typesToStringArray(v.returns),",") .. "\n"
string = string .. "function " .. data.name .. ":" .. v.name .. "(" ..table.concat(argNames,",") .. ")end\n\n"
table.insert(functionStrings, string)
end
end
end
local file = io.open(savePath, "w");
file:write("---@class " .. data.name .. (data.extend and (" : " .. data.extend) or "").. "\n") ;
file:write(data.name .. " = \n{\n")
for i, field in ipairs(fieldsStrings) do
-- body
file:write("\t" .. field)
end
file:write("}\n")
for index, func in ipairs(functionStrings) do
-- body
file:write( func)
end
file:close()
end
local fileSaveFunction = io.open(targetDir .. "/a_GlobalFunctions.lua", "w")
for i,v in ipairs(allData) do
if v.kind == "class" then
saveClass(v)
elseif v.kind == "function" then
fileSaveFunction:write("--"..(v.description or "").."\n");
fileSaveFunction:write("--available:" .. v.available .. "\n")
local argNames = {}
for i, arg in ipairs(v.args) do
fileSaveFunction:write("---@param " .. arg.name .. " " .. table.concat(typesToStringArray(arg.types),"|") .. "\n")
table.insert(argNames, arg.name)
end
fileSaveFunction:write("---@return " .. table.concat(typesToStringArray(v.returns),",") .. "\n")
fileSaveFunction:write("function " .. v.name .. "(" ..table.concat(argNames,",") .. ")end\n\n")
end
end
fileSaveFunction:close();
--then fetch all constants and enum
fileSaveFunction = io.open(targetDir .. "/a_ConstantsAndEnum.lua", "w");
sourcePath = string.gsub(sourcePath, "[^/]+$", "ConstantAndEnum.json")
fullString = io.open(sourcePath,"r"):read("*a")
allData = json.decode(fullString)
for index, v in ipairs(allData) do
-- body
if v.kind == "constant" then
fileSaveFunction:write("--available:" .. v.available .. "\n")
fileSaveFunction:write(v.name .. " = " .. v.value .. "\n\n")
elseif v.kind == "enum" then
fileSaveFunction:write("--available:" .. v.available .. "\n")
fileSaveFunction:write(v.name .. " = \n{\n")
for index, enum in ipairs(v.members) do
-- body
if enum.description then
fileSaveFunction:write("\t--" .. enum.description .. "\n")
end
fileSaveFunction:write("\t" .. enum.name .. " = " .. tostring(enum.value) .. ";\n\n")
end
fileSaveFunction:write("}\n\n")
end
end
fileSaveFunction:close()