feat: 添加了TranslateLast 查看上一次的查询记录, 还有一些bug

This commit is contained in:
JuanZoran 2023-01-14 17:16:49 +08:00
parent 273fb8d2c1
commit 8f0aae8b77
8 changed files with 98 additions and 107 deletions

View File

@ -1,26 +1,25 @@
local function feedkey(mode, key)
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(key, true, false, true), mode, false)
-- local util = require('Trans.util')
local bufnr = require('Trans.core.window').bufnr
-- local winid = require('Trans.core.window').id
local api = vim.api
local function buf_feedkey(key)
if bufnr and api.nvim_buf_is_valid(bufnr) then
api.nvim_buf_call(bufnr, function()
vim.cmd([[normal! ]] .. key)
end)
return true
else
return false
end
end
local util = require('Trans.util')
local M = {
pageup = function(bufnr, winid)
local top = math.min(10, util.get_height(bufnr, winid) - vim.api.nvim_win_get_height(winid) + 1)
return function()
vim.api.nvim_buf_call(bufnr, function()
-- TODO :
vim.cmd([[normal!]] .. top .. 'zt')
-- vim.cmd([[normal!]] .. 'G')
-- vim.api.nvim_command("noautocmd silent! normal! " .. vim.wo.scroll .. "zt")
-- vim.cmd([[do WinScrolled]])
end)
end
pageup = function()
return buf_feedkey('gg')
end,
pagedown = function()
feedkey('n', '<C-d>')
return buf_feedkey('G')
end
}
return M

View File

