Home

Awesome

Grapple.nvim

<!-- panvimdoc-ignore-start --> <img width="1080" alt="image" src="https://github.com/cbochs/grapple.nvim/assets/2467016/1b350ddf-78f2-4457-899b-5b3cdeade01e">

Theme: kanagawa

<details> <summary>Showcase</summary>

grapple-showcase

</details> <!-- panvimdoc-ignore-end -->

Introduction

Grapple is a plugin that aims to provide immediate navigation to important files. See the quickstart section to get started.

Goals

While Grapple shares similarities to Harpoon (and other file navigation plugins), it aims to differentiate itself in the following ways:

Features

Requirements

Quickstart

-- Lua
vim.keymap.set("n", "<leader>m", require("grapple").toggle)
vim.keymap.set("n", "<leader>M", require("grapple").toggle_tags)

-- User command
vim.keymap.set("n", "<leader>1", "<cmd>Grapple select index=1<cr>")

Next steps

Installation

<details> <summary>lazy.nvim</summary>
{
    "cbochs/grapple.nvim",
    dependencies = {
        { "nvim-tree/nvim-web-devicons", lazy = true }
    },
}
</details> <details> <summary>packer</summary>
use {
    "cbochs/grapple.nvim",
    requires = { "nvim-tree/nvim-web-devicons" }
}
</details> <details> <summary>vim-plug</summary>
Plug "nvim-tree/nvim-web-devicons"
Plug "cbochs/grapple.nvim"
</details>

Example Setups

Note, these examples assume you are using the lazy.nvim package manager.

<details> <summary><b>Recommended</b></summary>
{
    "cbochs/grapple.nvim",
    opts = {
        scope = "git", -- also try out "git_branch"
    },
    event = { "BufReadPost", "BufNewFile" },
    cmd = "Grapple",
    keys = {
        { "<leader>m", "<cmd>Grapple toggle<cr>", desc = "Grapple toggle tag" },
        { "<leader>M", "<cmd>Grapple toggle_tags<cr>", desc = "Grapple open tags window" },
        { "<leader>n", "<cmd>Grapple cycle_tags next<cr>", desc = "Grapple cycle next tag" },
        { "<leader>p", "<cmd>Grapple cycle_tags prev<cr>", desc = "Grapple cycle previous tag" },
    },
},
</details> <details> <summary><b>Harpoon</b></summary>

Example configuration similar to harpoon.nvim (based off of this example setup).

{
    "cbochs/grapple.nvim",
    opts = {
        scope = "git", -- also try out "git_branch"
        icons = false, -- setting to "true" requires "nvim-web-devicons"
        status = false,
    },
    keys = {
        { "<leader>a", "<cmd>Grapple toggle<cr>", desc = "Tag a file" },
        { "<c-e>", "<cmd>Grapple toggle_tags<cr>", desc = "Toggle tags menu" },

        { "<c-h>", "<cmd>Grapple select index=1<cr>", desc = "Select first tag" },
        { "<c-t>", "<cmd>Grapple select index=2<cr>", desc = "Select second tag" },
        { "<c-n>", "<cmd>Grapple select index=3<cr>", desc = "Select third tag" },
        { "<c-s>", "<cmd>Grapple select index=4<cr>", desc = "Select fourth tag" },

        { "<c-s-n>", "<cmd>Grapple cycle_tags next<cr>", desc = "Go to next tag" },
        { "<c-s-p>", "<cmd>Grapple cycle_tags prev<cr>", desc = "Go to previous tag" },
    },
},
</details> <details> <summary><b>Arrow</b></summary>

Example configuration similar to arrow.nvim.

{
    "cbochs/grapple.nvim",
    dependencies = {
        { "nvim-tree/nvim-web-devicons" }
    },
    opts = {
        scope = "git_branch",
        icons = true,
        quick_select = "123456789",
    },
    keys = {
        { ";", "<cmd>Grapple toggle_tags<cr>", desc = "Toggle tags menu" },

        { "<c-s>", "<cmd>Grapple toggle<cr>", desc = "Toggle tag" },
        { "H", "<cmd>Grapple cycle_tags next<cr>", desc = "Go to next tag" },
        { "L", "<cmd>Grapple cycle_tags prev<cr>", desc = "Go to previous tag" },
    },
},
</details>

Settings

The following are the default settings for Grapple. Setup is not required, but settings may be overridden by passing them as table arguments to the Grapple.setup function.

