Skip to content

Update doc.json #2562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions script/cli/doc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions script/cli/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions script/global.d.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down