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

Harpoon Setup #589

Closed
amalavet opened this issue Jun 21, 2024 · 2 comments
Closed

Harpoon Setup #589

amalavet opened this issue Jun 21, 2024 · 2 comments
Assignees
Labels
discussion A conversation
Milestone

Comments

@amalavet
Copy link

amalavet commented Jun 21, 2024

Just thought if anyone wanted to have pinned buffers map to harpoon, this would be helpful

return {
	"romgrk/barbar.nvim",
	dependencies = { "ThePrimeagen/harpoon" },
	config = function()
		local barbar = require("barbar")
		local state = require("barbar.state")
		local render = require("barbar.ui.render")
		local harpoon = require("harpoon")

		barbar.setup({
			hide = {
				inactive = true,
			},
			icons = {
				pinned = { filename = true, buffer_index = true },
				diagnostics = { { enabled = true } },
			},
		})

		local function unpin_all()
			for _, buf in ipairs(state.buffers) do
				local data = state.get_buffer_data(buf)
				data.pinned = false
			end
		end

		local function get_buffer_by_mark(mark)
			for _, buf in ipairs(vim.api.nvim_list_bufs()) do
				local buffer_path = vim.api.nvim_buf_get_name(buf)

				if buffer_path == "" or mark.value == "" then
					goto continue
				end

				local mark_pattern = mark.value:gsub("([%(%)%.%%%+%-%*%?%[%]%^%$])", "%%%1")
				if string.match(buffer_path, mark_pattern) then
					return buf
				end

				local buffer_path_pattern = buffer_path:gsub("([%(%)%.%%%+%-%*%?%[%]%^%$])", "%%%1")
				if string.match(mark.value, buffer_path_pattern) then
					return buf
				end

				::continue::
			end
		end

		local function refresh_all_harpoon_tabs()
			local list = harpoon:list()
			unpin_all()
			for _, mark in ipairs(list.items) do
				local buf = get_buffer_by_mark(mark)
				if buf == nil then
					vim.cmd("badd " .. mark.value)
					buf = get_buffer_by_mark(mark)
				end
				if buf ~= nil then
					state.toggle_pin(buf)
				end
			end
			render.update()
		end

		vim.api.nvim_create_autocmd({ "BufEnter", "BufAdd", "BufLeave", "User" }, {
			callback = refresh_all_harpoon_tabs,
		})
	end,
}
@amalavet
Copy link
Author

amalavet commented Jun 28, 2024

One other note, I have my harpoon add call the "User" autocmd to update the tabs

		vim.keymap.set("n", "<leader>pa", function()
			harpoon:list():add()
			vim.cmd(":do User")
		end, { desc = "Harpoon: Add file" })

@isseii10
Copy link

It might be better not to use ipairs to handle harpoon item deletion.
ipairs stops at nil value.

  local function refresh_all_harpoon_tabs()
    local list = harpoon:list()
    unpin_all()

    for i = 1, list:length() do
      local mark = list.items[i]
      if mark == nil then
        goto continue
      end

      local buf = get_buffer_by_mark(mark)
      if buf == nil then
        vim.cmd("badd " .. mark.value)
        buf = get_buffer_by_mark(mark)
      end
      if buf ~= nil then
        state.toggle_pin(buf)
      end

      ::continue::
    end
    render.update()
  end

@Iron-E Iron-E added this to the unplanned milestone Sep 2, 2024
@Iron-E Iron-E added the discussion A conversation label Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion A conversation
Projects
None yet
Development

No branches or pull requests

3 participants