harpoon icon indicating copy to clipboard operation
harpoon copied to clipboard

API Docs Are Wrong

Open dragonlobster opened this issue 8 months ago • 0 comments

The API docs state that you can customize your add function by using the following line of code:

local harpoon = require("harpoon")

harpoon:setup({
    -- Setting up custom behavior for a list named "cmd"
    "cmd" = {

        -- When you call list:add() this function is called and the return
        -- value will be put in the list at the end.
        --
        -- which means same behavior for prepend except where in the list the
        -- return value is added
        --
        -- @param possible_value string only passed in when you alter the ui manual
        add = function(possible_value)
            -- get the current line idx
            local idx = vim.fn.line(".")

            -- read the current line
            local cmd = vim.api.nvim_buf_get_lines(0, idx - 1, idx, false)[1]
            if cmd == nil then
                return nil
            end

            return {
                value = cmd,
                context = { ... any data you want ... },
            }
        end,

        --- This function gets invoked with the options being passed in from
        --- list:select(index, <...options...>)
        --- @param list_item {value: any, context: any}
        --- @param list { ... }
        --- @param option any
        select = function(list_item, list, option)
            -- WOAH, IS THIS HTMX LEVEL XSS ATTACK??
            vim.cmd(list_item.value)
        end

    }
})

Firstly this is not proper lua code, the dictionary key should be ["cmd"].

Secondly, the function you're supposed to customize is create_list_item, not add. Just look at config.lua line 188:

---@param config HarpoonPartialConfigItem
            ---@param name? any
            ---@return HarpoonListItem
            create_list_item = function(config, name)
                name = name
                    or normalize_path(
                        vim.api.nvim_buf_get_name(
...

I would create a PR but it seems like PRs on docs don't get merged. The fact the only example of API in the README doesn't even work is frustrating.

dragonlobster avatar Jun 16 '24 23:06 dragonlobster