Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

refactor(rome_formatter): Part 3: Delete FormatElement::Comment #3240

Merged
merged 1 commit into from
Sep 19, 2022
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
67 changes: 0 additions & 67 deletions crates/rome_formatter/src/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,73 +457,6 @@ impl<Context> Format<Context> for LineSuffixBoundary {
}
}

/// Marks some content as a comment trivia.
///
/// This does not directly influence how this content will be printed, but some
/// parts of the formatter may chose to handle this element in a specific way
///
/// ## Examples
///
/// ```
/// use rome_formatter::{format, write, format_args};
/// use rome_formatter::prelude::*;
///
/// let elements = format!(
/// SimpleFormatContext::default(),
/// [
/// group(&format_args![
/// comment(&format_args![text("// test"), hard_line_break()]),
/// format_with(|f| {
/// write!(f, [
/// comment(&format_args![text("/* inline */"), hard_line_break()]).memoized(),
/// text("a"),
/// soft_line_break_or_space(),
/// ])
/// }).memoized(),
/// text("b"),
/// soft_line_break_or_space(),
/// text("c")
/// ])
/// ]
/// ).unwrap();
///
/// assert_eq!(
/// "// test\n/* inline */\na b c",
/// elements.print().as_code()
/// );
/// ```
#[inline]
pub fn comment<Content, Context>(content: &Content) -> FormatComment<Context>
where
Content: Format<Context>,
{
FormatComment {
content: Argument::new(content),
}
}

#[derive(Copy, Clone)]
pub struct FormatComment<'a, Context> {
content: Argument<'a, Context>,
}

impl<Context> Format<Context> for FormatComment<'_, Context> {
fn fmt(&self, f: &mut Formatter<Context>) -> FormatResult<()> {
let mut buffer = VecBuffer::new(f.state_mut());

buffer.write_fmt(Arguments::from(&self.content))?;
let content = buffer.into_vec();

f.write_element(FormatElement::Comment(content.into_boxed_slice()))
}
}

impl<Context> std::fmt::Debug for FormatComment<'_, Context> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("Comment").field(&"{{content}}").finish()
}
}

/// Marks some content with a label.
///
/// This does not directly influence how this content will be printed, but some
Expand Down
15 changes: 2 additions & 13 deletions crates/rome_formatter/src/format_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,6 @@ pub enum FormatElement {
/// line suffixes, potentially by inserting a hard line break.
LineSuffixBoundary,

/// Special semantic element letting the printer and formatter know this is
/// a comment content, and it should only have a limited influence on the
/// formatting (for instance line breaks contained within will not cause
/// the parent group to break if this element is at the start of it).
Comment(Box<[FormatElement]>),

/// A token that tracks tokens/nodes that are printed as verbatim.
Verbatim(Verbatim),

Expand Down Expand Up @@ -193,7 +187,6 @@ impl std::fmt::Debug for FormatElement {
fmt.debug_tuple("LineSuffix").field(content).finish()
}
FormatElement::LineSuffixBoundary => write!(fmt, "LineSuffixBoundary"),
FormatElement::Comment(content) => fmt.debug_tuple("Comment").field(content).finish(),
FormatElement::Verbatim(verbatim) => fmt
.debug_tuple("Verbatim")
.field(&verbatim.content)
Expand Down Expand Up @@ -675,7 +668,6 @@ impl FormatElement {
FormatElement::Group(Group { content, .. })
| FormatElement::ConditionalGroupContent(ConditionalGroupContent { content, .. })
| FormatElement::IndentIfGroupBreaks(IndentIfGroupBreaks { content, .. })
| FormatElement::Comment(content)
| FormatElement::Fill(content)
| FormatElement::Verbatim(Verbatim { content, .. })
| FormatElement::Label(Label { content, .. })
Expand Down Expand Up @@ -705,13 +697,13 @@ impl FormatElement {

/// Utility function to get the "last element" of a [FormatElement], recursing
/// into lists and groups to find the last element that's not
/// a line break, or a comment.
/// a line break
pub fn last_element(&self) -> Option<&FormatElement> {
match self {
FormatElement::List(list) => {
list.iter().rev().find_map(|element| element.last_element())
}
FormatElement::Line(_) | FormatElement::Comment(_) => None,
FormatElement::Line(_) => None,

FormatElement::Group(Group { content, .. }) | FormatElement::Indent(content) => {
content.iter().rev().find_map(FormatElement::last_element)
Expand Down Expand Up @@ -863,9 +855,6 @@ impl Format<IrFormatContext> for FormatElement {
FormatElement::LineSuffix(line_suffix) => {
write!(f, [text("line_suffix("), line_suffix.as_ref(), text(")")])
}
FormatElement::Comment(content) => {
write!(f, [text("comment("), content.as_ref(), text(")")])
}
FormatElement::Verbatim(verbatim) => {
write!(f, [text("verbatim("), verbatim.content.as_ref(), text(")")])
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rome_formatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use crate::printer::{Printer, PrinterOptions};
pub use arguments::{Argument, Arguments};
pub use buffer::{Buffer, BufferExtensions, BufferSnapshot, Inspect, PreambleBuffer, VecBuffer};
pub use builders::{
block_indent, comment, empty_line, get_lines_before, group, hard_line_break, if_group_breaks,
block_indent, empty_line, get_lines_before, group, hard_line_break, if_group_breaks,
if_group_fits_on_line, indent, labelled, line_suffix, soft_block_indent, soft_line_break,
soft_line_break_or_space, soft_line_indent_or_space, space, text, BestFitting,
};
Expand Down