From cb47a09cbcfaa795d174e7b5ba70ae1af7527a51 Mon Sep 17 00:00:00 2001 From: xiaomo Date: Mon, 15 May 2023 09:04:40 +0800 Subject: [PATCH 1/2] feat: get visual-block text --- lua/Trans/core/util.lua | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/lua/Trans/core/util.lua b/lua/Trans/core/util.lua index 134ff37..a8d1514 100644 --- a/lua/Trans/core/util.lua +++ b/lua/Trans/core/util.lua @@ -56,6 +56,33 @@ function M.get_lines() end end +---Get selected text +---@return string +function M.get_block() + local _start = fn.getpos 'v' + local _end = fn.getpos '.' + + local s_row, e_row = math.min(_start[2],_end[2]), math.max(_start[2],_end[2]) + local s_col, e_col = math.min(_start[3], _end[3]), math.max(_start[3], _end[3]) + + ---@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)) + ---@diagnostic disable-next-line: param-type-mismatch + 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) + for col, l in pairs(lines) do + lines[col] = l:sub(s_col,e_col) + end + return table.concat(lines, ' ') + end +end + ---Get Text which need to be translated ---@param mode string ---@return string @@ -75,6 +102,10 @@ function M.get_str(mode) api.nvim_input '' return M.get_lines() end, + [''] = function() + api.nvim_input '' + return M.get_block() + end, })[mode]():match '^%s*(.-)%s*$' end From 2de2a713af616cf4b2f981f353497442a1f3a266 Mon Sep 17 00:00:00 2001 From: xiaomo Date: Mon, 15 May 2023 12:43:42 +0800 Subject: [PATCH 2/2] refactor: add get_range() --- lua/Trans/core/util.lua | 43 ++++++++++++++++------------------------- 1 file changed, 17 insertions(+), 26 deletions(-) diff --git a/lua/Trans/core/util.lua b/lua/Trans/core/util.lua index a8d1514..b031073 100644 --- a/lua/Trans/core/util.lua +++ b/lua/Trans/core/util.lua @@ -3,20 +3,23 @@ local fn, api = vim.fn, vim.api ---@class TransUtil local M = require 'Trans'.metatable 'util' +---Get the range of visual modes +---@return table +function M.get_range() + local _start = fn.getpos 'v' + local _end = fn.getpos '.' + + local s_row, e_row = math.min(_start[2], _end[2]), math.max(_start[2], _end[2]) + local s_col, e_col = math.min(_start[3], _end[3]), math.max(_start[3], _end[3]) + + return { s_row, e_row, s_col, e_col } +end ---Get selected text ---@return string function M.get_select() - local _start = fn.getpos 'v' - local _end = fn.getpos '.' + local s_row, e_row, s_col, e_col = unpack(M.get_range()) - if _start[2] > _end[2] or (_start[3] > _end[3] and _start[2] == _end[2]) then - _start, _end = _end, _start - end - local s_row, s_col = _start[2], _start[3] - local e_row, e_col = _end[2], _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) @@ -24,7 +27,6 @@ function M.get_select() ---@diagnostic disable-next-line: param-type-mismatch e_col = vim.str_byteindex(line, uidx) - if s_row == e_row then return line:sub(s_col, e_col) else @@ -39,19 +41,12 @@ end ---Get selected text ---@return string function M.get_lines() - local _start = vim.fn.getpos 'v' - local _end = vim.fn.getpos '.' - - if _start[2] > _end[2] then - _start, _end = _end, _start - end - - local s_row, e_row = _start[2], _end[2] + local s_row, e_row = unpack(M.get_range()) if s_row == e_row then - return vim.fn.getline(s_row) + return fn.getline(s_row) else - local lines = vim.fn.getline(s_row, e_row) + local lines = fn.getline(s_row, e_row) return table.concat(lines, " ") end end @@ -59,11 +54,7 @@ end ---Get selected text ---@return string function M.get_block() - local _start = fn.getpos 'v' - local _end = fn.getpos '.' - - local s_row, e_row = math.min(_start[2],_end[2]), math.max(_start[2],_end[2]) - local s_col, e_col = math.min(_start[3], _end[3]), math.max(_start[3], _end[3]) + local s_row, e_row, s_col, e_col = unpack(M.get_range()) ---@type string ---@diagnostic disable-next-line: assign-type-mismatch @@ -79,7 +70,7 @@ function M.get_block() for col, l in pairs(lines) do lines[col] = l:sub(s_col,e_col) end - return table.concat(lines, ' ') + return table.concat(lines, " ") end end