style: code format

This commit is contained in:
JuanZoran
2023-03-18 13:53:09 +08:00
parent a6a5a33bff
commit 46c69fb758
8 changed files with 190 additions and 272 deletions

View File

@ -3,35 +3,33 @@ local api = vim.api
---@type table<string, fun(hover: TransHover)>
local strategy = {
pageup = function(hover)
hover.buffer:normal('gg')
hover.buffer:normal("gg")
end,
pagedown = function(hover)
hover.buffer:normal('G')
hover.buffer:normal("G")
end,
pin = function(hover)
if hover.pin then return end
if hover.pin then
return
end
hover.pin = true
local window = hover.window
local width, height = window:width(), window:height()
local col = vim.o.columns - width - 3
window:try_close()
window = hover:init_window({
width = width,
height = height,
relative = 'editor',
col = col,
})
window = hover:init_window {
col = col,
width = width,
height = height,
relative = "editor",
}
window:set('wrap', true)
window:set("wrap", true)
end,
close = function(hover)
hover:destroy()
end,
toggle_entry = function(hover)
if api.nvim_get_current_win() ~= hover.window.winid then
api.nvim_set_current_win(hover.window.winid)
@ -47,7 +45,6 @@ local strategy = {
end,
}
---@class TransHover
---@field execute fun(hover: TransHover, action: string)
return function(hover, action)

View File

@ -1,4 +1,4 @@
local Trans = require('Trans')
local Trans = require("Trans")
-- FIXME :Adjust Window Size
@ -10,8 +10,8 @@ local Trans = require('Trans')
---@field destroy_funcs fun(hover:TransHover)[] @functions to be executed when hover window is closed
---@field opts TransHoverOpts @options for hover window
---@field pin boolean @whether hover window is pinned
local M = Trans.metatable('frontend.hover', {
ns = vim.api.nvim_create_namespace('TransHoverWin'),
local M = Trans.metatable("frontend.hover", {
ns = vim.api.nvim_create_namespace("TransHoverWin"),
queue = {},
})
M.__index = M
@ -55,9 +55,12 @@ function M:destroy()
func(self)
end
if self.window:is_valid() then self.window:try_close() end
if self.buffer:is_valid() then self.buffer:destroy() end
if self.window:is_valid() then
self.window:try_close()
end
if self.buffer:is_valid() then
self.buffer:destroy()
end
self.pin = false
end)()
end
@ -67,25 +70,27 @@ end
---|{width?: integer, height?: integer, col?: integer, row?: integer, relative?: string}
---@return unknown
function M:init_window(opts)
opts = opts or {}
local m_opts = self.opts
local option = {
ns = self.ns,
buffer = self.buffer,
opts = opts or {}
local m_opts = self.opts
local option = {
ns = self.ns,
buffer = self.buffer,
animation = m_opts.animation,
}
-- stylua: ignore start
local win_opts = {
col = opts.col or 1,
row = opts.row or 1,
title = m_opts.title,
relative = opts.relative or 'cursor',
width = opts.width or m_opts.width,
height = opts.height or m_opts.height,
relative = opts.relative or "cursor",
}
-- stylua: ignore end
if win_opts.title then
win_opts.title_pos = 'center'
win_opts.title_pos = "center"
end
option.win_opts = win_opts
@ -98,7 +103,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, 1)
end
---Get Check function for waiting
@ -122,7 +127,7 @@ function M:wait()
return function()
cur = cur + 1
buffer[1] = spinner[cur % size + 1] .. (cell):rep(cur)
buffer:add_highlight(1, 'TransWaitting')
buffer:add_highlight(1, "TransWaitting")
pause(interval)
return cur < times
end
@ -137,7 +142,6 @@ function M:fallback()
}
end
local buffer = self.buffer
buffer:wipe()
@ -146,22 +150,23 @@ function M:fallback()
-- TODO :Center
buffer[1] = Trans.util.center(fallback_msg, opts.width)
buffer:add_highlight(1, 'TransFailed')
buffer:add_highlight(1, "TransFailed")
self:defer()
end
---Defer function when process done
function M:defer()
self.window:set('wrap', true)
self.buffer:set('modifiable', false)
self.window:set("wrap", true)
self.buffer:set("modifiable", false)
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
if self.pin then
return
end
self:destroy()
end,
})
@ -172,7 +177,9 @@ end
---@param data TransData
---@overload fun(result:TransResult)
function M:process(data)
if self.pin then return end
if self.pin then
return
end
local result, name = data:get_available_result()
if not result then
@ -182,7 +189,7 @@ function M:process(data)
-- vim.pretty_print(result)
local opts = self.opts
if opts.auto_play then
(data.from == 'en' and data.str or result.definition[1]):play()
(data.from == "en" and data.str or result.definition[1]):play()
end
-- local node = Trans.util.node
-- local it, t, f = node.item, node.text, node.format
@ -213,6 +220,7 @@ function M:process(data)
width = math.min(opts.width, display_size.width + opts.padding),
}
end
self:defer()
end
@ -225,90 +233,3 @@ end
---@class TransFrontend
---@field hover TransHover @hover frontend
return M
-- local cmd_id
-- local next
-- local action = {
-- pageup = function()
-- buffer:normal('gg')
-- end,
-- pagedown = function()
-- buffer:normal('G')
-- end,
-- pin = function()
-- if lock then
-- error('请先关闭窗口')
-- else
-- lock = true
-- end
-- pcall(api.nvim_del_autocmd, cmd_id)
-- local width, height = win.width, win.height
-- local col = vim.o.columns - width - 3
-- local buf = buffer.bufnr
-- local run = win:try_close()
-- run(function()
-- local w, r = open_window {
-- width = width,
-- height = height,
-- relative = 'editor',
-- col = col,
-- }
-- next = w.winid
-- win = w
-- r(function()
-- w:set('wrap', true)
-- end)
-- del('n', keymap.pin)
-- api.nvim_create_autocmd('BufWipeOut', {
-- callback = function(opt)
-- if opt.buf == buf or opt.buf == cur_buf then
-- lock = false
-- api.nvim_del_autocmd(opt.id)
-- end
-- end
-- })
-- end)
-- end,
-- close = function()
-- pcall(api.nvim_del_autocmd, cmd_id)
-- local run = win:try_close()
-- run(function()
-- buffer:delete()
-- end)
-- try_del_keymap()
-- end,
-- toggle_entry = function()
-- if lock and win:is_valid() then
-- local prev = api.nvim_get_current_win()
-- api.nvim_set_current_win(next)
-- next = prev
-- else
-- del('n', keymap.toggle_entry)
-- end
-- end,
-- play = function()
-- if word then
-- word:play()
-- end
-- end,
-- }
-- local set = vim.keymap.set
-- for act, key in pairs(hover.keymap) do
-- set('n', key, action[act])
-- end
-- if hover.auto_close_events then
-- cmd_id = api.nvim_create_autocmd(
-- hover.auto_close_events, {
-- buffer = 0,
-- callback = action.close,
-- })
-- end
-- end