harpoon icon indicating copy to clipboard operation
harpoon copied to clipboard

Have optional Key per List

Open aldevv opened this issue 1 year ago • 7 comments

What issue are you having that you need harpoon to solve? You can only set a key in the settings, this will work for all lists

Why doesn't the current config help? It would be useful to be able to overwrite the key setting in a per list basis, so I can have a list in cwd and a different list per git branch

aldevv avatar Dec 30 '23 14:12 aldevv

I'm not sure I get what you mean. After testing on a project using git-worktree my harpoon2 list updates accordingly when I switch branches.

hmmm.... Maybe it's this: The harpoon list only updates when I change the active branch. However, if I instead navigate to a buffer with a file from a non-active branch then the harpoon list does not update. Is that it?

lpanebr avatar Jan 02 '24 21:01 lpanebr

I'm not having issues with behavior, is a feature request.

I want to have a list that changes with branch, and a different list that only changes with cwd

aldevv avatar Jan 03 '24 11:01 aldevv

Oh I see. Actually of I'm not mistaken the default behavior creates and displays lists based on the cwd. That's why when I change branches it works, cause with worktrees each branch lives in a different directory.

So you'd like to be able to add files to a branch list regardless of the cwd set, so whenever you switch branches the list also changes.

I was trying to figure out if it could be done with either the custom lists, or the extend features. Maybe it can be done using both but I'm not really familiar with those yet.

You'd need one extend command to save and another to open lists named after the branch.

Then you'd need to create the two key maps.

lpanebr avatar Jan 03 '24 11:01 lpanebr

Oh I see. Actually of I'm not mistaken the default behavior creates and displays lists based on the cwd. That's why when I change branches it works, cause with worktrees each branch lives in a different directory.

So you'd like to be able to add files to a branch list regardless of the cwd set, so whenever you switch branches the list also changes.

I was trying to figure out if it could be done with either the custom lists, or the extend features. Maybe it can be done using both but I'm not really familiar with those yet.

You'd need one extend command to save and another to open lists named after the branch.

Then you'd need to create the two key maps.

Great idea! I was able to implement that idea with just two sets of keymaps one that uses the default list and another that gets the branch name as the list. My default list is a little customized so I have full paths as the value, but this is what the resulting harpoon.json looks like for the default key:

{
  "/Users/max/dev/test-repo": {
    "main": [
      "{\"value\":\"\\/Users\\/max\\/dev\\/test-repo\\/file02.txt\",\"context\":{\"row\":1,\"col\":0}}"
    ],
    "__harpoon_files": [
      "{\"context\":{\"row\":1,\"col\":0},\"value\":\"\\/Users\\/max\\/dev\\/test-repo\\/README.md\"}",
      "{\"context\":{\"row\":1,\"col\":0},\"value\":\"\\/Users\\/max\\/dev\\/test-repo\\/file03.txt\"}"
    ],
    "test-branch": [
      "{\"context\":{\"row\":1,\"col\":0},\"value\":\"\\/Users\\/max\\/dev\\/test-repo\\/file01.txt\"}"
    ]
  }
}

The default branch is __harpoon_files and the other two lists are the branch names.

Here are the keymaps I set to accomplish this:

local function get_git_branch()
    local git_branch = vim.fn.systemlist("git branch --show-current")
    if git_branch[1] == "fatal: not a git repository (or any of the parent directories): .git" then
        return nil
    end
    return git_branch[1]
end

        vim.keymap.set("n", "<leader>m", function()
            Harpoon:list():append()
        end)
        vim.keymap.set("n", "<leader>h", function()
            Harpoon.ui:toggle_quick_menu(Harpoon:list(), {})
        end)

        vim.keymap.set("n", "<leader>mm", function()
            local branch = get_git_branch()
            if branch == nil then
                return
            end
            Harpoon:list(branch):append()
        end)
        vim.keymap.set("n", "<leader>hh", function()
            local branch = get_git_branch()
            if branch == nil then
                return
            end
            Harpoon.ui:toggle_quick_menu(Harpoon:list(branch), {})
        end)

maxrzaw avatar Jan 04 '24 03:01 maxrzaw

Great @maxrzaw ! It's a lot simpler than I imagined! 😆😂😂

lpanebr avatar Jan 04 '24 04:01 lpanebr

I'll make this happen at some point

I think it should be easy to make, I'm just a bit worried at adding that level of configuration. It makes the project overall more complex

ThePrimeagen avatar Jan 06 '24 15:01 ThePrimeagen

An alternative I'm thinking is to have the list_name as an argument of the key function, and then we could use that if we wanted to

aldevv avatar Jan 06 '24 20:01 aldevv