2023-03-24 00:56:36 +08:00
|
|
|
local node = require 'Trans'.util.node
|
2023-03-26 20:07:04 +08:00
|
|
|
local it, pr = node.item, node.prompt
|
2023-03-16 11:49:26 +08:00
|
|
|
local interval = (' '):rep(4)
|
2023-03-13 21:41:17 +08:00
|
|
|
|
2023-03-16 21:15:00 +08:00
|
|
|
local M = setmetatable({}, {
|
|
|
|
__call = function(self, hover, result, name, order)
|
|
|
|
order = order or hover.opts.order.default
|
|
|
|
|
|
|
|
local method = self.renderer[name]
|
|
|
|
|
|
|
|
for _, field in ipairs(order) do
|
|
|
|
method[field](hover, result)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|
2023-03-13 21:41:17 +08:00
|
|
|
|
2023-03-16 17:17:30 +08:00
|
|
|
---@alias TransHoverFormatter fun(hover:TransHover, result: TransResult)
|
|
|
|
---@alias TransHoverRenderer table<string, TransHoverFormatter>
|
|
|
|
|
|
|
|
---@type TransHoverRenderer
|
2023-03-16 11:49:26 +08:00
|
|
|
local default = {
|
|
|
|
str = function(hover, result)
|
2023-03-23 09:52:44 +08:00
|
|
|
hover.buffer:setline(it { result.str, 'TransWord' })
|
2023-03-16 11:49:26 +08:00
|
|
|
end,
|
|
|
|
translation = function(hover, result)
|
|
|
|
local translation = result.translation
|
|
|
|
if not translation then return end
|
|
|
|
|
|
|
|
local buffer = hover.buffer
|
2023-03-26 20:07:04 +08:00
|
|
|
buffer:setline(pr(hover.opts.icon.translation .. ' 中文翻译'))
|
2023-03-16 11:49:26 +08:00
|
|
|
|
|
|
|
for _, value in ipairs(translation) do
|
|
|
|
buffer:setline(
|
2023-03-23 09:52:44 +08:00
|
|
|
it { interval .. value, 'TransTranslation' }
|
2023-03-16 11:49:26 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2023-03-24 00:56:36 +08:00
|
|
|
buffer:setline ''
|
2023-03-16 11:49:26 +08:00
|
|
|
end,
|
|
|
|
definition = function(hover, result)
|
|
|
|
local definition = result.definition
|
|
|
|
if not definition then return end
|
2023-03-13 21:41:17 +08:00
|
|
|
|
2023-03-16 11:49:26 +08:00
|
|
|
local buffer = hover.buffer
|
2023-03-26 20:07:04 +08:00
|
|
|
buffer:setline(pr(hover.opts.icon.definition .. ' 英文注释'))
|
2023-03-16 11:49:26 +08:00
|
|
|
|
|
|
|
for _, value in ipairs(definition) do
|
|
|
|
buffer:setline(
|
2023-03-23 09:52:44 +08:00
|
|
|
it { interval .. value, 'TransDefinition' }
|
2023-03-16 11:49:26 +08:00
|
|
|
)
|
2023-03-13 21:41:17 +08:00
|
|
|
end
|
|
|
|
|
2023-03-24 00:56:36 +08:00
|
|
|
buffer:setline ''
|
2023-03-16 11:49:26 +08:00
|
|
|
end,
|
|
|
|
}
|
|
|
|
|
|
|
|
---@diagnostic disable-next-line: assign-type-mismatch
|
|
|
|
default.__index = default
|
|
|
|
|
2023-03-16 17:17:30 +08:00
|
|
|
---@type table<string, TransHoverRenderer>
|
2023-03-16 21:15:00 +08:00
|
|
|
M.renderer = setmetatable({}, {
|
2023-03-16 11:49:26 +08:00
|
|
|
__index = function(tbl, key)
|
2023-03-16 17:17:30 +08:00
|
|
|
local status, method = pcall(require, 'Trans.frontend.hover.' .. key)
|
2023-03-18 21:31:14 +08:00
|
|
|
tbl[key] = setmetatable(status and method or {}, default)
|
|
|
|
return tbl[key]
|
2023-03-13 21:41:17 +08:00
|
|
|
end,
|
2023-03-16 17:17:30 +08:00
|
|
|
})
|
2023-03-13 21:41:17 +08:00
|
|
|
|
2023-03-16 17:17:30 +08:00
|
|
|
|
2023-03-16 11:49:26 +08:00
|
|
|
---@class TransHover
|
|
|
|
---@field load fun(hover: TransHover, result: TransResult, name: string, order: string[])
|
2023-03-16 21:15:00 +08:00
|
|
|
return M
|