feat: begin to render youdao Translation window

This commit is contained in:
JuanZoran
2023-03-16 17:17:30 +08:00
parent 870be41bee
commit 0d4eec6f2e
6 changed files with 169 additions and 159 deletions

View File

@ -1,16 +1,13 @@
local node = require('Trans').util.node
local it, t, f = node.item, node.text, node.format
local it, conjunction = node.item, node.conjunction
local interval = (' '):rep(4)
local function conjunction(text)
return {
it('', 'TransTitleRound'),
it(text, 'TransTitle'),
it('', 'TransTitleRound'),
}
end
---@type table<string, fun(hover:TransHover, result: TransResult)>
---@alias TransHoverFormatter fun(hover:TransHover, result: TransResult)
---@alias TransHoverRenderer table<string, TransHoverFormatter>
---@type TransHoverRenderer
local default = {
str = function(hover, result)
hover.buffer:setline(it(result.str, 'TransWord'))
@ -50,106 +47,28 @@ local default = {
---@diagnostic disable-next-line: assign-type-mismatch
default.__index = default
local strategy = setmetatable({}, {
---@type table<string, TransHoverRenderer>
local renderer = setmetatable({}, {
__index = function(tbl, key)
tbl[key] = default
return tbl[key]
local status, method = pcall(require, 'Trans.frontend.hover.' .. key)
if not status then
print(key)
return
end
tbl[key] = setmetatable(method, default)
return method
end,
__newindex = function(tbl, key, value)
rawset(tbl, key, setmetatable(value, default))
end
})
strategy.offline = {
title = function(hover, result)
local title = result.title
if not title then return end
local icon = hover.opts.icon
local word = title.word
local oxford = title.oxford
local collins = title.collins
local phonetic = title.phonetic
hover.buffer:setline(f {
width = hover.opts.width,
text = t {
it(word, 'TransWord'),
t {
it('['),
it((phonetic and phonetic ~= '') and phonetic or icon.notfound, 'TransPhonetic'),
it(']')
},
it(collins and icon.star:rep(collins) or icon.notfound, 'TransCollins'),
it(oxford == 1 and icon.yes or icon.no)
},
})
end,
tag = function(hover, result)
local tag = result.tag
if not tag then return end
local buffer = hover.buffer
buffer:setline(conjunction('标签'))
local size = #tag
for i = 1, size, 3 do
buffer:setline(
it(
interval .. tag[i] ..
(tag[i + 1] and interval .. tag[i + 1] ..
(tag[i + 2] and interval .. tag[i + 2] or '') or ''),
'TransTag'
)
)
end
buffer:setline('')
end,
exchange = function(hover, result)
local exchange = result.exchange
if not exchange then return end
local buffer = hover.buffer
buffer:setline(conjunction('词形变化'))
for description, value in pairs(exchange) do
buffer:setline(
it(interval .. description .. interval .. value, 'TransExchange')
)
end
buffer:setline('')
end,
pos = function(hover, result)
local pos = result.pos
if not pos then return end
local buffer = hover.buffer
buffer:setline(conjunction('词性'))
for description, value in pairs(pos) do
buffer:setline(
it(interval .. description .. interval .. value, 'TransPos')
)
end
buffer:setline('')
end,
}
-- FIXME :
---@class TransHover
---@field load fun(hover: TransHover, result: TransResult, name: string, order: string[])
return function(hover, result, name, order)
order = order or hover.opts.order.default
local method = strategy[name]
local method = renderer[name]
for _, field in ipairs(order) do
method[field](hover, result)

View File

@ -0,0 +1,90 @@
local node = require('Trans').util.node
local it, t, f, co = node.item, node.text, node.format, node.conjunction
local interval = (' '):rep(4)
---@type TransHoverRenderer
local M = {}
function M.tag(hover, result)
local tag = result.tag
if not tag then return end
local buffer = hover.buffer
buffer:setline(co('标签'))
local size = #tag
for i = 1, size, 3 do
buffer:setline(
it(
interval .. tag[i] ..
(tag[i + 1] and interval .. tag[i + 1] ..
(tag[i + 2] and interval .. tag[i + 2] or '') or ''),
'TransTag'
)
)
end
buffer:setline('')
end
function M.exchange(hover, result)
local exchange = result.exchange
if not exchange then return end
local buffer = hover.buffer
buffer:setline(co('词形变化'))
for description, value in pairs(exchange) do
buffer:setline(
it(interval .. description .. interval .. value, 'TransExchange')
)
end
buffer:setline('')
end
function M.pos(hover, result)
local pos = result.pos
if not pos then return end
local buffer = hover.buffer
buffer:setline(co('词性'))
for description, value in pairs(pos) do
buffer:setline(
it(interval .. description .. interval .. value, 'TransPos')
)
end
buffer:setline('')
end
function M.title(hover, result)
local title = result.title
if not title then return end
local icon = hover.opts.icon
local word = title.word
local oxford = title.oxford
local collins = title.collins
local phonetic = title.phonetic
hover.buffer:setline(f {
width = hover.opts.width,
text = t {
it(word, 'TransWord'),
t {
it('['),
it((phonetic and phonetic ~= '') and phonetic or icon.notfound, 'TransPhonetic'),
it(']')
},
it(collins and icon.star:rep(collins) or icon.notfound, 'TransCollins'),
it(oxford == 1 and icon.yes or icon.no)
},
})
end
return M

View File

@ -0,0 +1,8 @@
---@type TransHoverRenderer
local M = {}
function M.web()
end
return M