From 273fb8d2c1af31acc8964b0dbf0b225432745fd2 Mon Sep 17 00:00:00 2001 From: JuanZoran <1430359574@qq.com> Date: Sat, 14 Jan 2023 14:22:25 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=BA=86wrap=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lua/Trans/core/action.lua | 15 ++++++--- lua/Trans/core/content.lua | 61 +++++++++++++++++------------------- lua/Trans/core/handler.lua | 15 ++++----- lua/Trans/core/init.lua | 2 +- lua/Trans/core/window.lua | 55 ++++++++++++++++---------------- lua/Trans/init.lua | 10 +++--- lua/Trans/setup.lua | 4 +++ lua/Trans/util/init.lua | 18 +++++++++++ lua/Trans/util/test/test.lua | 17 ++++++++-- 9 files changed, 118 insertions(+), 79 deletions(-) create mode 100644 lua/Trans/util/init.lua diff --git a/lua/Trans/core/action.lua b/lua/Trans/core/action.lua index 497193d..f3617d8 100644 --- a/lua/Trans/core/action.lua +++ b/lua/Trans/core/action.lua @@ -1,18 +1,23 @@ - local function feedkey(mode, key) vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(key, true, false, true), mode, false) end +local util = require('Trans.util') local M = { - pageup = function (bufnr) - return function () - vim.api.nvim_buf_call(bufnr, function () + 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 end, - pagedown = function () + pagedown = function() feedkey('n', '') end } diff --git a/lua/Trans/core/content.lua b/lua/Trans/core/content.lua index 791fcf5..8d8b797 100644 --- a/lua/Trans/core/content.lua +++ b/lua/Trans/core/content.lua @@ -1,7 +1,7 @@ local 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.api.nvim_strwidth @@ -75,7 +75,7 @@ function M:alloc_items() table.insert(self.highlights[self.len], { name = items[index][2], _start = _start, - _end = _end + _end = _end, }) end value = value .. items[index][1] @@ -92,30 +92,28 @@ function M:alloc_items() } end -function M:alloc_text() - local value = '' - return { - add_text = function(text, highlight) - if highlight then - local _start = #value - local _end = _start + #text - table.insert(self.highlights[self.len + 1], { - name = highlight, - _start = _start, - _end = _end, - }) - end - value = value .. text - end, - load = function () - self.len = self.len + 1 - self.lines[self.len] = value +---返回新行的包装函数 +---@return function +function M:text_wrapper() + local l = self.len + 1 -- 取出当前行 + self.lines[l] = '' + self.len = l + return function(text, highlight) + if highlight then + local _start = #self.lines[l] + local _end = _start + #text + table.insert(self.highlights[l], { + name = highlight, + _start = _start, + _end = _end, + }) end - } + self.lines[l] = self.lines[l] .. text + end end - function M:addline(text, highlight) + assert(text, 'empty text') self.len = self.len + 1 if highlight then table.insert(self.highlights[self.len], { @@ -127,11 +125,11 @@ function M:addline(text, highlight) self.lines[self.len] = text end --- 窗口宽度 function M:new(width) vim.validate { width = { width, 'n' } } + local default = (' '):rep(width) -- default value is empty line local new_content = { width = width, @@ -142,7 +140,6 @@ function M:new(width) return tbl[key] end }), - } new_content.lines = setmetatable({}, { @@ -152,17 +149,17 @@ function M:new(width) end, __newindex = function(tbl, key, value) - if value then - for _ = new_content.len + 1, key - 1 do - rawset(tbl, key, '') - end - - rawset(tbl, key, value) - new_content.len = key + assert(value, 'add no value as new line') + for i = new_content.len + 1, key - 1 do + rawset(tbl, i, '') end + rawset(tbl, key, value) + + new_content.len = key end }) - return setmetatable(new_content, self) + + return setmetatable(new_content, M) end return M diff --git a/lua/Trans/core/handler.lua b/lua/Trans/core/handler.lua index 67a1807..15b480b 100644 --- a/lua/Trans/core/handler.lua +++ b/lua/Trans/core/handler.lua @@ -55,11 +55,10 @@ local function exist(res) end local function expl(c, text) - local t = c:alloc_text() - t.add_text('', 'TransTitleRound') - t.add_text(text, 'TransTitle') - t.add_text('', 'TransTitleRound') - t.load() + local wrapper = c:text_wrapper() + wrapper('', 'TransTitleRound') + wrapper(text, 'TransTitle') + wrapper('', 'TransTitleRound') end local indent = ' ' @@ -134,14 +133,16 @@ M.hover = { expl(content, '英文注释') 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 })) content:addline('') end end, + failed = function(content) - content:addline(icon.notfound .. indent .. '没有找到相关的翻译') + content:addline(icon.notfound .. indent .. '没有找到相关的翻译', 'TransNotFound') end, } diff --git a/lua/Trans/core/init.lua b/lua/Trans/core/init.lua index 062747f..20b3ba1 100644 --- a/lua/Trans/core/init.lua +++ b/lua/Trans/core/init.lua @@ -26,7 +26,7 @@ M.translate = function(method, view) local word if method == 'input' then ---@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 word = vim.fn.expand('') elseif method == 'v' then diff --git a/lua/Trans/core/window.lua b/lua/Trans/core/window.lua index 2c436bb..5580f54 100644 --- a/lua/Trans/core/window.lua +++ b/lua/Trans/core/window.lua @@ -1,11 +1,12 @@ -local M = {} -local api = vim.api -local conf = require('Trans').conf +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.id = 0 +M.bufnr = 0 +M.ns = api.nvim_create_namespace('Trans') function M.init(view) @@ -25,9 +26,9 @@ function M.init(view) style = 'minimal', border = conf.window.border, title = { - {'', 'TransTitleRound'}, - {'Trans', 'TransTitle'}, - {'', 'TransTitleRound'}, + { '', 'TransTitleRound' }, + { 'Trans', 'TransTitle' }, + { '', 'TransTitleRound' }, }, title_pos = 'center', focusable = true, @@ -57,20 +58,21 @@ M.draw = function(content) 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, '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) + 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 @@ -82,6 +84,8 @@ M.draw = function(content) -- TODO : set keymaps for window M.auto_close() end + + M['load_' .. M.view .. '_keymap']() end @@ -100,15 +104,12 @@ M.auto_close = function() end --- M.load_keymap = function (once) --- local keymap = conf.keymap[M.view] --- local function warp(func) --- return func or function () --- vim.api.nvim_get_keymap(' th') --- end --- end --- --- 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 +end return M diff --git a/lua/Trans/init.lua b/lua/Trans/init.lua index 24f66e7..ec2966f 100644 --- a/lua/Trans/init.lua +++ b/lua/Trans/init.lua @@ -10,7 +10,7 @@ M.conf = { border = 'rounded', hover = { width = 36, - height = 23, + height = 30, }, float = { width = 0.8, @@ -49,11 +49,11 @@ M.conf = { -- -- TODO -- 'offline', -- } - map = { + keymap = { -- TODO hover = { - pageup = '', - pagedown = '', + pageup = ']]', + -- pagedown = '', }, }, -- 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.width = math.floor(vim.o.columns * window.float.width) - -- TODO : replace the height and width for float options + M.translate = require('Trans.core').translate require("Trans.setup") end diff --git a/lua/Trans/setup.lua b/lua/Trans/setup.lua index 2aa9b7f..f74c45e 100644 --- a/lua/Trans/setup.lua +++ b/lua/Trans/setup.lua @@ -24,6 +24,7 @@ local highlights = { TransTitle = { fg = '#0f0f15', bg = '#75beff', + bold = true, }, TransTitleRound = { fg = '#75beff', @@ -53,6 +54,9 @@ local highlights = { fg = '#faf743', bold = true, }, + TransNotFound = { + fg = '#7aa89f', + }, } -- TODO diff --git a/lua/Trans/util/init.lua b/lua/Trans/util/init.lua new file mode 100644 index 0000000..c0461c3 --- /dev/null +++ b/lua/Trans/util/init.lua @@ -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, +} diff --git a/lua/Trans/util/test/test.lua b/lua/Trans/util/test/test.lua index cc11b32..fd1be57 100644 --- a/lua/Trans/util/test/test.lua +++ b/lua/Trans/util/test/test.lua @@ -1,10 +1,23 @@ -- 记录开始时间 local starttime = os.clock(); --> os.clock()用法 -for i = 1, 10, 2 do - print(i) +local function pw(str) + -- local res = vim.fn.strdisplaywidth(str) + local res = vim.fn.strwidth(str) + print(res) 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()用法