fix: 修复了wrap计算错误

This commit is contained in:
JuanZoran 2023-01-14 14:22:25 +08:00
parent fdd4f23181
commit 273fb8d2c1
9 changed files with 118 additions and 79 deletions

View File

@ -1,18 +1,23 @@
local function feedkey(mode, key) local function feedkey(mode, key)
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(key, true, false, true), mode, false) vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(key, true, false, true), mode, false)
end end
local util = require('Trans.util')
local M = { local M = {
pageup = function (bufnr) pageup = function(bufnr, winid)
return function () local top = math.min(10, util.get_height(bufnr, winid) - vim.api.nvim_win_get_height(winid) + 1)
vim.api.nvim_buf_call(bufnr, function () return function()
vim.api.nvim_buf_call(bufnr, function()
-- TODO : -- 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)
end end
end, end,
pagedown = function () pagedown = function()
feedkey('n', '<C-d>') feedkey('n', '<C-d>')
end end
} }

View File

@ -1,7 +1,7 @@
local M = {} local M = {}
M.__index = M M.__index = M
M.get_width = vim.fn.strdisplaywidth M.get_width = vim.fn.strwidth
-- local get_width = vim.fn.strwidth -- local get_width = vim.fn.strwidth
-- local get_width = vim.api.nvim_strwidth -- local get_width = vim.api.nvim_strwidth
@ -75,7 +75,7 @@ function M:alloc_items()
table.insert(self.highlights[self.len], { table.insert(self.highlights[self.len], {
name = items[index][2], name = items[index][2],
_start = _start, _start = _start,
_end = _end _end = _end,
}) })
end end
value = value .. items[index][1] value = value .. items[index][1]
@ -92,30 +92,28 @@ function M:alloc_items()
} }
end end
function M:alloc_text() ---返回新行的包装函数
local value = '' ---@return function
return { function M:text_wrapper()
add_text = function(text, highlight) local l = self.len + 1 -- 取出当前行
if highlight then self.lines[l] = ''
local _start = #value self.len = l
local _end = _start + #text return function(text, highlight)
table.insert(self.highlights[self.len + 1], { if highlight then
name = highlight, local _start = #self.lines[l]
_start = _start, local _end = _start + #text
_end = _end, table.insert(self.highlights[l], {
}) name = highlight,
end _start = _start,
value = value .. text _end = _end,
end, })
load = function ()
self.len = self.len + 1
self.lines[self.len] = value
end end
} self.lines[l] = self.lines[l] .. text
end
end end
function M:addline(text, highlight) function M:addline(text, highlight)
assert(text, 'empty text')
self.len = self.len + 1 self.len = self.len + 1
if highlight then if highlight then
table.insert(self.highlights[self.len], { table.insert(self.highlights[self.len], {
@ -127,11 +125,11 @@ function M:addline(text, highlight)
self.lines[self.len] = text self.lines[self.len] = text
end end
-- 窗口宽度
function M:new(width) function M:new(width)
vim.validate { vim.validate {
width = { width, 'n' } width = { width, 'n' }
} }
local default = (' '):rep(width) -- default value is empty line local default = (' '):rep(width) -- default value is empty line
local new_content = { local new_content = {
width = width, width = width,
@ -142,7 +140,6 @@ function M:new(width)
return tbl[key] return tbl[key]
end end
}), }),
} }
new_content.lines = setmetatable({}, { new_content.lines = setmetatable({}, {
@ -152,17 +149,17 @@ function M:new(width)
end, end,
__newindex = function(tbl, key, value) __newindex = function(tbl, key, value)
if value then assert(value, 'add no value as new line')
for _ = new_content.len + 1, key - 1 do for i = new_content.len + 1, key - 1 do
rawset(tbl, key, '') rawset(tbl, i, '')
end
rawset(tbl, key, value)
new_content.len = key
end end
rawset(tbl, key, value)
new_content.len = key
end end
}) })
return setmetatable(new_content, self)
return setmetatable(new_content, M)
end end
return M return M

View File

@ -55,11 +55,10 @@ local function exist(res)
end end
local function expl(c, text) local function expl(c, text)
local t = c:alloc_text() local wrapper = c:text_wrapper()
t.add_text('', 'TransTitleRound') wrapper('', 'TransTitleRound')
t.add_text(text, 'TransTitle') wrapper(text, 'TransTitle')
t.add_text('', 'TransTitleRound') wrapper('', 'TransTitleRound')
t.load()
end end
local indent = ' ' local indent = ' '
@ -134,14 +133,16 @@ M.hover = {
expl(content, '英文注释') expl(content, '英文注释')
vim.tbl_map(function(def) vim.tbl_map(function(def)
content:addline(def, 'TransDefinition') def = def:gsub('%s+', '', 1)
content:addline(indent .. def, 'TransDefinition')
end, vim.split(indent .. result.definition, '\n', { plain = true, trimempry = true })) end, vim.split(indent .. result.definition, '\n', { plain = true, trimempry = true }))
content:addline('') content:addline('')
end end
end, end,
failed = function(content) failed = function(content)
content:addline(icon.notfound .. indent .. '没有找到相关的翻译') content:addline(icon.notfound .. indent .. '没有找到相关的翻译', 'TransNotFound')
end, end,
} }

View File

@ -26,7 +26,7 @@ M.translate = function(method, view)
local word local word
if method == 'input' then if method == 'input' then
---@diagnostic disable-next-line: param-type-mismatch ---@diagnostic disable-next-line: param-type-mismatch
word = vim.fn.input('请输入您要查询的单词:') -- TODO Use Telescope with fuzzy finder word = vim.fn.input('请输入您要查询的单词: ') -- TODO Use Telescope with fuzzy finder
elseif method == 'n' then elseif method == 'n' then
word = vim.fn.expand('<cword>') word = vim.fn.expand('<cword>')
elseif method == 'v' then elseif method == 'v' then

