feat: highlight current file

This commit is contained in:
Prime Again 2025-02-09 15:09:22 -07:00
parent 19309b636f
commit 09c6d045fb
4 changed files with 31 additions and 29 deletions

View File

@ -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

View File

@ -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

View File

@ -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,

View File

@ -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