diff --git a/lua/Trans/init.lua b/lua/Trans/init.lua index 94796a8..035bc13 100644 --- a/lua/Trans/init.lua +++ b/lua/Trans/init.lua @@ -20,11 +20,33 @@ local win_title = fn.has('nvim-0.9') == 1 and { -- " ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝╚══════╝", --} +-- 中文字符匹配函数 +local function is_chinese(str) + local len = #str + local left = 0 + local right = len + local mid = 0 + while left <= right do + mid = math.floor((left + right) / 2) + local cur_byte = string.byte(str, mid) + local cur_char = string.sub(str, mid, mid) + if cur_byte == nil then + break + end + if cur_byte > 128 then + return true + else + left = mid + 1 + end + end + return false +end + string.width = api.nvim_strwidth string.isEn = function(self) local char = { self:byte(1, -1) } for i = 1, #self do - if char[i] > 127 then + if char[i] > 128 then return false end end diff --git a/lua/Trans/view/float.lua b/lua/Trans/view/float.lua index 16fae4a..15c7d49 100644 --- a/lua/Trans/view/float.lua +++ b/lua/Trans/view/float.lua @@ -82,6 +82,7 @@ local function process() end return function(word) + buffer:init() -- TODO :online query -- local float = conf.float vim.notify([[ diff --git a/lua/Trans/view/hover.lua b/lua/Trans/view/hover.lua index 37ee5b8..7b09fca 100644 --- a/lua/Trans/view/hover.lua +++ b/lua/Trans/view/hover.lua @@ -1,9 +1,10 @@ local api = vim.api local conf = require('Trans').conf local hover = conf.hover -local buffer = require('Trans.buffer')() local error_msg = conf.icon.notfound .. ' 没有找到相关的翻译' +local buffer = require('Trans.buffer')() + local node = require('Trans.node') local it, t, f = node.item, node.text, node.format @@ -375,6 +376,7 @@ return function(word) width = width, height = math.min(buffer:height(width), hover.height) } + run(function() win:set('wrap', true) handle_keymap(win, word) diff --git a/lua/Trans/window.lua b/lua/Trans/window.lua index 621142e..72dccf0 100644 --- a/lua/Trans/window.lua +++ b/lua/Trans/window.lua @@ -27,7 +27,9 @@ end ---@param option string option name ---@param value any function window:set(option, value) - api.nvim_win_set_option(self.winid, option, value) + if self:is_valid() then + api.nvim_win_set_option(self.winid, option, value) + end end ---@param name string option name