Oak Pretty Print
A high-performance, language-agnostic code formatting library built on the Oak ecosystem.
🎯 Overview
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.
✨ Features
- Language Agnostic: Works with any language that implements
oak-core::Language. - Rule-Based System: Highly customizable through a priority-based rule engine.
- Universal Roles: Can apply formatting based on universal semantic roles (e.g., Container, Definition).
- Advanced Layout: Supports complex line-breaking logic using groups and indentation levels.
- Configurable: Easily adjust indentation styles, line endings, and maximum line lengths.
- no_std Support: Designed to work in restricted environments with only
allocrequired.
🚀 Quick Start
Basic usage of the formatter:
use ;
use MyLanguage;
📋 Configuration
The FormatConfig allows you to customize the global behavior of the formatter:
use ;
let config = new
.with_indent_style
.with_line_ending
.with_max_line_length
.with_trim_trailing_whitespace;
🔧 Advanced Usage
Custom Formatting Rules
You can create custom rules to handle specific language constructs:
use ;
use UniversalElementRole;
let rule = new
.with_priority
.with_node_rule;
Using the doc! Macro
For more manual control over document structure, you can use the doc! macro from oak-macros:
use doc;
let my_doc = doc! ;
🏗️ Architecture
Oak Pretty Print uses a two-stage process:
- Rule Application: AST nodes are visited and rules generate a
Doctree. - Printing: The
Doctree is rendered into a string based on the current configuration and layout constraints.
📊 Performance
- Fast Rendering: The
Doctree printer is optimized for linear time complexity relative to the document size. - Efficient Memory: Uses internal pooling for document fragments to minimize allocations.
- Streaming Support: Capable of generating output in chunks for large files.
🤝 Contributing
Contributions are welcome! Please feel free to submit issues or pull requests.
Oak Pretty Print - Beautiful code for every language 🚀