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

Commit

Permalink
Format union type comments
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Sep 13, 2022
1 parent 52a51e3 commit 462e2d2
Show file tree
Hide file tree
Showing 15 changed files with 399 additions and 749 deletions.
35 changes: 29 additions & 6 deletions crates/rome_formatter/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,18 +236,18 @@ pub enum CommentPlacement<L: Language> {
/// Overrides the positioning of the comment to be a leading node comment.
Leading {
node: SyntaxNode<L>,
comment: DecoratedComment<L>,
comment: SourceComment<L>,
},
/// Overrides the positioning of the comment to be a trailing node comment.
Trailing {
node: SyntaxNode<L>,
comment: DecoratedComment<L>,
comment: SourceComment<L>,
},

/// Makes this comment a dangling comment of `node`
Dangling {
node: SyntaxNode<L>,
comment: DecoratedComment<L>,
comment: SourceComment<L>,
},

/// Uses the default positioning rules for the comment.
Expand All @@ -256,6 +256,29 @@ pub enum CommentPlacement<L: Language> {
}

impl<L: Language> CommentPlacement<L> {
#[inline]
pub fn leading(node: SyntaxNode<L>, comment: impl Into<SourceComment<L>>) -> Self {
Self::Leading {
node,
comment: comment.into(),
}
}

pub fn dangling(node: SyntaxNode<L>, comment: impl Into<SourceComment<L>>) -> Self {
Self::Dangling {
node,
comment: comment.into(),
}
}

#[inline]
pub fn trailing(node: SyntaxNode<L>, comment: impl Into<SourceComment<L>>) -> Self {
Self::Trailing {
node,
comment: comment.into(),
}
}

#[inline]
pub fn or_else<F>(self, or_else: F) -> Self
where
Expand All @@ -273,7 +296,7 @@ pub trait CommentStyle: Default {
type Language: Language;

/// Returns `true` if a comment with the given `text` is a `rome-ignore format:` suppression comment.
fn is_suppression(text: &str) -> bool {
fn is_suppression(_text: &str) -> bool {
false
}

Expand Down Expand Up @@ -326,15 +349,15 @@ impl<L: Language> Comments<L> {
let (comments, skipped) = builder.visit(root);

Self {
data: Rc::new(dbg!(CommentsData {
data: Rc::new(CommentsData {
root: Some(root.clone()),
is_suppression: Style::is_suppression,

comments,
with_skipped: skipped,
#[cfg(debug_assertions)]
checked_suppressions: RefCell::new(Default::default()),
})),
}),
}
}

Expand Down
26 changes: 20 additions & 6 deletions crates/rome_formatter/src/comments/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,16 @@ where
}
}

let trailing_end = trailing_end.unwrap_or(self.pending_comments.len());

// Process the leading trivia of the current token. the trailing trivia is handled as part of the next token
for leading in token.leading_trivia().pieces() {
if leading.is_newline() {
lines_before += 1;
// All comments following from here are own line comments
position = CommentPosition::OwnLine;

if trailing_end.is_none() {
trailing_end = Some(self.pending_comments.len());
}
} else if leading.is_skipped() {
self.builder.mark_has_skipped(&token);

Expand All @@ -173,7 +177,7 @@ where
following_token: token.clone(),
lines_before,
lines_after: 0,
position: CommentPosition::OwnLine,
position,
kind,
comment,
});
Expand All @@ -182,6 +186,8 @@ where
}
}

let trailing_end = trailing_end.unwrap_or(self.pending_comments.len());

self.last_token = Some(token);

let has_leading_comments = self.pending_comments.len() > trailing_end;
Expand Down Expand Up @@ -352,15 +358,23 @@ impl<L: Language> CommentsBuilder<L> {
self.skipped.insert(token.key());
}

