ThePrimeagen/harpoon

File opened with Harpoon can't be folded (E490: No fold found)

Closed this issue ยท 8 comments

I reallyyyyyy want to use harpoon (or harpoon2 if you will ๐Ÿ˜…) as I love it so much, but I can't get around this issue.

I use oil.nvim as my file explorer (my config here) overriding netrw.

When I enter Neovim and are presented with oil's file explorer, I fire harpoon to take me to x file. When doing so, folds don't seem to appear. As soon as I go into insert mode, the folds appear out of nowhere and I'm off to the races. But I gotta do this on a per-file basis and it get's quite overwhelming.

You can find my entire config here. Could I be missing something that's not harpoon related maybe?

Here's a recording of what I mean

Screen.Recording.2023-12-24.at.17.05.58.mov

Harpoon simply opens a buffer. It doesn't do anything special. If there's something special you wish to have happened upon opening a file, you need to override the configuration, check out the select command on the default list config

@ThePrimeagen thanks for the reply! I solved it by doing this:

harpoon:setup({
  ufo = {
    select = function(list_item, list, option)
      local bufnr = vim.fn.bufnr(list_item.value)
      local set_position = false

      if bufnr == -1 then
        set_position = true
        bufnr = vim.fn.bufnr(list_item.value, true)
      end
      if not vim.api.nvim_buf_is_loaded(bufnr) then
        vim.fn.bufload(bufnr)
        vim.api.nvim_set_option_value('buflisted', true, {
          buf = bufnr,
        })
      end

      vim.api.nvim_set_current_buf(bufnr)

      if set_position then
        vim.api.nvim_win_set_cursor(0, {
          list_item.context.row or 1,
          list_item.context.col or 0,
        })
      end

      vim.cmd('UfoEnableFold') -- Custom behavior
    end
  }
})

Basically I head to the default config, copy/pasta it, and added the vim.cmd('UfoEnableFold') at the end. Would that be a correct approach? What I don't like about this is that if the default config changes I won't get any of that :/

Also, I don't see a way to leverage items from this list on the :extend() method:

harpoon:extend({
  UI_CREATE = function(cx)
    vim.keymap.set(
      'n',
      '<C-v>',
      function() harpoon.ui:select_menu_item({vsplit = true}) end, -- How can I select items from the 'ufo' list here?
      {desc = 'Open as vertical split', buffer = cx.bufnr}
    )
    vim.keymap.set(
      'n',
      '<C-x>',
      function() harpoon.ui:select_menu_item({split = true}) end,
      {desc = 'Open as split', buffer = cx.bufnr}
    )
    vim.keymap.set(
      'n',
      '<C-t>',
      function() harpoon.ui:select_menu_item({tabedit = true}) end,
      {desc = 'Open in new tab', buffer = cx.bufnr}
    )
  end,
})

What do you need for me to extend harpoon for you? I could add a new method

Logger:log(

I just have to add a new event in this function that emits out the buffer, I believe

I'm not really sure what'd be the right approach, not that experienced on vim plugin development ๐Ÿ˜… I think I'm ok with how it got solved, but in essence all I need is for ufo to be enabled every time I enter a buffer with harpoon. I figured this can also be done with an autocommand, but (to the best of my knowledge) that'd be triggered on every BufEnter, not just harpooned files.

Maybe a BufEnter method on the list config is an valid option?

@ThePrimeagen awesome, works like a charm โœจ thanks a lot for adding this!

Could it maybe have broken the other events being emitted with :extend? For example, opening files with vsplit ain't working anymore.

How my config looks:

harpoon:extend(extensions.builtins.command_on_nav('UfoEnableFold'))
harpoon:extend({
  UI_CREATE = function(cx)
    vim.keymap.set(
      'n',
      '<C-v>',
      function()
        harpoon.ui:select_menu_item({vsplit = true})
      end,
      {desc = 'Open as vertical split', buffer = cx.bufnr}
    )
    vim.keymap.set(
      'n',
      '<C-x>',
      function() harpoon.ui:select_menu_item({split = true}) end,
      {desc = 'Open as split', buffer = cx.bufnr}
    )
    vim.keymap.set(
      'n',
      '<C-t>',
      function() harpoon.ui:select_menu_item({tabedit = true}) end,
      {desc = 'Open in new tab', buffer = cx.bufnr}
    )
  end,
  -- Also tried using NAVIGATE event here instead of extensions.builtins, same luck
})

Logger shows this:

config_default#select { context = { col = 0, row = 1 }, value = "nvim/lua/plugins/harpoon.lua" } __harpoon_files
ui#toggle_quick_menu#opening __harpoon_files
ui#select_menu_item selecting item 2 from { "nvim/lua/plugins/harpoon.lua", "nvim/lua/plugins/oil.lua" } options { vsplit = true }
config_default#select { context = { col = 0, row = 1 }, value = "nvim/lua/plugins/oil.lua" } __harpoon_files { vsplit = true }
toggle by BufLeave
ui#toggle_quick_menu#closing
ui#close_menu name: __harpoon_files win and bufnr { bufnr = 5, win = 1002 }
ui#close_menu name: nil win and bufnr {}

Never mind, nuked harpoon and restarted everything and now it works. @ThePrimeagen thanks a lot again! By the way I love your stuff man, you are the reason I started learning vim this year so I have a lot to be thankful from you ๐Ÿ™