feat: add engine tag hl

This commit is contained in:
JuanZoran 2023-01-22 22:01:47 +08:00
parent 5135f2f6f9
commit 7066ecfc7e
4 changed files with 118 additions and 100 deletions

View File

@ -54,20 +54,23 @@ M.conf = {
open = 'fold',
close = 'fold',
interval = 10,
},
tag = {
wait = '#519aba',
fail = '#e46876',
success = '#10b981',
},
engine = {
'本地',
}
},
order = {
-- offline = {
order = { -- only work on hover mode
'title',
'tag',
'pos',
'exchange',
'translation',
'definition',
-- },
-- online = {
-- -- TODO
-- },
},
icon = {
star = '',

View File

@ -1,13 +1,43 @@
local conf = require('Trans').conf
local m_window
local m_result
local m_content
local engine_map = {
['本地'] = 'offline',
['百度'] = 'baidu',
['有道'] = 'youdao',
}
local function set_tag_hl(name, status)
local hl = conf.float.tag[status]
m_window:set_hl(name, {
fg = '#000000',
bg = hl,
})
m_window:set_hl(name .. 'round', {
fg = hl,
})
end
local function set_title()
local title = m_window.contents[1]
local github = ' https://github.com/JuanZoran/Trans.nvim'
title:center_line(github, '@text.uri')
for i, engine_ch in ipairs(conf.float.engine) do
local engine_us = engine_map[engine_ch]
set_tag_hl(engine_us, 'wait')
local round = engine_us .. 'round'
local wrap = title:line_wrap()
wrap('', round)
wrap(engine_ch .. '(' .. i .. ')', engine_us)
wrap('', round)
end
end
local action = {
@ -25,8 +55,11 @@ local handle = {
return function(word)
-- TODO :online query
local float = require('Trans').conf.float
m_result = require('Trans.query.offline')(word)
local float = conf.float
local engine_ch = '本地'
local engine_us = engine_map[engine_ch]
m_result = require('Trans.query.' .. engine_us)(word)
local opt = {
relative = 'editor',
@ -43,11 +76,16 @@ return function(word)
m_window.animation = float.animation
set_title()
m_content = m_window.contents[2]
if m_result then
set_tag_hl(engine_us, 'success')
for _, proc in pairs(handle) do
proc()
end
else
set_tag_hl(engine_us, 'fail')
end
m_window:draw()
m_window:open()

View File

@ -1,6 +1,5 @@
local api = vim.api
local conf = require('Trans').conf
local icon = conf.icon
local m_window
local m_result
@ -9,54 +8,11 @@ local m_indent = ' '
local title = function(str)
local wrapper = m_content:line_wrap()
-- wrapper('', 'TransTitleRound')
wrapper('', 'TransTitleRound')
wrapper(str, 'TransTitle')
wrapper('', 'TransTitleRound')
-- wrapper('', 'TransTitleRound')
end
local tag_map = {
zk = '中考',
gk = '高考',
ky = '考研',
cet4 = '四级',
cet6 = '六级',
ielts = '雅思',
toefl = '托福',
gre = 'gre ',
}
local pos_map = {
a = '代词pron ',
c = '连接词conj ',
i = '介词prep ',
j = '形容词adj ',
m = '数词num ',
n = '名词n ',
p = '代词pron ',
r = '副词adv ',
u = '感叹词int ',
v = '动词v ',
x = '否定标记not ',
t = '不定式标记infm ',
d = '限定词determiner ',
}
local exchange_map = {
['p'] = '过去式 ',
['d'] = '过去分词 ',
['i'] = '现在分词 ',
['r'] = '比较级 ',
['t'] = '最高级 ',
['s'] = '复数 ',
['0'] = '原型 ',
['1'] = '类别 ',
['3'] = '第三人称单数',
['f'] = '第三人称单数',
}
local exist = function(str)
return str and str ~= ''
end
@ -64,29 +20,31 @@ end
local process = {
title = function()
local line = m_content:items_wrap()
line.add_item(
m_result.word,
'TransWord'
)
local icon = conf.icon
line.add_item(
'[' .. (exist(m_result.phonetic) and m_result.phonetic or icon.notfound) .. ']',
'TransPhonetic'
)
line.add_item(m_result.word, 'TransWord')
line.add_item(
(exist(m_result.collins) and icon.star:rep(m_result.collins) or icon.notfound),
'TransCollins'
)
line.add_item(
(m_result.oxford == 1 and icon.yes or icon.no)
)
line.add_item('[' .. (exist(m_result.phonetic) and m_result.phonetic or icon.notfound) .. ']', 'TransPhonetic')
line.add_item((exist(m_result.collins) and icon.star:rep(m_result.collins) or icon.notfound), 'TransCollins')
line.add_item((m_result.oxford == 1 and icon.yes or icon.no))
line.load()
end,
tag = function()
if exist(m_result.tag) then
title('标签')
local tag_map = {
zk = '中考',
gk = '高考',
ky = '考研',
cet4 = '四级',
cet6 = '六级',
ielts = '雅思',
toefl = '托福',
gre = 'gre ',
}
local tags = {}
local size = 0
local interval = ' '
@ -109,6 +67,21 @@ local process = {
pos = function()
if exist(m_result.pos) then
title('词性')
local pos_map = {
a = '代词pron ',
c = '连接词conj ',
i = '介词prep ',
j = '形容词adj ',
m = '数词num ',
n = '名词n ',
p = '代词pron ',
r = '副词adv ',
u = '感叹词int ',
v = '动词v ',
x = '否定标记not ',
t = '不定式标记infm ',
d = '限定词determiner ',
}
for pos in vim.gsplit(m_result.pos, '/', true) do
m_content:addline(
@ -124,6 +97,18 @@ local process = {
exchange = function()
if exist(m_result.exchange) then
title('词形变化')
local exchange_map = {
['p'] = '过去式 ',
['d'] = '过去分词 ',
['i'] = '现在分词 ',
['r'] = '比较级 ',
['t'] = '最高级 ',
['s'] = '复数 ',
['0'] = '原型 ',
['1'] = '类别 ',
['3'] = '第三人称单数',
['f'] = '第三人称单数',
}
local interval = ' '
for exc in vim.gsplit(m_result.exchange, '/', true) do
@ -167,10 +152,8 @@ local process = {
end,
failed = function()
m_content:addline(
icon.notfound .. m_indent .. '没有找到相关的翻译',
'TransFailed'
)
m_content:addline(conf.icon.notfound .. m_indent .. '没有找到相关的翻译', 'TransFailed')
m_window:set_width(m_content.lines[1]:width())
end,
}
@ -182,11 +165,10 @@ local try_del_keymap = function()
end
local cmd_id
local pin = false
local action
local pin
local next
local _word
local action
action = {
pageup = function()
m_window:normal('gg')
@ -255,10 +237,10 @@ action = {
end,
play = vim.fn.has('linux') == 1 and function()
vim.fn.jobstart('echo ' .. _word .. ' | festival --tts')
vim.fn.jobstart('echo ' .. m_result.word .. ' | festival --tts')
end or function()
local file = debug.getinfo(1, "S").source:sub(2):match('(.*)lua/') .. 'tts/say.js'
vim.fn.jobstart('node ' .. file .. ' ' .. _word)
vim.fn.jobstart('node ' .. file .. ' ' .. m_result.word)
end,
}
@ -267,12 +249,11 @@ return function(word)
vim.validate {
word = { word, 's' },
}
_word = word
-- 目前只处理了本地数据库的查询
m_result = require('Trans.query.offline')(word)
m_result = require('Trans.query.offline')(word) -- 目前只处理了本地数据库的查询
local hover = conf.hover
local opt = {
m_window = require("Trans.window")(false, {
relative = 'cursor',
width = hover.width,
height = hover.height,
@ -280,23 +261,23 @@ return function(word)
border = hover.border,
col = 2,
row = 2,
}
})
m_window = require("Trans.window")(false, opt)
m_window.animation = hover.animation
m_content = m_window.contents[1]
if m_result then
if hover.auto_play then
action.play()
end
if hover.auto_play then action.play() end
for _, field in ipairs(conf.order) do
process[field]()
end
for act, key in pairs(hover.keymap) do
vim.keymap.set('n', key, action[act], { buffer = true, silent = true })
end
else
process.failed()
m_window:set_width(m_content.lines[1]:width())
end
m_window:draw()
@ -306,13 +287,10 @@ return function(word)
m_window:set_height(height)
end
-- action.pageup()
m_window:open(function()
m_window:set('wrap', true)
end)
-- Auto Close
cmd_id = api.nvim_create_autocmd(
hover.auto_close_events, {
@ -324,10 +302,4 @@ return function(word)
api.nvim_del_autocmd(cmd_id)
end,
})
if m_result then
for act, key in pairs(hover.keymap) do
vim.keymap.set('n', key, action[act], { buffer = true, silent = true })
end
end
end

View File

@ -192,6 +192,10 @@ local window = {
self.winid = api.nvim_open_win(self.bufnr, entry, self.config)
self:open(callback)
end,
set_hl = function(self, name, hl)
api.nvim_set_hl(self.hl, name, hl)
end
}
@ -258,6 +262,7 @@ return function(entry, option)
win:bufset('filetype', 'Trans')
win:bufset('buftype', 'nofile')
api.nvim_win_set_hl_ns(win.winid, win.hl)
---@diagnostic disable-next-line: return-type-mismatch
return win
end