Skip to content

Commit 57451c8

Browse files
authored
Merge pull request #2562 from AndreasMatthias/doc-update
Update `doc.json`
2 parents a66d002 + 77b2946 commit 57451c8

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
@@ -278,12 +278,26 @@ local function collectVars(global, results)
278278
results[#results+1] = result
279279
end
280280

281+
---Add config settings to JSON output.
282+
---@param results table
283+
local function collectConfig(results)
284+
local result = {
285+
name = 'LuaLS',
286+
type = 'luals.config',
287+
DOC = fs.absolute(fs.path(DOC)):string(),
288+
defines = {},
289+
fields = {}
290+
}
291+
results[#results+1] = result
292+
end
293+
281294
---@async
282295
---@param callback fun(i, max)
283296
function export.export(outputPath, callback)
284297
local results = {}
285298
local globals = vm.getAllGlobals()
286299

300+
collectConfig(results)
287301
local max = 0
288302
for _ in pairs(globals) do
289303
max = max + 1
@@ -345,9 +359,53 @@ function export.makeDoc(outputPath)
345359
return docPath, mdPath
346360
end
347361

362+
363+
---Find file 'doc.json'.
364+
---@return fs.path
365+
local function findDocJson()
366+
local doc_json_path
367+
if type(DOC_UPDATE) == 'string' then
368+
doc_json_path = fs.absolute(fs.path(DOC_UPDATE)) .. '/doc.json'
369+
else
370+
doc_json_path = fs.current_path() .. '/doc.json'
371+
end
372+
if fs.exists(doc_json_path) then
373+
return doc_json_path
374+
else
375+
error(string.format('Error: File "%s" not found.', doc_json_path))
376+
end
377+
end
378+
379+
---@return string # path of 'doc.json'
380+
---@return string # path to be documented
381+
local function getPathDocUpdate()
382+
local doc_json_path = findDocJson()
383+
local ok, doc_path = pcall(
384+
function ()
385+
local json = require('json')
386+
local json_file = io.open(doc_json_path:string(), 'r'):read('*all')
387+
local json_data = json.decode(json_file)
388+
for _, section in ipairs(json_data) do
389+
if section.type == 'luals.config' then
390+
return section.DOC
391+
end
392+
end
393+
end)
394+
if ok then
395+
local doc_json_dir = doc_json_path:string():gsub('/doc.json', '')
396+
return doc_json_dir, doc_path
397+
else
398+
error(string.format('Error: Cannot update "%s".', doc_json_path .. '/doc.json'))
399+
end
400+
end
401+
348402
function export.runCLI()
349403
lang(LOCALE)
350404

405+
if DOC_UPDATE then
406+
DOC_OUT_PATH, DOC = getPathDocUpdate()
407+
end
408+
351409
if type(DOC) ~= 'string' then
352410
print(lang.script('CLI_CHECK_ERROR_TYPE', type(DOC)))
353411
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)