@ -1,9 +1,8 @@
local M = {}
M.__index = M
M.get_width = vim.fn.strwidth
-- local get_width = vim.fn.strwidth
-- local get_width = vim.api.nvim_strwidth
---@alias block table add_hl(key, hl_name)
---返回分配的块状区域, e_col 设置为-1则为末尾
@ -52,32 +51,31 @@ end
function M:alloc_items()
local items = {}
local len = 0
local size = 0
local width = 0 -- 所有item的总width
local size = 0 -- item数目
return {
add_item = function(item, highlight)
size = size + 1
local wid = self.get_width(item)
items[size] = { item, highlight }
len = len + wid
width = width + wid
end,
load = function()
self.len = self.len + 1
local space = math.floor((self.width - len) / (size - 1))
local space = math.floor((self.width - width) / (size - 1))
assert(space > 0)
local interval = (' '):rep(space)
local value = ''
local function load_item(index)
if items[index][2] then
local _start = #value
local _end = _start + #items[index][1]
table.insert(self.highlights[self.len], {
name = items[index][2],
_start = _start,
_end = _end,
_start = #value,
_end = #value + #items[index][1],
})
end
value = value .. items[index][1]
end
@ -162,4 +160,8 @@ function M:new(width)
return setmetatable(new_content, M)
end
function M:clear()
require('table.clear')(self)
end
return M

View File

@ -127,7 +127,6 @@ M.hover = {
content:addline('')
end,
definition = function(result, content)
if exist(result.definition) then
expl(content, '英文注释')

View File

@ -24,16 +24,17 @@ M.translate = function(method, view)
method = method or vim.api.nvim_get_mode().mode
view = view or conf.view[method]
local word
if method == 'input' then
if method == 'v' then
---@diagnostic disable-next-line: param-type-mismatch
word = vim.fn.input('请输入您要查询的单词: ') -- TODO Use Telescope with fuzzy finder
elseif method == 'n' then
word = vim.fn.expand('<cword>')
elseif method == 'v' then
elseif method == 'input' then
word = get_select()
elseif method == 'last' then
return win.show()
end
win.init(view)
local result = api.query('offline', word)
local content = c:new(win.width)

View File

@ -1,12 +1,11 @@
local M = {}
local api = vim.api
local conf = require('Trans').conf
local action = require('Trans.core.action')
local util = require('Trans.util')
M.id = 0
M.bufnr = 0
M.ns = api.nvim_create_namespace('Trans')
M.bufnr = api.nvim_create_buf(false, true)
function M.init(view)
@ -15,19 +14,19 @@ function M.init(view)
}
M.view = view
M.float = view == 'float'
local is_float = view == 'float'
M.height = conf.window[view].height
M.width = conf.window[view].width
local opts = {
relative = M.float and 'editor' or 'cursor',
relative = is_float and 'editor' or 'cursor',
width = M.width,
height = M.height,
style = 'minimal',
border = conf.window.border,
title = {
{ '', 'TransTitleRound' },
{ 'Trans', 'TransTitle' },
{ conf.icon.title .. ' Trans', 'TransTitle' },
{ '', 'TransTitleRound' },
},
title_pos = 'center',
@ -35,7 +34,7 @@ function M.init(view)
zindex = 100,
}
if M.float then
if is_float then
opts.row = math.floor((vim.o.lines - M.height) / 2)
opts.col = math.floor((vim.o.columns - M.width) / 2)
else
@ -43,13 +42,13 @@ function M.init(view)
opts.col = 2
end
M.bufnr = api.nvim_create_buf(false, true)
M.id = api.nvim_open_win(M.bufnr, M.float, opts)
end
M.draw = function(content)
api.nvim_buf_set_lines(M.bufnr, 0, -1, false, content.lines)
api.nvim_buf_set_option(M.bufnr, 'modifiable', true)
api.nvim_buf_set_lines(M.bufnr, 0, -1, false, content.lines)
if content.highlights then
for l, _hl in pairs(content.highlights) do
for _, hl in ipairs(_hl) do
@ -57,39 +56,34 @@ M.draw = function(content)
end
end
end
M.load_opts()
if content.len == 1 then
api.nvim_win_set_width(M.id, content.get_width(content.lines[1]))
end
end
M.load_opts = function()
api.nvim_buf_set_option(M.bufnr, 'modifiable', false)
api.nvim_buf_set_option(M.bufnr, 'filetype', 'Trans')
api.nvim_win_set_option(M.id, 'wrap', not M.float)
api.nvim_win_set_option(M.id, 'winhl', ('Normal:Trans%sWin,FloatBorder:Trans%sBorder'):format(M.view, M.view))
local height = util.get_height(M.bufnr, M.id)
M['load_' .. M.view .. '_opts']()
if M.height > height then
api.nvim_win_set_height(M.id, height)
end
if height == 1 then
api.nvim_win_set_width(M.id, content.get_width(content.lines[1]))
end
if M.float then
vim.keymap.set('n', 'q', function()
if api.nvim_win_is_valid(M.id) then
api.nvim_win_close(M.id, true)
end
end, { buffer = M.bufnr, silent = true })
M.load_hover_opts = function()
local keymap = conf.keymap[M.view]
local action = require('Trans.core.action')
else
-- TODO : set keymaps for window
M.auto_close()
for act, key in pairs(keymap) do
vim.keymap.set('n', key, action[act])
end
M['load_' .. M.view .. '_keymap']()
end
M.auto_close = function()
api.nvim_create_autocmd(
{ 'InsertEnter', 'CursorMoved', 'BufLeave', }, {
buffer = 0,
@ -100,15 +94,22 @@ M.auto_close = function()
end
end,
})
api.nvim_win_set_option(M.id, 'wrap', not M.float)
end
M.load_float_opts = function()
vim.keymap.set('n', 'q', function()
if api.nvim_win_is_valid(M.id) then
api.nvim_win_close(M.id, true)
end
end, { buffer = M.bufnr, silent = true })
end
M.load_hover_keymap = function()
local keymap = conf.keymap[M.view]
for act, key in pairs(keymap) do
vim.keymap.set('n', key, action[act](M.bufnr, M.id))
end
M.show = function()
M.init(M.view or 'float')
M.load_opts()
end

View File

@ -2,7 +2,7 @@ local M = {}
M.conf = {
view = {
input = 'hover',
input = 'float',
n = 'hover',
v = 'hover',
},
@ -10,7 +10,7 @@ M.conf = {
border = 'rounded',
hover = {
width = 36,
height = 30,
height = 26,
},
float = {
width = 0.8,
@ -34,14 +34,15 @@ M.conf = {
-- },
},
icon = {
title = '',
star = '',
notfound = '',
yes = '',
no = ''
-- notfound = '',
-- yes = '',
-- no = ''
-- star = '⭐',
-- notfound = '❔',
-- yes = '✔️',
-- no = '❌'
notfound = '',
yes = '✔️',
no = ''
},
db_path = '$HOME/.vim/dict/ultimate.db',
-- TODO :
@ -52,8 +53,8 @@ M.conf = {
keymap = {
-- TODO
hover = {
pageup = ']]',
-- pagedown = '<C-d>',
pageup = '[[',
pagedown = ']]',
},
},
-- history = {

View File

@ -12,6 +12,10 @@ vim.api.nvim_create_user_command('TranslateInput', function()
require("Trans").translate('input')
end, { desc = ' 搜索翻译' })
vim.api.nvim_create_user_command('TranslateLast', function()
require("Trans").translate('last')
end, { desc = ' 显示上一次查询的内容' })
local highlights = {
TransWord = {
@ -59,11 +63,6 @@ local highlights = {
},
}
-- TODO
-- vim.api.nvim_create_user_command('TranslateHistory', require("Trans.core").query_input, {
-- desc = '翻译输入的单词',
-- })
for highlight, opt in pairs(highlights) do
vim.api.nvim_set_hl(0, highlight, opt)
end

View File

@ -1,24 +1,13 @@
-- 记录开始时间
local starttime = os.clock(); --> os.clock()用法
local function pw(str)
-- local res = vim.fn.strdisplaywidth(str)
local res = vim.fn.strwidth(str)
print(res)
local str = 'test'
local len = #str
for i = 1, 100000000 do
local size = len
end
pw('n. either extremity of something that has length')
pw('n.the point in time at which something ends')
pw('n.the concluding parts of an event or occurrence')
pw('n.a final part or section')
-- 48
-- 43
-- 48
-- 25
-- 记录结束时间
local endtime = os.clock(); --> os.clock()用法
print(string.format("end time : %.4f", endtime));