feat: try to use panvimdoc
This commit is contained in:
105
lua/Trans/core/conf.lua
Normal file
105
lua/Trans/core/conf.lua
Normal file
@@ -0,0 +1,105 @@
|
||||
return {
|
||||
view = {
|
||||
i = 'float',
|
||||
n = 'hover',
|
||||
v = 'hover',
|
||||
},
|
||||
hover = {
|
||||
width = 37,
|
||||
height = 27,
|
||||
border = 'rounded',
|
||||
title = vim.fn.has('nvim-0.9') == 1 and {
|
||||
{ '', 'TransTitleRound' },
|
||||
{ ' Trans', 'TransTitle' },
|
||||
{ '', 'TransTitleRound' },
|
||||
} or nil,
|
||||
keymap = {
|
||||
pageup = '[[',
|
||||
pagedown = ']]',
|
||||
pin = '<leader>[',
|
||||
close = '<leader>]',
|
||||
toggle_entry = '<leader>;',
|
||||
play = '_',
|
||||
},
|
||||
animation = {
|
||||
-- open = 'fold',
|
||||
-- close = 'fold',
|
||||
open = 'slid',
|
||||
close = 'slid',
|
||||
interval = 12,
|
||||
},
|
||||
auto_close_events = {
|
||||
'InsertEnter',
|
||||
'CursorMoved',
|
||||
'BufLeave',
|
||||
},
|
||||
auto_play = true,
|
||||
timeout = 2000,
|
||||
spinner = 'dots', -- 查看所有样式: /lua/Trans/util/spinner
|
||||
-- spinner = 'moon'
|
||||
},
|
||||
order = { -- only work on hover mode
|
||||
'title',
|
||||
'tag',
|
||||
'pos',
|
||||
'exchange',
|
||||
'translation',
|
||||
'definition',
|
||||
},
|
||||
icon = {
|
||||
star = '',
|
||||
notfound = ' ',
|
||||
yes = '✔',
|
||||
no = '',
|
||||
-- --- char: ■ | □ | ▇ | ▏ ▎ ▍ ▌ ▋ ▊ ▉ █
|
||||
-- --- ◖■■■■■■■◗▫◻ ▆ ▆ ▇⃞ ▉⃞
|
||||
cell = '■',
|
||||
-- star = '⭐',
|
||||
-- notfound = '❔',
|
||||
-- yes = '✔️',
|
||||
-- no = '❌'
|
||||
},
|
||||
theme = 'default',
|
||||
db_path = '$HOME/.vim/dict/ultimate.db',
|
||||
-- float = {
|
||||
-- width = 0.8,
|
||||
-- height = 0.8,
|
||||
-- border = 'rounded',
|
||||
-- keymap = {
|
||||
-- quit = 'q',
|
||||
-- },
|
||||
-- animation = {
|
||||
-- open = 'fold',
|
||||
-- close = 'fold',
|
||||
-- interval = 10,
|
||||
-- },
|
||||
-- tag = {
|
||||
-- wait = '#519aba',
|
||||
-- fail = '#e46876',
|
||||
-- success = '#10b981',
|
||||
-- },
|
||||
-- },
|
||||
}
|
||||
|
||||
|
||||
-- ---Pasue Handler for {ms} milliseconds
|
||||
-- ---@param ms number @milliseconds
|
||||
-- M.pause = function(ms)
|
||||
-- local co = coroutine.running()
|
||||
-- vim.defer_fn(function()
|
||||
-- coroutine.resume(co)
|
||||
-- end, ms)
|
||||
-- coroutine.yield()
|
||||
-- end
|
||||
|
||||
|
||||
-- local title = {
|
||||
-- "████████╗██████╗ █████╗ ███╗ ██╗███████╗",
|
||||
-- "╚══██╔══╝██╔══██╗██╔══██╗████╗ ██║██╔════╝",
|
||||
-- " ██║ ██████╔╝███████║██╔██╗ ██║███████╗",
|
||||
-- " ██║ ██╔══██╗██╔══██║██║╚██╗██║╚════██║",
|
||||
-- " ██║ ██║ ██║██║ ██║██║ ╚████║███████║",
|
||||
-- " ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝╚══════╝",
|
||||
--}
|
||||
|
||||
-- string.width = api.nvim_strwidth
|
24
lua/Trans/core/setup.lua
Normal file
24
lua/Trans/core/setup.lua
Normal file
@@ -0,0 +1,24 @@
|
||||
return function(opts)
|
||||
local M = require('Trans')
|
||||
if opts then
|
||||
M.conf = vim.tbl_deep_extend('force', M.conf, opts)
|
||||
end
|
||||
local conf = M.conf
|
||||
local set_hl = vim.api.nvim_set_hl
|
||||
local hls = require('Trans.style.theme')[conf.theme]
|
||||
for hl, opt in pairs(hls) do
|
||||
set_hl(0, hl, opt)
|
||||
end
|
||||
|
||||
|
||||
local path = vim.fn.expand("$HOME/.vim/dict/Trans.json")
|
||||
local file = io.open(path, "r")
|
||||
if file then
|
||||
local content = file:read("*a")
|
||||
file:close()
|
||||
local status, engine = pcall(vim.json.decode, content)
|
||||
assert(status, 'Unable to parse json file: ' .. path)
|
||||
|
||||
conf.engine = engine
|
||||
end
|
||||
end
|
33
lua/Trans/core/translate.lua
Normal file
33
lua/Trans/core/translate.lua
Normal file
@@ -0,0 +1,33 @@
|
||||
local M = require('Trans')
|
||||
local util = M.util
|
||||
|
||||
|
||||
local process
|
||||
process = function(opts)
|
||||
opts = opts or {}
|
||||
local mode = opts.mode or vim.api.nvim_get_mode().mode
|
||||
local str = util.get_str(mode)
|
||||
if str == '' then return end
|
||||
|
||||
local data = {
|
||||
str = str,
|
||||
view = opts.view or M.conf.view[mode],
|
||||
mode = mode,
|
||||
}
|
||||
|
||||
if util.is_English(str) then
|
||||
data.from = 'en'
|
||||
data.to = 'zh'
|
||||
else
|
||||
data.from = 'zh'
|
||||
data.to = 'en'
|
||||
end
|
||||
|
||||
|
||||
local res = require('Trans.backend').offline.query(data)
|
||||
-- vim.pretty_print(res)
|
||||
|
||||
M.translate = coroutine.wrap(process)
|
||||
end
|
||||
|
||||
return process
|
63
lua/Trans/core/util.lua
Normal file
63
lua/Trans/core/util.lua
Normal file
@@ -0,0 +1,63 @@
|
||||
local M = {}
|
||||
|
||||
local fn, api = vim.fn, vim.api
|
||||
|
||||
M.get_select = function()
|
||||
local _start = fn.getpos("v")
|
||||
local _end = fn.getpos('.')
|
||||
|
||||
if _start[2] > _end[2] or (_start[3] > _end[3] and _start[2] == _end[2]) then
|
||||
_start, _end = _end, _start
|
||||
end
|
||||
local s_row, s_col = _start[2], _start[3]
|
||||
local e_row, e_col = _end[2], _end[3]
|
||||
|
||||
-- print(s_row, e_row, s_col, e_col)
|
||||
---@type string
|
||||
---@diagnostic disable-next-line: assign-type-mismatch
|
||||
local line = fn.getline(e_row)
|
||||
local uidx = vim.str_utfindex(line, math.min(#line, e_col))
|
||||
---@diagnostic disable-next-line: param-type-mismatch
|
||||
e_col = vim.str_byteindex(line, uidx)
|
||||
|
||||
|
||||
if s_row == e_row then
|
||||
return line:sub(s_col, e_col)
|
||||
else
|
||||
local lines = fn.getline(s_row, e_row)
|
||||
local e = #lines
|
||||
lines[1] = lines[1]:sub(s_col)
|
||||
lines[e] = line:sub(1, e_col)
|
||||
return table.concat(lines)
|
||||
end
|
||||
end
|
||||
|
||||
---Get Text which need to be translated
|
||||
---@param mode string 'n' | 'v' | 'i'
|
||||
---@return string
|
||||
M.get_str = function(mode)
|
||||
if mode == 'n' then
|
||||
return fn.expand('<cword>')
|
||||
elseif mode == 'v' then
|
||||
api.nvim_input('<ESC>')
|
||||
return M.get_select()
|
||||
elseif mode == 'i' then
|
||||
-- TODO Use Telescope with fuzzy finder
|
||||
---@diagnostic disable-next-line: param-type-mismatch
|
||||
return fn.input('请输入需要查询的单词:')
|
||||
else
|
||||
error('invalid mode: ' .. mode)
|
||||
end
|
||||
end
|
||||
|
||||
M.is_English = function(str)
|
||||
local char = { str:byte(1, -1) }
|
||||
for i = 1, #str do
|
||||
if char[i] > 128 then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
return M
|
Reference in New Issue
Block a user