diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2023-03-09 11:50:55 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2023-03-09 11:50:55 +0800 |
commit | 84ef83e6a8dcb059e75def092a9327f2ce6a8e43 (patch) | |
tree | 5c5c11846311b4b454417b47cc6899141eb87e3d /script | |
parent | 7ef36fadb3607f8472239bd435b666399b0327d1 (diff) | |
download | lua-language-server-84ef83e6a8dcb059e75def092a9327f2ce6a8e43.zip |
export global variables
resolve #1943
Diffstat (limited to 'script')
-rw-r--r-- | script/cli/doc.lua | 63 | ||||
-rw-r--r-- | script/cli/doc2md.lua | 30 |
2 files changed, 77 insertions, 16 deletions
diff --git a/script/cli/doc.lua b/script/cli/doc.lua index d07375e4..ae821527 100644 --- a/script/cli/doc.lua +++ b/script/cli/doc.lua @@ -100,12 +100,13 @@ end ---@async ---@param global vm.global ---@param results table -local function collect(global, results) +local function collectTypes(global, results) if guide.isBasicType(global.name) then return end local result = { name = global.name, + type = 'type', desc = nil, defines = {}, fields = {}, @@ -207,6 +208,48 @@ local function collect(global, results) end ---@async +---@param global vm.global +---@param results table +local function collectVars(global, results) + local result = { + name = global:getCodeName(), + type = 'variable', + desc = nil, + defines = {}, + } + for _, set in ipairs(global:getSets(ws.rootUri)) do + local uri = guide.getUri(set) + if files.isLibrary(uri) then + goto CONTINUE + end + if set.type == 'setglobal' + or set.type == 'setfield' + or set.type == 'setmethod' + or set.type == 'setindex' then + result.defines[#result.defines+1] = { + type = set.type, + file = guide.getUri(set), + start = set.start, + finish = set.finish, + extends = packObject(set.value), + } + result.desc = result.desc or getDesc(set) + end + ::CONTINUE:: + end + if #result.defines == 0 then + return + end + table.sort(result.defines, function (a, b) + if a.file ~= b.file then + return a.file < b.file + end + return a.start < b.start + end) + results[#results+1] = result +end + +---@async ---@param outputPath string function export.makeDoc(outputPath) local results = {} @@ -222,15 +265,23 @@ function export.makeDoc(outputPath) await.sleep(0.1) local prog <close> = progress.create(ws.rootUri, '正在生成文档...', 0) - local globals = vm.getGlobals 'type' + local globalTypes = vm.getGlobals 'type' + local globalVars = vm.getGlobals 'variable' - local max = #globals - for i, global in ipairs(globals) do - collect(global, results) + local max = #globalTypes + #globalVars + + for i, global in ipairs(globalTypes) do + collectTypes(global, results) prog:setMessage(('%d/%d'):format(i, max)) prog:setPercentage(i / max * 100) end + for i, global in ipairs(globalVars) do + collectVars(global, results) + prog:setMessage(('%d/%d'):format(i + #globalTypes, max)) + prog:setPercentage((i + #globalTypes) / max * 100) + end + table.sort(results, function (a, b) return a.name < b.name end) @@ -284,7 +335,7 @@ function export.runCLI() local max = #globals for i, global in ipairs(globals) do - collect(global, results) + collectTypes(global, results) if os.clock() - lastClock > 0.2 then lastClock = os.clock() local output = '\x0D' diff --git a/script/cli/doc2md.lua b/script/cli/doc2md.lua index 9cc6c385..70c1b2a0 100644 --- a/script/cli/doc2md.lua +++ b/script/cli/doc2md.lua @@ -18,16 +18,26 @@ function export.buildMD(outputPath) md:emptyLine() md:add('md', class.desc) md:emptyLine() - local mark = {} - for _, field in ipairs(class.fields) do - if not mark[field.name] then - mark[field.name] = true - md:add('md', '## ' .. field.name) - md:emptyLine() - md:add('lua', field.extends.view) - md:emptyLine() - md:add('md', field.desc) - md:emptyLine() + if class.defines then + for _, define in ipairs(class.defines) do + if define.extends then + md:add('lua', define.extends.view) + md:emptyLine() + end + end + end + if class.fields then + local mark = {} + for _, field in ipairs(class.fields) do + if not mark[field.name] then + mark[field.name] = true + md:add('md', '## ' .. field.name) + md:emptyLine() + md:add('lua', field.extends.view) + md:emptyLine() + md:add('md', field.desc) + md:emptyLine() + end end end md:splitLine() |