<details> <summary>Default Settings</summary>
require("grapple").setup({
    ---Grapple save location
    ---@type string
    save_path = vim.fs.joinpath(vim.fn.stdpath("data"), "grapple"),

    ---Default scope to use when managing Grapple tags
    ---For more information, please see the Scopes section
    ---@type string
    scope = "git",

    ---User-defined scopes or overrides
    ---For more information about scopes, please see the Scope API section
    ---@type grapple.scope_definition[]
    scopes = {},

    ---Default scopes provided by Grapple
    ---For more information about default scopes, please see the Scopes section
    ---Disable by setting scope to "false". For example, { lsp = false }
    ---@type table<string, grapple.scope_definition | boolean>
    default_scopes = { ... }

    ---Show icons next to tags or scopes in Grapple windows
    ---Requires "nvim-tree/nvim-web-devicons"
    ---@type boolean
    icons = true,

    ---Highlight the current selection in Grapple windows
    ---Also, indicates when a tag path does not exist
    ---@type boolean
    status = true,

    ---Position a tag's name should be shown in Grapple windows
    ---@type "start" | "end"
    name_pos = "end",

    ---How a tag's path should be rendered in Grapple windows
    ---  "relative": show tag path relative to the scope's resolved path
    ---  "basename": show tag path basename and directory hint
    ---@type "basename" | "relative"
    style = "relative",

    ---A string of characters used for quick selecting in Grapple windows
    ---An empty string or false will disable quick select
    ---@type string | boolean
    quick_select = "123456789",

    ---Default command to use when selecting a tag
    ---@type fun(path: string)
    command = vim.cmd.edit,

    ---Time limit used for pruning unused scope (IDs). If a scope's save file
    ---modified time exceeds this limit, then it will be deleted when a prune
    ---requested. Can be an integer (in seconds) or a string time limit
    ---(e.g. "30d" or "2h" or "15m")
    ---@type integer | string
    prune = "30d",

    ---User-defined tags title function for Grapple windows
    ---By default, uses the resolved scope's ID
    ---@type fun(scope: grapple.resolved_scope): string?
    tag_title = nil,

    ---User-defined scopes title function for Grapple windows
    ---By default, renders "Grapple Scopes"
    ---@type fun(): string?
    scope_title = nil,

    ---User-defined loaded scopes title function for Grapple windows
    ---By default, renders "Grapple Loaded Scopes"
    ---@type fun(): string?
    loaded_title = nil,

    ---Additional window options for Grapple windows
    ---See :h nvim_open_win
    ---@type grapple.vim.win_opts
    win_opts = {
        -- Can be fractional
        width = 80,
        height = 12,
        row = 0.5,
        col = 0.5,

        relative = "editor",
        border = "single",
        focusable = false,
        style = "minimal",

        title = "Grapple", -- fallback title for Grapple windows
        title_pos = "center",
        title_padding = " ", -- custom: adds padding around window title

        -- footer = "", -- disable footer
        footer_pos = "center",
    },
})
</details>

Usage

In general, the API is as follows:

Lua: require("grapple").{method}(...)

Command: :Grapple [method] [opts...]

Where opts in the user command is a list of value arguments and key=value keyword arguments. For example,

:Grapple cycle_tags next scope=cwd

Has the equivalent form

require("grapple").cycle_tags("next", { scope = "cwd" })

Grapple API

<details> <summary>Grapple API and Examples</summary>

Grapple.tag

Create a grapple tag.

Command: :Grapple tag [buffer={buffer}] [path={path}] [index={index}] [name={name}] [scope={scope}]

API: require("grapple").tag(opts)

opts?: grapple.options

Note: only one tag can be created per scope per file. If a tag already exists for the given file or buffer, it will be overridden with the new tag.

<details> <summary><b>Examples</b></summary>
-- Tag the current buffer
require("grapple").tag()

-- Tag a file by its file path
require("grapple").tag({ path = "some_file.lua" })

-- Tag the current buffer in a different scope
require("grapple").tag({ scope = "global" })

-- Tag the file path under the cursor
require("grapple").tag({ path = "<cfile>" })
</details>

Grapple.untag

Remove a Grapple tag.

API: require("grapple").untag(opts)

opts?: grapple.options (one of)

Note: Tag is removed based on one of (in order): index, name, path, buffer

<details> <summary><b>Examples</b></summary>
-- Remove a tag on the current buffer
require("grapple").untag()

-- Remove a tag on a file
require("grapple").untag({ file_path = "{file_path}" })

-- Remove a tag on the current buffer in a different scope
require("grapple").untag({ scope = "global" })
</details>

Grapple.toggle

Toggle a Grapple tag.

API: require("grapple").toggle(opts)

