Skip to content

Commit

Permalink
hover supports detail level
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Dec 13, 2024
1 parent 8d3a4c8 commit d8759ca
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 25 deletions.
2 changes: 1 addition & 1 deletion script/cli/doc/export.lua
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ end

export.makeDocObject['function'] = function(source, obj, has_seen)
obj.args = export.documentObject(source.args, has_seen)
obj.view = getLabel(source, source.parent.type == 'setmethod')
obj.view = getLabel(source, source.parent.type == 'setmethod', 1)
local _, _, max = vm.countReturnsOfFunction(source)
if max > 0 then obj.returns = {} end
for i = 1, max do
Expand Down
2 changes: 1 addition & 1 deletion script/core/completion/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ end
---@async
local function buildDesc(source)
local desc = markdown()
local hover = getHover.get(source)
local hover = getHover.get(source, 1)
desc:add('md', hover)
desc:splitLine()
desc:add('lua', getSnip(source))
Expand Down
6 changes: 3 additions & 3 deletions script/core/hover/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ local guide = require 'parser.guide'
local wssymbol = require 'core.workspace-symbol'

---@async
local function getHover(source)
local function getHover(source, level)
local md = markdown()
local defMark = {}
local labelMark = {}
Expand All @@ -32,7 +32,7 @@ local function getHover(source)
defMark[def] = true

if checkLable then
local label = getLabel(def, oop)
local label = getLabel(def, oop, level)
if not labelMark[tostring(label)] then
labelMark[tostring(label)] = true
md:add('lua', label)
Expand Down Expand Up @@ -126,7 +126,7 @@ local accept = {
}

---@async
local function getHoverByUri(uri, position)
local function getHoverByUri(uri, position, level)
local ast = files.getState(uri)
if not ast then
return nil
Expand Down
37 changes: 21 additions & 16 deletions script/core/hover/label.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ local function asDocTypeName(source)
end

---@async
local function asValue(source, title)
---@param level integer
local function asValue(source, title, level)
local name = buildName(source, false) or ''
local ifr = vm.getInfer(source)
local type = ifr:view(guide.getUri(source))
local literal = ifr:viewLiterals()
local cont = buildTable(source)
local cont = buildTable(source, level)
local pack = {}
pack[#pack+1] = title
pack[#pack+1] = name .. ':'
Expand All @@ -73,7 +74,8 @@ local function asValue(source, title)
end

---@async
local function asLocal(source)
---@param level integer
local function asLocal(source, level)
local node
if source.type == 'local'
or source.type == 'self' then
Expand All @@ -82,20 +84,21 @@ local function asLocal(source)
node = source.node
end
if node.type == 'self' then
return asValue(source, '(self)')
return asValue(source, '(self)', level)
end
if node.parent.type == 'funcargs' then
return asValue(source, '(parameter)')
return asValue(source, '(parameter)', level)
elseif guide.getParentFunction(source) ~= guide.getParentFunction(node) then
return asValue(source, '(upvalue)')
return asValue(source, '(upvalue)', level)
else
return asValue(source, 'local')
return asValue(source, 'local', level)
end
end

---@async
local function asGlobal(source)
return asValue(source, '(global)')
---@param level integer
local function asGlobal(source, level)
return asValue(source, '(global)', level)
end

local function isGlobalField(source)
Expand Down Expand Up @@ -126,11 +129,12 @@ local function isGlobalField(source)
end

---@async
local function asField(source)
---@param level integer
local function asField(source, level)
if isGlobalField(source) then
return asGlobal(source)
return asGlobal(source, level)
end
return asValue(source, '(field)')
return asValue(source, '(field)', level)
end

local function asDocFieldName(source)
Expand Down Expand Up @@ -192,26 +196,27 @@ local function asNumber(source)
end

---@async
return function (source, oop)
---@param level integer
return function (source, oop, level)
if source.type == 'function'
or source.type == 'doc.type.function' then
return asFunction(source, oop)
elseif source.type == 'local'
or source.type == 'self'
or source.type == 'getlocal'
or source.type == 'setlocal' then
return asLocal(source)
return asLocal(source, level)
elseif source.type == 'setglobal'
or source.type == 'getglobal' then
return asGlobal(source)
return asGlobal(source, level)
elseif source.type == 'getfield'
or source.type == 'setfield'
or source.type == 'getmethod'
or source.type == 'setmethod'
or source.type == 'tablefield'
or source.type == 'field'
or source.type == 'method' then
return asField(source)
return asField(source, level)
elseif source.type == 'string' then
return asString(source)
elseif source.type == 'number'
Expand Down
7 changes: 6 additions & 1 deletion script/core/hover/table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,18 @@ local function getNodeMap(uri, fields, keyMap)
end

---@async
---@param level integer
---@return string?
return function (source)
return function (source, level)
if level <= 0 then
return nil
end
local uri = guide.getUri(source)
local maxFields = config.get(uri, 'Lua.hover.previewFields')
if maxFields <= 0 then
return nil
end
maxFields = maxFields * level

local node = vm.compileNode(source)
for n in node:eachObject() do
Expand Down
2 changes: 1 addition & 1 deletion script/core/signature.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ end

---@async
local function makeOneSignature(source, oop, index)
local label = hoverLabel(source, oop)
local label = hoverLabel(source, oop, 0)
if not label then
return nil
end
Expand Down
2 changes: 1 addition & 1 deletion script/provider/provider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ m.register 'textDocument/hover' {
return nil
end
local pos = converter.unpackPosition(state, params.position)
local hover, source = core.byUri(uri, pos)
local hover, source = core.byUri(uri, pos, 1)
if not hover or not source then
return nil
end
Expand Down
2 changes: 1 addition & 1 deletion test/crossfile/hover.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function TEST(expect)
end
end

local hover = core.byUri(sourceUri, sourcePos)
local hover = core.byUri(sourceUri, sourcePos, 1)
assert(hover)
local content = tostring(hover):gsub('\r\n', '\n')
assert(eq(content, expect.hover))
Expand Down

0 comments on commit d8759ca

Please sign in to comment.