ExLisp.Formatter (exlisp v0.1.2)

Copy Markdown

Code formatter for Common Lisp.

Based on CST (Concrete Syntax Tree), handles indentation, parenthesis placement, and comment preservation according to standard Common Lisp conventions.

Features

  1. Parenthesis placement: Does not place closing ) on separate lines, collecting them at line ends.
  2. Standard indentation:
    • Body of definition forms like defun, defmacro, defmethod: 2 spaces
    • Binding forms like let, let*, flet, labels: binding part and 2-space body
    • Control structures like if, when, unless, cond, case: appropriate indentation
    • Blocks like with-*, dolist, dotimes, loop, progn: 2 spaces
    • General function calls: aligned with first argument or 2 spaces
  3. Preserving comments and blank lines:
    • Preserves positions of ;;;;, ;;;, ;;, ; and inline comments.
    • Preserves intentional blank lines (compresses consecutive blank lines to one).

Summary

Functions

Formats and returns a Lisp code string.

Reads a Lisp file, writes back formatted content, or returns as a string.

Generates a Concrete Syntax Tree (CST) from a token stream.

Splits source code into a token stream for CST generation.

Types

format_opts()

@type format_opts() :: [max_width: pos_integer(), indent: pos_integer()]

Functions

format(input, opts \\ [])

@spec format(String.t(), format_opts()) :: String.t()

Formats and returns a Lisp code string.

format_file(path, opts \\ [])

@spec format_file(Path.t(), keyword()) :: {:ok, String.t()} | {:error, term()}

Reads a Lisp file, writes back formatted content, or returns as a string.

parse_cst(tokens)

Generates a Concrete Syntax Tree (CST) from a token stream.

tokenize(input)

Splits source code into a token stream for CST generation.