Skip to content

Commit

Permalink
feat: closes #542 : out of bounds navigation. this was a pain in the ass
Browse files Browse the repository at this point in the history
  • Loading branch information
theprimeagen committed Apr 4, 2024
1 parent 77d52b2 commit 5b34471
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 17 deletions.
1 change: 1 addition & 0 deletions lua/harpoon/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<CR>'")
harpoon.ui:select_menu_item()
Expand Down
28 changes: 27 additions & 1 deletion lua/harpoon/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand All @@ -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, {
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lua/harpoon/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lua/harpoon/list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
68 changes: 68 additions & 0 deletions lua/harpoon/test/harpoon_spec.lua
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand Down
17 changes: 4 additions & 13 deletions lua/harpoon/test/ui_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -100,7 +91,7 @@ describe("harpoon", function()

harpoon.ui:toggle_quick_menu(harpoon:list())

key("<CR>")
utils.key("<CR>")

eq(3, harpoon:list():length())
eq({
Expand Down Expand Up @@ -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("<C-w><C-w>")
utils.key("<C-w><C-w>")

eq(vim.api.nvim_buf_is_valid(bufnr), false)
eq(vim.api.nvim_win_is_valid(win_id), false)
Expand All @@ -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)
Expand All @@ -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("<Esc>")
utils.key("<Esc>")

eq(vim.api.nvim_buf_is_valid(bufnr), false)
eq(vim.api.nvim_win_is_valid(win_id), false)
Expand Down
12 changes: 11 additions & 1 deletion lua/harpoon/test/utils.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local Data = require("harpoon.data")
local Path = require("plenary.path")
local Config = require("harpoon.config")

local M = {}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5b34471

Please sign in to comment.