diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2024-03-15 17:18:01 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-15 17:18:01 +0800 |
commit | 57451c8161b44f4a0e761d2224e61bcb0afb7032 (patch) | |
tree | c789ecef75c4145b2128ae5ae087f2dc68c52c5b /script | |
parent | a66d00246f47c9ed14f0598ea958b67a2eb73ff5 (diff) | |
parent | 77b2946434c161bffbe5ba21457a10de89cf402a (diff) | |
download | lua-language-server-57451c8161b44f4a0e761d2224e61bcb0afb7032.zip |
Merge pull request #2562 from AndreasMatthias/doc-update
Update `doc.json`
Diffstat (limited to 'script')
-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 7e7c8534..c413d354 100644 --- a/script/cli/doc.lua +++ b/script/cli/doc.lua @@ -278,12 +278,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 @@ -345,9 +359,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 40329996..ead46ca9 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' |