npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

typstyle-core

v0.12.13

Published

A pretty printer for typst.

Downloads

246

Readme

typstyle

A beautiful and reliable code formatter for Typst.

Packaging status

Installation and Usage

Use as a CLI

Installation

You can install typstyle using any of the following methods:

  1. Download the binary from the release page.

  2. Install it from your package manager: https://repology.org/project/typstyle/versions.

  3. Install using cargo-binstall:

    cargo binstall typstyle
  4. Install it with cargo:

    cargo install typstyle --locked

CLI Usage

Beautiful and reliable typst code formatter

Usage: typstyle.exe [OPTIONS] [INPUT]... [COMMAND]

Commands:
  format-all   Format all files in-place in the given directory
  help         Print this message or the help of the given subcommand(s)

Arguments:
  [INPUT]...  Path to the input files, if not provided, read from stdin. If multiple files are provided, they will be processed in order

Options:
  -i, --inplace  Format the file in place
      --check    Run in 'check' mode. Exits with 0 if input is formatted correctly. Exits with 1 if formatting is required
  -h, --help     Print help
  -V, --version  Print version

Format Configuration:
  -c, --column <COLUMN>  The column width of the output [default: 80]

Debug Options:
  -a, --ast         Print the AST of the input file
  -p, --pretty-doc  Print the pretty document

Log Levels:
  -v, --verbose  Enable verbose logging
  -q, --quiet    Print diagnostics, but nothing else
Format all files in-place in the given directory

Usage: typstyle.exe format-all [OPTIONS] [DIRECTORY]

Arguments:
  [DIRECTORY]  The directory to format. If not provided, the current directory is used

Options:
      --check  Run in 'check' mode. Exits with 0 if input is formatted correctly. Exits with 1 if formatting is required
  -h, --help   Print help

Format Configuration:
  -c, --column <COLUMN>  The column width of the output [default: 80]

Log Levels:
  -v, --verbose  Enable verbose logging
  -q, --quiet    Print diagnostics, but nothing else

Examples

  • Format a file in place:

    typstyle -i file.typ
  • Format a file and print the result to stdout:

    typstyle file.typ
  • Format multiple files in place:

    typstyle -i file1.typ file2.typ file3.typ
  • Format all files in a directory. If the argument is not provided, it will recursively format all files in the current directory:

    typstyle format-all dir
  • Read from stdin and print the result to stdout:

    cat file.typ | typstyle > file-formatted.typ

Use in your editor

Typstyle has been integrated into tinymist. You can use it in your editor by installing the tinymist plugin and set tinymist.formatterMode to typstyle.

Use as a web app

Try the online version of the formatter at: https://enter-tainer.github.io/typstyle/demo/. You can see how it formats your code.

Use with pre-commit

Add this to your .pre-commit-config.yaml:

- repo: https://github.com/Enter-tainer/typstyle
  rev: "" # specify the revision or tag to use
  hooks:
    - id: typstyle

Use as a Library

Features & Design

Design Goals

  1. Opinionated: We want to have a consistent style across all codebases.
  2. Code only: We want to format only the code. Contents should be left untouched as much as possible.
  3. Convergence: Running the formatter twice should not change the code.
  4. Correctness: The formatter should not change the looking of the rendered output.

Escape Hatch

If you find typstyle is not working as expected, you can use // @typstyle off or /* @typstyle off */ to disable the formatter on the next node of code.

Typstyle also gives up formatting part of the code if it is not able to format it correctly. Specifically, it will print that part as is if:

  • contains syntax error
  • contains syntaxes that are not supported by the formatter

And please let us know the issue by creating an issue on the GitHub repository

Known Issues

You tell us! Bad formatting? Incorrect output? Please create an issue on the GitHub repository!

We've set up comprehensive test suites to ensure the correctness of the formatter. If you find any issues, please let us know! And we can add more tests to prevent the issue from happening again.

Roadmap

See the tracking issue.

Documentation

See https://enter-tainer.github.io/typstyle/.

Testing

We maintain a comprehensive suite of tests to ensure the correctness and reliability of typstyle.

Types of Tests

  1. Convergence tests: format result must be the same when applied twice.
  2. Snapshot tests: format result are stored in the snapshots directory and are compared to the current result when running the tests.
  3. Correctness test: We compare the rendered output of the code before and after formatting and ensure they are the same.
  4. E2E Correctness test: We collect a bunch of typst code repo including tablex, cetz, fletcher... and format them to ensure (a) the format result converges and (b) the rendered output is the same.

Running Tests

For developers, you need to install cargo-nextest and cargo-insta to run tests.

  • List all tests:

    cargo nextest list --workspace
  • Run all tests and review snapshots:

    cargo nextest run --workspace --no-fail-fast
    cargo insta review
  • Run snapshot tests only:

    cargo nextest run --workspace -E 'test([typst])' --no-fail-fast --no-default-features
    cargo insta review
  • Run tests excluding end-to-end (e2e):

    cargo nextest run --workspace -E 'not test(~e2e)' --no-fail-fast

Benchmark

We provide benchmarks for node attribute computation and pretty printing. Typically, it can format a large document (given parsed source) within 5ms (e.g., tablex.typ with ~3000 lines).

We also have continuous benchmarking for each commit on master branch. See https://enter-tainer.github.io/typstyle-bench-results/dev/bench/.

Running benches

  • List benchmarks:

    cargo bench --workspace -- --list
  • Run benchmarks:

    cargo bench --workspace

The benchmark results is generated by criterion.rs. You can check target/criterion/report to see the reports.

Why Another Formatter?

Why there is a need for another formatter? We already have typstfmt, typstfmt, prettypst. Why another one?

Typstyle started because I had a bunch of ideas on how to improve typst source code formatting but kept finding typstfmt wasn't a good codebase to explore them with. Namely:

  • I wanted to use Wadler's pretty printer (implemented by pretty.rs) to get consistent and beautiful output for any width. (Note that it is the same technique used in the prettier formatter)
  • I didn't have much energy to maintain a bunch combination of configuration options. It turns out to be very hard to make everything correct. So I decided to make it opinionated.
  • I wanted to experiment with more testing techniques and make sure the formatter is correct.

So I decided to write something from scratch. I started it about half a year ago and kept working on it in my spare time. Currently it lacks some advanced features but it is already usable for most of the cases. I hope you like it!