Skip to content

Widget API

Federico Conticello edited this page Mar 25, 2026 · 2 revisions

Widget API

Plugins can display rich UI by returning widget requests from hooks. Three widget types are available: split views, tree views, and terminal views.

Split View

A two-pane panel for displaying diffs, comparisons, or side-by-side content.

return {
    split_view = {
        title = "Git Diff",
        left = {
            content = original_text,
            label = "Original",
            line_numbers = true,
            read_only = true,
            highlights = {
                { line = 5, color = "removed" },
                { line = 10, color = "modified", spans = { { start = 0, end = 12 } } }
            }
        },
        right = {
            content = modified_text,
            label = "Modified",
            line_numbers = true,
            read_only = false,  -- allow editing
            highlights = {
                { line = 5, color = "added" },
                { line = 10, color = "modified", spans = { { start = 0, end = 15 } } }
            }
        },
        actions = {
            { label = "Accept", action = "accept" },
            { label = "Revert", action = "revert" }
        },
        display_mode = "panel"  -- "panel" (default) or "tab"
    }
}

Highlight Colors

  • "added" — green
  • "removed" — red
  • "modified" — yellow
  • {r, g, b} — custom RGB color (e.g., {255, 128, 0})

Intraline Spans

Within a highlighted line, spans marks specific character ranges. Each span has start (inclusive, 0-indexed) and end (exclusive, 0-indexed).

Actions

Action buttons appear below the split view. When clicked, on_widget_action is called with widget_type = "split_view" and the action name. For the "accept" action, data.right_content contains the (possibly edited) right pane content.


Tree View

A hierarchical tree panel for file browsers, search results, or structured data.

return {
    tree_view = {
        title = "File Explorer",
        root = {
            label = "src",
            data = "/project/src",
            expanded = true,
            icon = "folder",
            children = {
                {
                    label = "main.rs",
                    data = "/project/src/main.rs",
                    icon = "file",
                    label_color = "modified"
                },
                {
                    label = "lib.rs",
                    data = "/project/src/lib.rs",
                    icon = "file"
                }
            }
        },
        on_click = "open_file",
        click_mode = "double",   -- "single" or "double" (default)
        expand_depth = 1,        -- 0 = collapsed, -1 = expand all
        persistent = true,       -- keep visible across tab switches
        context_path = "/project",
        context_menu = {
            {
                label = "New File",
                action = "new_file",
                target = "folder",           -- show only on folders
                input_prompt = "File name:"  -- show input dialog
            },
            {
                label = "Delete",
                action = "delete",
                target = "all",
                confirm_prompt = "Delete this item?"
            },
            {
                label = "Copy Path",
                target = "all",
                clipboard = true             -- copies node path, no action sent
            }
        }
    }
}

Node Properties

Field Type Description
label string Display text
data string Payload (file path, URL, metadata)
children array Child nodes (makes this a folder)
expanded boolean Initial expand state
icon string Hint: "file", "folder", "error", etc.
label_color string Semantic color: "modified", "added", "untracked", "conflict", "ignored"

Alternative: YAML Content

Instead of building a tree programmatically, you can pass YAML:

return {
    tree_view = {
        title = "Config Viewer",
        yaml_content = api:get_text()
    }
}

Context Menu Targets

Target Shows on
"folder" Nodes with children
"file" Leaf nodes
"empty" Empty area (right-click on background)
"all" Everything

Terminal View

An embedded PTY-backed terminal panel.

return {
    terminal_view = {
        title = "Claude Code",
        command = "claude",
        persistent = true
    }
}
Field Type Default Description
title string required Panel title
command string required Command to run (must be in [permissions] execute)
args array {} Command arguments
working_dir string project root Working directory
persistent boolean false Keep alive across tab switches

The terminal supports ANSI escape sequences (SGR colors, reverse video) and auto-resizes when the panel is resized.

Security Requirements

Terminal view commands go through the same permission gate as run_command():

  • command is required — omitting it (default shell) is blocked. No plugin can open a raw $SHELL.
  • Command must be declared in [permissions] execute in plugin.toml
  • Command must be approved by the user (same approval flow as run_command)
  • Command name is validated against shell metacharacters

If the command is not approved, the terminal is not opened and a toast error is shown.


Handling Widget Actions

When the user interacts with a widget (clicks a button, selects a tree node), the on_widget_action hook is called:

function M:on_widget_action(api, widget_type, action, session_id, data)
    if widget_type == "tree_view" and action == "open_file" then
        -- data.node_path contains the clicked node's path array
        local file_path = data.content  -- or construct from node_path
        return { open_file = file_path }
    end

    if widget_type == "split_view" and action == "accept" then
        local new_content = data.right_content
        -- write the accepted content
    end

    if widget_type == "tree_view" and action == "new_file" then
        local filename = data.input_text  -- from input_prompt
        -- create the file
    end
end

Widget Action Data

Field Available for Description
right_content split_view accept Edited right pane content
node_path tree_view click Array of labels from root to clicked node
input_text tree_view context menu User input from input_prompt
content all Current buffer content
target_path tree_view drag-drop Drop target node path

Clone this wiki locally