| Crates.io | oak-pretty-print |
| lib.rs | oak-pretty-print |
| version | 0.0.1 |
| created_at | 2025-10-20 09:58:48.675325+00 |
| updated_at | 2026-01-23 04:14:16.389063+00 |
| description | Syntax highlighter supporting multiple programming languages. |
| homepage | https://github.com/ygg-lang/oaks |
| repository | https://github.com/ygg-lang/oaks |
| max_upload_size | |
| id | 1891700 |
| size | 53,524 |
A high-performance, language-agnostic code formatting library built on the Oak ecosystem.
oak-pretty-print provides a flexible and powerful engine for formatting source code. By leveraging oak-core's red-green trees and universal roles, it can format code for any language that implements the Oak traits. It features a rule-based system that allows for fine-grained control over indentation, spacing, line breaks, and more.
oak-core::Language.alloc required.Basic usage of the formatter:
use oak_pretty_print::{Formatter, FormatConfig, RuleSet, create_builtin_rules};
use my_language::MyLanguage;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// 1. Configure the formatter
let config = FormatConfig::default()
.with_indent_style(IndentStyle::Spaces(4))
.with_max_line_length(80);
// 2. Set up rules
let mut rules = RuleSet::new();
rules.add_rules(create_builtin_rules::<MyLanguage>());
// 3. Create the formatter
let mut formatter = Formatter::<MyLanguage>::new(config, rules);
// 4. Format a node
let result = formatter.format(&red_node, &source_code)?;
println!("Formatted code:\n{}", result.content);
Ok(())
}
The FormatConfig allows you to customize the global behavior of the formatter:
use oak_pretty_print::{FormatConfig, IndentStyle, LineEnding};
let config = FormatConfig::new()
.with_indent_style(IndentStyle::Tabs)
.with_line_ending(LineEnding::Unix)
.with_max_line_length(100)
.with_trim_trailing_whitespace(true);
You can create custom rules to handle specific language constructs:
use oak_pretty_print::{BasicFormatRule, FormatRule, FormatContext};
use oak_core::language::UniversalElementRole;
let rule = BasicFormatRule::new("custom_indent")
.with_priority(10)
.with_node_rule(
|node| node.green.kind.is_universal(UniversalElementRole::Container),
|_node, context| {
context.increase_indent();
Ok(())
},
);
doc! MacroFor more manual control over document structure, you can use the doc! macro from oak-macros:
use oak_macros::doc;
let my_doc = doc! {
[
"fn", " ", "main", "()", " ",
group {
[
"{",
indent {
[hard_line, "println!(\"Hello World\");"]
},
hard_line,
"}"
]
}
]
};
Oak Pretty Print uses a two-stage process:
Doc tree.Doc tree is rendered into a string based on the current configuration and layout constraints.Doc tree printer is optimized for linear time complexity relative to the document size.Contributions are welcome! Please feel free to submit issues or pull requests.
Oak Pretty Print - Beautiful code for every language 🚀