From 673709071025d8184b09f3ef1ea0b6fa190ce06a Mon Sep 17 00:00:00 2001 From: JuanZoran <1430359574@qq.com> Date: Thu, 2 Feb 2023 12:01:23 +0800 Subject: [PATCH] fix: fix vistual selection region mistakes --- lua/Trans/content.lua | 2 +- lua/Trans/init.lua | 68 ++++++++++++++++++++++--------------- lua/Trans/query/offline.lua | 2 +- lua/Trans/window.lua | 1 - 4 files changed, 42 insertions(+), 31 deletions(-) diff --git a/lua/Trans/content.lua b/lua/Trans/content.lua index 911fb54..ee5f400 100644 --- a/lua/Trans/content.lua +++ b/lua/Trans/content.lua @@ -135,4 +135,4 @@ return function(window) lines = {}, highlights = {}, }, content) -end \ No newline at end of file +end diff --git a/lua/Trans/init.lua b/lua/Trans/init.lua index 4822031..39dba3c 100644 --- a/lua/Trans/init.lua +++ b/lua/Trans/init.lua @@ -1,13 +1,15 @@ -local M = {} +local M = {} +local api = vim.api +local fn = vim.fn -local title = vim.fn.has('nvim-0.9') == 1 and { +local title = fn.has('nvim-0.9') == 1 and { { '', 'TransTitleRound' }, { ' Trans', 'TransTitle' }, { '', 'TransTitleRound' }, } or nil -string.width = vim.fn.strwidth +string.width = api.nvim_strwidth string.isEn = function(self) local char = { self:byte(1, -1) } for i = 1, #self do @@ -19,13 +21,13 @@ string.isEn = function(self) end -string.play = vim.fn.has('linux') == 1 and function(self) +string.play = fn.has('linux') == 1 and function(self) local cmd = ([[echo "%s" | festival --tts]]):format(self) - vim.fn.jobstart(cmd) + fn.jobstart(cmd) end or function(self) - local seperator = vim.fn.has('unix') and '/' or '\\' + local seperator = fn.has('unix') and '/' or '\\' local file = debug.getinfo(1, "S").source:sub(2):match('(.*)lua') .. seperator .. 'tts' .. seperator .. 'say.js' - vim.fn.jobstart('node ' .. file .. ' ' .. self) + fn.jobstart('node ' .. file .. ' ' .. self) end @@ -154,13 +156,12 @@ M.setup = function(opts) times = times + 1 if times == 1 then - local api = vim.api local get_mode = api.nvim_get_mode local set_hl = api.nvim_set_hl local new_command = api.nvim_create_user_command - if vim.fn.executable('sqlite3') ~= 1 then + if fn.executable('sqlite3') ~= 1 then error('Please check out sqlite3') end @@ -187,30 +188,44 @@ M.setup = function(opts) end local function get_select() - local s_start = vim.fn.getpos("v") - local s_end = vim.fn.getpos(".") - if s_start[2] > s_end[2] or s_start[3] > s_end[3] then - s_start, s_end = s_end, s_start - end + local _start = fn.getpos("v") + local _end = fn.getpos('.') - local n_lines = math.abs(s_end[2] - s_start[2]) + 1 - local lines = vim.api.nvim_buf_get_lines(0, s_start[2] - 1, s_end[2], false) - lines[1] = string.sub(lines[1], s_start[3], -1) - if n_lines == 1 then - lines[n_lines] = string.sub(lines[n_lines], 1, s_end[3] - s_start[3] + 1) - else - lines[n_lines] = string.sub(lines[n_lines], 1, s_end[3]) + if _start[2] > _end[2] or (_start[3] > _end[3] and _start[2] == _end[2]) then + _start, _end = _end, _start + end + local s_row = _start[2] + local e_row = _end[2] + local s_col = _start[3] + local e_col = _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)) + 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 i = #lines + lines[1] = lines[1]:sub(s_col) + lines[i] = line:sub(1, e_col) + return table.concat(lines) end - return table.concat(lines, '') end M.get_word = function(mode) local word if mode == 'n' then - word = vim.fn.expand('') + word = fn.expand('') + elseif mode == 'v' then - vim.api.nvim_input('') + api.nvim_input('') word = get_select() + elseif mode == 'i' then -- TODO Use Telescope with fuzzy finder vim.ui.input({ prompt = '请输入需要查询的单词: ' }, function(input) @@ -229,7 +244,7 @@ M.translate = function(mode, view) view = { view, 's', true } } - mode = mode or vim.api.nvim_get_mode().mode + mode = mode or api.nvim_get_mode().mode view = view or M.conf.view[mode] assert(mode and view) local word = M.get_word(mode) @@ -240,7 +255,4 @@ M.translate = function(mode, view) end end - -M.augroup = vim.api.nvim_create_augroup('Trans', { clear = true }) - return M diff --git a/lua/Trans/query/offline.lua b/lua/Trans/query/offline.lua index 703d84f..46a2733 100644 --- a/lua/Trans/query/offline.lua +++ b/lua/Trans/query/offline.lua @@ -8,7 +8,7 @@ local path = require('Trans').conf.db_path local dict = db:open(path) vim.api.nvim_create_autocmd('VimLeavePre', { - group = require("Trans").augroup, + once = true, callback = function() if db:isopen() then db:close() diff --git a/lua/Trans/window.lua b/lua/Trans/window.lua index 4eedb42..b7a19bf 100644 --- a/lua/Trans/window.lua +++ b/lua/Trans/window.lua @@ -237,7 +237,6 @@ return function(entry, option) ---@diagnostic disable-next-line: param-type-mismatch setmetatable(win, window) - win:bufset('filetype', 'Trans') win:bufset('buftype', 'nofile') api.nvim_win_set_hl_ns(win.winid, win.hl)