From fb7cdc668793f3181936f9418a55d39a8b0af8ae Mon Sep 17 00:00:00 2001 From: Boshen Date: Sat, 18 Nov 2023 11:17:03 +0800 Subject: [PATCH] refactor(prettier): impl `Display` for `Doc` (#1401) --- crates/oxc_prettier/src/doc.rs | 133 ++++++++++++++++----------------- crates/oxc_prettier/src/lib.rs | 1 - crates/oxc_wasm/src/lib.rs | 5 +- 3 files changed, 67 insertions(+), 72 deletions(-) diff --git a/crates/oxc_prettier/src/doc.rs b/crates/oxc_prettier/src/doc.rs index e5097a7bd..8f8a7067a 100644 --- a/crates/oxc_prettier/src/doc.rs +++ b/crates/oxc_prettier/src/doc.rs @@ -3,7 +3,8 @@ //! References: //! * -use oxc_allocator::{Allocator, Box, String, Vec}; +use oxc_allocator::{Box, String, Vec}; +use std::fmt; use crate::{array, line, ss, Prettier}; @@ -34,73 +35,6 @@ pub enum Doc<'a> { IfBreak(Box<'a, Doc<'a>>), } -pub struct DocPrinter<'a> { - allocator: &'a Allocator, -} - -impl<'a> DocPrinter<'a> { - pub fn new(allocator: &'a Allocator) -> Self { - Self { allocator } - } - - pub fn print(&mut self, doc: &Doc<'a>) -> String { - let mut str = String::new_in(self.allocator); - match doc { - Doc::Str(s) => { - str.push('"'); - str.push_str(s); - str.push('"'); - } - Doc::Array(docs) => { - str.push('['); - for (idx, doc) in docs.iter().enumerate() { - str.push_str(&self.print(doc)); - if idx != docs.len() - 1 { - str.push_str(", "); - } - } - str.push(']'); - } - Doc::Indent(contents) => { - str.push_str("indent(["); - for (idx, doc) in contents.iter().enumerate() { - str.push_str(&self.print(doc)); - if idx != contents.len() - 1 { - str.push_str(", "); - } - } - str.push_str("])"); - } - Doc::Group(contents) => { - str.push_str("group(["); - for (idx, doc) in contents.iter().enumerate() { - str.push_str(&self.print(doc)); - if idx != contents.len() - 1 { - str.push_str(", "); - } - } - str.push_str("])"); - } - Doc::Line => { - str.push_str("line"); - } - Doc::Softline => { - str.push_str("softline"); - } - Doc::Hardline => { - str.push_str("hardline"); - } - Doc::IfBreak(break_contents) => { - str.push_str("ifBreak("); - str.push_str(&self.print(break_contents)); - str.push(')'); - } - } - - str - } -} - #[derive(Clone, Copy)] #[allow(unused)] pub enum Separator { @@ -146,3 +80,66 @@ impl<'a> Prettier<'a> { parts } } + +impl<'a> fmt::Display for Doc<'a> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{})", print_do_to_debug(self)) + } +} + +// https://github.com/prettier/prettier/blob/main/src/document/debug.js +fn print_do_to_debug(doc: &Doc<'_>) -> std::string::String { + use std::string::String; + let mut string = String::new(); + match doc { + Doc::Str(s) => { + string.push('"'); + string.push_str(s); + string.push('"'); + } + Doc::Array(docs) => { + string.push_str("[\n"); + for (idx, doc) in docs.iter().enumerate() { + string.push_str(&print_do_to_debug(doc)); + if idx != docs.len() - 1 { + string.push_str(", "); + } + } + string.push_str("]\n"); + } + Doc::Indent(contents) => { + for (idx, doc) in contents.iter().enumerate() { + string.push_str(&print_do_to_debug(doc)); + if idx != contents.len() - 1 { + string.push_str(", "); + } + } + } + Doc::Group(contents) => { + string.push_str("group([\n"); + for (idx, doc) in contents.iter().enumerate() { + string.push_str(&print_do_to_debug(doc)); + if idx != contents.len() - 1 { + string.push_str(", "); + } + } + string.push_str("])\n"); + } + Doc::Line => { + string.push_str("line"); + } + Doc::Softline => { + string.push_str("softline"); + } + Doc::Hardline => { + string.push_str("hardline"); + } + Doc::IfBreak(break_contents) => { + string.push_str("ifBreak("); + string.push_str(&print_do_to_debug(break_contents)); + string.push(')'); + } + } + + string +} diff --git a/crates/oxc_prettier/src/lib.rs b/crates/oxc_prettier/src/lib.rs index 9cec009b6..cdd1045d6 100644 --- a/crates/oxc_prettier/src/lib.rs +++ b/crates/oxc_prettier/src/lib.rs @@ -16,7 +16,6 @@ use doc::Doc; use oxc_allocator::Allocator; use oxc_ast::{ast::Program, CommentKind, Trivias}; -pub use crate::doc::DocPrinter; pub use crate::options::{ArrowParens, EndOfLine, PrettierOptions, QuoteProps, TrailingComma}; use crate::{format::Format, printer::Printer}; diff --git a/crates/oxc_wasm/src/lib.rs b/crates/oxc_wasm/src/lib.rs index 0d3107cd8..2b62c311f 100644 --- a/crates/oxc_wasm/src/lib.rs +++ b/crates/oxc_wasm/src/lib.rs @@ -14,7 +14,7 @@ use oxc::{ transformer::{TransformOptions, TransformTarget, Transformer}, }; use oxc_linter::{LintContext, Linter}; -use oxc_prettier::{DocPrinter, Prettier, PrettierOptions}; +use oxc_prettier::{Prettier, PrettierOptions}; use oxc_query::{schema, Adapter, SCHEMA_TEXT}; use oxc_type_synthesis::{synthesize_program, Diagnostic as TypeCheckDiagnostic}; use serde::Serialize; @@ -196,8 +196,7 @@ impl Oxc { Prettier::new(&allocator, source_text, trivias.clone(), PrettierOptions::default()) .doc(program); - let mut doc_printer = DocPrinter::new(&allocator); - self.prettier_ir = format!("{}", doc_printer.print(&prettier_doc)); + self.prettier_ir = prettier_doc.to_string(); if run_options.syntax() && !run_options.lint() { let semantic_ret = SemanticBuilder::new(source_text, source_type)