View File

@ -1,11 +1,12 @@
local M = {} local M = {}
local api = vim.api local api = vim.api
local conf = require('Trans').conf local conf = require('Trans').conf
local action = require('Trans.core.action') local action = require('Trans.core.action')
local util = require('Trans.util')
M.id = 0 M.id = 0
M.bufnr = 0 M.bufnr = 0
M.ns = api.nvim_create_namespace('Trans') M.ns = api.nvim_create_namespace('Trans')
function M.init(view) function M.init(view)
@ -25,9 +26,9 @@ function M.init(view)
style = 'minimal', style = 'minimal',
border = conf.window.border, border = conf.window.border,
title = { title = {
{'', 'TransTitleRound'}, { '', 'TransTitleRound' },
{'Trans', 'TransTitle'}, { 'Trans', 'TransTitle' },
{'', 'TransTitleRound'}, { '', 'TransTitleRound' },
}, },
title_pos = 'center', title_pos = 'center',
focusable = true, focusable = true,
@ -57,20 +58,21 @@ M.draw = function(content)
end end
end end
local len = #content.lines
if M.height > len then
api.nvim_win_set_height(M.id, len)
end
if len == 1 then
api.nvim_win_set_width(M.id, content.get_width(content.lines[1]))
end
api.nvim_buf_set_option(M.bufnr, 'modifiable', false) api.nvim_buf_set_option(M.bufnr, 'modifiable', false)
api.nvim_buf_set_option(M.bufnr, 'filetype', 'Trans') 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, 'wrap', not M.float)
api.nvim_win_set_option(M.id, 'winhl', ('Normal:Trans%sWin,FloatBorder:Trans%sBorder'):format(M.view, M.view)) 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)
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 if M.float then
vim.keymap.set('n', 'q', function() vim.keymap.set('n', 'q', function()
if api.nvim_win_is_valid(M.id) then if api.nvim_win_is_valid(M.id) then
@ -82,6 +84,8 @@ M.draw = function(content)
-- TODO : set keymaps for window -- TODO : set keymaps for window
M.auto_close() M.auto_close()
end end
M['load_' .. M.view .. '_keymap']()
end end
@ -100,15 +104,12 @@ M.auto_close = function()
end end
-- M.load_keymap = function (once) M.load_hover_keymap = function()
-- local keymap = conf.keymap[M.view] local keymap = conf.keymap[M.view]
-- local function warp(func) for act, key in pairs(keymap) do
-- return func or function () vim.keymap.set('n', key, action[act](M.bufnr, M.id))
-- vim.api.nvim_get_keymap(' th') end
-- end end
-- end
--
-- end
return M return M

View File

@ -10,7 +10,7 @@ M.conf = {
border = 'rounded', border = 'rounded',
hover = { hover = {
width = 36, width = 36,
height = 23, height = 30,
}, },
float = { float = {
width = 0.8, width = 0.8,
@ -49,11 +49,11 @@ M.conf = {
-- -- TODO -- -- TODO
-- 'offline', -- 'offline',
-- } -- }
map = { keymap = {
-- TODO -- TODO
hover = { hover = {
pageup = '<C-u>', pageup = ']]',
pagedown = '<C-d>', -- pagedown = '<C-d>',
}, },
}, },
-- history = { -- history = {
@ -82,7 +82,7 @@ M.setup = function(opts)
window.float.height = math.floor((vim.o.lines - vim.o.cmdheight - 1) * window.float.height) window.float.height = math.floor((vim.o.lines - vim.o.cmdheight - 1) * window.float.height)
window.float.width = math.floor(vim.o.columns * window.float.width) window.float.width = math.floor(vim.o.columns * window.float.width)
-- TODO : replace the height and width for float options
M.translate = require('Trans.core').translate M.translate = require('Trans.core').translate
require("Trans.setup") require("Trans.setup")
end end

View File

@ -24,6 +24,7 @@ local highlights = {
TransTitle = { TransTitle = {
fg = '#0f0f15', fg = '#0f0f15',
bg = '#75beff', bg = '#75beff',
bold = true,
}, },
TransTitleRound = { TransTitleRound = {
fg = '#75beff', fg = '#75beff',
@ -53,6 +54,9 @@ local highlights = {
fg = '#faf743', fg = '#faf743',
bold = true, bold = true,
}, },
TransNotFound = {
fg = '#7aa89f',
},
} }
-- TODO -- TODO

18
lua/Trans/util/init.lua Normal file
View File

@ -0,0 +1,18 @@
local function get_height(bufnr, winid)
if not vim.wo[winid].wrap then
return vim.api.nvim_buf_line_count(bufnr)
end
local width = vim.api.nvim_win_get_width(winid)
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
local height = 0
for i = 1, #lines do
height = height + math.max(1, (math.ceil(vim.fn.strwidth(lines[i]) / width)))
end
return height
end
return {
get_height = get_height,
}

View File

@ -1,10 +1,23 @@
-- 记录开始时间 -- 记录开始时间
local starttime = os.clock(); --> os.clock()用法 local starttime = os.clock(); --> os.clock()用法
for i = 1, 10, 2 do local function pw(str)
print(i) -- local res = vim.fn.strdisplaywidth(str)
local res = vim.fn.strwidth(str)
print(res)
end 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()用法 local endtime = os.clock(); --> os.clock()用法