Skip to content

Snacks.nvim picker for Harpoon (Code already done! Just don't know where it fits here) #696

@weisbrja

Description

@weisbrja

I wrote a picker for Snacks.nvim similar to the one for Telescope and thought it might be nice to add this to Harpoon directly.
It doesn't support editing the Harpoon entries like the Telescope picker since I never used this feature anyways.

I'm creating this issue since Snacks.nvim doesn't really have a similar extension mechanism like Telescope and I don't quite know where my picker would fit here.

Anyway, here's the code for the picker source:

local harpoon_source = {
	finder = function()
		local output = {}
		for _, item in ipairs(require("harpoon"):list().items) do
			if item and item.value:match("%S") then
				table.insert(output, {
					text = item.value,
					file = item.value,
					pos = { item.context.row, item.context.col },
				})
			end
		end
		return output
	end,
	filter = {
		transform = function()
			return true
		end,
	},
	format = function(item)
		return {
			{ item.text },
			{ ":", "SnacksPickerDelim" },
			{ tostring(item.pos[1]), "SnacksPickerRow" },
			{ ":", "SnacksPickerDelim" },
			{ tostring(item.pos[2]), "SnacksPickerCol" },
		}
	end,
	preview = function(ctx)
		if Snacks.picker.util.path(ctx.item) then
			return Snacks.picker.preview.file(ctx)
		else
			return Snacks.picker.preview.none(ctx)
		end
	end,
	confirm = "jump",
}

To use it with Snacks.nvim, simply set the following options and keybinds:

opts = {
	picker = {
		enabled = true,
		sources = { harpoon = harpoon_source },
	}
},
keys = {
		{
			"<Leader>H",
			function()
				Snacks.picker.pick("harpoon")
			end,
			desc = "Harpoon Marks",
		},
}

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