mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
feat(prettier): Doc::IfBreak should a accept single doc and add if_break macro (#1335)
This commit is contained in:
parent
039b53f6e7
commit
2bed242016
4 changed files with 14 additions and 22 deletions
|
|
@ -31,15 +31,10 @@ pub enum Doc<'a> {
|
|||
/// no matter if the expression fits on one line or not.
|
||||
Hardline,
|
||||
/// Print something if the current `group` or the current element of `fill` breaks and something else if it doesn't.
|
||||
IfBreak(Vec<'a, Doc<'a>>),
|
||||
IfBreak(Box<Doc<'a>>),
|
||||
}
|
||||
|
||||
impl<'a> Doc<'a> {
|
||||
#[must_use]
|
||||
pub fn if_break(break_contents: Vec<'a, Doc<'a>>) -> Self {
|
||||
Doc::IfBreak(break_contents)
|
||||
}
|
||||
}
|
||||
impl<'a> Doc<'a> {}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
#[allow(unused)]
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ mod ternary;
|
|||
use crate::{
|
||||
array,
|
||||
doc::{Doc, Separator},
|
||||
format, group, hardline, indent, softline, ss, string,
|
||||
format, group, hardline, if_break, indent, softline, ss, string,
|
||||
util::is_next_line_empty,
|
||||
Prettier,
|
||||
};
|
||||
|
|
@ -784,10 +784,7 @@ impl<'a> Format<'a> for ArrayExpression<'a> {
|
|||
parts_inner.push(format!(p, element));
|
||||
}
|
||||
|
||||
let mut if_break_comma = p.vec();
|
||||
if_break_comma.push(Doc::Str(","));
|
||||
|
||||
parts_inner.push(Doc::if_break(if_break_comma));
|
||||
parts_inner.push(if_break!(","));
|
||||
|
||||
parts.push(group!(p, Doc::Indent(parts_inner)));
|
||||
|
||||
|
|
|
|||
|
|
@ -81,3 +81,10 @@ macro_rules! group {
|
|||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! if_break {
|
||||
($s:expr) => {{
|
||||
Doc::IfBreak(std::boxed::Box::new(Doc::Str($s)))
|
||||
}};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -52,7 +52,7 @@ impl<'a> Printer<'a> {
|
|||
Doc::Line => self.handle_line(indent, mode),
|
||||
Doc::Softline => self.handle_softline(indent, mode),
|
||||
Doc::Hardline => self.handle_hardline(indent),
|
||||
Doc::IfBreak(if_break) => self.handle_if_break(if_break, indent, mode),
|
||||
Doc::IfBreak(if_break) => self.handle_if_break(*if_break, indent, mode),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -120,16 +120,9 @@ impl<'a> Printer<'a> {
|
|||
self.pos = self.indent(indent.length);
|
||||
}
|
||||
|
||||
fn handle_if_break(
|
||||
&mut self,
|
||||
if_break: oxc_allocator::Vec<'a, Doc<'a>>,
|
||||
indent: Indent,
|
||||
mode: Mode,
|
||||
) {
|
||||
fn handle_if_break(&mut self, doc: Doc<'a>, indent: Indent, mode: Mode) {
|
||||
if mode == Mode::Break {
|
||||
self.cmds.extend(
|
||||
if_break.into_iter().rev().map(|doc| Command::new(indent, Mode::Break, doc)),
|
||||
);
|
||||
self.cmds.push(Command::new(indent, Mode::Break, doc));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue