From 574adeb96d1972fbd1b6fe0011ba84d2a06f8da6 Mon Sep 17 00:00:00 2001 From: mpaulson Date: Thu, 30 Nov 2023 20:07:28 -0700 Subject: [PATCH] fix: small fixes --- README.md | 56 ++++++++++++++++++++++++++++++++++++++++++ lua/harpoon/config.lua | 16 ++++++------ lua/harpoon/ui.lua | 5 ++-- 3 files changed, 68 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 47ffeb6..7111271 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,62 @@ vim.keymap.set("n", "", function() harpoon:list():select(3) end) vim.keymap.set("n", "", function() harpoon:list():select(4) end) ``` +### Custom Lists +You can define custom behavior of a harpoon list by providing your own calls. + +Here is a simple example where i create a list named `cmd` that takes the +current line in the editor and adds it to harpoon menu. When +`list:select(...)` is called, we take the contents of the line and execute it +as a vim command + +I don't think this is a great use of harpoon, but its meant to show how to add +your own custom lists. You could imagine that a terminal list would be just as +easy to create. + +```lua +local harpoon = require("harpoon") + +harpoon:setup({ + -- Setting up custom behavior for a list named "cmd" + "cmd" = { + + -- When you call list:append() 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 option any + select = function(list_item, option) + -- WOAH, IS THIS HTMX LEVEL XSS ATTACK?? + vim.cmd(list_item.value) + end + + } +}) + +``` + ## ⇁ Social For questions about Harpoon, there's a #harpoon channel on [the Primeagen's Discord](https://discord.gg/theprimeagen) server. * [Discord](https://discord.gg/theprimeagen) diff --git a/lua/harpoon/config.lua b/lua/harpoon/config.lua index 54d7a77..8254ff9 100644 --- a/lua/harpoon/config.lua +++ b/lua/harpoon/config.lua @@ -2,6 +2,7 @@ local M = {} ---@alias HarpoonListItem {value: any, context: any} ---@alias HarpoonListFileItem {value: string, context: {row: number, col: number}} +---@alias HarpoonListFileOptions {split: boolean, vsplit: boolean} ---@class HarpoonPartialConfigItem ---@field encode? (fun(list_item: HarpoonListItem): string) @@ -74,18 +75,19 @@ function M.get_default_config() return list_item.value end, - ---@param file_item HarpoonListFileItem - select = function(file_item, options) + ---@param list_item HarpoonListFileItem + ---@param options HarpoonListFileOptions + select = function(list_item, options) options = options or {} - if file_item == nil then + if list_item == nil then return end - local bufnr = vim.fn.bufnr(file_item.value) + local bufnr = vim.fn.bufnr(list_item.value) local set_position = false if bufnr == -1 then set_position = true - bufnr = vim.fn.bufnr(file_item.value, true) + bufnr = vim.fn.bufnr(list_item.value, true) end if options.vsplit then @@ -100,8 +102,8 @@ function M.get_default_config() if set_position then vim.api.nvim_win_set_cursor(0, { - file_item.context.row or 1, - file_item.context.col or 0, + list_item.context.row or 1, + list_item.context.col or 0, }) end end, diff --git a/lua/harpoon/ui.lua b/lua/harpoon/ui.lua index 1543d3f..6e42709 100644 --- a/lua/harpoon/ui.lua +++ b/lua/harpoon/ui.lua @@ -108,7 +108,8 @@ function HarpoonUI:toggle_quick_menu(list) vim.api.nvim_buf_set_lines(self.bufnr, 0, -1, false, contents) end -function HarpoonUI:select_menu_item() +---@param options? any +function HarpoonUI:select_menu_item(options) local idx = vim.fn.line(".") -- must first save any updates potentially made to the list before @@ -116,7 +117,7 @@ function HarpoonUI:select_menu_item() local list = Buffer.get_contents(self.bufnr) self.active_list:resolve_displayed(list) - self.active_list:select(idx) + self.active_list:select(idx, options) self:close_menu() end