diff --git a/lua/Trans/core/buffer.lua b/lua/Trans/core/buffer.lua index 24f3ecd..1d8fc71 100644 --- a/lua/Trans/core/buffer.lua +++ b/lua/Trans/core/buffer.lua @@ -7,7 +7,9 @@ local buffer = {} ---Clear all content in buffer function buffer:wipe() + print('begin') api.nvim_buf_set_lines(self.bufnr, 0, -1, false, {}) + print('end') end ---Delete buffer [_start, _end] line content [one index] diff --git a/lua/Trans/core/conf.lua b/lua/Trans/core/conf.lua index 36b4650..c1e4e19 100644 --- a/lua/Trans/core/conf.lua +++ b/lua/Trans/core/conf.lua @@ -61,7 +61,7 @@ return { auto_resize = true, padding = 10, -- padding for hover window width keymaps = { - play = '_', + -- play = '_', -- Deprecated pageup = '[[', pagedown = ']]', pin = '[', diff --git a/lua/Trans/core/window.lua b/lua/Trans/core/window.lua index d5374a2..f51c637 100644 --- a/lua/Trans/core/window.lua +++ b/lua/Trans/core/window.lua @@ -189,12 +189,10 @@ local default_opts = { } - - ---@class TransWindow ---@field buffer TransBuffer attached buffer object ---@field win_opts table window config [**When open**] ----@field private winid integer window handle +---@field winid integer window handle ---@field ns integer namespace for highlight ---@field enter boolean cursor should [enter] window when open ---@field animation diff --git a/lua/Trans/frontend/hover/execute.lua b/lua/Trans/frontend/hover/execute.lua index 745dd84..1c17ef3 100644 --- a/lua/Trans/frontend/hover/execute.lua +++ b/lua/Trans/frontend/hover/execute.lua @@ -1,21 +1,49 @@ +local api = vim.api + +---@type table local strategy = { - play = function() - print('TODO: play') + pageup = function(hover) + hover.buffer:normal('gg') end, - pageup = function() - print('TODO: pageup') + + pagedown = function(hover) + hover.buffer:normal('G') end, - pagedown = function() - print('TODO: pagedown') - end, - pin = function() - print('TODO: pin') + + pin = function(hover) + if hover.pin then return end + 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:set('wrap', true) + hover.pin = true end, + close = function(hover) hover:destroy() end, - toggle_entry = function() - print('TODO: toggle_entry') + + toggle_entry = function(hover) + if api.nvim_get_current_win() ~= hover.window.winid then + api.nvim_set_current_win(hover.window.winid) + return + end + + for _, winid in ipairs(api.nvim_list_wins()) do + if winid ~= hover.window.winid then + api.nvim_set_current_win(winid) + break + end + end end, } diff --git a/lua/Trans/frontend/hover/init.lua b/lua/Trans/frontend/hover/init.lua index 517ed59..8939d99 100644 --- a/lua/Trans/frontend/hover/init.lua +++ b/lua/Trans/frontend/hover/init.lua @@ -9,6 +9,7 @@ local Trans = require('Trans') ---@field queue TransHover[] @hover queue for all hover instances ---@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'), queue = {}, @@ -19,6 +20,7 @@ M.__index = M ---@return TransHover new_instance function M.new() local new_instance = { + pin = false, buffer = Trans.buffer.new(), destroy_funcs = {}, } @@ -47,18 +49,21 @@ end ---Destroy hover instance and execute destroy functions function M:destroy() - for _, func in ipairs(self.destroy_funcs) do - func(self) - end + coroutine.wrap(function() + for _, func in ipairs(self.destroy_funcs) do + 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 ---Init hover window ---@param opts? ----|{width?: integer, height?: integer} +---|{width?: integer, height?: integer, col?: integer, row?: integer, relative?: string} ---@return unknown function M:init_window(opts) opts = opts or {} @@ -70,10 +75,10 @@ function M:init_window(opts) } local win_opts = { - col = 1, - row = 1, - relative = 'cursor', + 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, } @@ -121,7 +126,8 @@ function M:wait(tbl, name, timeout) -- FIXME : - -- vim.api.nvim_buf_set_lines(buffer.bufnr, 1, -1, true, { '' }) + -- buffer:wipe() + -- vim.api.nvim_buf_set_lines(buffer.bufnr, 1, -1, true, {}) -- print('jklajsdk') -- print(vim.fn.deletebufline(buffer.bufnr, 1)) -- buffer:del() @@ -129,16 +135,19 @@ function M:wait(tbl, name, timeout) end ---Display Result in hover window ----@param _ TransData +---@param data TransData ---@param result TransResult ---@overload fun(result:TransResult) -function M:process(_, result) +function M:process(data, result) -- local node = Trans.util.node -- local it, t, f = node.item, node.text, node.format -- self.buffer:setline(it('hello', 'MoreMsg')) local opts = self.opts if not self.buffer:is_valid() then self.buffer:init() end + if opts.auto_play then + (data.from == 'en' and data.str or result.definition[1]):play() + end for _, field in ipairs(opts.order) do if result[field] then @@ -155,7 +164,6 @@ function M:process(_, result) display_size.width = nil end window:resize(display_size) - else window = self:init_window { height = math.min(opts.height, display_size.height), @@ -164,6 +172,33 @@ function M:process(_, result) end window:set('wrap', true) + + + local auto_close_events = 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 + self:destroy() + end, + }) + end + + -- vim.api.nvim_create_autocmd('User', { + -- pattern = 'TransHoverReady', + -- callback = function(opts) + -- vim.print(opts) + -- ---@type TransHover + -- local hover = opts.data + -- end, + -- desc = 'Auto Close Hover Window', + -- }) + + -- vim.api.nvim_exec_autocmds('User', { + -- pattern = 'TransHoverReady', + -- data = self, + -- }) end ---Check if hover window and buffer are valid diff --git a/lua/Trans/init.lua b/lua/Trans/init.lua index 565f3ae..85b015a 100644 --- a/lua/Trans/init.lua +++ b/lua/Trans/init.lua @@ -26,6 +26,7 @@ end local M = metatable('core', { style = metatable("style"), cache = {}, + augroup = vim.api.nvim_create_augroup('Trans', { clear = true }) }) M.metatable = metatable