diff --git a/README.md b/README.md index 05ebdbe5..4a532106 100644 --- a/README.md +++ b/README.md @@ -171,6 +171,7 @@ Settings can alter the experience of harpoon ```lua ---@class HarpoonSettings ---@field save_on_toggle boolean defaults to false +---@field select_current boolean defalts to false ---@field sync_on_ui_close boolean defaults to false ---@field key (fun(): string) @@ -178,6 +179,7 @@ Settings can alter the experience of harpoon **Descriptions** * `save_on_toggle`: any time the ui menu is closed then we will sync the state back to the backing list +* `select_current`: when the ui menu is opened, if the current file is present on the list, select it * `border_chars`: the ui's border characters to be displayed * `key` how the out list key is looked up. This can be useful when using worktrees and using git remote instead of file path @@ -185,6 +187,7 @@ Settings can alter the experience of harpoon ```lua settings = { save_on_toggle = false, + select_current = false, sync_on_ui_close = false, border_chars = { "─", "│", "─", "│", "╭", "╮", "╯", "╰" }, key = function() diff --git a/lua/harpoon/buffer.lua b/lua/harpoon/buffer.lua index 494001f4..ccbd7034 100644 --- a/lua/harpoon/buffer.lua +++ b/lua/harpoon/buffer.lua @@ -32,18 +32,6 @@ end ---strings back into the ui. it feels gross and it puts odd coupling ---@param bufnr number function M.setup_autocmds_and_keymaps(bufnr) - local curr_file = vim.api.nvim_buf_get_name(0) - local cmd = string.format( - "autocmd Filetype harpoon " - .. "let path = '%s' | call clearmatches() | " - -- move the cursor to the line containing the current filename - .. "call search('\\V'.path.'\\$') | " - -- add a hl group to that line - .. "call matchadd('HarpoonCurrentFile', '\\V'.path.'\\$')", - curr_file:gsub("\\", "\\\\") - ) - vim.cmd(cmd) - if vim.api.nvim_buf_get_name(bufnr) == "" then vim.api.nvim_buf_set_name(bufnr, get_harpoon_menu_name()) end diff --git a/lua/harpoon/config.lua b/lua/harpoon/config.lua index 3c12713e..b4bfd8bd 100644 --- a/lua/harpoon/config.lua +++ b/lua/harpoon/config.lua @@ -1,9 +1,5 @@ local Logger = require("harpoon.logger") -local Path = require("plenary.path") -local function normalize_path(buf_name, root) - return Path:new(buf_name):make_relative(root) -end - +local utils = require("harpoon.utils") local M = {} local DEFAULT_LIST = "__harpoon_files" M.DEFAULT_LIST = DEFAULT_LIST @@ -27,6 +23,7 @@ M.DEFAULT_LIST = DEFAULT_LIST ---@class HarpoonSettings ---@field border_chars string[] defaults to { "─", "│", "─", "│", "╭", "╮", "╯", "╰" } ---@field save_on_toggle boolean defaults to true +---@field select_current boolean default to false ---@field sync_on_ui_close? boolean ---@field ui_fallback_width number defaults 69, nice ---@field ui_width_ratio number defaults to 0.62569 @@ -34,6 +31,7 @@ M.DEFAULT_LIST = DEFAULT_LIST ---@class HarpoonPartialSettings ---@field save_on_toggle? boolean +---@field select_current? boolean ---@field sync_on_ui_close? boolean ---@field key? (fun(): string) @@ -58,6 +56,7 @@ function M.get_default_config() settings = { save_on_toggle = false, + select_current = false, sync_on_ui_close = false, border_chars = { "─", @@ -166,7 +165,7 @@ function M.get_default_config() -- path, if that is the case we can use the context to -- store the bufname and then have value be the normalized -- value - or normalize_path( + or utils.normalize_path( vim.api.nvim_buf_get_name( vim.api.nvim_get_current_buf() ), diff --git a/lua/harpoon/data.lua b/lua/harpoon/data.lua index c23f40cf..2efd1931 100644 --- a/lua/harpoon/data.lua +++ b/lua/harpoon/data.lua @@ -59,7 +59,7 @@ local function read_data() local out_data = path:read() - if not out_data or out_data == '' then + if not out_data or out_data == "" then write_data({}) out_data = path:read() end diff --git a/lua/harpoon/logger.lua b/lua/harpoon/logger.lua index 9fd5fcc8..aff979b7 100644 --- a/lua/harpoon/logger.lua +++ b/lua/harpoon/logger.lua @@ -51,7 +51,7 @@ function HarpoonLog:log(...) table.insert(self.lines, table.concat(lines, " ")) - while #self.lines > self.max_lines do + while #self.lines > self.max_lines do table.remove(self.lines, 1) end end diff --git a/lua/harpoon/test/logger_spec.lua b/lua/harpoon/test/logger_spec.lua index d97054d8..486569d6 100644 --- a/lua/harpoon/test/logger_spec.lua +++ b/lua/harpoon/test/logger_spec.lua @@ -14,8 +14,8 @@ describe("harpoon", function() end) it("new lines with vim.inspect get removed too", function() - Logger:log({hello = "world", world = "hello"}) - eq({ "{ hello = \"world\", world = \"hello\" }" }, Logger.lines) + Logger:log({ hello = "world", world = "hello" }) + eq({ '{ hello = "world", world = "hello" }' }, Logger.lines) end) it("max lines", function() @@ -25,6 +25,4 @@ describe("harpoon", function() Logger:log("two") eq({ "two" }, Logger.lines) end) - end) - diff --git a/lua/harpoon/ui.lua b/lua/harpoon/ui.lua index 65cdff42..bc2e7b33 100644 --- a/lua/harpoon/ui.lua +++ b/lua/harpoon/ui.lua @@ -1,6 +1,7 @@ local popup = require("plenary").popup local Buffer = require("harpoon.buffer") local Logger = require("harpoon.logger") +local utils = require("harpoon.utils") ---@class HarpoonUI ---@field win_id number @@ -107,6 +108,10 @@ end ---@param list? HarpoonList function HarpoonUI:toggle_quick_menu(list) + local currentFileName = utils.normalize_path( + vim.api.nvim_buf_get_name(vim.api.nvim_get_current_buf()) + ) + if list == nil or self.win_id ~= nil then Logger:log("ui#toggle_quick_menu#closing", list and list.name) if self.settings.save_on_toggle then @@ -125,6 +130,22 @@ function HarpoonUI:toggle_quick_menu(list) local contents = self.active_list:display() vim.api.nvim_buf_set_lines(self.bufnr, 0, -1, false, contents) + for idx, line in ipairs(contents) do + if line == currentFileName then + vim.api.nvim_buf_add_highlight( + self.bufnr, + -1, + "HarpoonCurrentFile", + idx - 1, + 0, + -1 + ) + if self.settings.select_current then + vim.api.nvim_win_set_cursor(self.win_id, { idx, 0 }) + end + break + end + end end ---@param options? any diff --git a/lua/harpoon/utils.lua b/lua/harpoon/utils.lua index de632c31..fe6abcd6 100644 --- a/lua/harpoon/utils.lua +++ b/lua/harpoon/utils.lua @@ -1,8 +1,14 @@ +local Path = require("plenary.path") local M = {} +function M.normalize_path(buf_name, root) + return Path:new(buf_name):make_relative(root) +end + function M.trim(str) return str:gsub("^%s+", ""):gsub("%s+$", "") end + function M.remove_duplicate_whitespace(str) return str:gsub("%s+", " ") end