summaryrefslogtreecommitdiff
path: root/script
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2022-09-21 21:06:46 +0800
committer最萌小汐 <sumneko@hotmail.com>2022-09-21 21:06:46 +0800
commit23055f5a6c0fc6a6d7f13ba9e635dde03b96c92f (patch)
tree4f01f3d9762efd11d417159bda0935f412a75051 /script
parent25acc18ff5ec25d5cf4a052dfba11c777847f86e (diff)
downloadlua-language-server-23055f5a6c0fc6a6d7f13ba9e635dde03b96c92f.zip
stack
Diffstat (limited to 'script')
-rw-r--r--script/cli/doc.lua175
-rw-r--r--script/cli/init.lua5
-rw-r--r--script/global.d.lua4
-rw-r--r--script/parser/luadoc.lua1
-rw-r--r--script/vm/compiler.lua2
-rw-r--r--script/vm/infer.lua10
6 files changed, 195 insertions, 2 deletions
diff --git a/script/cli/doc.lua b/script/cli/doc.lua
new file mode 100644
index 00000000..69a0d04c
--- /dev/null
+++ b/script/cli/doc.lua
@@ -0,0 +1,175 @@
+local lclient = require 'lclient'
+local furi = require 'file-uri'
+local ws = require 'workspace'
+local files = require 'files'
+local util = require 'utility'
+local json = require 'json-beautify'
+local lang = require 'language'
+local define = require 'proto.define'
+local config = require 'config.config'
+local await = require 'await'
+local vm = require 'vm'
+local guide = require 'parser.guide'
+local getDesc = require 'core.hover.description'
+
+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 function packObject(source, mark)
+ if type(source) ~= 'table' then
+ return source
+ end
+ if not mark then
+ mark = {}
+ end
+ if mark[source] then
+ return
+ end
+ mark[source] = true
+ local new = {}
+ if #source > 0 and next(source, #source) == nil then
+ new = {}
+ for i = 1, #source do
+ new[i] = packObject(source[i], mark)
+ end
+ else
+ for k, v in pairs(source) do
+ if type(k) == 'number' then
+ k = ('[%d]'):format(k)
+ end
+ if k == 'parent'
+ or k == 'typeGeneric'
+ or k == 'originalComment'
+ or k == 'node'
+ or k == 'class'
+ or k:find '_'
+ or k:find 'Cache'
+ or k:find 'bind' then
+ goto CONTINUE
+ end
+ new[k] = packObject(v, mark)
+ ::CONTINUE::
+ end
+ end
+ new['*view*'] = vm.viewObject(source, rootUri)
+ return new
+end
+
+local function getExtends(source)
+ if source.type == 'doc.class' then
+ if not source.extends then
+ return nil
+ end
+ return packObject(source.extends)
+ end
+ if source.type == 'doc.alias' then
+ if not source.extends then
+ return nil
+ end
+ return packObject(source.extends)
+ end
+end
+
+---@param global vm.global
+local function collect(global)
+ if guide.isBasicType(global.name) then
+ return
+ end
+ local result = {
+ name = global.name,
+ defines = {},
+ fields = {},
+ }
+ for _, set in ipairs(global:getSets(rootUri)) do
+ local uri = guide.getUri(set)
+ if files.isLibrary(uri) then
+ goto CONTINUE
+ end
+ result.defines[#result.defines+1] = {
+ type = set.type,
+ file = guide.getUri(set),
+ start = set.start,
+ finish = set.finish,
+ desc = getDesc(set),
+ extends = getExtends(set),
+ }
+ ::CONTINUE::
+ end
+ if #result.defines == 0 then
+ return
+ end
+ results[#results+1] = result
+ vm.getClassFields(rootUri, global, nil, false, function (source)
+ local field = {}
+ result.fields[#result.fields+1] = field
+ if source.type == 'doc.field' then
+ ---@cast source parser.object
+ field.type = source.type
+ field.key = vm.viewObject(source.field, rootUri)
+ field.file = guide.getUri(source)
+ field.start = source.start
+ field.finish = source.finish
+ field.desc = getDesc(source)
+ field.extends = packObject(source.extends)
+ return
+ end
+ print(1)
+ end)
+end
+
+---@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)
+
+ ws.awaitReady(rootUri)
+ await.sleep(0.1)
+
+ 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
+ end
+ io.write('\x0D')
+
+ table.sort(results, function (a, b)
+ return a.name < b.name
+ end)
+end)
+
+local outpath = LOGPATH .. '/doc.json'
+json.supportSparseArray = true
+util.saveFile(outpath, json.beautify(results))
diff --git a/script/cli/init.lua b/script/cli/init.lua
index c2fc7aa8..ad5cc3fe 100644
--- a/script/cli/init.lua
+++ b/script/cli/init.lua
@@ -7,3 +7,8 @@ if _G['CHECK'] then
require 'cli.check'
os.exit(0, true)
end
+
+if _G['DOC'] then
+ require 'cli.doc'
+ os.exit(0, true)
+end
diff --git a/script/global.d.lua b/script/global.d.lua
index f794ff32..d647a69e 100644
--- a/script/global.d.lua
+++ b/script/global.d.lua
@@ -44,6 +44,10 @@ PREVIEW = false
---@type string
CHECK = ''
+--make docs path
+---@type string
+DOC = ''
+
---@type string | '"Error"' | '"Warning"' | '"Information"' | '"Hint"'
CHECKLEVEL = 'Warning'
diff --git a/script/parser/luadoc.lua b/script/parser/luadoc.lua
index 793903b0..176d6f03 100644
--- a/script/parser/luadoc.lua
+++ b/script/parser/luadoc.lua
@@ -148,6 +148,7 @@ Symbol <- ({} {
---@field versions? table[]
---@field names? parser.object[]
---@field path? string
+---@field bindComments? parser.object[]
local function parseTokens(text, offset)
Ci = 0
diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua
index e59d7a6f..05ad0634 100644
--- a/script/vm/compiler.lua
+++ b/script/vm/compiler.lua
@@ -278,7 +278,7 @@ local searchFieldSwitch = util.switch()
---@param suri uri
---@param object vm.global
----@param key string|vm.global
+---@param key? string|vm.global
---@param ref boolean
---@param pushResult fun(field: vm.object, isMark?: boolean)
function vm.getClassFields(suri, object, key, ref, pushResult)
diff --git a/script/vm/infer.lua b/script/vm/infer.lua
index 47b458dc..26cfdf44 100644
--- a/script/vm/infer.lua
+++ b/script/vm/infer.lua
@@ -34,7 +34,7 @@ local inferSorted = {
['nil'] = 100,
}
-local viewNodeSwitch = util.switch()
+local viewNodeSwitch;viewNodeSwitch = util.switch()
: case 'nil'
: case 'boolean'
: case 'string'
@@ -82,6 +82,14 @@ local viewNodeSwitch = util.switch()
return source.name
end
end)
+ : case 'doc.type'
+ : call(function (source, infer, uri)
+ local buf = {}
+ for _, tp in ipairs(source.types) do
+ buf[#buf+1] = viewNodeSwitch(tp.type, tp, infer, uri)
+ end
+ return table.concat(buf, '|')
+ end)
: case 'doc.type.name'
: call(function (source, infer, uri)
if source.signs then