Crates.io | wat_formatter |
lib.rs | wat_formatter |
version | 0.1.0 |
source | src |
created_at | 2024-11-30 04:17:31.575245 |
updated_at | 2024-11-30 04:17:31.575245 |
description | WebAssembly Text Format formatter. |
homepage | |
repository | https://github.com/g-plane/wasm-language-tools |
max_upload_size | |
id | 1466290 |
size | 100,087 |
The formatter (pretty printer) for the WebAssembly Text Format.
This formatter can format a tree that contains syntax errors.
The [format()
] function only accepts parsed syntax tree,
so you should use wat_parser::Parser
to parse source code first.
use rowan::ast::AstNode;
use wat_formatter::format;
use wat_parser::Parser;
use wat_syntax::ast::Root;
let input = "( module )";
let mut parser = Parser::new(input);
let root = Root::cast(parser.parse()).unwrap();
assert_eq!("(module)\n", format(&root, &Default::default()));
For customizing the formatting behavior, please refer to [config
].
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:
use line_index::LineIndex;
use rowan::{ast::AstNode, TextRange, TextSize};
use wat_formatter::format_range;
use wat_parser::Parser;
use wat_syntax::ast::Root;
let input = "( module ( func ) )";
let line_index = LineIndex::new(input);
let mut parser = Parser::new(input);
let root = Root::cast(parser.parse()).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);