From 27fce89fe803ce02f226688968b0ff3145c31262 Mon Sep 17 00:00:00 2001 From: Elvis Hernandez Date: Thu, 9 Jan 2025 15:48:16 -0500 Subject: [PATCH] Highlight the current buffer in the harpoon menu and match the cursor position if it exists --- lua/harpoon/buffer.lua | 23 +++++++++++++++++++++++ lua/harpoon/ui.lua | 7 +++++++ 2 files changed, 30 insertions(+) diff --git a/lua/harpoon/buffer.lua b/lua/harpoon/buffer.lua index a6a9fd3..b351f62 100644 --- a/lua/harpoon/buffer.lua +++ b/lua/harpoon/buffer.lua @@ -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 diff --git a/lua/harpoon/ui.lua b/lua/harpoon/ui.lua index eb76c17..2e524e5 100644 --- a/lua/harpoon/ui.lua +++ b/lua/harpoon/ui.lua @@ -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,