From 6df43db3262a163bf5efce965c68be9f7696f86c Mon Sep 17 00:00:00 2001 From: JuanZoran <1430359574@qq.com> Date: Fri, 20 Jan 2023 23:06:51 +0800 Subject: [PATCH] fix: add BufLeave autocmd for remove window --- lua/Trans/view/float.lua | 56 ++++++++++++++++++++-------------------- lua/Trans/view/hover.lua | 18 ++++++++----- lua/Trans/window.lua | 3 ++- 3 files changed, 42 insertions(+), 35 deletions(-) diff --git a/lua/Trans/view/float.lua b/lua/Trans/view/float.lua index 10b64db..b16f929 100644 --- a/lua/Trans/view/float.lua +++ b/lua/Trans/view/float.lua @@ -1,28 +1,28 @@ -local m_window -local m_result - - -return function(word) - -- TODO :online query - m_result = require('Trans.query.offline')(word) - m_window = require('Trans.window') - local float = require('Trans').conf.float - - local opt = { - relative = 'editor', - width = float.width, - height = float.height, - border = float.border, - title = float.title, - row = math.floor((vim.o.lines - float.height) / 2), - col = math.floor((vim.o.columns - float.width) / 2), - } - - -- 创建窗口 - m_window.init(true, opt) - m_window.center('https:github.com/JuanZoran/Trans.nvim', '@text.uri') -- only show color with treesiter - m_window.draw() - m_window.map('q', function() - m_window.try_close(float.animation) - end) -end +-- local m_window +-- local m_result +-- +-- +-- return function(word) +-- -- TODO :online query +-- m_result = require('Trans.query.offline')(word) +-- m_window = require('Trans.window') +-- local float = require('Trans').conf.float +-- +-- local opt = { +-- relative = 'editor', +-- width = float.width, +-- height = float.height, +-- border = float.border, +-- title = float.title, +-- row = math.floor((vim.o.lines - float.height) / 2), +-- col = math.floor((vim.o.columns - float.width) / 2), +-- } +-- +-- -- 创建窗口 +-- m_window.init(true, opt) +-- m_window.center('https:github.com/JuanZoran/Trans.nvim', '@text.uri') -- only show color with treesiter +-- m_window.draw() +-- m_window.map('q', function() +-- m_window.try_close(float.animation) +-- end) +-- end diff --git a/lua/Trans/view/hover.lua b/lua/Trans/view/hover.lua index d94b3a7..02e9f89 100644 --- a/lua/Trans/view/hover.lua +++ b/lua/Trans/view/hover.lua @@ -176,6 +176,7 @@ local process = { local cmd_id +local pin = false local try_del_keymap = function() for _, key in pairs(conf.hover.keymap) do @@ -184,8 +185,8 @@ local try_del_keymap = function() end -local pin = false -local action = { +local action +action = { pageup = function() m_window:normal('gg') end, @@ -217,14 +218,19 @@ local action = { m_window:bufset('bufhidden', 'wipe') vim.keymap.del('n', conf.hover.keymap.pin, { buffer = true }) + + + --- NOTE : 只允许存在一个pin窗口 local buf = m_window.bufnr pin = true - - api.nvim_create_autocmd('BufWipeOut', { + api.nvim_create_autocmd({ 'BufWipeOut', 'BufLeave' }, { callback = function(opt) - if opt.buf == buf then + if opt.event == 'BufLeave' or opt.buf == buf then pin = false api.nvim_del_autocmd(opt.id) + if opt.event == 'BufLeave' then + action.close() + end end end }) @@ -283,11 +289,11 @@ return function(word) cmd_id = api.nvim_create_autocmd( { 'InsertEnter', 'CursorMoved', 'BufLeave', }, { buffer = 0, - once = true, callback = function() m_window:set('wrap', false) m_window:try_close() try_del_keymap() + api.nvim_del_autocmd(cmd_id) end, }) diff --git a/lua/Trans/window.lua b/lua/Trans/window.lua index 6abc068..c081d58 100644 --- a/lua/Trans/window.lua +++ b/lua/Trans/window.lua @@ -191,7 +191,7 @@ local window = { end end, - reopen = function (self, entry, opt, callback) + reopen = function(self, entry, opt, callback) check_busy() self.config.win = nil if opt then @@ -240,6 +240,7 @@ return function(entry, option) zindex = 100, style = 'minimal', } + for k, v in pairs(option) do opt[k] = v end