Skip to content

Seems the line isn't updated when harpoon status changes #211

Open
@theherk

Description

@theherk

Using the harpoon sorter as given in the wiki, doesn't update on change. After changing the harpoon files list, one must switch buffers for the update to have effect. Or I'm missing something, but I can't sort out what. You can see my configuration here and here, but the sorter is:

function M.harpoon_sorter()
  local cache = {}
  local setup = false
  local function marknum(buf, force)
    local b = cache[buf.number]
    if b == nil or force then
      ---@diagnostic disable-next-line: undefined-field
      local path = require("plenary.path"):new(buf.path):make_relative(vim.uv.cwd())
      for i, mark in ipairs(Harpoon:list():display()) do
        if mark == path then
          b = i
          cache[buf.number] = b
          break
        end
      end
    end
    return b
  end
  -- Use this in `config.bufs.new_bufs_position`
  return function(a, b)
    -- Only run this if harpoon is loaded, otherwise just use the default sorting.
    -- This could be used to only run if a user has harpoon installed, but
    -- I'm mainly using it to avoid loading harpoon on UiEnter.
    local has_harpoon = package.loaded["harpoon"] ~= nil
    if not has_harpoon then
      ---@diagnostic disable-next-line: undefined-field
      return a._valid_index < b._valid_index
    elseif not setup then
      local refresh = function() cache = {} end
      Harpoon:extend({
        ADD = refresh,
        REMOVE = refresh,
        REORDER = refresh,
        LIST_CHANGE = refresh,
      })
      setup = true
    end
    -- switch the a and b._valid_index to place non-harpoon bufs on the left
    -- side of the tabline - this puts them on the right.
    local ma = marknum(a)
    local mb = marknum(b)
    if ma and not mb then
      return true
    elseif mb and not ma then
      return false
    elseif ma == nil and mb == nil then
      ma = a._valid_index
      mb = b._valid_index
    end
    return ma < mb
  end
end

I should clarify that it does work when using the harpoon interface, but I mean when using the :add() and :remove() methods. That's when it doesn't update until switching buffers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions