fix: fix toggle entry will trigger auto_close events

This commit is contained in:
JuanZoran 2023-03-19 20:15:36 +08:00
parent 3eae6a40e9
commit 8be4fb7303
7 changed files with 34 additions and 27 deletions

View File

@ -152,9 +152,6 @@ function M.formatter(body, data)
},
web = body.web,
explains = body.basic.explains,
-- phrases = body.phrases,
-- synonyms = body.synonyms,
-- sentenceSample = body.sentenceSample,
[data.from == 'en' and 'translation' or 'definition'] = body.translation,
}

View File

@ -17,9 +17,9 @@ return {
dir = os.getenv('HOME') .. '/.vim/dict',
query = 'fallback',
debug = true,
-- backend_order = {},
---@type 'default' | 'dracula' | 'tokyonight' global Trans theme [see lua/Trans/style/theme.lua]
theme = 'default', -- default | tokyonight | dracula
backend_order = nil,
strategy = {
---@type { frontend:string, backend:string | string[] } fallback strategy for mode
default = {
@ -32,10 +32,9 @@ return {
---@class TransFrontendOpts
---@field keymaps table<string, string>
default = {
---@type boolean Whether to auto play the audio
auto_play = true,
border = 'rounded',
title = title, -- need nvim-0.9
title = title, -- need nvim-0.9+
auto_play = true,
---@type {open: string | boolean, close: string | boolean, interval: integer} Hover Window Animation
animation = {
open = 'slid', -- 'fold', 'slid'
@ -55,6 +54,7 @@ return {
---@type string -- TODO :support replace with {{special word}}
fallback_message = '{{notfound}} 翻译超时或没有找到相关的翻译',
auto_resize = true,
-- strict_sentence_width = false,
-- strict = false, -- TODO :No Width limit when str is a sentence
padding = 10, -- padding for hover window width
keymaps = {
@ -96,18 +96,18 @@ return {
---@type table<string, string>
icon = {
-- or use emoji
list = '', -- ● | ○ | ◉ | ◯ | ◇ | ◆ | ▪ | ▫ | ⬤ | 🟢 | 🟡 | 🟣 | 🟤 | 🟦 | 🟨 | 🟧 | 🟥 | 🟪 | 🟫 | 🟩 | 🟠 | 🟦 | 🟨 | 🟧 | 🟥 | 🟪 | 🟫 | 🟩 | 🟠
list = '', -- ● | ○ | ◉ | ◯ | ◇ | ◆ | ▪ | ▫ | ⬤ | 🟢 | 🟡 | 🟣 | 🟤 | 🟠| 🟦 | 🟨 | 🟧 | 🟥 | 🟪 | 🟫 | 🟩 | 🟦
star = '', -- ⭐ | ✴ | ✳ | ✲ | ✱ | ✰ | ★ | ☆ | 🌟 | 🌠 | 🌙 | 🌛 | 🌜 | 🌟 | 🌠 | 🌌 | 🌙 |
notfound = '', --❔ | ❓ | ❗ | ❕|
yes = '', -- ✅ | ✔️ | ☑
no = '', -- ❌ | ❎ | ✖ | ✘ | ✗ |
cell = '', -- ■ | □ | ▇ | ▏ ▎ ▍ ▌ ▋ ▊ ▉ █
cell = '', -- ■ | □ | ▇ | ▏ ▎ ▍ ▌ ▋ ▊ ▉
web = '󰖟', --🌍 | 🌎 | 🌏 | 🌐 |
tag = '',
pos = '',
translation = '󰊿',
definition = '󰗊',
exchange = '',
definition = '󰗊',
translation = '󰊿',
},
},
},

View File

@ -47,8 +47,7 @@ function M.new(opts)
data.to = 'en'
end
-- FIXME : Check if the str is a word
data.is_word = true
data.is_word = Trans.util.is_word(str)
return data
end

View File

@ -60,6 +60,7 @@ local strategy = {
local update = data.frontend:wait()
for _, backend in ipairs(data.backends) do
do_query(data, backend)
local name = backend.name
---@cast backend TransBackend
while result[name] == nil do

View File

@ -181,6 +181,14 @@ function M.visible_lines(opts)
return api.nvim_buf_get_lines(0, _start, _end, false)
end
---Detect whether the string is a word
---@param str string
---@return boolean
function M.is_word(str)
return str:match('%w+') == str
end
---@class Trans
---@field util TransUtil
return M

View File

@ -1,3 +1,4 @@
---@type Trans
local Trans = require("Trans")
-- FIXME :Adjust Window Size
@ -103,7 +104,7 @@ end
---@return string formatted text
---@return integer _ replaced count
function M:icon_format(format)
return format:gsub("{{(%w+)}}", self.opts.icon, 1)
return format:gsub("{{(%w+)}}", self.opts.icon)
end
---Get Check function for waiting
@ -162,12 +163,13 @@ function M:defer()
local auto_close_events = self.opts.auto_close_events
if auto_close_events then
vim.api.nvim_create_autocmd(auto_close_events, {
once = true,
callback = function()
if self.pin then
return
end
callback = function(opts)
vim.defer_fn(function()
if not self.pin and vim.api.nvim_get_current_win() ~= self.window.winid then
pcall(vim.api.nvim_del_autocmd, opts.id)
self:destroy()
end
end, 0)
end,
})
end
@ -213,6 +215,7 @@ function M:process(data)
else
display_size.width = nil
end
window:resize(display_size)
else
window = self:init_window {

View File

@ -22,7 +22,6 @@ end
---@field style table @Style module
---@field cache table<string, TransData> @Cache for translated data object
---@field modes string[] @all modes name
local M = metatable("core", {
cache = {},
style = metatable("style"),