Repository navigation
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.
Called once when the plugin is loaded.
function M:init(api)
api:log("Plugin loaded")
-- initialize state, check for tools, etc.
endCalled when FerrisPad is shutting down. Use for cleanup.
function M:shutdown(api)
api:log("Plugin shutting down")
endCalled 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
endCalled 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
endCalled when a document is closed. path is nil for untitled documents.
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)
endCalled 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
endCalled 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{
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
}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} }
}
}
endCalled 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
endCalled 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) |