13 releases (breaking)

Uses new Rust 2024

0.10.1 Mar 9, 2026
0.9.0 Feb 7, 2026
0.8.0 Dec 27, 2025
0.7.0 Nov 15, 2025
0.1.0 Nov 30, 2024

#139 in Value formatting


Used in 2 crates (via wat_service)

MIT license

265KB
8K SLoC

The formatter (pretty printer) for the WebAssembly Text Format.

This formatter can format a tree that contains syntax errors.

Usage

Full

The [format()] function only accepts parsed syntax tree, so you should use the parser to parse source code first.

use wat_formatter::format;
use wat_syntax::{SyntaxNode, ast::{AstNode, Root}};

let input = "( module )";
let (root, _) = wat_parser::parse(input);
assert_eq!("(module)\n", format(&root, &Default::default()));

For customizing the formatting behavior, please refer to config.

Range

You can format only a specific range of code by calling format_range function.

Beside the root syntax tree and format options, this function also accepts requested range and LineIndex.

Notes:

  • Returned formatted string is corresponding to specific syntax node. It isn't full text, so you may replace by yourself.
  • Affected range will equal or be wider than the range you give, so you should use returned range when replacing, not original range.
use line_index::LineIndex;
use wat_formatter::format_range;
use wat_syntax::{SyntaxNode, TextRange, TextSize, ast::{AstNode, Root}};

let input = "( module ( func ) )";
let line_index = LineIndex::new(input);
let (tree, _) = wat_parser::parse(input);
let root = Root::cast(SyntaxNode::new_root(tree)).unwrap();
let (formatted, range) = format_range(
    &root,
    &Default::default(),
    TextRange::new(TextSize::new(13), TextSize::new(17)),
    &line_index,
).unwrap();
assert_eq!("(func)", &formatted);
assert_eq!(TextRange::new(TextSize::new(9), TextSize::new(17)), range);

Dependencies

~0.5–1MB
~22K SLoC