Skip to content

Filesystem API

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

Filesystem API

All filesystem operations are sandboxed to the project root. Attempting to access files outside the project directory returns nil or an error.

Read Operations

api:file_exists(path)

Returns true if the file or directory exists within the project root.

if api:file_exists("src/main.rs") then
    -- file exists
end

api:is_file(path)

Returns true if the path is a regular file (not a directory).

api:read_file(path)

Reads file contents. Returns (content, nil) on success or (nil, error_message) on failure.

local content, err = api:read_file("config.toml")
if content then
    api:log("File has " .. #content .. " bytes")
else
    api:log("Error: " .. err)
end

api:list_dir(path)

Lists a single directory (non-recursive). Returns an array of entries or nil.

Each entry is a table: { name = "filename", is_dir = true/false }.

local entries = api:list_dir("src")
if entries then
    for _, e in ipairs(entries) do
        api:log(e.name .. (e.is_dir and " (dir)" or ""))
    end
end

api:scan_dir(path, max_depth)

Recursively scans a directory. max_depth is optional (default: 5, maximum: 10).

Returns an array of entries: { name = "filename", rel_path = "relative/path", is_dir = true/false }.

local files = api:scan_dir("src", 3)

Write Operations

All write operations return (true, "") on success or (false, error_message) on failure.

api:create_file(path)

Creates a new empty file. Fails if the file already exists.

api:create_dir(path)

Creates a directory and all parent directories.

api:write_file(path, content)

Writes content to a file (creates or overwrites). Creates parent directories if needed.

local ok, err = api:write_file("output/result.txt", "Hello!")
if not ok then
    api:log("Write failed: " .. err)
end

api:rename(old_path, new_path)

Renames or moves a file/directory. Both paths must be within the project root.

api:remove(path)

Removes a file or directory (recursive). Use with caution.

Git

api:git_status(path)

Returns a table mapping relative file paths to git status codes, or nil if not a git repository.

Status codes: "M" (modified), "A" (added), "??" (untracked), "!!" (ignored), "D" (deleted), "R" (renamed), "U" (conflict).

local status = api:git_status(".")
if status then
    for file, code in pairs(status) do
        if code == "M" then
            api:log("Modified: " .. file)
        end
    end
end

Diff

api:diff_text(old_text, new_text)

Computes a line-aligned diff between two strings. Returns a table with:

  • left_content — aligned left text (with filler lines for insertions)
  • right_content — aligned right text (with filler lines for deletions)
  • left_highlights — array of highlight info for removed/modified lines
  • right_highlights — array of highlight info for added/modified lines

This is used internally by the Git Diff plugin to populate split views.

Path Rules

  • Relative paths are resolved against the project root
  • Absolute paths are allowed only if they fall within the project root
  • Path traversal (../../etc/passwd) is blocked — .. is canonicalized and verified
  • Symlinks are resolved to their real path before validation

Clone this wiki locally