refactor: remove TransMode, use vim-mode

This commit is contained in:
JuanZoran 2023-03-22 23:09:09 +08:00
parent e90b55d297
commit 8e9ccfc8f7
6 changed files with 26 additions and 42 deletions

View File

@ -21,10 +21,8 @@ function M.new(opts)
local mode = opts.mode
local str = opts.str
local strategy = Trans.conf.strategy[mode]
local data = setmetatable({
str = str,
mode = mode,

View File

@ -1,7 +1,6 @@
local Trans = require('Trans')
local function set_strategy_opts(conf)
local all_modes = Trans.modes
local all_backends = Trans.backend.all_name
@ -15,22 +14,19 @@ local function set_strategy_opts(conf)
local default_strategy = conf.strategy.default
default_strategy.backend = parse_backend(default_strategy.backend)
default_strategy.__index = default_strategy
conf.strategy.default = nil
local meta = {
__index = function(tbl, key)
tbl[key] = default_strategy[key]
return tbl[key]
setmetatable(conf.strategy, {
__index = function()
return default_strategy
end
}
})
local strategy = conf.strategy
for _, mode in ipairs(all_modes) do
strategy[mode] = setmetatable(strategy[mode] or {}, meta)
if type(strategy[mode].backend) == 'string' then
strategy[mode].backend = parse_backend(strategy[mode].backend)
end
for _, strategy in pairs(conf.strategy) do
strategy.backend = parse_backend(strategy.backend)
setmetatable(strategy, default_strategy)
end
end
@ -45,12 +41,8 @@ local function define_highlights(conf)
end
---@alias TransMode
---|'normal' # Normal mode
---|'visual' # Visual mode
---|'input' # Input mode
---@class Trans
---@field setup fun(opts: { mode: string, mode: TransMode })
---@field setup fun(opts: { mode: string, mode: string })
return function(opts)
if opts then
Trans.conf = vim.tbl_deep_extend('force', Trans.conf, opts)

View File

@ -3,12 +3,7 @@ local util = Trans.util
local function init_opts(opts)
opts = opts or {}
opts.mode = opts.mode or ({
n = 'normal',
v = 'visual',
})[vim.api.nvim_get_mode().mode]
opts.mode = opts.mode or vim.fn.mode()
opts.str = util.get_str(opts.mode)
return opts
end

View File

@ -37,20 +37,23 @@ function M.get_select()
end
---Get Text which need to be translated
---@param mode TransMode
---@param mode string
---@return string
function M.get_str(mode)
return ({
normal = function()
n = function()
return fn.expand('<cword>')
end,
visual = function()
v = function()
api.nvim_input('<Esc>')
return M.get_select()
end,
input = function()
i = function()
return fn.input('需要翻译的字符串: ')
end,
V = function()
print('TODO')
end
})[mode]():match('^%s*(.-)%s*$')
end
@ -166,9 +169,9 @@ end
---@param opts { winid: integer, height: integer }?
---@return string[]
function M.visible_lines(opts)
opts = opts or {}
opts = opts or {}
-- TODO : Use getpos('w0') and getpos('w$') to get the visible lines
-- INFO : don't calculate the height of statusline and cmdheight or winbar?
local winid = opts.winid or 0
local win_height = opts.height or api.nvim_win_get_height(winid)

View File

@ -18,6 +18,7 @@ end
---@field width function @Get string display width
---@field play function @Use tts to play string
---@class Trans
---@field style table @Style module
---@field cache table<string, TransData> @Cache for translated data object
@ -25,11 +26,6 @@ end
local M = metatable("core", {
cache = {},
style = metatable("style"),
modes = {
"normal",
"visual",
"input",
},
augroup = vim.api.nvim_create_augroup("Trans", { clear = true }),
})

View File

@ -30,13 +30,13 @@ end, { desc = " Translate cursor word" })
command("TranslateInput", function()
Trans.translate({ mode = 'input' })
Trans.translate({ mode = 'i' })
end, { desc = " Translate input word" })
command("TransPlay", function()
--- FIXME :
local str = Trans.util.get_str(api.nvim_get_mode().mode)
if str and str ~= "" and Trans.util.is_English(str) then
local util = Trans.util
local str = util.get_str(vim.fn.mode())
if str and str ~= "" and util.is_English(str) then
str:play()
end
end, { desc = "auto play" })
end, { desc = "Auto play" })