Skip to content

Commit c8476de

Browse files
CLI option --doc_update.
Update an existing 'doc.json' without using --doc again.
1 parent 25cb0bd commit c8476de

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

script/cli/doc.lua

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,26 @@ local function collectVars(global, results)
264264
results[#results+1] = result
265265
end
266266

267+
---Add config settings to JSON output.
268+
---@param results table
269+
local function collectConfig(results)
270+
local result = {
271+
name = 'LuaLS',
272+
type = 'luals.config',
273+
DOC = fs.absolute(fs.path(DOC)):string(),
274+
defines = {},
275+
fields = {}
276+
}
277+
results[#results+1] = result
278+
end
279+
267280
---@async
268281
---@param callback fun(i, max)
269282
function export.export(outputPath, callback)
270283
local results = {}
271284
local globals = vm.getAllGlobals()
272285

286+
collectConfig(results)
273287
local max = 0
274288
for _ in pairs(globals) do
275289
max = max + 1
@@ -331,9 +345,53 @@ function export.makeDoc(outputPath)
331345
return docPath, mdPath
332346
end
333347

348+
349+
---Find file 'doc.json'.
350+
---@return fs.path
351+
local function findDocJson()
352+
local doc_json_path
353+
if type(DOC_UPDATE) == 'string' then
354+
doc_json_path = fs.absolute(fs.path(DOC_UPDATE)) .. '/doc.json'
355+
else
356+
doc_json_path = fs.current_path() .. '/doc.json'
357+
end
358+
if fs.exists(doc_json_path) then
359+
return doc_json_path
360+
else
361+
error(string.format('Error: File "%s" not found.', doc_json_path))
362+
end
363+
end
364+
365+
---@return string # path of 'doc.json'
366+
---@return string # path to be documented
367+
local function getPathDocUpdate()
368+
local doc_json_path = findDocJson()
369+
local ok, doc_path = pcall(
370+
function ()
371+
local json = require('json')
372+
local json_file = io.open(doc_json_path:string(), 'r'):read('*all')
373+
local json_data = json.decode(json_file)
374+
for _, section in ipairs(json_data) do
375+
if section.type == 'luals.config' then
376+
return section.DOC
377+
end
378+
end
379+
end)
380+
if ok then
381+
local doc_json_dir = doc_json_path:string():gsub('/doc.json', '')
382+
return doc_json_dir, doc_path
383+
else
384+
error(string.format('Error: Cannot update "%s".', doc_json_path .. '/doc.json'))
385+
end
386+
end
387+
334388
function export.runCLI()
335389
lang(LOCALE)
336390

391+
if DOC_UPDATE then
392+
DOC_OUT_PATH, DOC = getPathDocUpdate()
393+
end
394+
337395
if type(DOC) ~= 'string' then
338396
print(lang.script('CLI_CHECK_ERROR_TYPE', type(DOC)))
339397
return

script/cli/init.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ if _G['CHECK'] then
88
os.exit(0, true)
99
end
1010

11+
if _G['DOC_UPDATE'] then
12+
require 'cli.doc' .runCLI()
13+
os.exit(0, true)
14+
end
15+
1116
if _G['DOC'] then
1217
require 'cli.doc' .runCLI()
1318
os.exit(0, true)

script/global.d.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ DOC = ''
5656
---@type string
5757
DOC_OUT_PATH = ''
5858

59+
---update an existing doc.json
60+
---@type string
61+
DOC_UPDATE = ''
62+
5963
---@type string | '"Error"' | '"Warning"' | '"Information"' | '"Hint"'
6064
CHECKLEVEL = 'Warning'
6165

0 commit comments

Comments
 (0)