opts?: grapple.options

<details> <summary><b>Examples</b></summary>
-- Toggle a tag on the current buffer
require("grapple").toggle()
</details>

Grapple.select

Select a Grapple tag.

API: require("grapple").select(opts)

opts?: grapple.options (one of)

Note: Tag is selected based on one of (in order): index, name, path, buffer

<details> <summary><b>Examples</b></summary>
-- Select the third tag
require("grapple").select({ index = 3 })
</details>

Grapple.cycle_tags

Cycle through and select the next or previous available tag for a given scope.

API: require("grapple").cycle_tags(direction, opts)

Where:

Note: Starting tag is searched based on one of (in order): index, name, path, buffer

<details> <summary><b>Examples</b></summary>
-- Cycle to the next tagged file
require("grapple").cycle_tags("next")

-- Cycle to the previous tagged file
require("grapple").cycle_tags("prev")
</details>

Grapple.cycle_scopes

Cycle through and use the next or previous available scope. By default, will only cycle through non-hidden scopes. Use { all = true } to cycle through all defined scopes.

API: require("grapple").cycle_scopes(direction, opts)

Where:

<details> <summary><b>Examples</b></summary>
-- Cycle to the next scope
require("grapple").cycle_scopes("next")

-- Cycle to the previous scope
require("grapple").cycle_scopes("prev")

-- Hide a scope during Grapple setup
require("grapple").setup({
    default_scopes = {
        cwd = { hidden = true }
    }
})
</details>

Grapple.unload

Unload tags for a give (scope) name or loaded scope (id).

API: require("grapple").unload(opts)

opts?: table

<details> <summary><b>Examples</b></summary>
-- Unload the current scope
require("grapple").unload()

-- Unload a scope (dynamic)
require("grapple").unload({ scope = "git" })

-- Unload a specific resolved scope ID
require("grapple").unload({ id = "~/git" })
</details>

Grapple.reset

Reset tags for a given (scope) name or loaded scope (id).

API: require("grapple").reset(opts)

opts?: table

<details> <summary><b>Examples</b></summary>
-- Reset the current scope
require("grapple").reset()

-- Reset a scope (dynamic)
require("grapple").reset({ scope = "git" })

-- Reset a specific resolved scope ID
require("grapple").reset({ id = "~/git" })
</details>

Grapple.prune

Prune save files based on their last modified time.

API: require("grapple").prune(opts)

opts?: table

<details> <summary><b>Examples</b></summary>
-- Prune using the default time limit
require("grapple").prune()

-- Prune longer than 30 days
require("grapple").prune({ limit = "30d" })

-- Prune longer than 6 hours
require("grapple").prune({ limit = "6h" })

-- Prune longer than 15 minutes
require("grapple").prune({ limit = "15m" })

-- Prune longer than 120 seconds
require("grapple").prune({ limit = "120s" })
require("grapple").prune({ limit = 120 })
</details>

Grapple.quickfix

Open the quickfix window populated with paths from a given scope

API: require("grapple").quickfix(opts)

opts?: table

<details> <summary><b>Examples</b></summary>
-- Open the quickfix window for the current scope
require("grapple").quickfix()

-- Open the quickfix window for a specified scope
require("grapple").quickfix("global")
</details>

Grapple.exists

Return if a tag exists. Used for statusline components

API: require("grapple").exists(opts)

returns: boolean

opts?: grapple.options (one of)

Note: Tag is searched based on one of (in order): index, name, path, buffer

<details> <summary><b>Examples</b></summary>
-- Check whether the current buffer is tagged or not
require("grapple").exists()

-- Check for a tag in a different scope
require("grapple").exists({ scope = "global" })
</details>

Grapple.find

Search for a tag in a given scope.

API: require("grapple").find(opts)

opts?: grapple.options (one of)

returns: grapple.tag | nil, string? error

Note: Tag is searched based on one of (in order): index, name, path, buffer

<details> <summary><b>Examples</b></summary>
-- Search for a tag by index in the current scope
require("grapple").find({ index = 1 })

-- Search for a named tag in a different scope
require("grapple").find({ name = "bob", scope = "global" })
</details> </details>

Scope API

<details> <summary>Scopes API and Examples</summary>

Grapple.define_scope

Create a user-defined scope.

API: require("grapple").define_scope(definition)

definition: grapple.scope_definition

<details> <summary><b>Examples</b></summary>

For more examples, see settings.lua

