@@ -245,10 +245,41 @@ local function collectVars(global, results)
245245end
246246
247247--- @async
248- --- @param outputPath string
249- function export .makeDoc (outputPath )
248+ --- @param callback fun ( i , max )
249+ function export .export (outputPath , callback )
250250 local results = {}
251+ local globals = vm .getAllGlobals ()
252+
253+ local max = 0
254+ for _ in pairs (globals ) do
255+ max = max + 1
256+ end
257+ local i = 0
258+ for _ , global in pairs (globals ) do
259+ if global .cate == ' variable' then
260+ collectVars (global , results )
261+ elseif global .cate == ' type' then
262+ collectTypes (global , results )
263+ end
264+ i = i + 1
265+ callback (i , max )
266+ end
267+
268+ table.sort (results , function (a , b )
269+ return a .name < b .name
270+ end )
271+
272+ local docPath = outputPath .. ' /doc.json'
273+ jsonb .supportSparseArray = true
274+ util .saveFile (docPath , jsonb .beautify (results ))
275+
276+ local mdPath = doc2md .buildMD (outputPath )
277+ return docPath , mdPath
278+ end
251279
280+ --- @async
281+ --- @param outputPath string
282+ function export .makeDoc (outputPath )
252283 ws .awaitReady (ws .rootUri )
253284
254285 local expandAlias = config .get (ws .rootUri , ' Lua.hover.expandAlias' )
@@ -260,32 +291,11 @@ function export.makeDoc(outputPath)
260291 await .sleep (0.1 )
261292
262293 local prog <close> = progress .create (ws .rootUri , ' 正在生成文档...' , 0 )
263- local globalTypes = vm .getGlobals ' type'
264- local globalVars = vm .getGlobals ' variable'
265-
266- local max = # globalTypes + # globalVars
267-
268- for i , global in ipairs (globalTypes ) do
269- collectTypes (global , results )
294+ local docPath , mdPath = export .export (outputPath , function (i , max )
270295 prog :setMessage ((' %d/%d' ):format (i , max ))
271- prog :setPercentage (i / max * 100 )
272- end
273-
274- for i , global in ipairs (globalVars ) do
275- collectVars (global , results )
276- prog :setMessage ((' %d/%d' ):format (i + # globalTypes , max ))
277- prog :setPercentage ((i + # globalTypes ) / max * 100 )
278- end
279-
280- table.sort (results , function (a , b )
281- return a .name < b .name
296+ prog :setPercentage ((i ) / max * 100 )
282297 end )
283298
284- local docPath = outputPath .. ' /doc.json'
285- jsonb .supportSparseArray = true
286- util .saveFile (docPath , jsonb .beautify (results ))
287-
288- local mdPath = doc2md .buildMD (outputPath )
289299 return docPath , mdPath
290300end
291301
@@ -308,7 +318,6 @@ function export.runCLI()
308318 util .enableCloseFunction ()
309319
310320 local lastClock = os.clock ()
311- local results = {}
312321
313322 --- @async
314323 lclient ():start (function (client )
@@ -326,11 +335,7 @@ function export.runCLI()
326335 ws .awaitReady (rootUri )
327336 await .sleep (0.1 )
328337
329- local globals = vm .getGlobals ' type'
330-
331- local max = # globals
332- for i , global in ipairs (globals ) do
333- collectTypes (global , results )
338+ local docPath , mdPath = export .export (LOGPATH , function (i , max )
334339 if os.clock () - lastClock > 0.2 then
335340 lastClock = os.clock ()
336341 local output = ' \x0D '
@@ -341,24 +346,15 @@ function export.runCLI()
341346 .. tostring (i ) .. ' /' .. tostring (max )
342347 io.write (output )
343348 end
344- end
345- io.write (' \x0D ' )
346-
347- table.sort (results , function (a , b )
348- return a .name < b .name
349349 end )
350- end )
351-
352- local docPath = LOGPATH .. ' /doc.json'
353- jsonb .supportSparseArray = true
354- util .saveFile (docPath , jsonb .beautify (results ))
355350
356- local mdPath = doc2md . buildMD ( LOGPATH )
351+ io.write ( ' \x0D ' )
357352
358- print (lang .script (' CLI_DOC_DONE'
359- , (' [%s](%s)' ):format (files .normalize (docPath ), furi .encode (docPath ))
360- , (' [%s](%s)' ):format (files .normalize (mdPath ), furi .encode (mdPath ))
361- ))
353+ print (lang .script (' CLI_DOC_DONE'
354+ , (' [%s](%s)' ):format (files .normalize (docPath ), furi .encode (docPath ))
355+ , (' [%s](%s)' ):format (files .normalize (mdPath ), furi .encode (mdPath ))
356+ ))
357+ end )
362358end
363359
364360return export
0 commit comments