ThePrimeagen/harpoon

[Harpoon2] Remove files from list when using telescope picker (and possibly also reorder them..?)

Opened this issue · 4 comments

What issue are you having that you need harpoon to solve?
It would be nice to use the Telescope picker to also remove files from the list,
and possibly also perform some other list operations, such as rearrange file order.

Why doesn't the current config help?
Currently it is only possible to remove a specific file from a harpoon list with require('harpoon'):list():remove() if the file is the currently opened buffer, or editing the list via harpoon.ui

What proposed api changes are you suggesting?
It would be nice being able to call the remove function specifying an index, something like require('harpoon'):list():remove(n). This would make it possible to write more complex telescope pickers with added functionality

I believe this already exists, as can be seen in lua/telescope/_extensions/marks.lua, with the default keybindings seen below:

return function(opts)
    opts = opts or {}

    pickers
        .new(opts, {
            prompt_title = "harpoon marks",
            finder = generate_new_finder(),
            sorter = conf.generic_sorter(opts),
            previewer = conf.grep_previewer(opts),
            attach_mappings = function(_, map)
                map("i", "<c-d>", delete_harpoon_mark)
                map("n", "<c-d>", delete_harpoon_mark)

                map("i", "<c-p>", move_mark_up)
                map("n", "<c-p>", move_mark_up)

                map("i", "<c-n>", move_mark_down)
                map("n", "<c-n>", move_mark_down)
                return true
            end,
        })
        :find()
end

I mean, not really.
The telescope extension you suggested is for marks, not for harpoon files.
What I'm talking about is the telescope picker found in the README for Harpoon2

local harpoon = require('harpoon')
harpoon:setup({})

-- basic telescope configuration
local conf = require("telescope.config").values
local function toggle_telescope(harpoon_files)
    local file_paths = {}
    for _, item in ipairs(harpoon_files.items) do
        table.insert(file_paths, item.value)
    end

    require("telescope.pickers").new({}, {
        prompt_title = "Harpoon",
        finder = require("telescope.finders").new_table({
            results = file_paths,
        }),
        previewer = conf.file_previewer({}),
        sorter = conf.generic_sorter({}),
    }):find()
end

vim.keymap.set("n", "<C-e>", function() toggle_telescope(harpoon:list()) end,
    { desc = "Open harpoon window" })

I tried the telescope extension you suggested and it works, providing most of the functionality I requested, but then, why is the code above mentioned in the README section for Telescope?

I mean, not really. The telescope extension you suggested is for marks, not for harpoon files. What I'm talking about is the telescope picker found in the README for Harpoon2
...

I don't think that there's a difference, in your context!?

I tried the telescope extension you suggested and it works, providing most of the functionality I requested, but then, why is the code above mentioned in the README section for Telescope?

I think it's just meant as an example, not a solution!?

You could try out PR #512! Maybe it behaves a little more as you'd expect?! It sounds like, it does everything that you mentioned here!?

Also, you can use require("harpoon"):list():remove_at(n), to remove an entry in the harpoon-list ;)

This should work

local function toggle_telescope(harpoon_files)
    local finder = function()
        local paths = {}
        for _, item in ipairs(harpoon_files.items) do
            table.insert(paths, item.value)
        end

        return require("telescope.finders").new_table({
            results = paths,
        })
    end

    require("telescope.pickers").new({}, {
        prompt_title = "Harpoon",
        finder = finder(),
        previewer = false,
        sorter = require("telescope.config").values.generic_sorter({}),
        layout_config = {
            height = 0.4,
            width = 0.5,
            prompt_position = "top",
            preview_cutoff = 120,
        },
        attach_mappings = function(prompt_bufnr, map)
            map("i", "<C-d>", function()
                local state = require("telescope.actions.state")
                local selected_entry = state.get_selected_entry()
                local current_picker = state.get_current_picker(prompt_bufnr)

                table.remove(harpoon_files.items, selected_entry.index)
                current_picker:refresh(finder())
            end)
            return true
        end,
    }):find()
end