Skip to content

Commit

Permalink
command lua.exportDocument
Browse files Browse the repository at this point in the history
finish #1833
  • Loading branch information
sumneko committed Jan 30, 2023
1 parent bb3f846 commit 4a9ab5b
Show file tree
Hide file tree
Showing 11 changed files with 195 additions and 72 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# changelog

## 3.6.8
* `NEW` command `lua.exportDocument` . VSCode will display this command in the right-click menu
* `FIX` [#1831]
[#1831]: https://github.com/sumneko/lua-language-server/issues/1831

Expand Down
8 changes: 8 additions & 0 deletions locale/en-us/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,14 @@ CLI_CHECK_SUCCESS =
'Diagnosis completed, no problems found'
CLI_CHECK_RESULTS =
'Diagnosis complete, {} problems found, see {}'
CLI_DOC_INITING =
'Loading documents ...'
CLI_DOC_DONE =
[[
Document exporting completed!
Raw data: {}
Markdown(example): {}
]]

TYPE_ERROR_ENUM_GLOBAL_DISMATCH =
'Type `{child}` cannot match enumeration type of `{parent}`'
Expand Down
8 changes: 8 additions & 0 deletions locale/pt-br/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,14 @@ CLI_CHECK_SUCCESS =
'Diagnóstico completo, nenhum problema encontrado'
CLI_CHECK_RESULTS =
'Diagnóstico completo, {} problemas encontrados, veja {}'
CLI_DOC_INITING = -- TODO: need translate!
'Loading documents ...'
CLI_DOC_DONE = -- TODO: need translate!
[[
Document exporting completed!
Raw data: {}
Markdown(example): {}
]]

TYPE_ERROR_ENUM_GLOBAL_DISMATCH = -- TODO: need translate!
'Type `{child}` cannot match enumeration type of `{parent}`'
Expand Down
8 changes: 8 additions & 0 deletions locale/zh-cn/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,14 @@ CLI_CHECK_SUCCESS =
'诊断完成,没有发现问题'
CLI_CHECK_RESULTS =
'诊断完成,共有 {} 个问题,请查看 {}'
CLI_DOC_INITING =
'加载文档 ...'
CLI_DOC_DONE =
[[
文档导出完成!
原始数据: {}
Markdown(演示用): {}
]]

TYPE_ERROR_ENUM_GLOBAL_DISMATCH =
'类型 `{child}` 无法匹配 `{parent}` 的枚举类型'
Expand Down
8 changes: 8 additions & 0 deletions locale/zh-tw/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,14 @@ CLI_CHECK_SUCCESS =
'診斷完成,沒有發現問題'
CLI_CHECK_RESULTS =
'診斷完成,共有 {} 個問題,請查看 {}'
CLI_DOC_INITING = -- TODO: need translate!
'Loading documents ...'
CLI_DOC_DONE = -- TODO: need translate!
[[
Document exporting completed!
Raw data: {}
Markdown(example): {}
]]

TYPE_ERROR_ENUM_GLOBAL_DISMATCH = -- TODO: need translate!
'Type `{child}` cannot match enumeration type of `{parent}`'
Expand Down
149 changes: 100 additions & 49 deletions script/cli/doc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,11 @@ local vm = require 'vm'
local guide = require 'parser.guide'
local getDesc = require 'core.hover.description'
local getLabel = require 'core.hover.label'
local doc2md = require 'cli.doc2md'
local progress = require 'progress'
local fs = require 'bee.filesystem'

lang(LOCALE)

if type(DOC) ~= 'string' then
print(lang.script('CLI_CHECK_ERROR_TYPE', type(DOC)))
return
end

local rootUri = furi.encode(DOC)
if not rootUri then
print(lang.script('CLI_CHECK_ERROR_URI', DOC))
return
end

util.enableCloseFunction()

local lastClock = os.clock()
local results = {}
local export = {}

---@async
local function packObject(source, mark)
Expand Down Expand Up @@ -89,7 +76,7 @@ local function packObject(source, mark)
if source.bindDocs then
new['desc'] = getDesc(source)
end
new['view'] = new['view'] or vm.getInfer(source):view(rootUri)
new['view'] = new['view'] or vm.getInfer(source):view(ws.rootUri)
end
return new
end
Expand All @@ -112,7 +99,8 @@ end

---@async
---@param global vm.global
local function collect(global)
---@param results table
local function collect(global, results)
if guide.isBasicType(global.name) then
return
end
Expand All @@ -122,7 +110,7 @@ local function collect(global)
defines = {},
fields = {},
}
for _, set in ipairs(global:getSets(rootUri)) do
for _, set in ipairs(global:getSets(ws.rootUri)) do
local uri = guide.getUri(set)
if files.isLibrary(uri) then
goto CONTINUE
Expand All @@ -149,7 +137,7 @@ local function collect(global)
results[#results+1] = result
---@async
---@diagnostic disable-next-line: not-yieldable
vm.getClassFields(rootUri, global, vm.ANY, function (source)
vm.getClassFields(ws.rootUri, global, vm.ANY, function (source)
if source.type == 'doc.field' then
---@cast source parser.object
if files.isLibrary(guide.getUri(source)) then
Expand All @@ -160,7 +148,7 @@ local function collect(global)
if source.field.type == 'doc.field.name' then
field.name = source.field[1]
else
field.name = ('[%s]'):format(vm.getInfer(source.field):view(rootUri))
field.name = ('[%s]'):format(vm.getInfer(source.field):view(ws.rootUri))
end
field.type = source.type
field.file = guide.getUri(source)
Expand Down Expand Up @@ -219,46 +207,109 @@ local function collect(global)
end

---@async
lclient():start(function (client)
client:registerFakers()

client:initialize {
rootUri = rootUri,
}
---@param outputPath string
function export.makeDoc(outputPath)
local results = {}

io.write(lang.script('CLI_DOC_INITING'))
ws.awaitReady(ws.rootUri)

config.set(nil, 'Lua.diagnostics.enable', false)
config.set(nil, 'Lua.hover.expandAlias', false)
local expandAlias = config.get(ws.rootUri, 'Lua.hover.expandAlias')
config.set(ws.rootUri, 'Lua.hover.expandAlias', false)
local _ <close> = function ()
config.set(ws.rootUri, 'Lua.hover.expandAlias', expandAlias)
end

ws.awaitReady(rootUri)
await.sleep(0.1)

local prog <close> = progress.create(ws.rootUri, '正在生成文档...', 0)
local globals = vm.getGlobals 'type'

local max = #globals
for i, global in ipairs(globals) do
collect(global)
if os.clock() - lastClock > 0.2 then
lastClock = os.clock()
local output = '\x0D'
.. ('>'):rep(math.ceil(i / max * 20))
.. ('='):rep(20 - math.ceil(i / max * 20))
.. ' '
.. ('0'):rep(#tostring(max) - #tostring(i))
.. tostring(i) .. '/' .. tostring(max)
io.write(output)
end
collect(global, results)
prog:setMessage(('%d/%d'):format(i, max))
prog:setPercentage(i / max * 100)
end
io.write('\x0D')

table.sort(results, function (a, b)
return a.name < b.name
end)
end)

local outpath = LOGPATH .. '/doc.json'
jsonb.supportSparseArray = true
util.saveFile(outpath, jsonb.beautify(results))
local docPath = outputPath .. '/doc.json'
jsonb.supportSparseArray = true
util.saveFile(docPath, jsonb.beautify(results))

local mdPath = doc2md.buildMD(outputPath)
return docPath, mdPath
end

function export.runCLI()
lang(LOCALE)

if type(DOC) ~= 'string' then
print(lang.script('CLI_CHECK_ERROR_TYPE', type(DOC)))
return
end

local rootUri = furi.encode(fs.absolute(fs.path(DOC)):string())
if not rootUri then
print(lang.script('CLI_CHECK_ERROR_URI', DOC))
return
end

print('root uri = ' .. rootUri)

util.enableCloseFunction()

local lastClock = os.clock()
local results = {}

---@async
lclient():start(function (client)
client:registerFakers()

client:initialize {
rootUri = rootUri,
}

io.write(lang.script('CLI_DOC_INITING'))

config.set(nil, 'Lua.diagnostics.enable', false)
config.set(nil, 'Lua.hover.expandAlias', false)

ws.awaitReady(rootUri)
await.sleep(0.1)

local globals = vm.getGlobals 'type'

local max = #globals
for i, global in ipairs(globals) do
collect(global, results)
if os.clock() - lastClock > 0.2 then
lastClock = os.clock()
local output = '\x0D'
.. ('>'):rep(math.ceil(i / max * 20))
.. ('='):rep(20 - math.ceil(i / max * 20))
.. ' '
.. ('0'):rep(#tostring(max) - #tostring(i))
.. tostring(i) .. '/' .. tostring(max)
io.write(output)
end
end
io.write('\x0D')

table.sort(results, function (a, b)
return a.name < b.name
end)
end)

local docPath = LOGPATH .. '/doc.json'
jsonb.supportSparseArray = true
util.saveFile(docPath, jsonb.beautify(results))

local mdPath = doc2md.buildMD(LOGPATH)

print(lang.script('CLI_DOC_DONE', furi.encode(docPath), furi.encode(mdPath)))
end

require 'cli.doc2md'
return export
54 changes: 32 additions & 22 deletions script/cli/doc2md.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,39 @@ local jsonc = require 'jsonc'
local util = require 'utility'
local markdown = require 'provider.markdown'

local doc = jsonc.decode_jsonc(util.loadFile(LOGPATH .. '/doc.json'))
local md = markdown()

assert(type(doc) == 'table')

for _, class in ipairs(doc) do
md:add('md', '# ' .. class.name)
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()
local export = {}

function export.buildMD(outputPath)
local doc = jsonc.decode_jsonc(util.loadFile(outputPath .. '/doc.json'))
local md = markdown()

assert(type(doc) == 'table')

for _, class in ipairs(doc) do
md:add('md', '# ' .. class.name)
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()
end
end
md:splitLine()
end
md:splitLine()

local mdPath = outputPath .. '/doc.md'

util.saveFile(mdPath, md:string())

return mdPath
end

util.saveFile(LOGPATH .. '/doc.md', md:string())
return export
2 changes: 1 addition & 1 deletion script/cli/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ if _G['CHECK'] then
end

if _G['DOC'] then
require 'cli.doc'
require 'cli.doc' .runCLI()
os.exit(0, true)
end
11 changes: 11 additions & 0 deletions script/core/command/exportDocument.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
local doc = require 'cli.doc'
local client = require 'client'
local furi = require 'file-uri'
local lang = require 'language'

---@async
return function (args)
local outputPath = args[1] and furi.decode(args[1]) or LOGPATH
local docPath, mdPath = doc.makeDoc(outputPath)
client.showMessage('Info', lang.script('CLI_DOC_DONE', furi.encode(docPath), furi.encode(mdPath)))
end
3 changes: 3 additions & 0 deletions script/provider/provider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,9 @@ m.register 'workspace/executeCommand' {
elseif command == 'lua.autoRequire' then
local core = require 'core.command.autoRequire'
return core(params.arguments[1])
elseif command == 'lua.exportDocument' then
local core = require 'core.command.exportDocument'
core(params.arguments)
end
end
}
Expand Down
15 changes: 15 additions & 0 deletions script/workspace/workspace.lua
Original file line number Diff line number Diff line change
Expand Up @@ -536,11 +536,26 @@ function m.awaitReady(uri)
end

---@param uri uri
---@return boolean
function m.isReady(uri)
local scp = scope.getScope(uri)
return scp:get('ready') == true
end

---@return boolean
function m.isAllReady()
local scp = scope.fallback
if not scp:get 'ready' then
return false
end
for _, folder in ipairs(scope.folders) do
if not folder:get 'ready' then
return false
end
end
return true
end

function m.getLoadingProcess(uri)
local scp = scope.getScope(uri)
---@type workspace.loading
Expand Down

0 comments on commit 4a9ab5b

Please sign in to comment.