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

Can't seem to add absolute path to harpoon list #484

Closed
Rizhiy opened this issue Jan 11, 2024 · 5 comments
Closed

Can't seem to add absolute path to harpoon list #484

Rizhiy opened this issue Jan 11, 2024 · 5 comments

Comments

@Rizhiy
Copy link

Rizhiy commented Jan 11, 2024

I want to use absolute paths in my harpoon list (my cwd changes based on which project the file belongs to).
I see that append takes an item, and tried passing in the absolute path, but it doesn't seem to work:

function() harpoon:list():append(vim.api.nvim_buf_get_name(0)) end,

What should I do instead?

@Rizhiy
Copy link
Author

Rizhiy commented Jan 11, 2024

Ok, got absolute paths working, but they still show up as relative in list:

opts[require("harpoon.config").DEFAULT_LIST] = {
     add = function() return { value = vim.api.nvim_buf_get_name(0) } end,
}
harpoon:setup(opts)

How can I make them show up as absolute as well?
Also, is there a better way to do this?

Ideally, I would want them to be displayed similar to buffers: relative when they are in cwd, absolute with ~ otherwise.

@maxrzaw
Copy link

maxrzaw commented Jan 13, 2024

Pretty sure you need to use create_list_item and not add.

I have spent a decent amount of time trying to get stuff like this to work.
I am not 100% sure what you are aiming for, but I think I have tried both.

If you want an item that you add when in one cwd to show up in the list when you are in another cwd, then you need to change your key to something other than vim.loop.cwd(). Then you could change the create_list_item to something like this:

            create_list_item = function(_, item)
                if item == nil then
                    item = vim.api.nvim_buf_get_name(
                        vim.api.nvim_get_current_buf()
                    )
                end

                if type(item) == "string" then
                    local name = require("plenary.path"):new(item):absolute()
                    local bufnr = vim.fn.bufnr(name, false)

                    local pos = { 1, 0 }
                    if bufnr ~= -1 then
                        pos = vim.api.nvim_win_get_cursor(0)
                    end
                    item = {
                        value = name,
                        context = {
                            row = pos[1],
                            col = pos[2],
                        },
                    }
                end
                return item
            end,

This will store the absolute path rather than the relative path. I have a PR out (#469) that does it's best to support absolute paths and will show it as an absolute path if it is not in your cwd, but relative to cwd if it is.

Check out the conversation in #466 and #467 if you are looking for the display to include something like ../../file.txt

@Rizhiy
Copy link
Author

Rizhiy commented Jan 13, 2024

Thanks, that works. The key was already set to be static.
I actually have my key set to cwd at the time when I open vim and would like relative paths be relative to that dir, so I changed name to:

local name = require("plenary.path"):new(item):make_relative(work_dir)

Where work_dir is my key.

I tried using normalise instead of make_relative, but that doesn't work after you close and re-open neovim.

@Rizhiy Rizhiy closed this as completed Jan 13, 2024
@Rizhiy
Copy link
Author

Rizhiy commented Jan 18, 2024

This solution actually fails a bit when using autochdir and such.
When you navigate to another project and try to switch back, if new project has the same path as your entry, it will navigate to that path instead.

@maxrzaw
Copy link

maxrzaw commented Jan 23, 2024

This solution actually fails a bit when using autochdir and such. When you navigate to another project and try to switch back, if new project has the same path as your entry, it will navigate to that path instead.

In order to solve that, you would need to store the absolute path, rather than the relative path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants