diff --git a/lua/harpoon/buffer.lua b/lua/harpoon/buffer.lua index 2afec323..60a207d3 100644 --- a/lua/harpoon/buffer.lua +++ b/lua/harpoon/buffer.lua @@ -16,6 +16,7 @@ local function get_harpoon_menu_name() end function M.run_select_command() + ---@type Harpoon local harpoon = require("harpoon") harpoon.logger:log("select by keymap ''") harpoon.ui:select_menu_item() diff --git a/lua/harpoon/config.lua b/lua/harpoon/config.lua index 6f58bde3..0edb78e9 100644 --- a/lua/harpoon/config.lua +++ b/lua/harpoon/config.lua @@ -100,11 +100,12 @@ function M.get_default_config() list.name, options ) - options = options or {} if list_item == nil then return end + options = options or {} + local bufnr = vim.fn.bufnr(to_exact_name(list_item.value)) local set_position = false if bufnr == -1 then -- must create a buffer! @@ -130,10 +131,33 @@ function M.get_default_config() vim.api.nvim_set_current_buf(bufnr) if set_position then + local lines = vim.api.nvim_buf_line_count(bufnr) + + local edited = false + if list_item.context.row > lines then + list_item.context.row = lines + edited = true + end + + local row = list_item.context.row + local row_text = vim.api.nvim_buf_get_lines(0, row - 1, row, false) + local col = #row_text[1] + + if list_item.context.col > col then + list_item.context.col = col + edited = true + end + vim.api.nvim_win_set_cursor(0, { list_item.context.row or 1, list_item.context.col or 0, }) + + if edited then + Extensions.extensions:emit(Extensions.event_names.POSITION_UPDATED, { + list_item = list_item + }) + end end Extensions.extensions:emit(Extensions.event_names.NAVIGATE, { @@ -192,6 +216,8 @@ function M.get_default_config() } end, + ---@param arg {buf: number} + ---@param list HarpoonList BufLeave = function(arg, list) local bufnr = arg.buf local bufname = vim.api.nvim_buf_get_name(bufnr) diff --git a/lua/harpoon/init.lua b/lua/harpoon/init.lua index 7482d11f..e34a7971 100644 --- a/lua/harpoon/init.lua +++ b/lua/harpoon/init.lua @@ -135,7 +135,7 @@ end local the_harpoon = Harpoon:new() ---@param self Harpoon ----@param partial_config HarpoonPartialConfig +---@param partial_config HarpoonPartialConfig? ---@return Harpoon function Harpoon.setup(self, partial_config) if self ~= the_harpoon then diff --git a/lua/harpoon/list.lua b/lua/harpoon/list.lua index 0c4b7914..2f9989ed 100644 --- a/lua/harpoon/list.lua +++ b/lua/harpoon/list.lua @@ -226,7 +226,7 @@ end function HarpoonList:get_by_display(name) local displayed = self:display() - local index = index_of(displayed, #displayed, name, self.config) + local index = index_of(displayed, #displayed, name) if index == -1 then return nil end diff --git a/lua/harpoon/test/harpoon_spec.lua b/lua/harpoon/test/harpoon_spec.lua index 0b0e4640..71bf2ab8 100644 --- a/lua/harpoon/test/harpoon_spec.lua +++ b/lua/harpoon/test/harpoon_spec.lua @@ -1,5 +1,9 @@ local utils = require("harpoon.test.utils") +local logger = require("harpoon.logger") + +---@type Harpoon local harpoon = require("harpoon") + local Extensions = require("harpoon.extensions") local Config = require("harpoon.config") local Data = require("harpoon.data") @@ -19,9 +23,43 @@ local function expect_data(data) end end +---@param out {row: number, col: number} +---@param expected {row: number, col: number} +local function out_of_bounds_test(out, expected) + local file_name = "/tmp/harpoon-test" + local list = harpoon:list() + local to_unload = utils.create_file(file_name, { + "foo", + "bar", + "baz", + "qux", + }) + list:add() + + utils.create_file(file_name .. "2", { + "foo", + "bar", + "baz", + "qux", + }) + + vim.api.nvim_buf_delete(to_unload, {force = true}) + + -- i have to force it to be out of bounds + list.items[1].context = out + + harpoon:list():select(1) + + eq({ + { value = file_name, context = expected} + }, harpoon:list().items) + +end + describe("harpoon", function() before_each(function() be() + logger:clear() harpoon = require("harpoon") end) @@ -85,6 +123,36 @@ describe("harpoon", function() }) end) + it("out of bounds test: row over", function() + out_of_bounds_test({ + row = 5, + col = 3 + }, { + row = 4, + col = 3 + }) + end) + + it("out of bounds test: col over", function() + out_of_bounds_test({ + row = 4, + col = 4 + }, { + row = 4, + col = 3 + }) + end) + + it("out of bounds test: both over", function() + out_of_bounds_test({ + row = 5, + col = 4 + }, { + row = 4, + col = 3 + }) + end) + it("prepend/add double add", function() local file_name_1 = "/tmp/harpoon-test" local row_1 = 3 diff --git a/lua/harpoon/test/ui_spec.lua b/lua/harpoon/test/ui_spec.lua index 02b318dd..26a06d7a 100644 --- a/lua/harpoon/test/ui_spec.lua +++ b/lua/harpoon/test/ui_spec.lua @@ -5,15 +5,6 @@ local harpoon = require("harpoon") local eq = assert.are.same local be = utils.before_each(os.tmpname()) ----@param k string -local function key(k) - vim.api.nvim_feedkeys( - vim.api.nvim_replace_termcodes(k, true, false, true), - "x", - true - ) -end - describe("harpoon", function() before_each(function() be() @@ -100,7 +91,7 @@ describe("harpoon", function() harpoon.ui:toggle_quick_menu(harpoon:list()) - key("") + utils.key("") eq(3, harpoon:list():length()) eq({ @@ -155,7 +146,7 @@ describe("harpoon", function() eq(vim.api.nvim_win_is_valid(win_id), true) eq(vim.api.nvim_get_current_buf(), bufnr) - key("") + utils.key("") eq(vim.api.nvim_buf_is_valid(bufnr), false) eq(vim.api.nvim_win_is_valid(win_id), false) @@ -173,7 +164,7 @@ describe("harpoon", function() eq(vim.api.nvim_win_is_valid(win_id), true) eq(vim.api.nvim_get_current_buf(), bufnr) - key("q") + utils.key("q") eq(vim.api.nvim_buf_is_valid(bufnr), false) eq(vim.api.nvim_win_is_valid(win_id), false) @@ -191,7 +182,7 @@ describe("harpoon", function() eq(vim.api.nvim_win_is_valid(win_id), true) eq(vim.api.nvim_get_current_buf(), bufnr) - key("") + utils.key("") eq(vim.api.nvim_buf_is_valid(bufnr), false) eq(vim.api.nvim_win_is_valid(win_id), false) diff --git a/lua/harpoon/test/utils.lua b/lua/harpoon/test/utils.lua index 75f0a5b0..82f9dd7e 100644 --- a/lua/harpoon/test/utils.lua +++ b/lua/harpoon/test/utils.lua @@ -1,4 +1,5 @@ local Data = require("harpoon.data") +local Path = require("plenary.path") local Config = require("harpoon.config") local M = {} @@ -21,6 +22,15 @@ function M.return_to_checkpoint() M.clean_files() end +---@param k string +function M.key(k) + vim.api.nvim_feedkeys( + vim.api.nvim_replace_termcodes(k, true, false, true), + "x", + true + ) +end + local function fullpath(name) return function() return name @@ -64,12 +74,12 @@ end ---@param name string ---@param contents string[] function M.create_file(name, contents, row, col) + Path:new(name):write(table.concat(contents, "\n"), "w") local bufnr = vim.fn.bufnr(name, true) vim.api.nvim_set_option_value("bufhidden", "hide", { buf = bufnr, }) vim.api.nvim_set_current_buf(bufnr) - vim.api.nvim_buf_set_text(0, 0, 0, 0, 0, contents) if row then vim.api.nvim_win_set_cursor(0, { row or 1, col or 0 }) end