diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2020-09-16 12:58:49 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2020-09-16 12:58:49 +0800 |
commit | cfd28f56f6c0083aede3f16d47976337fecb28e9 (patch) | |
tree | 1bf2c8b11d847f03f49445f3a19c7b68f9b44c9e /script-beta | |
parent | 720824b2b118df49b7fa03a7ccc98bd48cbff2c6 (diff) | |
download | lua-language-server-cfd28f56f6c0083aede3f16d47976337fecb28e9.zip |
查看文档
Diffstat (limited to 'script-beta')
-rw-r--r-- | script-beta/core/hover/description.lua | 59 | ||||
-rw-r--r-- | script-beta/provider/client.lua | 16 | ||||
-rw-r--r-- | script-beta/provider/markdown.lua | 2 | ||||
-rw-r--r-- | script-beta/provider/provider.lua | 2 | ||||
-rw-r--r-- | script-beta/without-check-nil.lua | 126 |
5 files changed, 197 insertions, 8 deletions
diff --git a/script-beta/core/hover/description.lua b/script-beta/core/hover/description.lua index 9f44b72d..746d602e 100644 --- a/script-beta/core/hover/description.lua +++ b/script-beta/core/hover/description.lua @@ -1,8 +1,12 @@ -local vm = require 'vm' -local ws = require 'workspace' -local furi = require 'file-uri' -local files = require 'files' -local guide = require 'parser.guide' +local vm = require 'vm' +local ws = require 'workspace' +local furi = require 'file-uri' +local files = require 'files' +local guide = require 'parser.guide' +local markdown = require 'provider.markdown' +local config = require 'config' +local client = require 'provider.client' +local lang = require 'language' local function asString(source) local literal = guide.getLiteral(source) @@ -40,12 +44,53 @@ local function asString(source) end end +local function getDocFormater() + local version = config.config.runtime.version + if client.client() == 'vscode' then + if version == 'Lua 5.1' then + return 'HOVER_NATIVE_DOCUMENT_LUA51' + elseif version == 'Lua 5.2' then + return 'HOVER_NATIVE_DOCUMENT_LUA52' + elseif version == 'Lua 5.3' then + return 'HOVER_NATIVE_DOCUMENT_LUA53' + elseif version == 'Lua 5.4' then + return 'HOVER_NATIVE_DOCUMENT_LUA54' + elseif version == 'LuaJIT' then + return 'HOVER_NATIVE_DOCUMENT_LUAJIT' + end + else + if version == 'Lua 5.1' then + return 'HOVER_DOCUMENT_LUA51' + elseif version == 'Lua 5.2' then + return 'HOVER_DOCUMENT_LUA52' + elseif version == 'Lua 5.3' then + return 'HOVER_DOCUMENT_LUA53' + elseif version == 'Lua 5.4' then + return 'HOVER_DOCUMENT_LUA54' + elseif version == 'LuaJIT' then + return 'HOVER_DOCUMENT_LUAJIT' + end + end +end + local function tryLibrary(source) local lib = vm.getLibrary(source) if not lib then - return + return nil + end + local fmt = getDocFormater() + local md = markdown() + if lib.value.description then + md:add('markdown', lib.value.description:gsub('%(doc%:(.-)%)', function (tag) + if fmt then + return '(' .. lang.script(fmt, tag) .. ')' + end + end)) + end + if lib.value.doc and fmt then + md:add('markdown', ('[%s](%s)'):format(lang.script.HOVER_VIEW_DOCUMENTS, lang.script(fmt, 'pdf-' .. lib.value.doc))) end - return lib.value.description + return md:string() end return function (source) diff --git a/script-beta/provider/client.lua b/script-beta/provider/client.lua new file mode 100644 index 00000000..a2671597 --- /dev/null +++ b/script-beta/provider/client.lua @@ -0,0 +1,16 @@ +local nonil = require 'without-check-nil' + +local m = {} + +function m.client() + nonil.enable() + local name = m.info.clientInfo.name + nonil.disable() + return name +end + +function m.init(t) + m.info = t +end + +return m diff --git a/script-beta/provider/markdown.lua b/script-beta/provider/markdown.lua index 874b3abd..7bd34444 100644 --- a/script-beta/provider/markdown.lua +++ b/script-beta/provider/markdown.lua @@ -9,7 +9,7 @@ function mt:add(language, text) if language == 'lua' then self[#self+1] = ('```lua\n%s\n```'):format(text) else - self[#self+1] = text + self[#self+1] = text .. '\n' end end diff --git a/script-beta/provider/provider.lua b/script-beta/provider/provider.lua index 1f49f641..fc8b8001 100644 --- a/script-beta/provider/provider.lua +++ b/script-beta/provider/provider.lua @@ -9,6 +9,7 @@ local workspace = require 'workspace' local config = require 'config' local library = require 'library' local markdown = require 'provider.markdown' +local client = require 'provider.client' local function updateConfig() local configs = proto.awaitRequest('workspace/configuration', { @@ -77,6 +78,7 @@ end) proto.on('initialized', function (params) updateConfig() + client.init(params) proto.awaitRequest('client/registerCapability', { registrations = { -- 监视文件变化 diff --git a/script-beta/without-check-nil.lua b/script-beta/without-check-nil.lua new file mode 100644 index 00000000..cc7da9d4 --- /dev/null +++ b/script-beta/without-check-nil.lua @@ -0,0 +1,126 @@ +local m = {} + +local mt = {} +mt.__add = function (a, b) + if a == nil then a = 0 end + if b == nil then b = 0 end + return a + b +end +mt.__sub = function (a, b) + if a == nil then a = 0 end + if b == nil then b = 0 end + return a - b +end +mt.__mul = function (a, b) + if a == nil then a = 0 end + if b == nil then b = 0 end + return a * b +end +mt.__div = function (a, b) + if a == nil then a = 0 end + if b == nil then b = 0 end + return a / b +end +mt.__mod = function (a, b) + if a == nil then a = 0 end + if b == nil then b = 0 end + return a % b +end +mt.__pow = function (a, b) + if a == nil then a = 0 end + if b == nil then b = 0 end + return a ^ b +end +mt.__unm = function () + return 0 +end +mt.__concat = function (a, b) + if a == nil then a = '' end + if b == nil then b = '' end + return a .. b +end +mt.__len = function () + return 0 +end +mt.__lt = function (a, b) + if a == nil then a = 0 end + if b == nil then b = 0 end + return a < b +end +mt.__le = function (a, b) + if a == nil then a = 0 end + if b == nil then b = 0 end + return a <= b +end +mt.__index = function () end +mt.__newindex = function () end +mt.__call = function () end +mt.__pairs = function () end +mt.__ipairs = function () end +if _VERSION == 'Lua 5.3' or _VERSION == 'Lua 5.4' then + mt.__idiv = load[[ + local a, b = ... + if a == nil then a = 0 end + if b == nil then b = 0 end + return a // b + ]] + mt.__band = load[[ + local a, b = ... + if a == nil then a = 0 end + if b == nil then b = 0 end + return a & b + ]] + mt.__bor = load[[ + local a, b = ... + if a == nil then a = 0 end + if b == nil then b = 0 end + return a | b + ]] + mt.__bxor = load[[ + local a, b = ... + if a == nil then a = 0 end + if b == nil then b = 0 end + return a ~ b + ]] + mt.__bnot = load[[ + return ~ 0 + ]] + mt.__shl = load[[ + local a, b = ... + if a == nil then a = 0 end + if b == nil then b = 0 end + return a << b + ]] + mt.__shr = load[[ + local a, b = ... + if a == nil then a = 0 end + if b == nil then b = 0 end + return a >> b + ]] +end + +for event, func in pairs(mt) do + mt[event] = function (...) + local watch = m.watch + if not watch then + return func(...) + end + local care, result = watch(event, ...) + if not care then + return func(...) + end + return result + end +end + +function m.enable() + debug.setmetatable(nil, mt) +end + +function m.disable() + if debug.getmetatable(nil) == mt then + debug.setmetatable(nil, nil) + end +end + +return m |