Skip to content

Cookbook

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

Cookbook

Common recipes for FerrisPad plugin development.

Show a Toast Notification

function M:on_document_save(api, path, content)
    return {
        status_message = {
            level = "info",
            text = "File saved: " .. (path or "untitled")
        }
    }
end

Run a Linter and Show Diagnostics

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

    local result = api:run_command("ruff", "check", "--output-format=concise", path)
    local diagnostics = {}

    for line_str, msg in result.stdout:gmatch(":(%d+):%d+: (.-)%\n") do
        table.insert(diagnostics, {
            line = tonumber(line_str),
            message = msg,
            level = "warning"
        })
    end

    return { diagnostics = diagnostics }
end

Format on Save

function M:on_document_save(api, path, content)
    if not path or not path:match("%.rs$") then return end
    if not api:command_exists("rustfmt") then return end

    local result = api:run_command("rustfmt", "--edition", "2024")
    if result.success then
        -- Read the formatted file
        local formatted, err = api:read_file(path)
        if formatted then
            return { modified_content = formatted }
        end
    end
end

Open a File from a Tree View Click

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 is the array of labels from root to clicked node
        -- Reconstruct the file path from your tree structure
        local file_path = reconstruct_path(data.node_path)
        return { open_file = file_path }
    end
end

Check Language Before Processing

function M:on_document_save(api, path, content)
    local lang = api:get_language()
    if lang ~= "Python" then return end
    -- Python-specific logic
end

Or check by file extension:

local ext = api:get_file_extension()
if ext ~= "py" and ext ~= "pyi" then return end

List Project Files in a Tree View

function M:on_menu_action(api, action, path, content)
    if action ~= "show_files" then return end

    local root = api:get_project_root()
    if not root then return end

    local files = api:scan_dir(root, 3)
    if not files then return end

    local tree_root = { label = root:match("([^/]+)$"), children = {} }
    for _, f in ipairs(files) do
        if not f.is_dir then
            table.insert(tree_root.children, {
                label = f.rel_path,
                data = root .. "/" .. f.rel_path,
                icon = "file"
            })
        end
    end

    return {
        tree_view = {
            title = "Project Files",
            root = tree_root,
            on_click = "open_file",
            click_mode = "double"
        }
    }
end

Conditional Menu Action Based on Config

function M:on_document_lint(api, path, content)
    local lint_on_save = api:get_config_bool("lint_on_save")
    if not lint_on_save then return end
    -- proceed with linting
end

function M:on_menu_action(api, action, path, content)
    if action == "lint" then
        -- manual lint always runs, regardless of config
        return do_lint(api, path, content)
    end
end

Copy to Clipboard

function M:on_menu_action(api, action, path, content)
    if action == "copy_path" then
        local file_path = api:get_file_path()
        if file_path then
            return { clipboard_text = file_path }
        end
    end
end

Show Git Status with Colors

local status = api:git_status(".")
if status then
    local children = {}
    for file, code in pairs(status) do
        local color = nil
        if code == "M" then color = "modified"
        elseif code == "A" then color = "added"
        elseif code == "??" then color = "untracked"
        elseif code == "U" then color = "conflict"
        end
        table.insert(children, {
            label = file .. " [" .. code .. "]",
            data = file,
            icon = "file",
            label_color = color
        })
    end
    return {
        tree_view = {
            title = "Git Status",
            root = { label = "Changes", children = children },
            on_click = "open_file"
        }
    }
end

Clone this wiki locally