fix: add BufLeave autocmd for remove window

This commit is contained in:
JuanZoran 2023-01-20 23:06:51 +08:00
parent 09806f01a6
commit 6df43db326
3 changed files with 42 additions and 35 deletions

View File

@ -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

View File

@ -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,
})

View File

@ -240,6 +240,7 @@ return function(entry, option)
zindex = 100,
style = 'minimal',
}
for k, v in pairs(option) do
opt[k] = v
end