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

refactor(rome_formatter): Unify comment/token spacing for leading/tra… #2766

Merged
merged 4 commits into from
Jun 23, 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
8 changes: 8 additions & 0 deletions crates/rome_formatter/src/comments.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::CommentStyle;
use rome_rowan::{Language, SyntaxTriviaPieceComments};

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
Expand Down Expand Up @@ -106,3 +107,10 @@ impl CommentKind {
matches!(self, CommentKind::InlineBlock | CommentKind::Block)
}
}

/// Stores comments specific context information.
pub trait CommentContext<L: Language> {
type Style: CommentStyle<L>;

fn comment_style(&self) -> Self::Style;
}
25 changes: 12 additions & 13 deletions crates/rome_formatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub use builders::{
soft_line_break, soft_line_break_or_space, soft_line_indent_or_space, space_token, token,
BestFitting,
};
pub use comments::{CommentKind, SourceComment};
pub use comments::{CommentContext, CommentKind, SourceComment};
pub use format_element::{normalize_newlines, FormatElement, Token, Verbatim, LINE_TERMINATORS};
pub use group_id::GroupId;
use indexmap::IndexSet;
Expand Down Expand Up @@ -1156,7 +1156,7 @@ impl<Context> FormatState<Context> {
}

/// Sets whether the last written content is an inline comment that has no trailing whitespace.
pub fn set_last_content_is_inline_comment(&mut self, has_comment: bool) {
pub fn set_last_content_inline_comment(&mut self, has_comment: bool) {
self.last_content_inline_comment = has_comment;
}

Expand All @@ -1167,13 +1167,14 @@ impl<Context> FormatState<Context> {

/// Sets the kind of the last formatted token and sets `last_content_inline_comment` to `false`.
pub fn set_last_token_kind<Kind: SyntaxKind + 'static>(&mut self, kind: Kind) {
// Reset the last comment kind before token because we've now seen a token.
self.last_content_inline_comment = false;

self.last_token_kind = Some(LastTokenKind {
self.set_last_token_kind_raw(Some(LastTokenKind {
kind_type: TypeId::of::<Kind>(),
kind: kind.to_raw(),
});
}));
}

pub fn set_last_token_kind_raw(&mut self, kind: Option<LastTokenKind>) {
self.last_token_kind = kind;
}

/// Mark the passed comment as formatted. This is necessary if a comment from a token is formatted
Expand Down Expand Up @@ -1302,20 +1303,18 @@ pub struct FormatStateSnapshot {
}

/// Defines how to format comments for a specific [Language].
pub trait CommentStyle: Copy {
type Language: Language;

pub trait CommentStyle<L: Language>: Copy {
/// Returns the kind of the comment
fn get_comment_kind(&self, comment: &SyntaxTriviaPieceComments<Self::Language>) -> CommentKind;
fn get_comment_kind(&self, comment: &SyntaxTriviaPieceComments<L>) -> CommentKind;

/// Returns `true` if a token with the passed `kind` marks the start of a group. Common group tokens are:
/// * left parentheses: `(`, `[`, `{`
fn is_group_start_token(&self, kind: <Self::Language as Language>::Kind) -> bool;
fn is_group_start_token(&self, kind: L::Kind) -> bool;

/// Returns `true` if a token with the passed `kind` marks the end of a group. Common group end tokens are:
/// * right parentheses: `)`, `]`, `}`
/// * end of statement token: `;`
/// * element separator: `,` or `.`.
/// * end of file token: `EOF`
fn is_group_end_token(&self, kind: <Self::Language as Language>::Kind) -> bool;
fn is_group_end_token(&self, kind: L::Kind) -> bool;
}
5 changes: 4 additions & 1 deletion crates/rome_formatter/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ pub use crate::format_element::*;
pub use crate::format_extensions::{FormatOptional as _, MemoizeFormat};
pub use crate::formatter::Formatter;
pub use crate::printer::PrinterOptions;
pub use crate::token::format_trimmed_token;
pub use crate::token::{
format_leading_trivia, format_only_if_breaks, format_removed, format_replaced,
format_trailing_trivia, format_trimmed_token,
};

pub use crate::{
best_fitting, dbg_write, format, format_args, write, Buffer as _, BufferExtensions, Format,
Expand Down
Loading