Skip to content

Editor API

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

Editor API

All methods are called on the api object passed to every hook.

Document Query

api:get_text()

Returns the full document text as a string, or nil if no document is active.

api:get_file_path()

Returns the file path as a string, or nil for untitled documents.

api:get_language()

Returns the detected syntax language name (e.g., "Rust", "Python", "JavaScript"), or nil.

api:is_dirty()

Returns true if the document has unsaved changes.

api:get_cursor_position()

Returns the cursor byte offset in the buffer as an integer.

api:get_selection()

Returns the currently selected text as a string, or nil if nothing is selected.

api:get_line(line_num)

Returns the text of a specific line (1-indexed), or nil if the line doesn't exist.

local first_line = api:get_line(1)

api:get_file_extension()

Returns the file extension without the dot (e.g., "rs", "py"), or nil.

api:get_file_dir()

Returns the directory containing the current file, or nil for untitled documents.

api:get_project_root()

Returns the project root directory, or nil if no project markers are found.

Project root is detected by walking up the directory tree looking for: .git, Cargo.toml, package.json, pyproject.toml, setup.py, .ferrispad.

Configuration

api:get_config(key)

Returns a string config value for the given key, or nil if unset.

local extra = api:get_config("extra_args")

api:get_config_number(key)

Returns a numeric config value, or nil if unset or not a valid number.

api:get_config_bool(key)

Returns a boolean config value. Returns false if unset.

Config keys correspond to the key field in [[config.params]] sections of plugin.toml.

MCP Integration

api:get_mcp_port()

Returns the MCP server TCP port (integer), or nil if the MCP server is not running.

api:setup_mcp_config(project_root)

Writes a .mcp.json configuration file to the given project root directory.

Returns (true, "") on success or (false, error_message) on failure.

Utility

api:log(message)

Prints a debug message to stderr. Useful for development — visible when running FerrisPad from a terminal.

api:log("Debug: processing " .. tostring(line_count) .. " lines")

api:command_exists(cmd)

Returns true if the given command exists on the system PATH.

if api:command_exists("ruff") then
    -- safe to run ruff
end

Clone this wiki locally