68 lines
1.7 KiB
Lua
Raw Normal View History

2023-03-26 20:07:04 +08:00
local node = require 'Trans'.util.node
local it, t, f, pr = node.item, node.text, node.format, node.prompt
2023-03-16 19:25:08 +08:00
---@type TransHoverRenderer
local M = {}
local interval = (' '):rep(4)
2023-03-16 19:25:08 +08:00
function M.web(hover, result)
if not result.web then return end
local buffer = hover.buffer
2023-03-26 20:07:04 +08:00
buffer:setline(pr(hover.opts.icon.web .. ' 网络释义'))
local function remove_duplicate(strs)
local uniq_strs = {}
local str_map = {}
local opts = { plain = true, trim_empty = true }
for i = 1, #strs do
local fields = vim.split(strs[i], '; ', opts)
for j = 1, #fields do
local field = fields[j]
if not str_map[field] then
uniq_strs[#uniq_strs + 1] = field
str_map[field] = true
end
end
end
return uniq_strs
end
local indent = interval .. ' ' .. hover.opts.icon.list .. ' '
for _, w in ipairs(result.web) do
buffer:setline(it {
interval .. w.key,
'TransWeb'
})
for _, v in ipairs(remove_duplicate(w.value)) do
buffer:setline(it {
indent .. v,
'TransWeb'
})
end
end
2023-03-26 20:07:04 +08:00
buffer:setline ''
end
2023-03-16 19:25:08 +08:00
function M.explains(hover, result)
local explains = result.explains
if not explains then return end
local buffer = hover.buffer
2023-03-26 20:07:04 +08:00
buffer:setline(pr '基本释义')
for i = 1, #explains, 2 do
buffer:setline(it {
interval .. explains[i] ..
(explains[i + 1] and interval .. explains[i + 1] or ''),
'TransExplains'
})
end
2023-03-26 20:07:04 +08:00
buffer:setline ''
end
2023-03-26 20:07:04 +08:00
M.title = require 'Trans'.frontend.hover.offline.title
return M