Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions crates/oxc_formatter/examples/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,16 @@ fn main() -> Result<(), String> {
};

let formatter = Formatter::new(&allocator, options);
let formatted = formatter.format(&ret.program);
if show_ir {
let doc = formatter.doc(&ret.program);
println!("[");
for el in doc.iter() {
println!(" {el:?},");
}
println!("]");
} else {
let code = formatter.build(&ret.program);
println!("{code}");
println!("--- IR ---");
println!("{}", &formatted.document().to_string());
println!("--- End IR ---\n");
}

println!("--- Formatted Code ---");
let code = formatted.print().map_err(|e| e.to_string())?.into_code();
println!("{code}");
println!("--- End Formatted Code ---");
Ok(())
}
10 changes: 6 additions & 4 deletions crates/oxc_formatter/src/formatter/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,18 @@ impl std::fmt::Debug for FormatContext<'_> {

impl<'ast> FormatContext<'ast> {
pub fn new(
program: &'ast Program<'ast>,
source_text: &'ast str,
source_type: SourceType,
comments: &'ast [Comment],
allocator: &'ast Allocator,
options: FormatOptions,
) -> Self {
let source_text = SourceText::new(program.source_text);
let source_text = SourceText::new(source_text);
Self {
options,
source_text,
source_type: program.source_type,
comments: Comments::new(source_text, &program.comments),
source_type,
comments: Comments::new(source_text, comments),
allocator,
cached_elements: FxHashMap::default(),
}
Expand Down
Loading
Loading