1. Introduction
  2. User Guide
  3. 1. Installation
  4. 2. Quick Start
  5. 3. Changelog
  6. Usage
  7. 4. Command Line Interface
  8. 5. Editor Integration
  9. Features
  10. 6. Formatting Features
    1. 6.1. Markup
    2. 6.2. Code
    3. 6.3. Math Equations
    4. 6.4. Tables
  11. 7. Escape Hatch
  12. 8. Limitations
  13. Advanced
  14. 9. How It Works
  15. 10. Developer Guide
    1. 10.1. Core
    2. 10.2. Documentation
    3. 10.3. Playground

typstyle

#

High Level Overview

Typstyle is a code formatter, the input is a string of typst source code and the output is a string of formatted typst source code.

To format the code, there are certain main steps that are followed:

  1. Parsing: The input code is parsed into an Abstract Syntax Tree (AST) using the typst-syntax package. If the input code is erroneous, the code will not be formatted and following steps will be skipped.
  2. Attach Attributes: The AST is traversed and certain attributes are attached to the nodes. Like some nodes should be skipped from formatting, some nodes should be formatted in a special way, etc.
  3. Formatting: The AST is traversed and the AST is transformed into a Wadler-style pretty-print-tree. This tree is then converted into a string of formatted code.
  4. Post Processing: The formatted code is post-processed to remove any trailing whitespaces, etc.
  5. Output: The formatted code is returned as the output.

The main work happens in step 2 and 3. We will discuss these steps in detail in the following sections.