Skip to content

Commit 4a9ab5b

Browse files
committed
command lua.exportDocument
finish #1833
1 parent bb3f846 commit 4a9ab5b

File tree

11 files changed

+195
-72
lines changed

11 files changed

+195
-72
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# changelog
22

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

locale/en-us/script.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,14 @@ CLI_CHECK_SUCCESS =
602602
'Diagnosis completed, no problems found'
603603
CLI_CHECK_RESULTS =
604604
'Diagnosis complete, {} problems found, see {}'
605+
CLI_DOC_INITING =
606+
'Loading documents ...'
607+
CLI_DOC_DONE =
608+
[[
609+
Document exporting completed!
610+
Raw data: {}
611+
Markdown(example): {}
612+
]]
605613

606614
TYPE_ERROR_ENUM_GLOBAL_DISMATCH =
607615
'Type `{child}` cannot match enumeration type of `{parent}`'

locale/pt-br/script.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,14 @@ CLI_CHECK_SUCCESS =
602602
'Diagnóstico completo, nenhum problema encontrado'
603603
CLI_CHECK_RESULTS =
604604
'Diagnóstico completo, {} problemas encontrados, veja {}'
605+
CLI_DOC_INITING = -- TODO: need translate!
606+
'Loading documents ...'
607+
CLI_DOC_DONE = -- TODO: need translate!
608+
[[
609+
Document exporting completed!
610+
Raw data: {}
611+
Markdown(example): {}
612+
]]
605613

606614
TYPE_ERROR_ENUM_GLOBAL_DISMATCH = -- TODO: need translate!
607615
'Type `{child}` cannot match enumeration type of `{parent}`'

locale/zh-cn/script.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,14 @@ CLI_CHECK_SUCCESS =
602602
'诊断完成,没有发现问题'
603603
CLI_CHECK_RESULTS =
604604
'诊断完成,共有 {} 个问题,请查看 {}'
605+
CLI_DOC_INITING =
606+
'加载文档 ...'
607+
CLI_DOC_DONE =
608+
[[
609+
文档导出完成!
610+
原始数据: {}
611+
Markdown(演示用): {}
612+
]]
605613

606614
TYPE_ERROR_ENUM_GLOBAL_DISMATCH =
607615
'类型 `{child}` 无法匹配 `{parent}` 的枚举类型'

locale/zh-tw/script.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,14 @@ CLI_CHECK_SUCCESS =
602602
'診斷完成,沒有發現問題'
603603
CLI_CHECK_RESULTS =
604604
'診斷完成,共有 {} 個問題,請查看 {}'
605+
CLI_DOC_INITING = -- TODO: need translate!
606+
'Loading documents ...'
607+
CLI_DOC_DONE = -- TODO: need translate!
608+
[[
609+
Document exporting completed!
610+
Raw data: {}
611+
Markdown(example): {}
612+
]]
605613

606614
TYPE_ERROR_ENUM_GLOBAL_DISMATCH = -- TODO: need translate!
607615
'Type `{child}` cannot match enumeration type of `{parent}`'

script/cli/doc.lua

Lines changed: 100 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,11 @@ local vm = require 'vm'
1212
local guide = require 'parser.guide'
1313
local getDesc = require 'core.hover.description'
1414
local getLabel = require 'core.hover.label'
15+
local doc2md = require 'cli.doc2md'
16+
local progress = require 'progress'
17+
local fs = require 'bee.filesystem'
1518

16-
lang(LOCALE)
17-
18-
if type(DOC) ~= 'string' then
19-
print(lang.script('CLI_CHECK_ERROR_TYPE', type(DOC)))
20-
return
21-
end
22-
23-
local rootUri = furi.encode(DOC)
24-
if not rootUri then
25-
print(lang.script('CLI_CHECK_ERROR_URI', DOC))
26-
return
27-
end
28-
29-
util.enableCloseFunction()
30-
31-
local lastClock = os.clock()
32-
local results = {}
19+
local export = {}
3320

3421
---@async
3522
local function packObject(source, mark)
@@ -89,7 +76,7 @@ local function packObject(source, mark)
8976
if source.bindDocs then
9077
new['desc'] = getDesc(source)
9178
end
92-
new['view'] = new['view'] or vm.getInfer(source):view(rootUri)
79+
new['view'] = new['view'] or vm.getInfer(source):view(ws.rootUri)
9380
end
9481
return new
9582
end
@@ -112,7 +99,8 @@ end
11299

