Skip to content

Commit 89290e1

Browse files
committed
feat(format/html): implement suppression comments
1 parent 850f496 commit 89290e1

40 files changed

+398
-837
lines changed

crates/biome_html_factory/src/generated/node_factory.rs

-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/biome_html_factory/src/generated/syntax_factory.rs

-33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/biome_html_formatter/src/generated.rs

-38
Original file line numberDiff line numberDiff line change
@@ -189,44 +189,6 @@ impl IntoFormat<HtmlFormatContext> for biome_html_syntax::HtmlClosingElement {
189189
)
190190
}
191191
}
192-
impl FormatRule<biome_html_syntax::HtmlComment>
193-
for crate::html::auxiliary::comment::FormatHtmlComment
194-
{
195-
type Context = HtmlFormatContext;
196-
#[inline(always)]
197-
fn fmt(
198-
&self,
199-
node: &biome_html_syntax::HtmlComment,
200-
f: &mut HtmlFormatter,
201-
) -> FormatResult<()> {
202-
FormatNodeRule::<biome_html_syntax::HtmlComment>::fmt(self, node, f)
203-
}
204-
}
205-
impl AsFormat<HtmlFormatContext> for biome_html_syntax::HtmlComment {
206-
type Format<'a> = FormatRefWithRule<
207-
'a,
208-
biome_html_syntax::HtmlComment,
209-
crate::html::auxiliary::comment::FormatHtmlComment,
210-
>;
211-
fn format(&self) -> Self::Format<'_> {
212-
FormatRefWithRule::new(
213-
self,
214-
crate::html::auxiliary::comment::FormatHtmlComment::default(),
215-
)
216-
}
217-
}
218-
impl IntoFormat<HtmlFormatContext> for biome_html_syntax::HtmlComment {
219-
type Format = FormatOwnedWithRule<
220-
biome_html_syntax::HtmlComment,
221-
crate::html::auxiliary::comment::FormatHtmlComment,
222-
>;
223-
fn into_format(self) -> Self::Format {
224-
FormatOwnedWithRule::new(
225-
self,
226-
crate::html::auxiliary::comment::FormatHtmlComment::default(),
227-
)
228-
}
229-
}
230192
impl FormatRule<biome_html_syntax::HtmlContent>
231193
for crate::html::auxiliary::content::FormatHtmlContent
232194
{

crates/biome_html_formatter/src/html/any/element.rs

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ impl FormatRule<AnyHtmlElement> for FormatAnyHtmlElement {
1010
match node {
1111
AnyHtmlElement::HtmlBogusElement(node) => node.format().fmt(f),
1212
AnyHtmlElement::HtmlCdataSection(node) => node.format().fmt(f),
13-
AnyHtmlElement::HtmlComment(node) => node.format().fmt(f),
1413
AnyHtmlElement::HtmlContent(node) => node.format().fmt(f),
1514
AnyHtmlElement::HtmlElement(node) => node.format().fmt(f),
1615
AnyHtmlElement::HtmlSelfClosingElement(node) => node.format().fmt(f),

crates/biome_html_formatter/src/html/auxiliary/comment.rs

-10
This file was deleted.

crates/biome_html_formatter/src/html/auxiliary/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ pub(crate) mod attribute_initializer_clause;
55
pub(crate) mod attribute_name;
66
pub(crate) mod cdata_section;
77
pub(crate) mod closing_element;
8-
pub(crate) mod comment;
98
pub(crate) mod content;
109
pub(crate) mod directive;
1110
pub(crate) mod element;

crates/biome_html_formatter/src/html/auxiliary/root.rs

+19-12
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
use crate::prelude::*;
22
use biome_formatter::write;
3-
use biome_html_syntax::HtmlRoot;
3+
use biome_html_syntax::{HtmlRoot, HtmlRootFields};
44
#[derive(Debug, Clone, Default)]
55
pub(crate) struct FormatHtmlRoot;
66
impl FormatNodeRule<HtmlRoot> for FormatHtmlRoot {
77
fn fmt_fields(&self, node: &HtmlRoot, f: &mut HtmlFormatter) -> FormatResult<()> {
8-
if let Some(bom) = node.bom_token() {
9-
bom.format().fmt(f)?;
10-
}
11-
if let Some(directive) = node.directive() {
12-
directive.format().fmt(f)?;
13-
}
8+
let HtmlRootFields {
9+
bom_token,
10+
directive,
11+
html,
12+
eof_token,
13+
} = node.as_fields();
1414

15-
node.html().format().fmt(f)?;
15+
dbg!(node.syntax().text_with_trivia());
16+
dbg!(eof_token.as_ref()?.leading_trivia());
1617

17-
if let Ok(eof) = node.eof_token() {
18-
eof.format().fmt(f)?;
19-
}
20-
write!(f, [hard_line_break()])?;
18+
write!(
19+
f,
20+
[
21+
bom_token.format(),
22+
directive.format(),
23+
html.format(),
24+
hard_line_break(),
25+
format_removed(&eof_token?),
26+
]
27+
)?;
2128

2229
Ok(())
2330
}

crates/biome_html_formatter/src/html/lists/element_list.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ impl FormatRuleWithOptions<HtmlElementList> for FormatHtmlElementList {
5656
impl FormatRule<HtmlElementList> for FormatHtmlElementList {
5757
type Context = HtmlFormatContext;
5858
fn fmt(&self, node: &HtmlElementList, f: &mut HtmlFormatter) -> FormatResult<()> {
59+
dbg!(node.syntax().text_with_trivia());
60+
5961
if node.is_empty() {
6062
return Ok(());
6163
}
@@ -181,6 +183,11 @@ impl FormatHtmlElementList {
181183
Some(WordSeparator::BetweenWords)
182184
}
183185

186+
Some(HtmlChild::Comment(_)) => {
187+
// FIXME: probably not correct behavior here
188+
Some(WordSeparator::Lines(0))
189+
}
190+
184191
// Last word or last word before an element without any whitespace in between
185192
Some(HtmlChild::NonText(next_child)) => Some(WordSeparator::EndOfText {
186193
is_soft_line_break: !matches!(
@@ -210,6 +217,12 @@ impl FormatHtmlElementList {
210217
}
211218
}
212219

220+
HtmlChild::Comment(comment) => {
221+
// FIXME: definitely wrong behavior
222+
flat.write(&format_args![comment], f);
223+
multiline.write_content(comment, f);
224+
}
225+
213226
// * Whitespace after the opening tag and before a meaningful text: `<div> a`
214227
// * Whitespace before the closing tag: `a </div>`
215228
// * Whitespace before an opening tag: `a <div>`
@@ -315,6 +328,11 @@ impl FormatHtmlElementList {
315328
}
316329
}
317330

331+
Some(HtmlChild::Comment(_)) => {
332+
// FIXME: definitely wrong behavior here.
333+
Some(LineMode::Hard)
334+
}
335+
318336
// Add a hard line break if what comes after the element is not a text or is all whitespace
319337
Some(HtmlChild::NonText(next_non_text)) => {
320338
// In the case of the formatter using the multiline layout, we want to treat inline elements like we do words.
@@ -426,7 +444,7 @@ impl FormatHtmlElementList {
426444
/// Tracks the tokens of [HtmlContent] nodes to be formatted and
427445
/// asserts that the suppression comments are checked (they get ignored).
428446
///
429-
/// This is necessary because the formatting of [HtmlContentList] bypasses the node formatting for
447+
/// This is necessary because the formatting of [HtmlElementList] bypasses the node formatting for
430448
/// [HtmlContent] and instead, formats the nodes itself.
431449
#[cfg(debug_assertions)]
432450
fn disarm_debug_assertions(&self, node: &HtmlElementList, f: &mut HtmlFormatter) {
@@ -524,6 +542,9 @@ enum WordSeparator {
524542
/// `a b`
525543
BetweenWords,
526544

545+
/// Seperator between 2 lines. Creates hard line breaks.
546+
Lines(usize),
547+
527548
/// A separator of a word at the end of a [HtmlText] element. Either because it is the last
528549
/// child in its parent OR it is right before the start of another child (element, expression, ...).
529550
///
@@ -571,6 +592,11 @@ impl Format<HtmlFormatContext> for WordSeparator {
571592
fn fmt(&self, f: &mut Formatter<HtmlFormatContext>) -> FormatResult<()> {
572593
match self {
573594
WordSeparator::BetweenWords => soft_line_break_or_space().fmt(f),
595+
WordSeparator::Lines(count) => match count {
596+
0 => Ok(()),
597+
1 => hard_line_break().fmt(f),
598+
_ => empty_line().fmt(f),
599+
},
574600
WordSeparator::EndOfText {
575601
is_soft_line_break,
576602
is_next_element_whitespace_sensitive,

0 commit comments

Comments
 (0)