From 0e54591fcc0c329266c40bd2f52b755ba248edfb Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Mon, 9 Sep 2024 16:50:31 -0400 Subject: [PATCH] feat(format/html): impl non-verbatim formatting for directive and element tags (#3781) --- .../src/html/auxiliary/closing_element.rs | 23 +++++++++-- .../src/html/auxiliary/directive.rs | 39 +++++++++++++++++-- .../src/html/auxiliary/element.rs | 21 ++++++++-- .../src/html/auxiliary/name.rs | 8 ++-- .../src/html/auxiliary/opening_element.rs | 23 +++++++++-- .../src/html/auxiliary/root.rs | 16 +++++++- .../html/auxiliary/self_closing_element.rs | 24 ++++++++++-- crates/biome_html_formatter/src/prelude.rs | 2 +- 8 files changed, 135 insertions(+), 21 deletions(-) diff --git a/crates/biome_html_formatter/src/html/auxiliary/closing_element.rs b/crates/biome_html_formatter/src/html/auxiliary/closing_element.rs index 8b0892d5f01f..ebace9fd5635 100644 --- a/crates/biome_html_formatter/src/html/auxiliary/closing_element.rs +++ b/crates/biome_html_formatter/src/html/auxiliary/closing_element.rs @@ -1,10 +1,27 @@ use crate::prelude::*; -use biome_html_syntax::HtmlClosingElement; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_html_syntax::{HtmlClosingElement, HtmlClosingElementFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatHtmlClosingElement; impl FormatNodeRule for FormatHtmlClosingElement { fn fmt_fields(&self, node: &HtmlClosingElement, f: &mut HtmlFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let HtmlClosingElementFields { + l_angle_token, + name, + slash_token, + r_angle_token, + } = node.as_fields(); + + write!( + f, + [ + l_angle_token.format(), + slash_token.format(), + name.format(), + r_angle_token.format(), + ] + )?; + + Ok(()) } } diff --git a/crates/biome_html_formatter/src/html/auxiliary/directive.rs b/crates/biome_html_formatter/src/html/auxiliary/directive.rs index 73c43c7884b5..fcb804f5c0e2 100644 --- a/crates/biome_html_formatter/src/html/auxiliary/directive.rs +++ b/crates/biome_html_formatter/src/html/auxiliary/directive.rs @@ -1,10 +1,43 @@ use crate::prelude::*; -use biome_html_syntax::HtmlDirective; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_html_syntax::{HtmlDirective, HtmlDirectiveFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatHtmlDirective; impl FormatNodeRule for FormatHtmlDirective { fn fmt_fields(&self, node: &HtmlDirective, f: &mut HtmlFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let HtmlDirectiveFields { + l_angle_token, + excl_token, + doctype_token, + html_token, + quirk_token, + public_id_token, + system_id_token, + r_angle_token, + } = node.as_fields(); + + write!( + f, + [ + l_angle_token.format(), + excl_token.format(), + doctype_token.format(), + ] + )?; + if let Some(html) = html_token { + write!(f, [space()])?; + html.format().fmt(f)?; + } + if let Some(quirk) = quirk_token { + quirk.format().fmt(f)?; + } + if let Some(public_id) = public_id_token { + public_id.format().fmt(f)?; + } + if let Some(system_id) = system_id_token { + system_id.format().fmt(f)?; + } + write!(f, [r_angle_token.format(), hard_line_break()])?; + Ok(()) } } diff --git a/crates/biome_html_formatter/src/html/auxiliary/element.rs b/crates/biome_html_formatter/src/html/auxiliary/element.rs index 5a60c907e4ae..6c468d16daea 100644 --- a/crates/biome_html_formatter/src/html/auxiliary/element.rs +++ b/crates/biome_html_formatter/src/html/auxiliary/element.rs @@ -1,10 +1,25 @@ use crate::prelude::*; -use biome_html_syntax::HtmlElement; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_html_syntax::{HtmlElement, HtmlElementFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatHtmlElement; impl FormatNodeRule for FormatHtmlElement { fn fmt_fields(&self, node: &HtmlElement, f: &mut HtmlFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let HtmlElementFields { + opening_element, + children, + closing_element, + } = node.as_fields(); + + write!( + f, + [ + opening_element.format(), + children.format(), + closing_element.format(), + ] + )?; + + Ok(()) } } diff --git a/crates/biome_html_formatter/src/html/auxiliary/name.rs b/crates/biome_html_formatter/src/html/auxiliary/name.rs index 59d339a3a6a6..2c40a1c9f4d9 100644 --- a/crates/biome_html_formatter/src/html/auxiliary/name.rs +++ b/crates/biome_html_formatter/src/html/auxiliary/name.rs @@ -1,10 +1,12 @@ use crate::prelude::*; -use biome_html_syntax::HtmlName; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_html_syntax::{HtmlName, HtmlNameFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatHtmlName; impl FormatNodeRule for FormatHtmlName { fn fmt_fields(&self, node: &HtmlName, f: &mut HtmlFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let HtmlNameFields { value_token } = node.as_fields(); + + write![f, [value_token.format()]] } } diff --git a/crates/biome_html_formatter/src/html/auxiliary/opening_element.rs b/crates/biome_html_formatter/src/html/auxiliary/opening_element.rs index 05d9e000feef..81e29996ea5d 100644 --- a/crates/biome_html_formatter/src/html/auxiliary/opening_element.rs +++ b/crates/biome_html_formatter/src/html/auxiliary/opening_element.rs @@ -1,10 +1,27 @@ use crate::prelude::*; -use biome_html_syntax::HtmlOpeningElement; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_html_syntax::{HtmlOpeningElement, HtmlOpeningElementFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatHtmlOpeningElement; impl FormatNodeRule for FormatHtmlOpeningElement { fn fmt_fields(&self, node: &HtmlOpeningElement, f: &mut HtmlFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let HtmlOpeningElementFields { + l_angle_token, + name, + attributes, + r_angle_token, + } = node.as_fields(); + + write!( + f, + [ + l_angle_token.format(), + name.format(), + attributes.format(), + r_angle_token.format(), + ] + )?; + + Ok(()) } } diff --git a/crates/biome_html_formatter/src/html/auxiliary/root.rs b/crates/biome_html_formatter/src/html/auxiliary/root.rs index 91e1f99f291d..f731f68b49f5 100644 --- a/crates/biome_html_formatter/src/html/auxiliary/root.rs +++ b/crates/biome_html_formatter/src/html/auxiliary/root.rs @@ -1,10 +1,22 @@ use crate::prelude::*; use biome_html_syntax::HtmlRoot; -use biome_rowan::AstNode; #[derive(Debug, Clone, Default)] pub(crate) struct FormatHtmlRoot; impl FormatNodeRule for FormatHtmlRoot { fn fmt_fields(&self, node: &HtmlRoot, f: &mut HtmlFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + if let Some(bom) = node.bom_token() { + bom.format().fmt(f)?; + } + if let Some(directive) = node.directive() { + directive.format().fmt(f)?; + } + if let Some(html) = node.html() { + html.format().fmt(f)?; + } + if let Ok(eof) = node.eof_token() { + eof.format().fmt(f)?; + } + + Ok(()) } } diff --git a/crates/biome_html_formatter/src/html/auxiliary/self_closing_element.rs b/crates/biome_html_formatter/src/html/auxiliary/self_closing_element.rs index a4c27599c388..ce6ff35ee041 100644 --- a/crates/biome_html_formatter/src/html/auxiliary/self_closing_element.rs +++ b/crates/biome_html_formatter/src/html/auxiliary/self_closing_element.rs @@ -1,10 +1,28 @@ use crate::prelude::*; -use biome_html_syntax::HtmlSelfClosingElement; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_html_syntax::{HtmlSelfClosingElement, HtmlSelfClosingElementFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatHtmlSelfClosingElement; impl FormatNodeRule for FormatHtmlSelfClosingElement { fn fmt_fields(&self, node: &HtmlSelfClosingElement, f: &mut HtmlFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let HtmlSelfClosingElementFields { + l_angle_token, + name, + attributes, + slash_token, + r_angle_token, + } = node.as_fields(); + + write!( + f, + [ + l_angle_token.format(), + name.format(), + attributes.format(), + soft_line_break_or_space(), + slash_token.format(), + r_angle_token.format() + ] + ) } } diff --git a/crates/biome_html_formatter/src/prelude.rs b/crates/biome_html_formatter/src/prelude.rs index 7efe9bcc1952..e5c3661049a9 100644 --- a/crates/biome_html_formatter/src/prelude.rs +++ b/crates/biome_html_formatter/src/prelude.rs @@ -3,6 +3,6 @@ pub(crate) use crate::{ format_verbatim_node, AsFormat, FormatNodeRule, FormatResult, FormatRule, FormattedIterExt, HtmlFormatContext, HtmlFormatter, }; -pub(crate) use biome_formatter::Format; +pub(crate) use biome_formatter::prelude::*; #[allow(unused_imports)] pub(crate) use biome_rowan::{AstNode, AstNodeList};