mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(prettier): impl Display for Doc (#1401)
This commit is contained in:
parent
3df810bf51
commit
fb7cdc6687
3 changed files with 67 additions and 72 deletions
|
|
@ -3,7 +3,8 @@
|
|||
//! References:
|
||||
//! * <https://github.com/prettier/prettier/blob/main/commands.md>
|
||||
|
||||
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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};
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue