diff --git a/lua/gitlinker/logger.lua b/lua/gitlinker/logger.lua index e2cac71..a6f42ba 100644 --- a/lua/gitlinker/logger.lua +++ b/lua/gitlinker/logger.lua @@ -42,7 +42,7 @@ local Configs = { ), } ---- @param opts table +--- @param opts Options? local function setup(opts) Configs = vim.tbl_deep_extend("force", vim.deepcopy(Configs), opts or {}) if type(Configs.level) == "string" then diff --git a/lua/gitlinker/util.lua b/lua/gitlinker/util.lua index 268919a..1491bc9 100644 --- a/lua/gitlinker/util.lua +++ b/lua/gitlinker/util.lua @@ -56,19 +56,19 @@ end --- @return {lstart:integer,lend:integer} local function line_range() - local mode = vim.fn.mode() - local pos1 = nil - local pos2 = nil - if is_visual_mode(mode) then + local m = vim.fn.mode() + local l1 = nil + local l2 = nil + if is_visual_mode(m) then vim.cmd([[execute "normal! \"]]) - pos1 = vim.fn.getpos("'<")[2] - pos2 = vim.fn.getpos("'>")[2] + l1 = vim.fn.getpos("'<")[2] + l2 = vim.fn.getpos("'>")[2] else - pos1 = vim.fn.getcurpos()[2] - pos2 = pos1 + l1 = vim.fn.getcurpos()[2] + l2 = l1 end - local lstart = math.min(pos1, pos2) - local lend = math.max(pos1, pos2) + local lstart = math.min(l1, l2) + local lend = math.max(l1, l2) return { lstart = lstart, lend = lend } end diff --git a/test/util_spec.lua b/test/util_spec.lua new file mode 100644 index 0000000..ca9642b --- /dev/null +++ b/test/util_spec.lua @@ -0,0 +1,50 @@ +local cwd = vim.fn.getcwd() + +describe("util", function() + local assert_eq = assert.is_equal + local assert_true = assert.is_true + local assert_false = assert.is_false + + before_each(function() + vim.api.nvim_command("cd " .. cwd) + vim.opt.swapfile = false + end) + + local logger = require("gitlinker.logger") + logger.setup() + local util = require("gitlinker.util") + describe("[path_normalize]", function() + it("normalize", function() + local lines = { + "~/github/linrongbin16/gitlinker.nvim/README.md", + "~/github/linrongbin16/gitlinker.nvim/lua/gitlinker.lua", + } + for i, line in ipairs(lines) do + local actual = util.path_normalize(line) + local expect = vim.fn.expand(line) + print( + string.format( + "path normalize[%d]:%s == %s\n", + i, + actual, + expect + ) + ) + assert_eq(actual, expect) + end + end) + it("relative", function() + local lines = { + "README.md", + "lua/gitlinker.lua", + "lua/gitlinker/util.lua", + } + for i, line in ipairs(lines) do + vim.cmd(string.format([[ edit %s ]], line)) + local actual = util.path_relative() + print(string.format("path relative:%s\n", actual)) + assert_eq(actual, line) + end + end) + end) +end)