Skip to content

Hooks Reference

Federico Conticello edited this page Mar 24, 2026 · 1 revision

Hooks Reference

Hooks are methods on your plugin table that FerrisPad calls when events occur. All hooks are synchronous and blocking — there is no async or threading.

Every hook receives self (the plugin table) and api (the editor API object) as the first two arguments.

Lifecycle Hooks

init(api)

Called once when the plugin is loaded.

function M:init(api)
    api:log("Plugin loaded")
    -- initialize state, check for tools, etc.
end

shutdown(api)

Called when FerrisPad is shutting down. Use for cleanup.

function M:shutdown(api)
    api:log("Plugin shutting down")
end

Document Hooks

on_document_open(api, path)

Called after a document is opened. path is nil for untitled documents.

function M:on_document_open(api, path)
    if path and path:match("%.py$") then
        api:log("Python file opened: " .. path)
    end
end

on_document_save(api, path, content)

Called before the document is saved to disk. The plugin can modify the content by returning modified_content.

function M:on_document_save(api, path, content)
    -- Add trailing newline if missing
    if not content:match("\n$") then
        return { modified_content = content .. "\n" }
    end
end

on_document_close(api, path)

Called when a document is closed. path is nil for untitled documents.

on_text_changed(api, position, inserted_len, deleted_len)

Called after text is edited, debounced by 300ms. Multiple rapid edits are batched into one call.

Parameter Type Description
position integer Byte offset of the earliest edit
inserted_len integer Cumulative bytes inserted
deleted_len integer Cumulative bytes deleted
function M:on_text_changed(api, position, inserted_len, deleted_len)
    api:log("Edit at byte " .. position .. ": +" .. inserted_len .. " -" .. deleted_len)
end

Appearance Hooks

on_theme_changed(api, is_dark)

Called when the user toggles dark/light mode. is_dark is a boolean.

function M:on_theme_changed(api, is_dark)
    -- adjust plugin behavior based on theme
end

Analysis Hooks

on_document_lint(api, path, content)

Called after a document is saved, for linting/diagnostics. Return a diagnostics array.

function M:on_document_lint(api, path, content)
    if not path or not path:match("%.py$") then return end

    local result = api:run_command("ruff", "check", "--output-format=json", path)
    if not result.success then return end

    local diagnostics = {}
    -- parse result.stdout into diagnostics
    -- ...
    return { diagnostics = diagnostics }
end

Diagnostic Object

{
    line = 10,                            -- Required: line number (1-indexed)
    message = "F401: os imported unused", -- Required: error message
    level = "error",                      -- Required: "error", "warning", "info", "hint"
    column = 8,                           -- Optional: column (1-indexed)
    fix_message = "Remove unused import", -- Optional: shown in hover tooltip
    url = "https://docs.example.com"      -- Optional: double-click opens in browser
}

on_highlight_request(api, path, content)

Called when the user presses Ctrl+Shift+L. Return line_annotations for visual highlighting.

function M:on_highlight_request(api, path, content)
    return {
        line_annotations = {
            { line = 5, text = "!", color = {255, 0, 0} }
        }
    }
end

Interaction Hooks

on_menu_action(api, action, path, content)

Called when the user clicks a custom menu item defined in plugin.toml.

Parameter Type Description
action string The action field from [[menu_items]]
path string or nil Current file path
content string or nil Current document content
function M:on_menu_action(api, action, path, content)
    if action == "lint" then
        -- run linting
    elseif action == "format" then
        -- run formatting
    end
end

on_widget_action(api, widget_type, action, session_id, data)

Called when the user interacts with a plugin widget. See Widget API for details.

Parameter Type Description
widget_type string "split_view", "tree_view", or "terminal_view"
action string The action name (from widget definition or context menu)
session_id integer Unique widget session identifier
data table Context data (varies by widget type)

Clone this wiki locally