diff options
author | Andreas <andreas.matthias@gmail.com> | 2024-02-21 15:22:20 +0100 |
---|---|---|
committer | Andreas <andreas.matthias@gmail.com> | 2024-02-21 15:22:20 +0100 |
commit | c8476de85e1abb8686f4b977240cdb54d1414886 (patch) | |
tree | a6fc6d0457143c7597a0a551f6f8c05d0842e0e4 | |
parent | 25cb0bdd2a27a5e3ebdd5a7ebc999add296c7c28 (diff) | |
download | lua-language-server-c8476de85e1abb8686f4b977240cdb54d1414886.zip |
CLI option --doc_update.
Update an existing 'doc.json' without using --doc again.
-rw-r--r-- | script/cli/doc.lua | 58 | ||||
-rw-r--r-- | script/cli/init.lua | 5 | ||||
-rw-r--r-- | script/global.d.lua | 4 |
3 files changed, 67 insertions, 0 deletions
diff --git a/script/cli/doc.lua b/script/cli/doc.lua index fb9b0a8e..1f0e977e 100644 --- a/script/cli/doc.lua +++ b/script/cli/doc.lua @@ -264,12 +264,26 @@ local function collectVars(global, results) results[#results+1] = result end +---Add config settings to JSON output. +---@param results table +local function collectConfig(results) + local result = { + name = 'LuaLS', + type = 'luals.config', + DOC = fs.absolute(fs.path(DOC)):string(), + defines = {}, + fields = {} + } + results[#results+1] = result +end + ---@async ---@param callback fun(i, max) function export.export(outputPath, callback) local results = {} local globals = vm.getAllGlobals() + collectConfig(results) local max = 0 for _ in pairs(globals) do max = max + 1 @@ -331,9 +345,53 @@ function export.makeDoc(outputPath) return docPath, mdPath end + +---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 + function export.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 diff --git a/script/cli/init.lua b/script/cli/init.lua index 6d7fc0ff..d37c50ae 100644 --- a/script/cli/init.lua +++ b/script/cli/init.lua @@ -8,6 +8,11 @@ if _G['CHECK'] then os.exit(0, true) end +if _G['DOC_UPDATE'] then + require 'cli.doc' .runCLI() + os.exit(0, true) +end + if _G['DOC'] then require 'cli.doc' .runCLI() os.exit(0, true) diff --git a/script/global.d.lua b/script/global.d.lua index cee9e01b..36fb6bfb 100644 --- a/script/global.d.lua +++ b/script/global.d.lua @@ -56,6 +56,10 @@ DOC = '' ---@type string DOC_OUT_PATH = '' +---update an existing doc.json +---@type string +DOC_UPDATE = '' + ---@type string | '"Error"' | '"Warning"' | '"Information"' | '"Hint"' CHECKLEVEL = 'Warning' |