From b50ca53b666cf61facd0ebdd4767d02d2639f720 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Sun, 16 Jul 2023 07:33:22 +0800 Subject: [PATCH] fix: visual lines (#57) * fix: visual lines * fix: visual lines --- lua/gitlinker/util.lua | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/lua/gitlinker/util.lua b/lua/gitlinker/util.lua index 3b31ebc..ea2b8fa 100644 --- a/lua/gitlinker/util.lua +++ b/lua/gitlinker/util.lua @@ -75,14 +75,34 @@ local function relative_path(cwd) return relpath end +local function is_visual_mode(m) + return type(m) == "string" and m:upper() == "V" + or m:upper() == "CTRL-V" + or m:upper() == "" + or m == "\22" +end + --- @class LineRange --- @field lstart integer --- @field lend integer --- @return LineRange local function line_range() - local pos1 = vim.fn.getpos("v")[2] - local pos2 = vim.fn.getcurpos()[2] + vim.cmd([[execute "normal! \"]]) + local mode = vim.fn.visualmode() + local pos1 = nil + local pos2 = nil + if is_visual_mode(mode) then + pos1 = vim.fn.getpos("'<")[2] + pos2 = vim.fn.getpos("'>")[2] + else + pos1 = vim.fn.getpos("v")[2] + -- if mode == "v" then + -- pos2 = vim.fn.getpos(".")[2] + -- else + pos2 = vim.fn.getcurpos()[2] + -- end + end local lstart = math.min(pos1, pos2) local lend = math.max(pos1, pos2) return { lstart = lstart, lend = lend }