diff --git a/README.md b/README.md index 44e8ebd..6feede2 100644 --- a/README.md +++ b/README.md @@ -258,6 +258,14 @@ harpoon:extend({ }) ``` +### Builtin Extensions +Highlight current file in the harpoon buffer list + +```lua +local harpoon_extensions = require("harpoon.extensions") +harpoon:extend(harpoon_extensions.highlight_current_file()) +``` + ### Highlight Groups TODO: Fill in the idea that we will emit out window information diff --git a/lua/harpoon/buffer.lua b/lua/harpoon/buffer.lua index 2c49920..a6a9fd3 100644 --- a/lua/harpoon/buffer.lua +++ b/lua/harpoon/buffer.lua @@ -99,27 +99,4 @@ 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.find(current_file, file, 1, true) 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/extensions/init.lua b/lua/harpoon/extensions/init.lua index a6638a4..39a3252 100644 --- a/lua/harpoon/extensions/init.lua +++ b/lua/harpoon/extensions/init.lua @@ -64,6 +64,28 @@ function Builtins.navigate_with_number() } end +function Builtins.highlight_current_file() + return { + UI_CREATE = function(cx) + for line_number, file in pairs(cx.contents) do + if string.find(cx.current_file, file, 1, true) then + -- highlight the harpoon menu line that corresponds to the current buffer + vim.api.nvim_buf_add_highlight( + cx.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(cx.win_id, { line_number, 0 }) + end + end + end, + } +end + return { builtins = Builtins, extensions = extensions, diff --git a/lua/harpoon/ui.lua b/lua/harpoon/ui.lua index 2e524e5..c5680b5 100644 --- a/lua/harpoon/ui.lua +++ b/lua/harpoon/ui.lua @@ -154,17 +154,12 @@ function HarpoonUI:toggle_quick_menu(list, opts) 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, bufnr = bufnr, current_file = current_file, + contents = contents, }) end