diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2022-07-13 18:04:28 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2022-07-13 18:04:28 +0800 |
commit | 79c00168c6026405fac58bfe2c6920f0d3d65b30 (patch) | |
tree | eef8e144a075b47145c97fb289f38c5461d03a4e /script/provider | |
parent | 4c64ad0cbfc659766ed6f296219a30faf55202d8 (diff) | |
download | lua-language-server-79c00168c6026405fac58bfe2c6920f0d3d65b30.zip |
build-meta
Diffstat (limited to 'script/provider')
-rw-r--r-- | script/provider/build-meta.lua | 106 | ||||
-rw-r--r-- | script/provider/provider.lua | 2 |
2 files changed, 100 insertions, 8 deletions
diff --git a/script/provider/build-meta.lua b/script/provider/build-meta.lua index 0da5d05c..9466301a 100644 --- a/script/provider/build-meta.lua +++ b/script/provider/build-meta.lua @@ -9,6 +9,9 @@ local m = {} ---@field classes meta.class[] ---@class meta.class +---@field name string +---@field comment string +---@field location string ---@field namespace string ---@field baseClass string ---@field attribute string @@ -30,14 +33,89 @@ local m = {} ---@field returnTypeName string ---@field params {name: string, typeName: string}[] ----@param api meta +---@param ... string ---@return string -local function buildText(api) +local function mergeString(...) + local buf = {} + for i = 1, select('#', ...) do + local str = select(i, ...) + if str ~= '' then + buf[#buf+1] = str + end + end + return table.concat(buf, '.') +end + +local function addComments(lines, comment) + if comment == '' then + return + end + lines[#lines+1] = '--' + lines[#lines+1] = '--' .. comment:gsub('[\r\n]+$', ''):gsub('\n', '\n--') + lines[#lines+1] = '--' +end + +---@param lines string[] +---@param name string +---@param method meta.method +local function addMethod(lines, name, method) + if not method.name:match '^[%a_][%w_]*$' then + return + end + addComments(lines, method.comment) + local params = {} + for _, param in ipairs(method.params) do + lines[#lines+1] = ('---@param %s %s'):format(param.name, param.typeName) + params[#params+1] = param.name + end + if method.returnTypeName ~= '' + and method.returnTypeName ~= 'Void' then + lines[#lines+1] = ('---@return %s'):format(method.returnTypeName) + end + lines[#lines+1] = ('function %s%s%s(%s) end'):format( + name, + method.isStatic and ':' or '.', + method.name, + table.concat(params, ', ') + ) + lines[#lines+1] = '' +end + +---@param root string +---@param class meta.class +---@return string +local function buildText(root, class) local lines = {} - for _, class in ipairs(api.classes) do - + + addComments(lines, class.comment) + if class.baseClass == '' then + lines[#lines+1] = ('---@class %s'):format(mergeString(class.namespace, class.name)) + else + lines[#lines+1] = ('---@class %s: %s'):format(mergeString(class.namespace, class.name), class.baseClass) end + for _, field in ipairs(class.fields) do + addComments(lines, field.comment) + lines[#lines+1] = ('---@source %s'):format(field.location:gsub('#', ':')) + lines[#lines+1] = ('---@field %s %s'):format(field.name, field.typeName) + end + + local name = mergeString(root, class.namespace, class.name) + lines[#lines+1] = ('%s = {}'):format(name) + lines[#lines+1] = '' + + for _, method in ipairs(class.methods) do + addMethod(lines, name, method) + end + + return table.concat(lines, '\n') +end + +local function buildRootText(api) + local lines = {} + + lines[#lines+1] = ('---@class %s'):format(api.root) + lines[#lines+1] = ('%s = {}'):format(api.root) lines[#lines+1] = '' return table.concat(lines, '\n') end @@ -46,11 +124,25 @@ end ---@param api meta function m.build(name, api) local encoding = config.get(nil, 'Lua.runtime.fileEncoding') - local filePath = fs.path(METAPATH) / (name .. ' ' .. encoding .. '.lua') + local fileDir = fs.path(METAPATH) / (name .. ' ' .. encoding) + fs.create_directories(fileDir) + + local files = util.multiTable(2, function () + return { '---@meta' } + end) + + files[api.root][#files[api.root]+1] = buildRootText(api) - local text = buildText(api) + for _, class in ipairs(api.classes) do + local space = class.namespace ~= '' and class.namespace or api.root + local text = buildText(api.root, class) + files[space][#files[space]+1] = text + end + + for space, texts in pairs(files) do + util.saveFile((fileDir / (space .. '.lua')):string(), table.concat(texts, '\n\n')) + end - util.saveFile(filePath:string(), text) end return m diff --git a/script/provider/provider.lua b/script/provider/provider.lua index 11b713bc..451a3fc4 100644 --- a/script/provider/provider.lua +++ b/script/provider/provider.lua @@ -1341,7 +1341,7 @@ m.register 'workspace/diagnostic' { m.register '$/api/report' { ---@async function (params) - require 'provider.build-meta'.build('reported', params) + require 'provider.build-meta'.build('default', params) end } |