mirror of
https://github.com/roc-lang/roc.git
synced 2024-10-04 22:27:55 +03:00
7d0027f428
Many times, in order to create a `ven_pretty::Doc` containing a text node, the pattern `alloc.text(format!(...))` would be used. This code then creates a fresh string that is then used in the `Doc`. However, many times only a small string is necessary and so the allocation could be optimized. The `ven_pretty` crate supports this through a `SmallString` type. Allocating a fresh string with `format!` also moves control away from the `DocAllocator` which isn't ideal, since it could also handle the string allocations. So, instead of creating a fresh string, one can simply call `alloc.as_string(format_args!(...))` and delegate the allocation to the `DocAllocator` without any loss in expressivity. So, in order to encorage this pattern, this commit also introduces the `text!` macro. In order to find all instances of the code pattern, the following tree-sitter query was used: ```scm (call_expression function: (field_expression field: (field_identifier) @field.name (#eq? @field.name "text")) arguments: (arguments (macro_invocation macro: (identifier) @macro.name (#eq? @macro.name "format")))) @reference.call ``` |
||
---|---|---|
.. | ||
src | ||
Cargo.toml | ||
LICENSE | ||
README.md |
pretty.rs
Pretty printing combinators for Rust
Synopsis
This crate provides functionality for defining pretty printers. It is particularly useful for printing structured recursive data like trees.
The implementation was originally based on Larsen's SML translation (https://github.com/kfl/wpp) of Wadler's Haskell pretty printer (https://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf). It has since been modified in various ways to better fit Rust's programming model. In particular, it uses iteration rather than recursion and provides streaming output.
Documentation
See the generated API documentation here.
Requirements
You can install both with the following:
$ curl -s https://static.rust-lang.org/rustup.sh | sudo sh
See Installation for further details.
Usage
$ cargo build ## build library and binary
$ cargo run --example trees ## run the example (pretty trees)
$ cargo run --example colored --features termcolor ## run the example (pretty colored output)
$ cargo bench ## run benchmarks
$ cargo test ## run tests