Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 3 additions & 52 deletions crates/biome_html_formatter/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,22 @@ use biome_formatter::{
FormatRule,
comments::{
CommentKind, CommentPlacement, CommentStyle, Comments, DecoratedComment, SourceComment,
is_alignable_comment,
},
prelude::*,
write,
};
use biome_html_syntax::HtmlLanguage;
use biome_rowan::{SyntaxTriviaPieceComments, TextLen};
use biome_rowan::SyntaxTriviaPieceComments;
use biome_suppression::parse_suppression_comment;

use crate::context::HtmlFormatContext;

pub type HtmlComments = Comments<HtmlLanguage>;

#[derive(Default)]
pub struct FormatHtmlLeadingComment;
pub struct FormatHtmlComment;

impl FormatRule<SourceComment<HtmlLanguage>> for FormatHtmlLeadingComment {
type Context = HtmlFormatContext;

fn fmt(
&self,
comment: &SourceComment<HtmlLanguage>,
f: &mut Formatter<Self::Context>,
) -> FormatResult<()> {
if is_alignable_comment(comment.piece()) {
let mut source_offset = comment.piece().text_range().start();

let mut lines = comment.piece().text().lines();

// SAFETY: Safe, `is_alignable_comment` only returns `true` for multiline comments
let first_line = lines.next().unwrap();
write!(f, [dynamic_text(first_line.trim_end(), source_offset)])?;

source_offset += first_line.text_len();

// Indent the remaining lines by one space so that all `*` are aligned.
write!(
f,
[&format_once(|f| {
for line in lines {
write!(
f,
[
hard_line_break(),
text(" "),
dynamic_text(line.trim(), source_offset)
]
)?;

source_offset += line.text_len();
}

Ok(())
})]
)
} else {
write!(f, [comment.piece().as_piece()])
}
}
}

#[derive(Default)]
pub struct FormatHtmlTrailingComment;

impl FormatRule<SourceComment<HtmlLanguage>> for FormatHtmlTrailingComment {
impl FormatRule<SourceComment<HtmlLanguage>> for FormatHtmlComment {
type Context = HtmlFormatContext;

fn fmt(
Expand Down
4 changes: 2 additions & 2 deletions crates/biome_html_formatter/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use biome_formatter::{
};
use biome_html_syntax::{HtmlFileSource, HtmlLanguage};

use crate::comments::{FormatHtmlLeadingComment, HtmlCommentStyle, HtmlComments};
use crate::comments::{FormatHtmlComment, HtmlCommentStyle, HtmlComments};

#[derive(Debug, Clone, Default)]
pub struct HtmlFormatOptions {
Expand Down Expand Up @@ -395,7 +395,7 @@ impl FormatContext for HtmlFormatContext {
impl CstFormatContext for HtmlFormatContext {
type Language = HtmlLanguage;
type Style = HtmlCommentStyle;
type CommentRule = FormatHtmlLeadingComment;
type CommentRule = FormatHtmlComment;

fn comments(&self) -> &HtmlComments {
&self.comments
Expand Down