feat: implete auto_close_window

This commit is contained in:
JuanZoran 2022-12-20 10:22:28 +08:00
parent c58e59edcf
commit 3902886ac0
5 changed files with 28 additions and 14 deletions

View File

@ -12,7 +12,7 @@ return {
exchange = true,
Trans_en = true,
Trans_zh = true,
wrap = false,
wrap = true,
},
view = {
-- TODO: style: buffer | cursor | window
@ -25,7 +25,8 @@ return {
star = '',
isOxford = '',
notOxford = ''
}
},
auto_close = true,
-- TODO: async process
-- async = false,

View File

@ -1,5 +1,4 @@
local M = {}
-- local db = require('Trans').db
local dict = require("Trans").dict
function M.query(arg)

View File

@ -1,12 +1,13 @@
-- local conf = require("Trans").conf
local M = {}
local display = require("Trans.conf").display
local icon = require("Trans.conf").icon
local display = require("Trans").conf.display
local icon = require("Trans").conf.icon
local buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_option(buf, 'filetype', 'Trans')
local win = 0
-- {
@ -25,7 +26,7 @@ vim.api.nvim_buf_set_option(buf, 'filetype', 'Trans')
-- }
local function show_win(width, height)
local win = vim.api.nvim_open_win(buf, false, {
win = vim.api.nvim_open_win(buf, false, {
relative = 'cursor',
title = 'Trans',
title_pos = 'center',
@ -141,4 +142,11 @@ function M.toggle()
-- TODO: wrap some function
end
function M.close_win()
if win > 0 then
vim.api.nvim_win_close(win, true)
win = 0
end
end
return M

View File

@ -15,8 +15,5 @@ end
M.db = require("sqlite.db")
M.dict = M.db:open(M.conf.db_path)
M.query = require("Trans.display").query
M.query_cursor = require("Trans.display").query_cursor
return M

View File

@ -1,7 +1,11 @@
local db = require("Trans").db
-- local conf = require("Trans").conf
local group = vim.api.nvim_create_augroup('closedb', { clear = true })
vim.api.nvim_create_user_command('TranslateCurosorWord', require("Trans.display").query_cursor, {})
local group = vim.api.nvim_create_augroup("Trans", { clear = true })
vim.api.nvim_create_autocmd('VimLeave', {
group = group,
pattern = '*',
@ -11,8 +15,13 @@ vim.api.nvim_create_autocmd('VimLeave', {
end
end,
})
vim.api.nvim_create_user_command('TranslateCurosorWord', require("Trans").query_cursor, {})
-- TODO: set command to close preview window automatically
local auto_close = require("Trans.conf").auto_close
if auto_close then
vim.api.nvim_create_autocmd(
{ 'InsertEnter', 'CursorMoved', 'BufLeave', }, {
group = group,
pattern = '*',
callback = require('Trans.display').close_win
})
end