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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
local lclient = require 'lclient'
local furi = require 'file-uri'
local ws = require 'workspace'
local files = require 'files'
local util = require 'utility'
local lang = require 'language'
local config = require 'config.config'
local await = require 'await'
local progress = require 'progress'
local fs = require 'bee.filesystem'
local doc = {}
---Find file 'doc.json'.
---@return fs.path
local function findDocJson()
local doc_json_path
if type(DOC_UPDATE) == 'string' then
doc_json_path = fs.absolute(fs.path(DOC_UPDATE)) .. '/doc.json'
else
doc_json_path = fs.current_path() .. '/doc.json'
end
if fs.exists(doc_json_path) then
return doc_json_path
else
error(string.format('Error: File "%s" not found.', doc_json_path))
end
end
---@return string # path of 'doc.json'
---@return string # path to be documented
local function getPathDocUpdate()
local doc_json_path = findDocJson()
local ok, doc_path = pcall(
function ()
local json = require('json')
local json_file = io.open(doc_json_path:string(), 'r'):read('*all')
local json_data = json.decode(json_file)
for _, section in ipairs(json_data) do
if section.type == 'luals.config' then
return section.DOC
end
end
end)
if ok then
local doc_json_dir = doc_json_path:string():gsub('/doc.json', '')
return doc_json_dir, doc_path
else
error(string.format('Error: Cannot update "%s".', doc_json_path .. '/doc.json'))
end
end
---clones a module and assigns any internal upvalues pointing to the module to the new clone
---useful for sandboxing
---@param tbl table module to be cloned
---@return table module_clone the cloned module
local function reinstantiateModule(tbl, _new_module, _old_module, _has_seen)
_old_module = _old_module or tbl --remember old module only at root
_has_seen = _has_seen or {} --remember visited indecies
if(type(tbl) == 'table') then
if _has_seen[tbl] then return _has_seen[tbl] end
local clone = {}
_has_seen[tbl] = true
for key, value in pairs(tbl) do
clone[key] = reinstantiateModule(value, _new_module or clone, _old_module, _has_seen)
end
setmetatable(clone, getmetatable(tbl))
return clone
elseif(type(tbl) == 'function') then
local func = tbl
if _has_seen[func] then return _has_seen[func] end --copy function pointers instead of building clones
local upvalues = {}
local i = 1
while true do
local label, value = debug.getupvalue(func, i)
if not value then break end
upvalues[i] = value == _old_module and _new_module or value
i = i + 1
end
local new_func = load(string.dump(func))--, 'function@reinstantiateModule()', 'b', _ENV)
for index, upvalue in ipairs(upvalues) do
debug.setupvalue(new_func, index, upvalue)
end
_has_seen[func] = new_func
return new_func
else
return tbl
end
end
--these modules need to be loaded by the time this function is created
--im leaving them here since this is a pretty strange function that might get moved somewhere else later
--so make sure to bring these with you!
require 'workspace'
require 'vm'
require 'parser.guide'
require 'core.hover.description'
require 'core.hover.label'
require 'json-beautify'
require 'utility'
require 'provider.markdown'
---Gets config file's doc gen overrides.
---@return table dirty_module clone of the export module modified by user buildscript
local function injectBuildScript()
local sub_path = config.get(ws.rootUri, 'Lua.docScriptPath')
local module = reinstantiateModule( ( require 'cli.doc.export' ) )
--if default, then no build script modifications
if sub_path == '' then
return module
end
local resolved_path = fs.absolute(fs.path(DOC)):string() .. sub_path
local f <close> = io.open(resolved_path, 'r')
if not f then
error('could not open config file at '..tostring(resolved_path))
end
--include all `require`s in script.cli.doc.export in enviroment
--NOTE: allows access to the global enviroment!
local data, err = loadfile(resolved_path, 't', setmetatable({
export = module,
ws = require 'workspace',
vm = require 'vm',
guide = require 'parser.guide',
getDesc = require 'core.hover.description',
getLabel = require 'core.hover.label',
jsonb = require 'json-beautify',
util = require 'utility',
markdown = require 'provider.markdown'
},
{__index = _G}))
if err or not data then
error(err, 0)
end
data()
return module
end
---runtime call for documentation exporting
---@async
---@param outputPath string
function doc.makeDoc(outputPath)
ws.awaitReady(ws.rootUri)
local expandAlias = config.get(ws.rootUri, 'Lua.hover.expandAlias')
config.set(ws.rootUri, 'Lua.hover.expandAlias', false)
local _ <close> = function ()
config.set(ws.rootUri, 'Lua.hover.expandAlias', expandAlias)
end
await.sleep(0.1)
-- ready --
local prog <close> = progress.create(ws.rootUri, lang.script('CLI_DOC_WORKING'), 0)
local dirty_export = injectBuildScript()
local globals = dirty_export.gatherGlobals()
local docs = dirty_export.makeDocs(globals, function (i, max)
prog:setMessage(('%d/%d'):format(i, max))
prog:setPercentage((i) / max * 100)
end)
local ok, outPaths, err = dirty_export.serializeAndExport(docs, outputPath)
if not ok then
error(err)
end
return table.unpack(outPaths)
end
---CLI call for documentation (parameter '--DOC=...' is passed to server)
function doc.runCLI()
lang(LOCALE)
if DOC_UPDATE then
DOC_OUT_PATH, DOC = getPathDocUpdate()
end
if type(DOC) ~= 'string' then
print(lang.script('CLI_CHECK_ERROR_TYPE', type(DOC)))
return
end
local rootUri = furi.encode(fs.absolute(fs.path(DOC)):string())
if not rootUri then
print(lang.script('CLI_CHECK_ERROR_URI', DOC))
return
end
print('root uri = ' .. rootUri)
util.enableCloseFunction()
local lastClock = os.clock()
---@async
lclient():start(function (client)
client:registerFakers()
client:initialize {
rootUri = rootUri,
}
io.write(lang.script('CLI_DOC_INITING'))
config.set(nil, 'Lua.diagnostics.enable', false)
config.set(nil, 'Lua.hover.expandAlias', false)
ws.awaitReady(rootUri)
await.sleep(0.1)
--ready--
local dirty_export = injectBuildScript()
local globals = dirty_export.gatherGlobals()
local docs = dirty_export.makeDocs(globals, function (i, max)
if os.clock() - lastClock > 0.2 then
lastClock = os.clock()
local output = '\x0D'
.. ('>'):rep(math.ceil(i / max * 20))
.. ('='):rep(20 - math.ceil(i / max * 20))
.. ' '
.. ('0'):rep(#tostring(max) - #tostring(i))
.. tostring(i) .. '/' .. tostring(max)
io.write(output)
end
end)
io.write('\x0D')
local ok, outPaths, err = dirty_export.serializeAndExport(docs, DOC_OUT_PATH)
print(lang.script('CLI_DOC_DONE'))
for i, path in ipairs(outPaths) do
local this_err = (type(err) == 'table') and err[i] or nil
print(this_err or files.normalize(path))
end
end)
end
return doc
|