-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfake-generator.lua
193 lines (178 loc) · 5.72 KB
/
fake-generator.lua
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
-- LuaLS environment setup
local basePath = arg[0]:gsub("[/\\]*[^/\\]-$", "") -- The dir under which this file is
package.path = "./script/?.lua;./script/?/init.lua;./test/?.lua;./test/?/init.lua;"
package.path = package.path .. basePath .. "/?.lua;"
package.path = package.path .. basePath .. "/?/init.lua"
_G.log = require "log"
local fs = require "bee.filesystem"
local util = require "utility"
require "tracy"
ROOT = fs.canonical(fs.absolute("."))
LOGPATH = ROOT / "log"
METAPATH = ROOT / "meta"
LUA_VER = "Lua 5.1"
local ws = require "workspace"
local await = require "await"
local furi = require "file-uri"
local export = require "cli.doc.export"
local lclient = require "lclient"
local function dump(obj, depth)
depth = depth or 0
if depth > 3 then return end
local prefix = ("\t"):rep(depth)
if type(obj) ~= "table" then
print(prefix .. tostring(obj))
else
for k, v in pairs(obj) do
print(prefix .. tostring(k), tostring(v))
if type(v) == "table" then
dump(v, depth + 1)
end
end
end
end
-- Very hacky fake generator based on LuaLS annotations, this doesn't handle the general case and probably won't work for anything other than the WoW annotations.
-- Most notable it does not have a full understanding of the type system, nor does it understand constants (especially those defined in terms of other constants).
-- Ideas for future improvements:
-- * support type checking of arguments and log if we call a method with an incorrect type
-- * support constants
-- * trace calls for debugging
local types = {}
local classes = {}
local function typeToDefaultValueLiteral(typeName)
local t = types[typeName]
if classes[typeName] then
return ("setmetatable({}, {__index = %s})"):format(typeName)
end
if type(t) == "table" then
local entries = {}
if t.ref then
return typeToDefaultValueLiteral(typeName)
end
for k, v in pairs(t.fields) do
if k:match("^[_%w]+$") then
entries[#entries + 1] = ("%s = %s"):format(k, typeToDefaultValueLiteral(v))
else
entries[#entries + 1] = ("[%q] = %s"):format(k, typeToDefaultValueLiteral(v))
end
end
return "{" .. table.concat(entries, ", ") .. "}"
elseif type(t) == "string" then
return t
else
if typeName:match("%[%]$") then
-- Note: there is a magic zero-width space after the %s to avoid having to use ugly [=[ ]=] comments
return ("{} --[[%s]]"):format(typeName)
elseif typeName:match("^Enum%.") then
return ("0 --[[%s]]"):format(typeName)
elseif typeName:match("%?$") then
return ("nil --[[%s]]"):format(typeName)
end
return ("magicFake(%q)"):format(typeName)
end
end
types["boolean"] = "false"
types["number"] = "0"
types["integer"] = "0"
types["string"] = "\"\""
types["nil"] = "nil"
local function makeFake(doc)
local returns = {}
local extends = doc.defines[1].extends
if not extends then
dump(doc)
error("extends missing")
end
if extends.type == "function" then
if extends.returns then
for _, v in ipairs(extends.returns) do
returns[#returns + 1] = typeToDefaultValueLiteral(v.view)
end
end
print(([[
function %s()
return %s
end]]):format(doc.name, table.concat(returns, ", ")))
elseif extends.type == "table" then
print(("%s = {}"):format(doc.name))
elseif classes[extends.view] then
print(("%s = setmetatable({}, {__index = %s})"):format(doc.name, extends.view))
else
-- dump(doc)
-- error("unknown definition")
end
end
local function learnType(doc)
local t = {fields = {}}
if #doc.fields == 0 and doc.defines[1].view ~= doc.view then
t.ref = doc.defines[1].view
else
for _, v in ipairs(doc.fields) do
t.fields[v.name] = v.view
end
end
types[doc.name] = t
end
local fileName = arg[1]
DOC = fileName -- export expects the DOC global
local rootUri = furi.encode(fs.absolute(fs.path(fileName)):string())
util.enableCloseFunction()
print[[
-- Auto-generated, do not edit by hand.
-- Generation command:
-- `./bin/lua-language-server <DBM-Offline path>/fake-generator.lua <vscode-wow-api path>/Annotations`
---@meta _ -- Actually not a meta file as it gets run, but this surpresses warnings about duplicate definitions
local magicFake = require "fakes.magicfake"
]]
---@async
lclient():start(function (client)
client:registerFakers()
client:initialize {
rootUri = rootUri,
}
ws.awaitReady(rootUri)
await.sleep(0.1)
-- TODO: this misses some class definitions that are done via locals, e.g., the frame types (but we have our own fake for those)
local globals = export.gatherGlobals()
local docs = export.makeDocs(globals, function() end)
for i = #docs, 1, -1 do
local doc = docs[i]
local filtered
for _, v in ipairs(doc.defines) do
if v.file and (
not v.file:find(rootUri, 0, true)
or v.file:find("Annotations/Lua")
or v.file:find("Annotations/Libraries")
or v.file:find("Annotations/Interface/Blizzard_FrameXML/Constants.lua$")
or v.file:find("Data/Enum.lua$")
) then
filtered = true
end
end
-- Globals that start with a lowercase letter aren't WoW API functions but core functions/Lua extensions that we need to re-implement instead of fake
-- Examples: getglobal, securecall, ...
if doc.name:match("^%l") then
filtered = true
end
if filtered then
table.remove(docs, i)
end
end
for _, doc in ipairs(docs) do
if doc.type == "type" then
learnType(doc)
elseif doc.type == "variable" then
local extends = doc.defines[1].extends
if extends and extends.view ~= "table" and extends.view ~= "function" and extends.view ~= "string" and extends.view ~= "number" and extends.view ~= "integer" and extends.view ~= "boolean" then
classes[extends.view] = true
end
end
end
for _, doc in ipairs(docs) do
if doc.type == "variable" then
makeFake(doc)
elseif doc.type ~= "type" then
error("unhandled doc type: " .. doc.type)
end
end
end)