Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto highlight selected #397

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,20 +171,23 @@ 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)

```

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

**Defaults**
```lua
settings = {
save_on_toggle = false,
select_current = false,
sync_on_ui_close = false,
border_chars = { "─", "│", "─", "│", "╭", "╮", "╯", "╰" },
key = function()
Expand Down
12 changes: 0 additions & 12 deletions lua/harpoon/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 5 additions & 6 deletions lua/harpoon/config.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -27,13 +23,15 @@ 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
---@field key (fun(): string)

---@class HarpoonPartialSettings
---@field save_on_toggle? boolean
---@field select_current? boolean
---@field sync_on_ui_close? boolean
---@field key? (fun(): string)

Expand All @@ -58,6 +56,7 @@ function M.get_default_config()

settings = {
save_on_toggle = false,
select_current = false,
sync_on_ui_close = false,
border_chars = {
"─",
Expand Down Expand Up @@ -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()
),
Expand Down
2 changes: 1 addition & 1 deletion lua/harpoon/data.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lua/harpoon/logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions lua/harpoon/test/logger_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -25,6 +25,4 @@ describe("harpoon", function()
Logger:log("two")
eq({ "two" }, Logger.lines)
end)

end)

21 changes: 21 additions & 0 deletions lua/harpoon/ui.lua
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
tris203 marked this conversation as resolved.
Show resolved Hide resolved
break
end
end
end

---@param options? any
Expand Down
6 changes: 6 additions & 0 deletions lua/harpoon/utils.lua
Original file line number Diff line number Diff line change
@@ -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+$", "")
tris203 marked this conversation as resolved.
Show resolved Hide resolved
end

tris203 marked this conversation as resolved.
Show resolved Hide resolved
function M.remove_duplicate_whitespace(str)
return str:gsub("%s+", " ")
end
Expand Down