113100
---@async
114101
---@param global vm.global
115-
local function collect(global)
102+
---@param results table
103+
local function collect(global, results)
116104
if guide.isBasicType(global.name) then
117105
return
118106
end
@@ -122,7 +110,7 @@ local function collect(global)
122110
defines = {},
123111
fields = {},
124112
}
125-
for _, set in ipairs(global:getSets(rootUri)) do
113+
for _, set in ipairs(global:getSets(ws.rootUri)) do
126114
local uri = guide.getUri(set)
127115
if files.isLibrary(uri) then
128116
goto CONTINUE
@@ -149,7 +137,7 @@ local function collect(global)
149137
results[#results+1] = result
150138
---@async
151139
---@diagnostic disable-next-line: not-yieldable
152-
vm.getClassFields(rootUri, global, vm.ANY, function (source)
140+
vm.getClassFields(ws.rootUri, global, vm.ANY, function (source)
153141
if source.type == 'doc.field' then
154142
---@cast source parser.object
155143
if files.isLibrary(guide.getUri(source)) then
@@ -160,7 +148,7 @@ local function collect(global)
160148
if source.field.type == 'doc.field.name' then
161149
field.name = source.field[1]
162150
else
163-
field.name = ('[%s]'):format(vm.getInfer(source.field):view(rootUri))
151+
field.name = ('[%s]'):format(vm.getInfer(source.field):view(ws.rootUri))
164152
end
165153
field.type = source.type
166154
field.file = guide.getUri(source)
@@ -219,46 +207,109 @@ local function collect(global)
219207
end
220208

221209
---@async
222-
lclient():start(function (client)
223-
client:registerFakers()
224-
225-
client:initialize {
226-
rootUri = rootUri,
227-
}
210+
---@param outputPath string
211+
function export.makeDoc(outputPath)
212+
local results = {}
228213

229-
io.write(lang.script('CLI_DOC_INITING'))
214+
ws.awaitReady(ws.rootUri)
230215

231-
config.set(nil, 'Lua.diagnostics.enable', false)
232-
config.set(nil, 'Lua.hover.expandAlias', false)
216+
local expandAlias = config.get(ws.rootUri, 'Lua.hover.expandAlias')
217+
config.set(ws.rootUri, 'Lua.hover.expandAlias', false)
218+
local _ <close> = function ()
219+
config.set(ws.rootUri, 'Lua.hover.expandAlias', expandAlias)
220+
end
233221

234-
ws.awaitReady(rootUri)
235222
await.sleep(0.1)
236223

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

239227
local max = #globals
240228
for i, global in ipairs(globals) do
241-
collect(global)
242-
if os.clock() - lastClock > 0.2 then
243-
lastClock = os.clock()
244-
local output = '\x0D'
245-
.. ('>'):rep(math.ceil(i / max * 20))
246-
.. ('='):rep(20 - math.ceil(i / max * 20))
247-
.. ' '
248-
.. ('0'):rep(#tostring(max) - #tostring(i))
249-
.. tostring(i) .. '/' .. tostring(max)
250-
io.write(output)
251-
end
229+
collect(global, results)
230+
prog:setMessage(('%d/%d'):format(i, max))
231+
prog:setPercentage(i / max * 100)
252232
end
253-
io.write('\x0D')
254233

255234
table.sort(results, function (a, b)
256235
return a.name < b.name
257236
end)
258-
end)
259237

260-
local outpath = LOGPATH .. '/doc.json'
261-
jsonb.supportSparseArray = true
262-
util.saveFile(outpath, jsonb.beautify(results))
238+
local docPath = outputPath .. '/doc.json'
239+
jsonb.supportSparseArray = true
240+
util.saveFile(docPath, jsonb.beautify(results))
241+
242+
local mdPath = doc2md.buildMD(outputPath)
243+
return docPath, mdPath
244+
end
245+
246+
function export.runCLI()
247+
lang(LOCALE)
248+
249+
if type(DOC) ~= 'string' then
250+
print(lang.script('CLI_CHECK_ERROR_TYPE', type(DOC)))
251+
return
252+
end
253+
254+
local rootUri = furi.encode(fs.absolute(fs.path(DOC)):string())
255+
if not rootUri then
256+
print(lang.script('CLI_CHECK_ERROR_URI', DOC))
257+
return
258+
end
259+
260+
print('root uri = ' .. rootUri)
261+
262+
util.enableCloseFunction()
263+
264+
local lastClock = os.clock()
265+
local results = {}
266+
267+
---@async
268+
lclient():start(function (client)
269+
client:registerFakers()
270+
271+
client:initialize {
272+
rootUri = rootUri,
273+
}
274+
275+
io.write(lang.script('CLI_DOC_INITING'))
276+
277+
config.set(nil, 'Lua.diagnostics.enable', false)
278+
config.set(nil, 'Lua.hover.expandAlias', false)
279+
280+
ws.awaitReady(rootUri)
281+
await.sleep(0.1)
282+
283+
local globals = vm.getGlobals 'type'
284+
285+
local max = #globals
286+
for i, global in ipairs(globals) do
287+
collect(global, results)
288+
if os.clock() - lastClock > 0.2 then
289+
lastClock = os.clock()
290+
local output = '\x0D'
291+
.. ('>'):rep(math.ceil(i / max * 20))
292+
.. ('='):rep(20 - math.ceil(i / max * 20))
293+
.. ' '
294+
.. ('0'):rep(#tostring(max) - #tostring(i))
295+
.. tostring(i) .. '/' .. tostring(max)
296+
io.write(output)
297+
end
298+
end
299+
io.write('\x0D')
300+
301+
table.sort(results, function (a, b)
302+
return a.name < b.name
303+
end)
304+
end)
305+
306+
local docPath = LOGPATH .. '/doc.json'
307+
jsonb.supportSparseArray = true
308+
util.saveFile(docPath, jsonb.beautify(results))
309+
310+
local mdPath = doc2md.buildMD(LOGPATH)
311+
312+
print(lang.script('CLI_DOC_DONE', furi.encode(docPath), furi.encode(mdPath)))
313+
end
263314

264-
require 'cli.doc2md'
315+
return export

script/cli/doc2md.lua

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,39 @@ local jsonc = require 'jsonc'
55
local util = require 'utility'
66
local markdown = require 'provider.markdown'
77

8-
local doc = jsonc.decode_jsonc(util.loadFile(LOGPATH .. '/doc.json'))
9-
local md = markdown()
10-
11-
assert(type(doc) == 'table')
12-
13-
for _, class in ipairs(doc) do
14-
md:add('md', '# ' .. class.name)
15-
md:emptyLine()
16-
md:add('md', class.desc)
17-
md:emptyLine()
18-
local mark = {}
19-
for _, field in ipairs(class.fields) do
20-
if not mark[field.name] then
21-
mark[field.name] = true
22-
md:add('md', '## ' .. field.name)
23-
md:emptyLine()
24-
md:add('lua', field.extends.view)
25-
md:emptyLine()
26-
md:add('md', field.desc)
27-
md:emptyLine()
8+
local export = {}
9+
10+
function export.buildMD(outputPath)
11+
local doc = jsonc.decode_jsonc(util.loadFile(outputPath .. '/doc.json'))
12+
local md = markdown()
13+
14+
assert(type(doc) == 'table')
15+
16+
for _, class in ipairs(doc) do
17+
md:add('md', '# ' .. class.name)
18+
md:emptyLine()
19+
md:add('md', class.desc)
20+
md:emptyLine()
21+
local mark = {}
22+
for _, field in ipairs(class.fields) do
23+
if not mark[field.name] then
24+
mark[field.name] = true
25+
md:add('md', '## ' .. field.name)
26+
md:emptyLine()
27+
md:add('lua', field.extends.view)
28+
md:emptyLine()
29+
md:add('md', field.desc)
30+
md:emptyLine()
31+
end
2832
end
33+
md:splitLine()
2934
end
30-
md:splitLine()
35+
36+
local mdPath = outputPath .. '/doc.md'
37+
38+
util.saveFile(mdPath, md:string())
39+
40+
return mdPath
3141
end
3242

33-
util.saveFile(LOGPATH .. '/doc.md', md:string())
43+
return export

script/cli/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ if _G['CHECK'] then
99
end
1010

1111
if _G['DOC'] then
12-
require 'cli.doc'
12+
require 'cli.doc' .runCLI()
1313
os.exit(0, true)
1414
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
local doc = require 'cli.doc'
2+
local client = require 'client'
3+
local furi = require 'file-uri'
4+
local lang = require 'language'
5+
6+
---@async
7+
return function (args)
8+
local outputPath = args[1] and furi.decode(args[1]) or LOGPATH
9+
local docPath, mdPath = doc.makeDoc(outputPath)
10+
client.showMessage('Info', lang.script('CLI_DOC_DONE', furi.encode(docPath), furi.encode(mdPath)))
11+
end

script/provider/provider.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,9 @@ m.register 'workspace/executeCommand' {
10011001
elseif command == 'lua.autoRequire' then
10021002
local core = require 'core.command.autoRequire'
10031003
return core(params.arguments[1])
1004+
elseif command == 'lua.exportDocument' then
1005+
local core = require 'core.command.exportDocument'
1006+
core(params.arguments)
10041007
end
10051008
end
10061009
}

0 commit comments

Comments
 (0)