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

[harpoon2] when custom key is specified, persistent not working correctly #565

Open
rndev-io opened this issue Apr 9, 2024 · 6 comments
Labels
bug Something isn't working harpoon2

Comments

@rndev-io
Copy link

rndev-io commented Apr 9, 2024

WARNING
If this is about Harpoon1, the issue will be closed. All support and everything of harpoon1 will be frozen on master until 4/20 or 6/9 and then harpoon2 will become master

Please use harpoon2 for branch

My harpoon setup:

harpoon:setup({
    settings = {
        save_on_toggle = true,
        sync_on_ui_close = true,
        key = function()
            local key = vim.fn.getcwd()
            local current_branch = utils.get_current_branch()
            if current_branch ~= nil then
                key = string.format("%s#%s", key, current_branch)
            end
            return key
        end
    }
})

I add and remove file to harpoon, but when i close and open nvim again, my harpoon list is empty.

@ThePrimeagen
Copy link
Owner

let me try this out and i'll see if we can get a quick fix for this

seems surprising!

@ThePrimeagen ThePrimeagen added bug Something isn't working harpoon2 labels Apr 9, 2024
@gustavo-nakabayashi
Copy link

same bug here

This is my setup:

harpoon:setup({
    settings = {
      save_on_toggle = true,
      sync_on_ui_close = false,
      key = function()
        local handle = io.popen("git rev-parse --abbrev-ref HEAD 2> /dev/null")
        local branch_name = handle:read("*a")
        handle:close()
        branch_name = string.gsub(branch_name, "\n", "")
        if branch_name == nil or branch_name == '' then
          return vim.loop.cwd()
        else
          return branch_name .. vim.loop.cwd()
        end
      end,
    },
  })

@akyrey
Copy link

akyrey commented Apr 10, 2024

Same problem with a similar configuration (different lists based on branch).
It seems like the list is correctly saved but when opening a new instance of nvim the list isn't read and it's always displayed empty

@rndev-io
Copy link
Author

Problem with the_harpoon singleton. When module imported, singleton created, and using default key for data storage (vim.loop.cwd). Then when harpoon:setup called, default config updated, but data storage stay with default key

@dajabe
Copy link

dajabe commented May 17, 2024

Having this issue as well with a similar config, hoping that #557 can be merged soon

@g-pavlik
Copy link

g-pavlik commented Sep 27, 2024

I have the same issue, from what I see in the thread – this does not seem to be the issue of any particular key function implementation, but a bug in harpoon2?

Here's my approach: (Added logging to check the sanity, and the returned key appears to be correct and repeatable for the branch)

local function debug_print(...)
  print(vim.inspect(...))
end

harpoon:setup({
  settings = {
    key = function()
      debug_print("key")
      local pipe = io.popen("git branch --show-current")
      if pipe then
        debug_print("pipe")
        local c = pipe:read("*l"):match("^%s*(.-)%s*$")
        debug_print(c)
        pipe:close()
        return c
      end
      return "default list"
    end,
  },
})

Edit: Found a workaround. No need to set settings = { key what you can do instead is define your own way to figure out a key, e.g. current branch:

local function git_branch()
  debug_print("key")
  local pipe = io.popen("git branch --show-current")
  if pipe then
    debug_print("pipe")
    local c = pipe:read("*l"):match("^%s*(.-)%s*$")
    debug_print(c)
    pipe:close()
    return c
  end
  return "default list"
end

and use it in each keymap declaration, like this

{
    "<M-j>",
    function()
      harpoon:list(git_branch()):next()
    end,
    desc = "Select next",
  },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working harpoon2
Projects
None yet
Development

No branches or pull requests

6 participants