Repository navigation
Cookbook
Federico Conticello edited this page Mar 24, 2026
·
1 revision
Common recipes for FerrisPad plugin development.
function M:on_document_save(api, path, content)
return {
status_message = {
level = "info",
text = "File saved: " .. (path or "untitled")
}
}
endfunction 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 }
endfunction 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
endfunction 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
endfunction M:on_document_save(api, path, content)
local lang = api:get_language()
if lang ~= "Python" then return end
-- Python-specific logic
endOr check by file extension:
local ext = api:get_file_extension()
if ext ~= "py" and ext ~= "pyi" then return endfunction 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"
}
}
endfunction 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
endfunction 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
endlocal 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