fn push_leading_comment(&mut self, node: &SyntaxNode<L>, comment: DecoratedComment<L>) {
fn push_leading_comment(&mut self, node: &SyntaxNode<L>, comment: impl Into<SourceComment<L>>) {
self.comments.push_leading(node.key(), comment.into());
}

fn push_dangling_comment(&mut self, node: &SyntaxNode<L>, comment: DecoratedComment<L>) {
fn push_dangling_comment(
&mut self,
node: &SyntaxNode<L>,
comment: impl Into<SourceComment<L>>,
) {
self.comments.push_dangling(node.key(), comment.into());
}

fn push_trailing_comment(&mut self, node: &SyntaxNode<L>, comment: DecoratedComment<L>) {
fn push_trailing_comment(
&mut self,
node: &SyntaxNode<L>,
comment: impl Into<SourceComment<L>>,
) {
self.comments.push_trailing(node.key(), comment.into());
}

Expand Down
3 changes: 0 additions & 3 deletions crates/rome_formatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,6 @@ pub fn format_range<Language: FormatLanguage>(
let mut range_end = None;

let sourcemap = printed.sourcemap();
dbg!(printed.as_code());
for marker in sourcemap {
// marker.source <= range.start()
if let Some(start_dist) = range.start().checked_sub(marker.source) {
Expand Down Expand Up @@ -1233,9 +1232,7 @@ pub fn format_sub_tree<L: FormatLanguage>(
};

let formatted = format_node(root, language)?;
println!("{}", formatted.root);
let mut printed = formatted.print_with_indent(initial_indent);
dbg!(printed.as_code());
let sourcemap = printed.take_sourcemap();
let verbatim_ranges = printed.take_verbatim_ranges();

Expand Down
83 changes: 57 additions & 26 deletions crates/rome_js_formatter/src/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,17 @@ pub fn format_verbatim_node(node: &JsSyntaxNode) -> FormatVerbatimNode {
kind: VerbatimKind::Verbatim {
length: node.text_range().len(),
},
format_comments: true,
}
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct FormatVerbatimNode<'node> {
node: &'node JsSyntaxNode,
kind: VerbatimKind,
format_comments: bool,
}

impl Format<JsFormatContext> for FormatVerbatimNode<'_> {
fn fmt(&self, f: &mut JsFormatter) -> FormatResult<()> {
for element in self.node.descendants_with_tokens(Direction::Next) {
Expand Down Expand Up @@ -98,19 +101,21 @@ impl Format<JsFormatContext> for FormatVerbatimNode<'_> {
}

// Format all leading comments that are outside of the node's source range.
let comments = f.context().comments().clone();
let leading_comments = comments.leading_comments(self.node);
if self.format_comments {
let comments = f.context().comments().clone();
let leading_comments = comments.leading_comments(self.node);

let outside_trimmed_range = leading_comments.partition_point(|comment| {
comment.piece().text_range().end() <= trimmed_source_range.start()
});
let outside_trimmed_range = leading_comments.partition_point(|comment| {
comment.piece().text_range().end() <= trimmed_source_range.start()
});

write!(
f,
[FormatLeadingComments::Comments(
&leading_comments[..outside_trimmed_range]
)]
)?;
write!(
f,
[FormatLeadingComments::Comments(
&leading_comments[..outside_trimmed_range]
)]
)?;
}

// Find the first skipped token trivia, if any, and include it in the verbatim range because
// the comments only format **up to** but not including skipped token trivia.
Expand Down Expand Up @@ -145,20 +150,26 @@ impl Format<JsFormatContext> for FormatVerbatimNode<'_> {
.fmt(f)?;

// Format all trailing comments that are outside of the trimmed range.
let comments = f.context().comments().clone();
let trailing_comments = comments.trailing_comments(self.node);

let outside_trimmed_range_start = trailing_comments.partition_point(|comment| {
source_range(f, comment.piece().text_range()).end()
<= trimmed_source_range.end()
});

write!(
f,
[FormatTrailingComments::Comments(
&trailing_comments[outside_trimmed_range_start..]
)]
)
if self.format_comments {
let comments = f.context().comments().clone();

let trailing_comments = comments.trailing_comments(self.node);

let outside_trimmed_range_start =
trailing_comments.partition_point(|comment| {
source_range(f, comment.piece().text_range()).end()
<= trimmed_source_range.end()
});

write!(
f,
[FormatTrailingComments::Comments(
&trailing_comments[outside_trimmed_range_start..]
)]
)?;
}

Ok(())
})]
)?;

Expand All @@ -173,6 +184,13 @@ impl Format<JsFormatContext> for FormatVerbatimNode<'_> {
}
}

impl FormatVerbatimNode<'_> {
pub fn skip_comments(mut self) -> Self {
self.format_comments = false;
self
}
}

/// Formats unknown nodes. The difference between this method and `format_verbatim` is that this method
/// doesn't track nodes/tokens as [FormatElement::Verbatim]. They are just printed as they are.
pub fn format_unknown_node(node: &JsSyntaxNode) -> FormatUnknownNode {
Expand All @@ -189,19 +207,24 @@ impl Format<JsFormatContext> for FormatUnknownNode<'_> {
FormatVerbatimNode {
node: self.node,
kind: VerbatimKind::Unknown,
format_comments: true,
}
.fmt(f)
}
}

/// Format a node having formatter suppression comment applied to it
pub fn format_suppressed_node(node: &JsSyntaxNode) -> FormatSuppressedNode {
FormatSuppressedNode { node }
FormatSuppressedNode {
node,
format_comments: true,
}
}

#[derive(Debug, Clone)]
pub struct FormatSuppressedNode<'node> {
node: &'node JsSyntaxNode,
format_comments: bool,
}

impl Format<JsFormatContext> for FormatSuppressedNode<'_> {
Expand All @@ -211,11 +234,19 @@ impl Format<JsFormatContext> for FormatSuppressedNode<'_> {
[FormatVerbatimNode {
node: self.node,
kind: VerbatimKind::Suppressed,
format_comments: self.format_comments
}]
)
}
}

impl FormatSuppressedNode<'_> {
pub fn skip_comments(mut self) -> Self {
self.format_comments = false;
self
}
}

/// Formats a group delimited by an opening and closing token,
/// such as a function body delimited by '{' and '}' tokens
///
Expand Down
Loading

0 comments on commit 462e2d2

Please sign in to comment.