Skip to content

Commit

Permalink
Highlight the current buffer in the harpoon menu and match the cursor
Browse files Browse the repository at this point in the history
position if it exists
  • Loading branch information
ElvisHernandez committed Jan 9, 2025
1 parent a84ab82 commit d6098e9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lua/harpoon/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,27 @@ function M.set_contents(bufnr, contents)
vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, contents)
end

function M.set_current_menu_item_highlight(
bufnr,
win_id,
current_file,
contents
)
for line_number, file in pairs(contents) do
if string.match(current_file, file) then
-- highlight the harpoon menu line that corresponds to the current buffer
vim.api.nvim_buf_add_highlight(
bufnr,
-1,
"CursorLineNr",
line_number - 1,
0,
-1
)
-- set the position of the cursor in the harpoon menu to the start of the current buffer line
vim.api.nvim_win_set_cursor(win_id, { line_number, 0 })
end
end
end

return M
7 changes: 7 additions & 0 deletions lua/harpoon/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,14 @@ function HarpoonUI:toggle_quick_menu(list, opts)
self.active_list = list

local contents = self.active_list:display()

vim.api.nvim_buf_set_lines(self.bufnr, 0, -1, false, contents)
Buffer.set_current_menu_item_highlight(
bufnr,
win_id,
current_file,
contents
)

Extensions.extensions:emit(Extensions.event_names.UI_CREATE, {
win_id = win_id,
Expand Down

0 comments on commit d6098e9

Please sign in to comment.