-- Define a scope during setup
require("grapple").setup({
    scope = "cwd_branch",

    scopes = {
        {
            name = "cwd_branch",
            desc = "Current working directory and git branch",
            fallback = "cwd",
            cache = {
                event = { "BufEnter", "FocusGained" },
                debounce = 1000, -- ms
            },
            resolver = function()
                local git_files = vim.fs.find(".git", {
                    upward = true,
                    stop = vim.loop.os_homedir(),
                })

                if #git_files == 0 then
                    return
                end

                local root = vim.loop.cwd()

                local result = vim.fn.system({ "git", "symbolic-ref", "--short", "HEAD" })
                local branch = vim.trim(string.gsub(result, "\n", ""))

                local id = string.format("%s:%s", root, branch)
                local path = root

                return id, path
            end,
        }
    }
})

-- Define a scope outside of setup
require("grapple").define_scope({
    name = "projects",
    desc = "Project directory"
    fallback = "cwd",
    cache = { event = "DirChanged" },
    resolver = function()
        local projects_dir = vim.fs.find("projects", {
            upwards = true,
            stop = vim.loop.os_homedir()
        })

        if #projects_dir == 0 then
            return nil, nil, "Not in projects dir"
        end

        local path = projects_dir[1]
        local id = path
        return id, path, nil
    end
})

-- Use the scope
require("grapple").use_scope("projects")
</details>

Grapple.delete_scope

Delete a default or user-defined scope.

API: require("grapple").delete_scope(scope)

scope: string scope name

returns: string? error

Grapple.use_scope

Change the currently selected scope.

API: require("grapple").use_scope(scope)

scope: string scope name

<details> <summary><b>Examples</b></summary>
-- Clear the cached value (if any) for the "git" scope
require("grapple").use_scope("git_branch")
</details> </details>

Tags

A tag is a persistent tag on a file path or URL. It is a means of indicating a file you want to return to. When a file is tagged, Grapple will save your cursor location so that when you jump back, your cursor is placed right where you left off. In a sense, tags are like file-level marks (:h mark).

Once a tag has been added to a scope, it may be selected by index or name, cycled through, or even jumped to using plugins such as portal.nvim.

Scopes

A scope is a means of namespacing tags to a specific project. Scopes are resolved dynamically to produce a unique identifier for a set of tags (i.e. a root directory). This identifier determines where tags are created and deleted. Note, different scopes may resolve the same identifier (i.e. lsp and git scopes may share the same root directory).

Scopes can also be cached. Each scope may define a set of events and/or patterns for an autocommand (:h autocmd), an interval for a timer, or to be cached indefinitely (unless invalidated explicitly). Some examples of this are the cwd scope which only updates on DirChanged.

The following scopes are made available by default:

It is also possible to create your own custom scope. See the Scope API for more information.

<details> <summary><b>Examples</b></summary>
-- Use a builtin scope
require("grapple").setup({
    scope = "git_branch",
})

-- Define and use a custom scope
require("grapple").setup({
    scope = "custom",

    scopes = {
        {
            name = "custom",
            fallback = "cwd",
            cache = { event = "DirChanged" },
            resolver = function()
                local path = vim.env.HOME
                local id = path
                return id, path
            end
        }
    }
})

-- Disable a default scope
-- Note: be careful to disable default scopes that are used as fallbacks
require("grapple").setup({
    default_scopes = {
        lsp = false
    }
})
</details>

Grapple Windows

Popup windows are made available to enable easy management of tags and scopes. The opened buffer is given its own syntax (grapple) and file type (grapple) and can be modified like a regular buffer; meaning items can be selected, modified, reordered, or deleted with well-known vim motions. The floating window can be toggled or closed with either q or <esc>.

Tags Window

<img width="1080" alt="image" src="https://github.com/cbochs/grapple.nvim/assets/2467016/e1fda612-c4f2-4202-9264-c8c6aee68795">

Open a floating window with all the tags for a given scope. This buffer is modifiable. Several actions are available by default:

API:

opts?: table

<details> <summary><b>Examples</b></summary>
-- Open the tags window for the current scope
require("grapple").open_tags()

-- Open the tags window for a different scope
require("grapple").open_tags("global")
</details>

Scopes Window

<img width="1080" alt="image" src="https://github.com/cbochs/grapple.nvim/assets/2467016/6af61cfa-3765-4dbf-a117-d599791e9a74">

Open a floating window with all defined scopes. This buffer is not modifiable. Some basic actions are available by default:

API:

<details> <summary><b>Examples</b></summary>
-- Open the scopes window
require("grapple").open_scopes()
</details>

Loaded Scopes Window

