Skip to content
Snippets Groups Projects
Commit 49a08a52 authored by 最萌小汐's avatar 最萌小汐
Browse files

补充2个小功能

parent ed687309
No related branches found
No related tags found
No related merge requests found
......@@ -9,11 +9,7 @@ local client = require 'provider.client'
local lang = require 'language'
local platform = require 'bee.platform'
local function asString(source)
local literal = guide.getLiteral(source)
if type(literal) ~= 'string' then
return nil
end
local function asStringInRequire(source, literal)
local parent = source.parent
if parent and parent.type == 'callargs' then
local result, searchers
......@@ -47,9 +43,37 @@ local function asString(source)
end
end
table.sort(result)
return table.concat(result, '\n')
local md = markdown()
md:add('md', table.concat(result, '\n'))
return md:string()
end
end
end
local function asStringView(source, literal)
-- 内部包含转义符?
local rawLen = source.finish - source.start - 2 * #source[2] + 1
if config.config.hover.viewString
and (source[2] == '"' or source[2] == "'")
and rawLen > #literal then
local view = literal
local max = config.config.hover.viewStringMax
if #view > max then
view = view:sub(1, max) .. '...'
end
local md = markdown()
md:add('txt', view)
return md:string()
end
end
local function asString(source)
local literal = guide.getLiteral(source)
if type(literal) ~= 'string' then
return nil
end
return asStringInRequire(source, literal)
or asStringView(source, literal)
end
local function getDocFormater()
......@@ -140,18 +164,18 @@ local function tryLibrary(source)
local fmt = getDocFormater()
local md = markdown()
if lib.value.description then
md:add('markdown', lib.value.description:gsub('%(doc%:(.-)%)', function (tag)
md:add('md', lib.value.description:gsub('%(doc%:(.-)%)', function (tag)
if fmt then
return '(' .. lang.script(fmt, tag) .. ')'
end
end))
end
if lib.value.enums then
md:add('markdown', '-------------')
md:add('md', '-------------')
md:add('lua', buildLibEnums(lib.value))
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)))
md:add('md', ('[%s](%s)'):format(lang.script.HOVER_VIEW_DOCUMENTS, lang.script(fmt, 'pdf-' .. lib.value.doc)))
end
return md:string()
end
......
......@@ -95,6 +95,7 @@ local accept = {
['field'] = true,
['method'] = true,
['string'] = true,
['number'] = true,
}
local function getHoverByUri(uri, offset)
......
......@@ -6,6 +6,8 @@ local vm = require 'vm'
local util = require 'utility'
local guide = require 'parser.guide'
local lang = require 'language'
local config = require 'config'
local files = require 'files'
local function asFunction(source, oop)
local name = buildName(source, oop)
......@@ -108,6 +110,29 @@ local function asString(source)
end
end
local function formatNumber(n)
local str = ('%.10f'):format(n)
str = str:gsub('%.?0*$', '')
return str
end
local function asNumber(source)
if not config.config.hover.viewNumber then
return nil
end
local num = source[1]
if type(num) ~= 'number' then
return nil
end
local uri = guide.getUri(source)
local text = files.getText(uri)
local raw = text:sub(source.start, source.finish)
if not raw or not raw:find '[^%-%d%.]' then
return nil
end
return formatNumber(num)
end
return function (source, oop)
if source.type == 'function' then
return asFunction(source, oop)
......@@ -128,6 +153,8 @@ return function (source, oop)
return asField(source)
elseif source.type == 'string' then
return asString(source)
elseif source.type == 'number' then
return asNumber(source)
elseif source.type == 'library' then
return asLibrary(source)
end
......
......@@ -6,10 +6,10 @@ function mt:add(language, text)
if not text then
return
end
if language == 'lua' then
self[#self+1] = ('```lua\n%s\n```'):format(text)
if language == 'md' then
self[#self+1] = text
else
self[#self+1] = text .. '\n'
self[#self+1] = ('```%s\n%s\n```'):format(language, text)
end
end
......
......@@ -367,6 +367,11 @@ local s = <?'abc中文'?>
]]
[[9 个字节,5 个字符]]
TEST [[
local n = <?0xff?>
]]
[[255]]
TEST [[
local <?t?> = {
a = 1,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment