harpoon
harpoon copied to clipboard
Harpoon2: Would it be possible to get the current HarpoonListItem that I'm changing in the create_list_item function?
What issue are you having that you need harpoon to solve? I'm changing a path in my harpoon ui, but my harpoon ui is set up to shorten long path names.
Why doesn't the current config help? I need to use find to get the actual file, which seems a bit hacky.
What proposed api changes are you suggesting? get the current HarpoonListItem that I'm changing in the create_list_item function. I did not figure out the whole architecture, so I could see why this will not be possible as well, since the item and path might not be connected when doing loads of edits to the path list.
As a second (much more reasonable request): What is your opinion on a wiki where people could upload some configs? This could be a nice way to let people easily get a custom config, without needing to read into the code.
For reference, my workaround for my problem (Which does have the added benefit of being able to add a file that you know the name of, but not the correct path with .../filename in the harpoon ui):
require("harpoon"):setup({
settings = {
key = function()
local root = string.gsub(vim.fn.system("git rev-parse --show-toplevel"), "\n", "")
local branch = string.gsub(vim.fn.system("git rev-parse --abbrev-ref HEAD"), "\n", "")
if vim.v.shell_error == 0 then
return branch .. "-" .. root
end
return vim.loop.cwd()
end,
},
default = {
get_root_dir = function()
local root = string.gsub(vim.fn.system("git rev-parse --show-toplevel"), "\n", "")
if vim.v.shell_error == 0 then
return root
end
return vim.loop.cwd()
end,
create_list_item = function(config, short_path)
local root = config.get_root_dir()
local buf_path = vim.api.nvim_buf_get_name(vim.api.nvim_get_current_buf())
if short_path then
buf_path = "."
local prefix = "..."
local isShortened = string.sub(short_path, 1, #prefix) == prefix
if not isShortened then
buf_path = root .. "/" .. short_path
else
local searchPath = string.gsub(short_path, "^" .. prefix, "*")
local command = "find " .. root .. " -path " .. searchPath
local handle = io.popen(command)
if handle ~= nil then
local result = handle:read("*l")
handle:close()
if result then
buf_path = result
end
end
end
end
short_path = require("plenary.path"):new(buf_path):normalize(root)
local bufnr = vim.fn.bufnr(short_path, false)
local pos = { 1, 0 }
if bufnr ~= -1 then
pos = vim.api.nvim_win_get_cursor(0)
end
return {
value = buf_path,
context = { row = pos[1], col = pos[2], name = short_path },
}
end,
display = function(item)
local t = {}
local str = item.context.name
for s in string.gmatch(str, "([^" .. "/" .. "]+)") do
table.insert(t, s)
end
if #t <= 5 then
return str
end
return ".../" .. t[#t - 3] .. "/" .. t[#t - 2] .. "/" .. t[#t - 1] .. "/" .. t[#t]
end,
},
})
I feel that I don't understand the whole issue, so you have these file on harpoon
src/domain/my_domain.go
But you want to see the long path?
/home/user/my_project/src/domain/my_domain.go
Sorry, I'm a little confuse, if you provide the step-by-step of your issue.
About a wiki, I'm thinking this is not related directly with development of Harpoon, but we can create a project called awesome-harpoon with the session to people add different configurations, I like of the idea, we can discuss more and get more opinions!
@daltondiaz With a wiki I mean something like this: https://github.com/nvim-lualine/lualine.nvim/wiki/Component-snippets A place where you can share a config for Harpoon2, so others can just take it and be happy. Without the need of spending too much time tinkering themselves. I think it makes sense as people probably want similar things.
The path is a bit more complicated.
I save /home/user/my_project/one/two/three/four/five/six/myfile.go
as my absolute path. This is an easy way to be able to be in /home/user/my_project/one
and still be able to use the same harpoon for my_project (root = the git root).
This is not that relevant.
What is my issue is that I want to display one/two/three/four/five/six/myfile.go
as .../three/four/five/six/myfile.go
in my harpoon list. That's easy, I can just adjust the display function.
The issue lies when I want to edit my Harpoon list.
Say I also have a testfile for myfile.go I'd just open the harpoon list yank the myfile line and edit it to say .../three/four/five/six/myfile_test.go
.
At this point I think I need to adjust the create_list_item
function. What I have to work with in there is a config, and the display value form my edited list. I can't really be sure what the one/two
part of my path is, since I truncated that part.
The config can get me my root directory and the display path. It would be great to also receive the actual HarpoonListItem, as it would be easy to get the path - the last 5 folders (so one/two
in this example) and put that instead of ...
, and bam, you have the exact path you'd expect.
My workaround is using find to search for */three/four/five/six/myfile_test.goin my root directory, if I can't find any file it will fall back to the root of the project
.. The only real difference here is that with my fix I'd get
one/two/three/four/five/six/myfile_test.goevery time, even if the test file did not exist yet. I'd also be sure that I would not get
notone/nottwo/three/four/five/six/myfile_test.go` if for some reason my project had some very similar paths (That does seem like edge case).
I do see how that could be really tricky, especially when you can edit the harpoon list very freely, as it is it's own buffer. I'm not sure if this would be a wanted feature even.
Hope this clears stuff up :smile:
@ThePrimeagen Is this something you want added in Harpoon 2? If so, do you want me to investigate this and possibly create a PR? If not you can close this down.