<img width="1080" alt="image" src="https://github.com/cbochs/grapple.nvim/assets/2467016/8b91222f-cf5e-43b9-9286-56379a6a80f0">

Open a floating window with all loaded scope IDs. This buffer is not modifiable. Some basic actions are available by default:

API:

opts?: table

<details> <summary><b>Examples</b></summary>
-- Open the loaded scopes window, show only loaded scopes
require("grapple").open_loaded()

-- Open the loaded scopes window, show both loaded and unloaded scopes
require("grapple").open_loaded({ all = true })
</details>

Window Highlights

HighlightDefault LinkStyleUsed in
GrappleBoldN/Agui=boldScopes window for scope names
GrappleHintCommentN/ATags window for directory hints
GrappleNameDiagnosticHintN/ATags window for tag name
GrappleNoExistDiagnosticErrorN/ATags window for tag status
GrappleCurrentSpecialChargui=boldAll windows for current status
GrappleFloatNormalFloatN/AAll windows for background
GrappleBorderFloatBorderN/AAll windows for border
GrappleTitleFloatTitleN/AAll windows for title
GrappleFooterFloatFooterN/AAll windows for footer

Persistent State

Grapple saves all scopes to a common directory. The default directory is named grapple and lives in Neovim's "data" directory (:h standard-path). Each scope will be saved as its own individually serialized JSON blob. No scopes are loaded on startup and will are instead loaded on demand.

Integrations

Telescope

You can use telescope.nvim to search through your tagged files instead of the built in popup windows.

Load the extension with

require("telescope").load_extension("grapple")

Then use this command to see the grapple tags for the project in a telescope window

:Telescope grapple tags

Statusline

A statusline component can be easily added to show whether a buffer is tagged.

API:

opts?: grapple.statusline.options (default: settings.statusline)

Also available:

<details> <summary><b>Examples</b></summary>
require("grapple").statusline()
-- Returns "󰛢 [1] 2  3  4"

require("grapple").name_or_index()
-- Returns "1" or "bob"

-- Modify the statusline options
require("grapple").setup({
    statusline = {
        icon = "G",
        active = "|%s|",
        inactive = " %s "
    }
})
</details> <!-- panvimdoc-ignore-start -->

Lualine Component

<table> <tr> <td> Snippet </td> <td> Screenshot </td> </tr> <tr> <td>
require("lualine").setup({
    sections = {
        lualine_b = { "grapple" }
    }
})
</td> <td><img width="300" alt="image" src="https://github.com/cbochs/grapple.nvim/assets/2467016/3b875b99-1a39-43f9-88c7-37814db86184"></td> </tr> <tr> <td>
require("lualine").setup({
    sections = {
        lualine_b = {
            {
                function()
                    return require("grapple").name_or_index()
                end,
                cond = function()
                    return package.loaded["grapple"] and require("grapple").exists()
                end
            }
        }
    }
})
</td> <td><img width="300" alt="image" src="https://github.com/cbochs/grapple.nvim/assets/2467016/d10f4359-9463-4fb8-b131-f7867e4c8fcc"></td> </tr> </table> <!-- panvimdoc-ignore-start -->

Grapple Types

<details open> <summary>Type Definitions</summary>

grapple.options

Options available for most top-level tagging actions (e.g. tag, untag, select, toggle, etc).

Type: table

grapple.tag

Data object for a tagged file.

Type: table

grapple.cache.options

Options available for defining how a scope should be cached. Using the value of true will indicate a value should be cached indefinitely and is equivalent to providing an empty set of options ({}).

Type: table | boolean

grapple.scope_definition

Used for defining new scopes.

Type: table

Note: Scopes are given a priority based on their fallback ordering. By default, scopes without a fallback are given a priority of 1000; scopes with a fallback, but are also fallbacks themselves, are given a priority of 100; and all other scopes are given a priority of 1. Higher priority scopes are loaded first. This can be overridden by setting a scope's priority manually in the settings.

grapple.scope_resolver

Used for defining new scopes. Must return a tuple of (id, path, err). If successful, an id must be provided with an optional absolute path path. If unsuccessful, id must be nil with an optional err explaining what when wrong.

Type: function

Returns: string? id, string? path, string? err

grapple.resolved_scope

Result from observing a scope at a point in time.

Type class

</details> <!-- panvimdoc-ignore-start -->

Contributors

Thanks to these wonderful people for their contributions!

<a href="https://github.com/cbochs/grapple.nvim/graphs/contributors"> <img src="https://contrib.rocks/image?repo=cbochs/grapple.nvim" /> </a>

Inspiration and Thanks

<!-- panvimdoc-ignore-end -->