From a80bcbf0918d2fa66b63246559ae8f8b7e6734e1 Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Sun, 28 Jul 2024 11:02:50 +0800 Subject: [PATCH] refactor: decouple retrieving comments and formatting comments --- malva/src/ctx.rs | 44 ++-- malva/src/doc_gen/at_rule/container.rs | 88 ++++--- malva/src/doc_gen/at_rule/custom_media.rs | 4 +- malva/src/doc_gen/at_rule/custom_selector.rs | 6 +- malva/src/doc_gen/at_rule/import.rs | 14 +- malva/src/doc_gen/at_rule/media.rs | 76 ++++-- malva/src/doc_gen/at_rule/mod.rs | 2 +- malva/src/doc_gen/at_rule/namespace.rs | 4 +- malva/src/doc_gen/at_rule/scope.rs | 4 +- malva/src/doc_gen/at_rule/supports.rs | 42 +++- malva/src/doc_gen/helpers.rs | 65 +++--- malva/src/doc_gen/less.rs | 142 +++++++---- malva/src/doc_gen/sass.rs | 234 +++++++++++++------ malva/src/doc_gen/selector.rs | 92 +++++--- malva/src/doc_gen/stmt.rs | 35 ++- malva/src/doc_gen/token_seq.rs | 2 +- malva/src/doc_gen/value.rs | 19 +- 17 files changed, 569 insertions(+), 304 deletions(-) diff --git a/malva/src/ctx.rs b/malva/src/ctx.rs index 6931987..8d7f153 100644 --- a/malva/src/ctx.rs +++ b/malva/src/ctx.rs @@ -22,6 +22,8 @@ impl<'a, 's> Ctx<'a, 's> { start: usize, end: usize, ) -> impl Iterator> + Clone { + debug_assert!(start <= end); + self.comments .iter() .filter(move |comment| comment.span.start >= start && comment.span.end <= end) @@ -29,12 +31,9 @@ impl<'a, 's> Ctx<'a, 's> { pub(crate) fn start_spaced_comments( &'a self, - start: usize, - end: usize, + comments: impl Iterator> + 'a, ) -> impl Iterator> + 'a { - debug_assert!(start <= end); - - self.get_comments_between(start, end) + comments .scan(CommentKind::Block, |prev_kind, comment| { Some( [ @@ -56,15 +55,12 @@ impl<'a, 's> Ctx<'a, 's> { pub(crate) fn start_spaced_comments_without_last_hard_line( &'a self, - start: usize, - end: usize, + comments: impl Iterator> + 'a, has_last_line_comment: &'a mut bool, ) -> impl Iterator> + 'a { - debug_assert!(start <= end); - StartSpacedCommentsWithoutLastHardLine { ctx: self, - iter: self.get_comments_between(start, end).peekable(), + iter: comments.peekable(), prev_kind: CommentKind::Block, has_last_line_comment, } @@ -73,12 +69,9 @@ impl<'a, 's> Ctx<'a, 's> { pub(crate) fn end_spaced_comments( &'a self, - start: usize, - end: usize, + comments: impl Iterator> + 'a, ) -> impl Iterator> + 'a { - debug_assert!(start <= end); - - self.get_comments_between(start, end).flat_map(|comment| { + comments.flat_map(|comment| { [ comment.doc(self), match comment.kind { @@ -92,15 +85,12 @@ impl<'a, 's> Ctx<'a, 's> { pub(crate) fn end_spaced_comments_without_last_space( &'a self, - start: usize, - end: usize, + comments: impl Iterator> + 'a, comment_end: &'a mut Option, ) -> impl Iterator> + 'a { - debug_assert!(start <= end); - EndSpacedCommentsWithoutLastSpace { ctx: self, - iter: self.get_comments_between(start, end).peekable(), + iter: comments.peekable(), comment_end, } .flatten() @@ -108,16 +98,12 @@ impl<'a, 's> Ctx<'a, 's> { pub(crate) fn unspaced_comments( &'a self, - start: usize, - end: usize, + comments: impl Iterator> + 'a, ) -> impl Iterator> + 'a { - debug_assert!(start <= end); - - self.get_comments_between(start, end) - .filter_map(|comment| match comment.kind { - CommentKind::Block => Some(comment.doc(self)), - CommentKind::Line => None, - }) + comments.filter_map(|comment| match comment.kind { + CommentKind::Block => Some(comment.doc(self)), + CommentKind::Line => None, + }) } pub(crate) fn with_state( diff --git a/malva/src/doc_gen/at_rule/container.rs b/malva/src/doc_gen/at_rule/container.rs index e3872ef..84a788f 100644 --- a/malva/src/doc_gen/at_rule/container.rs +++ b/malva/src/doc_gen/at_rule/container.rs @@ -12,7 +12,9 @@ impl<'s> DocGen<'s> for ContainerCondition<'s> { (Vec::with_capacity(self.conditions.len()), self.span.start), |(mut docs, pos), condition| { let span = condition.span(); - docs.extend(ctx.start_spaced_comments(pos, span.start)); + docs.extend( + ctx.start_spaced_comments(ctx.get_comments_between(pos, span.start)), + ); docs.push(condition.doc(ctx)); (docs, span.end) }, @@ -30,9 +32,9 @@ impl<'s> DocGen<'s> for ContainerConditionAnd<'s> { OperatorLineBreak::Before => vec![Doc::line_or_space(), Doc::text("and"), Doc::space()], OperatorLineBreak::After => vec![Doc::space(), Doc::text("and"), Doc::line_or_space()], }; - docs.extend( - ctx.end_spaced_comments(self.keyword.span.end, self.query_in_parens.span.start), - ); + docs.extend(ctx.end_spaced_comments( + ctx.get_comments_between(self.keyword.span.end, self.query_in_parens.span.start), + )); docs.push(self.query_in_parens.doc(ctx)); Doc::list(docs).group().nest(ctx.indent_width) @@ -58,9 +60,9 @@ impl<'s> DocGen<'s> for ContainerConditionNot<'s> { OperatorLineBreak::Before => vec![Doc::line_or_nil(), Doc::text("not"), Doc::space()], OperatorLineBreak::After => vec![Doc::text("not"), Doc::line_or_space()], }; - docs.extend( - ctx.end_spaced_comments(self.keyword.span.end, self.query_in_parens.span.start), - ); + docs.extend(ctx.end_spaced_comments( + ctx.get_comments_between(self.keyword.span.end, self.query_in_parens.span.start), + )); docs.push(self.query_in_parens.doc(ctx)); Doc::list(docs).group().nest(ctx.indent_width) @@ -75,9 +77,9 @@ impl<'s> DocGen<'s> for ContainerConditionOr<'s> { OperatorLineBreak::Before => vec![Doc::line_or_space(), Doc::text("or"), Doc::space()], OperatorLineBreak::After => vec![Doc::space(), Doc::text("or"), Doc::line_or_space()], }; - docs.extend( - ctx.end_spaced_comments(self.keyword.span.end, self.query_in_parens.span.start), - ); + docs.extend(ctx.end_spaced_comments( + ctx.get_comments_between(self.keyword.span.end, self.query_in_parens.span.start), + )); docs.push(self.query_in_parens.doc(ctx)); Doc::list(docs).group().nest(ctx.indent_width) @@ -90,7 +92,9 @@ impl<'s> DocGen<'s> for ContainerPrelude<'s> { if let Some(name) = &self.name { docs.push(name.doc(ctx)); docs.push(Doc::space()); - docs.extend(ctx.end_spaced_comments(name.span().start, self.condition.span.start)); + docs.extend(ctx.end_spaced_comments( + ctx.get_comments_between(name.span().start, self.condition.span.start), + )); } docs.push(self.condition.doc(ctx)); Doc::list(docs) @@ -101,24 +105,44 @@ impl<'s> DocGen<'s> for QueryInParens<'s> { fn doc(&self, ctx: &Ctx<'_, 's>) -> Doc<'s> { match &self.kind { QueryInParensKind::ContainerCondition(condition) => Doc::text("(") - .concat(ctx.end_spaced_comments(self.span.start, condition.span.start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.span.start, condition.span.start), + )) .append(condition.doc(ctx)) - .concat(ctx.start_spaced_comments(condition.span.end, self.span.end)) + .concat(ctx.start_spaced_comments( + ctx.get_comments_between(condition.span.end, self.span.end), + )) .append(Doc::text(")")), QueryInParensKind::SizeFeature(size_feature) => { let span = size_feature.span(); Doc::text("(") - .concat(ctx.end_spaced_comments(self.span.start, span.start)) + .concat( + ctx.end_spaced_comments( + ctx.get_comments_between(self.span.start, span.start), + ), + ) .append(size_feature.doc(ctx)) - .concat(ctx.start_spaced_comments(span.end, self.span.end)) + .concat( + ctx.start_spaced_comments( + ctx.get_comments_between(span.end, self.span.end), + ), + ) .append(Doc::text(")")) } QueryInParensKind::StyleQuery(style_query) => { let span = style_query.span(); Doc::text("style(") - .concat(ctx.end_spaced_comments(self.span.start, span.start)) + .concat( + ctx.end_spaced_comments( + ctx.get_comments_between(self.span.start, span.start), + ), + ) .append(style_query.doc(ctx)) - .concat(ctx.start_spaced_comments(span.end, self.span.end)) + .concat( + ctx.start_spaced_comments( + ctx.get_comments_between(span.end, self.span.end), + ), + ) .append(Doc::text(")")) } } @@ -134,7 +158,9 @@ impl<'s> DocGen<'s> for StyleCondition<'s> { (Vec::with_capacity(self.conditions.len()), self.span.start), |(mut docs, pos), condition| { let span = condition.span(); - docs.extend(ctx.start_spaced_comments(pos, span.start)); + docs.extend( + ctx.start_spaced_comments(ctx.get_comments_between(pos, span.start)), + ); docs.push(condition.doc(ctx)); (docs, span.end) }, @@ -152,9 +178,9 @@ impl<'s> DocGen<'s> for StyleConditionAnd<'s> { OperatorLineBreak::Before => vec![Doc::line_or_space(), Doc::text("and"), Doc::space()], OperatorLineBreak::After => vec![Doc::space(), Doc::text("and"), Doc::line_or_space()], }; - docs.extend( - ctx.end_spaced_comments(self.keyword.span.end, self.style_in_parens.span.start), - ); + docs.extend(ctx.end_spaced_comments( + ctx.get_comments_between(self.keyword.span.end, self.style_in_parens.span.start), + )); docs.push(self.style_in_parens.doc(ctx)); Doc::list(docs).group().nest(ctx.indent_width) @@ -180,9 +206,9 @@ impl<'s> DocGen<'s> for StyleConditionNot<'s> { OperatorLineBreak::Before => vec![Doc::line_or_nil(), Doc::text("not"), Doc::space()], OperatorLineBreak::After => vec![Doc::text("not"), Doc::line_or_space()], }; - docs.extend( - ctx.end_spaced_comments(self.keyword.span.end, self.style_in_parens.span.start), - ); + docs.extend(ctx.end_spaced_comments( + ctx.get_comments_between(self.keyword.span.end, self.style_in_parens.span.start), + )); docs.push(self.style_in_parens.doc(ctx)); Doc::list(docs).group().nest(ctx.indent_width) @@ -197,9 +223,9 @@ impl<'s> DocGen<'s> for StyleConditionOr<'s> { OperatorLineBreak::Before => vec![Doc::line_or_space(), Doc::text("or"), Doc::space()], OperatorLineBreak::After => vec![Doc::space(), Doc::text("or"), Doc::line_or_space()], }; - docs.extend( - ctx.end_spaced_comments(self.keyword.span.end, self.style_in_parens.span.start), - ); + docs.extend(ctx.end_spaced_comments( + ctx.get_comments_between(self.keyword.span.end, self.style_in_parens.span.start), + )); docs.push(self.style_in_parens.doc(ctx)); Doc::list(docs).group().nest(ctx.indent_width) @@ -210,9 +236,13 @@ impl<'s> DocGen<'s> for StyleInParens<'s> { fn doc(&self, ctx: &Ctx<'_, 's>) -> Doc<'s> { let kind_span = self.kind.span(); Doc::text("(") - .concat(ctx.end_spaced_comments(self.span.start, kind_span.start)) + .concat( + ctx.end_spaced_comments(ctx.get_comments_between(self.span.start, kind_span.start)), + ) .append(self.kind.doc(ctx)) - .concat(ctx.start_spaced_comments(kind_span.end, self.span.end)) + .concat( + ctx.start_spaced_comments(ctx.get_comments_between(kind_span.end, self.span.end)), + ) .append(Doc::text(")")) } } diff --git a/malva/src/doc_gen/at_rule/custom_media.rs b/malva/src/doc_gen/at_rule/custom_media.rs index 20f6805..f859c90 100644 --- a/malva/src/doc_gen/at_rule/custom_media.rs +++ b/malva/src/doc_gen/at_rule/custom_media.rs @@ -8,7 +8,9 @@ impl<'s> DocGen<'s> for CustomMedia<'s> { self.name .doc(ctx) .append(Doc::space()) - .concat(ctx.end_spaced_comments(self.name.span().end, self.value.span().start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.name.span().end, self.value.span().start), + )) .append(self.value.doc(ctx)) } } diff --git a/malva/src/doc_gen/at_rule/custom_selector.rs b/malva/src/doc_gen/at_rule/custom_selector.rs index 71505d0..e457066 100644 --- a/malva/src/doc_gen/at_rule/custom_selector.rs +++ b/malva/src/doc_gen/at_rule/custom_selector.rs @@ -51,9 +51,9 @@ impl<'s> DocGen<'s> for CustomSelectorPrelude<'s> { self.custom_selector .doc(ctx) .append(Doc::line_or_space().nest(ctx.indent_width)) - .concat( - ctx.end_spaced_comments(self.custom_selector.span.end, self.selector.span.start), - ) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.custom_selector.span.end, self.selector.span.start), + )) .append(self.selector.doc(ctx)) .group() } diff --git a/malva/src/doc_gen/at_rule/import.rs b/malva/src/doc_gen/at_rule/import.rs index 99cae3a..6725aed 100644 --- a/malva/src/doc_gen/at_rule/import.rs +++ b/malva/src/doc_gen/at_rule/import.rs @@ -12,7 +12,7 @@ impl<'s> DocGen<'s> for ImportPrelude<'s> { if let Some(layer) = &self.layer { let span = layer.span(); docs.push(Doc::line_or_space()); - docs.extend(ctx.end_spaced_comments(pos, span.start)); + docs.extend(ctx.end_spaced_comments(ctx.get_comments_between(pos, span.start))); docs.push(layer.doc(ctx)); pos = span.end; } @@ -20,14 +20,14 @@ impl<'s> DocGen<'s> for ImportPrelude<'s> { if let Some(supports) = &self.supports { let span = supports.span(); docs.push(Doc::line_or_space()); - docs.extend(ctx.end_spaced_comments(pos, span.start)); + docs.extend(ctx.end_spaced_comments(ctx.get_comments_between(pos, span.start))); docs.push(supports.doc(ctx)); pos = span.end; } if let Some(media) = &self.media { docs.push(Doc::line_or_space()); - docs.extend(ctx.end_spaced_comments(pos, media.span.start)); + docs.extend(ctx.end_spaced_comments(ctx.get_comments_between(pos, media.span.start))); docs.push(media.doc(ctx)); } @@ -57,14 +57,18 @@ impl<'s> DocGen<'s> for ImportPreludeSupports<'s> { fn doc(&self, ctx: &Ctx<'_, 's>) -> Doc<'s> { let kind_span = self.kind.span(); Doc::text("supports(") - .concat(ctx.end_spaced_comments(self.span.start, kind_span.start)) + .concat( + ctx.end_spaced_comments(ctx.get_comments_between(self.span.start, kind_span.start)), + ) .append(match &self.kind { ImportPreludeSupportsKind::SupportsCondition(supports_condition) => { supports_condition.doc(ctx) } ImportPreludeSupportsKind::Declaration(declaration) => declaration.doc(ctx), }) - .concat(ctx.start_spaced_comments(kind_span.end, self.span.end)) + .concat( + ctx.start_spaced_comments(ctx.get_comments_between(kind_span.end, self.span.end)), + ) .append(Doc::text(")")) } } diff --git a/malva/src/doc_gen/at_rule/media.rs b/malva/src/doc_gen/at_rule/media.rs index 34553e8..3605841 100644 --- a/malva/src/doc_gen/at_rule/media.rs +++ b/malva/src/doc_gen/at_rule/media.rs @@ -11,9 +11,9 @@ impl<'s> DocGen<'s> for MediaAnd<'s> { OperatorLineBreak::Before => vec![Doc::line_or_space(), Doc::text("and"), Doc::space()], OperatorLineBreak::After => vec![Doc::space(), Doc::text("and"), Doc::line_or_space()], }; - docs.extend( - ctx.end_spaced_comments(self.keyword.span.end, self.media_in_parens.span.start), - ); + docs.extend(ctx.end_spaced_comments( + ctx.get_comments_between(self.keyword.span.end, self.media_in_parens.span.start), + )); docs.push(self.media_in_parens.doc(ctx)); Doc::list(docs).group().nest(ctx.indent_width) @@ -29,7 +29,9 @@ impl<'s> DocGen<'s> for MediaCondition<'s> { (Vec::with_capacity(self.conditions.len()), self.span.start), |(mut docs, pos), condition| { let span = condition.span(); - docs.extend(ctx.start_spaced_comments(pos, span.start)); + docs.extend( + ctx.start_spaced_comments(ctx.get_comments_between(pos, span.start)), + ); docs.push(condition.doc(ctx)); (docs, span.end) }, @@ -47,7 +49,9 @@ impl<'s> DocGen<'s> for MediaConditionAfterMediaType<'s> { OperatorLineBreak::Before => vec![Doc::text("and"), Doc::space()], OperatorLineBreak::After => vec![Doc::text("and"), Doc::line_or_space()], }; - docs.extend(ctx.end_spaced_comments(self.and.span.end, self.condition.span.start)); + docs.extend(ctx.end_spaced_comments( + ctx.get_comments_between(self.and.span.end, self.condition.span.start), + )); docs.push(self.condition.doc(ctx)); Doc::list(docs).group().nest(ctx.indent_width) @@ -112,9 +116,13 @@ impl<'s> DocGen<'s> for MediaFeaturePlain<'s> { fn doc(&self, ctx: &Ctx<'_, 's>) -> Doc<'s> { self.name .doc(ctx) - .concat(ctx.start_spaced_comments(self.name.span().end, self.colon_span.start)) + .concat(ctx.start_spaced_comments( + ctx.get_comments_between(self.name.span().end, self.colon_span.start), + )) .append(Doc::text(": ")) - .concat(ctx.end_spaced_comments(self.colon_span.start, self.value.span().start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.colon_span.start, self.value.span().start), + )) .append(self.value.doc(ctx)) } } @@ -124,10 +132,14 @@ impl<'s> DocGen<'s> for MediaFeatureRange<'s> { self.left .doc(ctx) .append(Doc::space()) - .concat(ctx.end_spaced_comments(self.left.span().end, self.comparison.span.start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.left.span().end, self.comparison.span.start), + )) .append(self.comparison.doc(ctx)) .append(Doc::space()) - .concat(ctx.end_spaced_comments(self.comparison.span.end, self.right.span().start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.comparison.span.end, self.right.span().start), + )) .append(self.right.doc(ctx)) } } @@ -138,18 +150,24 @@ impl<'s> DocGen<'s> for MediaFeatureRangeInterval<'s> { self.left .doc(ctx) .append(Doc::space()) - .concat(ctx.end_spaced_comments(self.left.span().end, self.left_comparison.span.start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.left.span().end, self.left_comparison.span.start), + )) .append(self.left_comparison.doc(ctx)) .append(Doc::space()) - .concat(ctx.end_spaced_comments(self.left_comparison.span.end, name_span.start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.left_comparison.span.end, name_span.start), + )) .append(self.name.doc(ctx)) .append(Doc::space()) - .concat(ctx.end_spaced_comments(name_span.end, self.right_comparison.span.start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(name_span.end, self.right_comparison.span.start), + )) .append(self.right_comparison.doc(ctx)) .append(Doc::space()) - .concat( - ctx.end_spaced_comments(self.right_comparison.span.end, self.right.span().start), - ) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.right_comparison.span.end, self.right.span().start), + )) .append(self.right.doc(ctx)) } } @@ -163,9 +181,13 @@ impl<'s> DocGen<'s> for MediaInParens<'s> { let kind_span = self.kind.span(); Doc::text("(") - .concat(ctx.end_spaced_comments(self.span.start, kind_span.start)) + .concat( + ctx.end_spaced_comments(ctx.get_comments_between(self.span.start, kind_span.start)), + ) .append(kind) - .concat(ctx.start_spaced_comments(kind_span.end, self.span.end)) + .concat( + ctx.start_spaced_comments(ctx.get_comments_between(kind_span.end, self.span.end)), + ) .append(Doc::text(")")) } } @@ -178,9 +200,9 @@ impl<'s> DocGen<'s> for MediaNot<'s> { OperatorLineBreak::Before => vec![Doc::line_or_nil(), Doc::text("not"), Doc::space()], OperatorLineBreak::After => vec![Doc::text("not"), Doc::line_or_space()], }; - docs.extend( - ctx.end_spaced_comments(self.keyword.span.end, self.media_in_parens.span.start), - ); + docs.extend(ctx.end_spaced_comments( + ctx.get_comments_between(self.keyword.span.end, self.media_in_parens.span.start), + )); docs.push(self.media_in_parens.doc(ctx)); Doc::list(docs).group().nest(ctx.indent_width) @@ -195,9 +217,9 @@ impl<'s> DocGen<'s> for MediaOr<'s> { OperatorLineBreak::Before => vec![Doc::line_or_space(), Doc::text("or"), Doc::space()], OperatorLineBreak::After => vec![Doc::space(), Doc::text("or"), Doc::line_or_space()], }; - docs.extend( - ctx.end_spaced_comments(self.keyword.span.end, self.media_in_parens.span.start), - ); + docs.extend(ctx.end_spaced_comments( + ctx.get_comments_between(self.keyword.span.end, self.media_in_parens.span.start), + )); docs.push(self.media_in_parens.doc(ctx)); Doc::list(docs).group().nest(ctx.indent_width) @@ -223,12 +245,16 @@ impl<'s> DocGen<'s> for MediaQueryWithType<'s> { if let Some(modifier) = &self.modifier { docs.push(modifier.doc(ctx)); docs.push(Doc::space()); - docs.extend(ctx.end_spaced_comments(modifier.span.end, media_type_span.start)); + docs.extend(ctx.end_spaced_comments( + ctx.get_comments_between(modifier.span.end, media_type_span.start), + )); } docs.push(self.media_type.doc(ctx)); if let Some(condition) = &self.condition { docs.push(Doc::space()); - docs.extend(ctx.end_spaced_comments(media_type_span.end, condition.span.start)); + docs.extend(ctx.end_spaced_comments( + ctx.get_comments_between(media_type_span.end, condition.span.start), + )); docs.push(condition.doc(ctx)); } Doc::list(docs) diff --git a/malva/src/doc_gen/at_rule/mod.rs b/malva/src/doc_gen/at_rule/mod.rs index 29a7732..34d5223 100644 --- a/malva/src/doc_gen/at_rule/mod.rs +++ b/malva/src/doc_gen/at_rule/mod.rs @@ -31,7 +31,7 @@ impl<'s> DocGen<'s> for AtRule<'s> { if let Some(prelude) = &self.prelude { docs.push(Doc::space()); let span = prelude.span(); - docs.extend(ctx.end_spaced_comments(pos, span.start)); + docs.extend(ctx.end_spaced_comments(ctx.get_comments_between(pos, span.start))); docs.push(prelude.doc(ctx)); pos = span.end; } diff --git a/malva/src/doc_gen/at_rule/namespace.rs b/malva/src/doc_gen/at_rule/namespace.rs index 44275f3..297e365 100644 --- a/malva/src/doc_gen/at_rule/namespace.rs +++ b/malva/src/doc_gen/at_rule/namespace.rs @@ -9,7 +9,9 @@ impl<'s> DocGen<'s> for NamespacePrelude<'s> { prefix .doc(ctx) .append(Doc::line_or_space()) - .concat(ctx.end_spaced_comments(prefix.span().end, self.uri.span().start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(prefix.span().end, self.uri.span().start), + )) .append(self.uri.doc(ctx)) .group() .nest(ctx.indent_width) diff --git a/malva/src/doc_gen/at_rule/scope.rs b/malva/src/doc_gen/at_rule/scope.rs index b4b14d5..5136bb7 100644 --- a/malva/src/doc_gen/at_rule/scope.rs +++ b/malva/src/doc_gen/at_rule/scope.rs @@ -6,7 +6,9 @@ use tiny_pretty::Doc; impl<'s> DocGen<'s> for ScopeEnd<'s> { fn doc(&self, ctx: &Ctx<'_, 's>) -> Doc<'s> { Doc::text("to ") - .concat(ctx.end_spaced_comments(self.to_span.end, self.lparen_span.start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.to_span.end, self.lparen_span.start), + )) .append(helpers::format_parenthesized( self.selector.doc(ctx), self.selector.span.end, diff --git a/malva/src/doc_gen/at_rule/supports.rs b/malva/src/doc_gen/at_rule/supports.rs index 6bc1ac4..660a352 100644 --- a/malva/src/doc_gen/at_rule/supports.rs +++ b/malva/src/doc_gen/at_rule/supports.rs @@ -11,7 +11,9 @@ impl<'s> DocGen<'s> for SupportsAnd<'s> { OperatorLineBreak::Before => vec![Doc::line_or_space(), Doc::text("and"), Doc::space()], OperatorLineBreak::After => vec![Doc::space(), Doc::text("and"), Doc::line_or_space()], }; - docs.extend(ctx.end_spaced_comments(self.keyword.span.end, self.condition.span.start)); + docs.extend(ctx.end_spaced_comments( + ctx.get_comments_between(self.keyword.span.end, self.condition.span.start), + )); docs.push(self.condition.doc(ctx)); Doc::list(docs).group().nest(ctx.indent_width) @@ -27,7 +29,9 @@ impl<'s> DocGen<'s> for SupportsCondition<'s> { (Vec::with_capacity(self.conditions.len()), self.span.start), |(mut docs, pos), condition| { let span = condition.span(); - docs.extend(ctx.start_spaced_comments(pos, span.start)); + docs.extend( + ctx.start_spaced_comments(ctx.get_comments_between(pos, span.start)), + ); docs.push(condition.doc(ctx)); (docs, span.end) }, @@ -53,9 +57,15 @@ impl<'s> DocGen<'s> for SupportsConditionKind<'s> { impl<'s> DocGen<'s> for SupportsDecl<'s> { fn doc(&self, ctx: &Ctx<'_, 's>) -> Doc<'s> { Doc::text("(") - .concat(ctx.end_spaced_comments(self.span.start, self.decl.span.start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.span.start, self.decl.span.start), + )) .append(self.decl.doc(ctx)) - .concat(ctx.start_spaced_comments(self.decl.span.end, self.span.end)) + .concat( + ctx.start_spaced_comments( + ctx.get_comments_between(self.decl.span.end, self.span.end), + ), + ) .append(Doc::text(")")) } } @@ -65,16 +75,24 @@ impl<'s> DocGen<'s> for SupportsInParens<'s> { match &self.kind { SupportsInParensKind::Feature(feature) => feature.doc(ctx), SupportsInParensKind::SupportsCondition(condition) => Doc::text("(") - .concat(ctx.end_spaced_comments(self.span.start, condition.span.start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.span.start, condition.span.start), + )) .append(condition.doc(ctx)) - .concat(ctx.start_spaced_comments(condition.span.end, self.span.end)) + .concat(ctx.start_spaced_comments( + ctx.get_comments_between(condition.span.end, self.span.end), + )) .append(Doc::text(")")), SupportsInParensKind::Selector(selector) => Doc::text("selector(") .append( Doc::line_or_nil() - .concat(ctx.end_spaced_comments(self.span.start, selector.span.start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.span.start, selector.span.start), + )) .append(selector.doc(ctx)) - .concat(ctx.start_spaced_comments(selector.span.end, self.span.end)) + .concat(ctx.start_spaced_comments( + ctx.get_comments_between(selector.span.end, self.span.end), + )) .nest(ctx.indent_width) .append(Doc::line_or_nil()) .group(), @@ -93,7 +111,9 @@ impl<'s> DocGen<'s> for SupportsNot<'s> { OperatorLineBreak::Before => vec![Doc::line_or_nil(), Doc::text("not"), Doc::space()], OperatorLineBreak::After => vec![Doc::text("not"), Doc::line_or_space()], }; - docs.extend(ctx.end_spaced_comments(self.keyword.span.end, self.condition.span.start)); + docs.extend(ctx.end_spaced_comments( + ctx.get_comments_between(self.keyword.span.end, self.condition.span.start), + )); docs.push(self.condition.doc(ctx)); Doc::list(docs).group().nest(ctx.indent_width) @@ -108,7 +128,9 @@ impl<'s> DocGen<'s> for SupportsOr<'s> { OperatorLineBreak::Before => vec![Doc::line_or_space(), Doc::text("or"), Doc::space()], OperatorLineBreak::After => vec![Doc::space(), Doc::text("or"), Doc::line_or_space()], }; - docs.extend(ctx.end_spaced_comments(self.keyword.span.end, self.condition.span.start)); + docs.extend(ctx.end_spaced_comments( + ctx.get_comments_between(self.keyword.span.end, self.condition.span.start), + )); docs.push(self.condition.doc(ctx)); Doc::list(docs).group().nest(ctx.indent_width) diff --git a/malva/src/doc_gen/helpers.rs b/malva/src/doc_gen/helpers.rs index 223923d..775f03f 100644 --- a/malva/src/doc_gen/helpers.rs +++ b/malva/src/doc_gen/helpers.rs @@ -70,8 +70,7 @@ impl SeparatedListFormatter { let list_item_span = list_item.span(); let mut docs = ctx .end_spaced_comments_without_last_space( - *pos, - list_item_span.start, + ctx.get_comments_between(*pos, list_item_span.start), &mut comment_end, ) .collect::>(); @@ -85,9 +84,9 @@ impl SeparatedListFormatter { } } docs.push(list_item.doc(ctx)); - docs.extend( - ctx.start_spaced_comments(list_item_span.end, separator_span.start), - ); + docs.extend(ctx.start_spaced_comments( + ctx.get_comments_between(list_item_span.end, separator_span.start), + )); *pos = separator_span.end; Some(docs.into_iter()) } @@ -96,8 +95,7 @@ impl SeparatedListFormatter { let list_item_span = list_item.span(); let mut docs = ctx .end_spaced_comments_without_last_space( - *pos, - list_item_span.start, + ctx.get_comments_between(*pos, list_item_span.start), &mut comment_end, ) .collect::>(); @@ -151,20 +149,22 @@ pub(super) fn format_values_list<'s>( EitherOrBoth::Both(value, comma_span) => { let value_span = value.span(); let mut docs = ctx - .end_spaced_comments( + .end_spaced_comments(ctx.get_comments_between( mem::replace(pos, comma_span.end), value_span.start, - ) + )) .collect::>(); docs.push(value.doc(ctx)); - docs.extend( - ctx.start_spaced_comments(value_span.end, comma_span.start), - ); + docs.extend(ctx.start_spaced_comments( + ctx.get_comments_between(value_span.end, comma_span.start), + )); Some(docs.into_iter()) } EitherOrBoth::Left(value) => { let mut docs = ctx - .end_spaced_comments(*pos, value.span().start) + .end_spaced_comments( + ctx.get_comments_between(*pos, value.span().start), + ) .collect::>(); docs.push(value.doc(ctx)); Some(docs.into_iter()) @@ -185,23 +185,29 @@ pub(super) fn format_values_list<'s>( }) .group() } else { - let mut docs = itertools::intersperse( - values.iter().scan(list_span.start, |pos, value| { - let value_span = value.span(); - Some( - ctx.end_spaced_comments(mem::replace(pos, value_span.end), value_span.start) + let mut docs = + itertools::intersperse( + values.iter().scan(list_span.start, |pos, value| { + let value_span = value.span(); + Some( + ctx.end_spaced_comments(ctx.get_comments_between( + mem::replace(pos, value_span.end), + value_span.start, + )) .chain(iter::once(value.doc(ctx))) .collect::>() .into_iter(), - ) - }), - vec![Doc::line_or_space()].into_iter(), - ) - .flatten() - .collect::>(); + ) + }), + vec![Doc::line_or_space()].into_iter(), + ) + .flatten() + .collect::>(); if let Some(last) = values.last() { - docs.extend(ctx.start_spaced_comments(last.span().end, list_span.end)); + docs.extend( + ctx.start_spaced_comments(ctx.get_comments_between(last.span().end, list_span.end)), + ); } Doc::list(docs).group() @@ -243,8 +249,7 @@ pub(super) fn format_parenthesized<'s>( Doc::line_or_nil() .append(body) .concat(ctx.start_spaced_comments_without_last_hard_line( - trailing_comments_start, - trailing_comments_end, + ctx.get_comments_between(trailing_comments_start, trailing_comments_end), &mut has_last_line_comment, )) .nest(ctx.indent_width) @@ -267,14 +272,14 @@ pub(super) fn format_space_before_block<'s>( let mut has_last_line_comment = false; Doc::list( ctx.start_spaced_comments_without_last_hard_line( - previous_end, - block_start, + ctx.get_comments_between(previous_end, block_start), &mut has_last_line_comment, ) .collect(), ) } else { - Doc::space().concat(ctx.end_spaced_comments(previous_end, block_start)) + Doc::space() + .concat(ctx.end_spaced_comments(ctx.get_comments_between(previous_end, block_start))) } } diff --git a/malva/src/doc_gen/less.rs b/malva/src/doc_gen/less.rs index bc7b0f4..4d9a755 100644 --- a/malva/src/doc_gen/less.rs +++ b/malva/src/doc_gen/less.rs @@ -16,10 +16,14 @@ impl<'s> DocGen<'s> for LessBinaryCondition<'s> { self.left .doc(ctx) .append(helpers::format_operator_prefix_space(ctx)) - .concat(ctx.end_spaced_comments(self.left.span().end, self.op.span.start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.left.span().end, self.op.span.start), + )) .append(self.op.doc(ctx)) .append(helpers::format_operator_suffix_space(ctx)) - .concat(ctx.end_spaced_comments(self.op.span.end, self.right.span().start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.op.span.end, self.right.span().start), + )) .append(self.right.doc(ctx)) .group() } @@ -46,10 +50,14 @@ impl<'s> DocGen<'s> for LessBinaryOperation<'s> { self.left .doc(ctx) .append(helpers::format_operator_prefix_space(ctx)) - .concat(ctx.end_spaced_comments(self.left.span().end, self.op.span.start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.left.span().end, self.op.span.start), + )) .append(self.op.doc(ctx)) .append(helpers::format_operator_suffix_space(ctx)) - .concat(ctx.end_spaced_comments(self.op.span.end, self.right.span().start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.op.span.end, self.right.span().start), + )) .append(self.right.doc(ctx)) .group() } @@ -76,7 +84,9 @@ impl<'s> DocGen<'s> for LessConditionalQualifiedRule<'s> { ) .append(Doc::soft_line()) .append(self.guard.doc(ctx)) - .concat(ctx.end_spaced_comments(self.selector.span.end, self.guard.span.start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.selector.span.end, self.guard.span.start), + )) .append(helpers::format_space_before_block( self.guard.span.end, self.block.span.start, @@ -140,10 +150,10 @@ impl<'s> DocGen<'s> for LessExtendRule<'s> { fn doc(&self, ctx: &Ctx<'_, 's>) -> Doc<'s> { self.nesting_selector .doc(ctx) - .concat(ctx.unspaced_comments( + .concat(ctx.unspaced_comments(ctx.get_comments_between( self.nesting_selector.span.end, self.name_of_extend.span.start, - )) + ))) .append(Doc::text(":extend(")) .append({ let mut extend_doc = vec![]; @@ -152,10 +162,14 @@ impl<'s> DocGen<'s> for LessExtendRule<'s> { extend_doc.push(Doc::line_or_nil()); } - extend_doc.extend(ctx.end_spaced_comments(self.span.start, self.extend.span.start)); + extend_doc.extend(ctx.end_spaced_comments( + ctx.get_comments_between(self.span.start, self.extend.span.start), + )); extend_doc.push(self.extend.doc(ctx)); - extend_doc.extend(ctx.start_spaced_comments(self.extend.span.end, self.span.end)); + extend_doc.extend(ctx.start_spaced_comments( + ctx.get_comments_between(self.extend.span.end, self.span.end), + )); if ctx.options.linebreak_in_pseudo_parens { Doc::list(extend_doc) .nest(ctx.indent_width) @@ -202,14 +216,14 @@ impl<'s> DocGen<'s> for LessImportPrelude<'s> { docs.push(Doc::line_or_space()); let href_span = self.href.span(); - docs.extend( - ctx.end_spaced_comments(mem::replace(&mut pos, href_span.end), href_span.start), - ); + docs.extend(ctx.end_spaced_comments( + ctx.get_comments_between(mem::replace(&mut pos, href_span.end), href_span.start), + )); docs.push(self.href.doc(ctx)); if let Some(media) = &self.media { docs.push(Doc::line_or_space()); - docs.extend(ctx.end_spaced_comments(pos, media.span.start)); + docs.extend(ctx.end_spaced_comments(ctx.get_comments_between(pos, media.span.start))); docs.push(media.doc(ctx)); } @@ -305,13 +319,23 @@ impl<'s> DocGen<'s> for LessLookup<'s> { if let Some(name) = &self.name { let name_span = name.span(); Doc::text("[") - .concat(ctx.end_spaced_comments(self.span.start, name_span.start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.span.start, name_span.start), + )) .append(name.doc(ctx)) - .concat(ctx.start_spaced_comments(name_span.end, self.span.end)) + .concat( + ctx.start_spaced_comments( + ctx.get_comments_between(name_span.end, self.span.end), + ), + ) .append(Doc::text("]")) } else { Doc::text("[") - .concat(ctx.end_spaced_comments(self.span.start, self.span.end)) + .concat( + ctx.end_spaced_comments( + ctx.get_comments_between(self.span.start, self.span.end), + ), + ) .append(Doc::text("]")) } } @@ -339,10 +363,10 @@ impl<'s> DocGen<'s> for LessLookups<'s> { .iter() .scan(self.span.start, |pos, lookup| { Some( - ctx.start_spaced_comments( + ctx.start_spaced_comments(ctx.get_comments_between( mem::replace(pos, lookup.span.end), lookup.span.start, - ) + )) .chain(iter::once(lookup.doc(ctx))), ) }) @@ -397,11 +421,13 @@ impl<'s> DocGen<'s> for LessMixinArguments<'s> { ) .concat( ctx.start_spaced_comments_without_last_hard_line( - self.args - .last() - .map(|arg| arg.span().end) - .unwrap_or(self.span.start), - self.span.end, + ctx.get_comments_between( + self.args + .last() + .map(|arg| arg.span().end) + .unwrap_or(self.span.start), + self.span.end, + ), &mut has_last_line_comment, ), ) @@ -433,7 +459,9 @@ impl<'s> DocGen<'s> for LessMixinCall<'s> { if let Some(important) = &self.important { docs.push(Doc::soft_line().nest(ctx.indent_width)); - docs.extend(ctx.end_spaced_comments(pos, important.span.start)); + docs.extend( + ctx.end_spaced_comments(ctx.get_comments_between(pos, important.span.start)), + ); docs.push(important.doc(ctx)); } @@ -455,9 +483,11 @@ impl<'s> DocGen<'s> for LessMixinCallee<'s> { let (docs, _) = iter.fold((docs, pos), |(mut docs, pos), child| { if pos < child.span.start { docs.push(Doc::line_or_space().nest(ctx.indent_width)); - docs.extend(ctx.end_spaced_comments(pos, child.span.start)); + docs.extend( + ctx.end_spaced_comments(ctx.get_comments_between(pos, child.span.start)), + ); } else { - docs.extend(ctx.unspaced_comments(pos, child.span.start)); + docs.extend(ctx.unspaced_comments(ctx.get_comments_between(pos, child.span.start))); } docs.push(child.doc(ctx)); (docs, child.span.end) @@ -473,7 +503,9 @@ impl<'s> DocGen<'s> for LessMixinCalleeChild<'s> { combinator .doc(ctx) .append(Doc::space()) - .concat(ctx.end_spaced_comments(combinator.span.end, self.name.span().start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(combinator.span.end, self.name.span().start), + )) .append(self.name.doc(ctx)) } else { self.name.doc(ctx) @@ -488,7 +520,9 @@ impl<'s> DocGen<'s> for LessMixinDefinition<'s> { if let Some(guard) = &self.guard { docs.push(Doc::soft_line()); - docs.extend(ctx.end_spaced_comments(self.params.span.end, guard.span.start)); + docs.extend(ctx.end_spaced_comments( + ctx.get_comments_between(self.params.span.end, guard.span.start), + )); docs.push(guard.doc(ctx)); pos = guard.span.end; } @@ -517,9 +551,13 @@ impl<'s> DocGen<'s> for LessMixinNamedArgument<'s> { fn doc(&self, ctx: &Ctx<'_, 's>) -> Doc<'s> { self.name .doc(ctx) - .concat(ctx.start_spaced_comments(self.name.span().end, self.colon_span.start)) + .concat(ctx.start_spaced_comments( + ctx.get_comments_between(self.name.span().end, self.colon_span.start), + )) .append(Doc::text(": ")) - .concat(ctx.end_spaced_comments(self.colon_span.end, self.value.span().start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.colon_span.end, self.value.span().start), + )) .append(self.value.doc(ctx)) } } @@ -528,8 +566,10 @@ impl<'s> DocGen<'s> for LessMixinNamedParameter<'s> { fn doc(&self, ctx: &Ctx<'_, 's>) -> Doc<'s> { let name = self.name.doc(ctx); if let Some(value) = &self.value { - name.concat(ctx.start_spaced_comments(self.name.span().end, value.span.start)) - .append(value.doc(ctx)) + name.concat(ctx.start_spaced_comments( + ctx.get_comments_between(self.name.span().end, value.span.start), + )) + .append(value.doc(ctx)) } else { name } @@ -539,7 +579,9 @@ impl<'s> DocGen<'s> for LessMixinNamedParameter<'s> { impl<'s> DocGen<'s> for LessMixinNamedParameterDefaultValue<'s> { fn doc(&self, ctx: &Ctx<'_, 's>) -> Doc<'s> { Doc::text(": ") - .concat(ctx.end_spaced_comments(self.colon_span.end, self.value.span().start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.colon_span.end, self.value.span().start), + )) .append(self.value.doc(ctx)) } } @@ -610,7 +652,9 @@ impl<'s> DocGen<'s> for LessNamespaceValue<'s> { fn doc(&self, ctx: &Ctx<'_, 's>) -> Doc<'s> { self.callee .doc(ctx) - .concat(ctx.unspaced_comments(self.callee.span().end, self.lookups.span.start)) + .concat(ctx.unspaced_comments( + ctx.get_comments_between(self.callee.span().end, self.lookups.span.start), + )) .append(self.lookups.doc(ctx)) } } @@ -629,8 +673,10 @@ impl<'s> DocGen<'s> for LessNegatedCondition<'s> { let condition_span = self.condition.span(); Doc::text("not").append(helpers::format_parenthesized( Doc::list( - ctx.end_spaced_comments(self.span.start, condition_span.start) - .collect(), + ctx.end_spaced_comments( + ctx.get_comments_between(self.span.start, condition_span.start), + ) + .collect(), ) .append(self.condition.doc(ctx)), condition_span.end, @@ -662,8 +708,10 @@ impl<'s> DocGen<'s> for LessParenthesizedCondition<'s> { let condition_span = self.condition.span(); helpers::format_parenthesized( Doc::list( - ctx.end_spaced_comments(self.span.start, condition_span.start) - .collect(), + ctx.end_spaced_comments( + ctx.get_comments_between(self.span.start, condition_span.start), + ) + .collect(), ) .append(self.condition.doc(ctx)), condition_span.end, @@ -678,8 +726,10 @@ impl<'s> DocGen<'s> for LessParenthesizedOperation<'s> { let operation_span = self.operation.span(); helpers::format_parenthesized( Doc::list( - ctx.end_spaced_comments(self.span.start, operation_span.start) - .collect(), + ctx.end_spaced_comments( + ctx.get_comments_between(self.span.start, operation_span.start), + ) + .collect(), ) .append(self.operation.doc(ctx)), operation_span.end, @@ -766,7 +816,9 @@ impl<'s> DocGen<'s> for LessVariableDeclaration<'s> { docs.push(self.name.doc(ctx)); - docs.extend(ctx.start_spaced_comments(self.name.span.end, self.colon_span.start)); + docs.extend(ctx.start_spaced_comments( + ctx.get_comments_between(self.name.span.end, self.colon_span.start), + )); docs.push(Doc::text(":")); let should_group = if let ComponentValue::LessList(LessList { @@ -777,7 +829,9 @@ impl<'s> DocGen<'s> for LessVariableDeclaration<'s> { }) = &self.value { docs.push(Doc::line_or_space()); - docs.extend(ctx.end_spaced_comments(self.colon_span.end, value_span.start)); + docs.extend(ctx.end_spaced_comments( + ctx.get_comments_between(self.colon_span.end, value_span.start), + )); docs.push( helpers::SeparatedListFormatter::new(",", Doc::line_or_space()).format( elements, @@ -792,7 +846,9 @@ impl<'s> DocGen<'s> for LessVariableDeclaration<'s> { true } else { docs.push(Doc::space()); - docs.extend(ctx.end_spaced_comments(self.colon_span.end, value_span.start)); + docs.extend(ctx.end_spaced_comments( + ctx.get_comments_between(self.colon_span.end, value_span.start), + )); docs.push(self.value.doc(ctx)); false }; diff --git a/malva/src/doc_gen/sass.rs b/malva/src/doc_gen/sass.rs index 97c3a50..e2e0d7f 100644 --- a/malva/src/doc_gen/sass.rs +++ b/malva/src/doc_gen/sass.rs @@ -36,10 +36,14 @@ impl<'s> DocGen<'s> for SassAtRootQuery<'s> { fn doc(&self, ctx: &Ctx<'_, 's>) -> Doc<'s> { let mut docs = Vec::with_capacity(7); docs.push(Doc::text("(")); - docs.extend(ctx.end_spaced_comments(self.span.start, self.modifier.span.start)); + docs.extend(ctx.end_spaced_comments( + ctx.get_comments_between(self.span.start, self.modifier.span.start), + )); docs.push(self.modifier.doc(ctx)); - docs.extend(ctx.start_spaced_comments(self.modifier.span.end, self.colon_span.start)); + docs.extend(ctx.start_spaced_comments( + ctx.get_comments_between(self.modifier.span.end, self.colon_span.start), + )); docs.push(Doc::text(": ")); docs.extend( @@ -47,10 +51,13 @@ impl<'s> DocGen<'s> for SassAtRootQuery<'s> { self.rules.iter().scan(self.colon_span.start, |pos, rule| { let rule_span = rule.span(); Some( - ctx.end_spaced_comments(mem::replace(pos, rule_span.end), rule_span.start) - .chain(iter::once(rule.doc(ctx))) - .collect::>() - .into_iter(), + ctx.end_spaced_comments(ctx.get_comments_between( + mem::replace(pos, rule_span.end), + rule_span.start, + )) + .chain(iter::once(rule.doc(ctx))) + .collect::>() + .into_iter(), ) }), vec![Doc::soft_line()].into_iter(), @@ -59,7 +66,9 @@ impl<'s> DocGen<'s> for SassAtRootQuery<'s> { ); if let Some(last) = self.rules.last() { - docs.extend(ctx.start_spaced_comments(last.span().end, self.span.end)); + docs.extend( + ctx.start_spaced_comments(ctx.get_comments_between(last.span().end, self.span.end)), + ); } docs.push(Doc::text(")")); @@ -90,10 +99,14 @@ impl<'s> DocGen<'s> for SassBinaryExpression<'s> { self.left .doc(ctx) .append(helpers::format_operator_prefix_space(ctx)) - .concat(ctx.end_spaced_comments(self.left.span().end, self.op.span.start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.left.span().end, self.op.span.start), + )) .append(self.op.doc(ctx)) .append(helpers::format_operator_suffix_space(ctx)) - .concat(ctx.end_spaced_comments(self.op.span.end, self.right.span().start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.op.span.end, self.right.span().start), + )) .append(self.right.doc(ctx)) .group() } @@ -161,12 +174,17 @@ impl<'s> DocGen<'s> for SassEach<'s> { .group() .nest(ctx.indent_width) .append(helpers::format_operator_prefix_space(ctx)) - .concat( - ctx.end_spaced_comments(self.bindings.last().unwrap().span.end, self.in_span.start), - ) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between( + self.bindings.last().unwrap().span.end, + self.in_span.start, + ), + )) .append(Doc::text("in")) .append(helpers::format_operator_suffix_space(ctx)) - .concat(ctx.end_spaced_comments(self.in_span.end, self.expr.span().start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.in_span.end, self.expr.span().start), + )) .append(self.expr.doc(ctx).nest(ctx.indent_width)) .group() } @@ -186,7 +204,9 @@ impl<'s> DocGen<'s> for SassExtend<'s> { if let Some(optional) = &self.optional { selectors .append(Doc::space()) - .concat(ctx.end_spaced_comments(self.selectors.span().end, optional.span.start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.selectors.span().end, optional.span.start), + )) .append(optional.doc(ctx)) } else { selectors @@ -211,25 +231,33 @@ impl<'s> DocGen<'s> for SassFor<'s> { OperatorLineBreak::Before => Doc::soft_line().nest(ctx.indent_width), OperatorLineBreak::After => Doc::space(), }) - .concat(ctx.end_spaced_comments(self.binding.span.end, self.from_span.start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.binding.span.end, self.from_span.start), + )) .append(Doc::text("from")) .append(match ctx.options.operator_linebreak { OperatorLineBreak::Before => Doc::space(), OperatorLineBreak::After => Doc::soft_line().nest(ctx.indent_width), }) - .concat(ctx.end_spaced_comments(self.from_span.end, start_value_span.start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.from_span.end, start_value_span.start), + )) .append(self.start.doc(ctx)) .append(match ctx.options.operator_linebreak { OperatorLineBreak::Before => Doc::soft_line().nest(ctx.indent_width), OperatorLineBreak::After => Doc::space(), }) - .concat(ctx.end_spaced_comments(start_value_span.end, self.boundary.span.start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(start_value_span.end, self.boundary.span.start), + )) .append(self.boundary.doc(ctx)) .append(match ctx.options.operator_linebreak { OperatorLineBreak::Before => Doc::space(), OperatorLineBreak::After => Doc::soft_line().nest(ctx.indent_width), }) - .concat(ctx.end_spaced_comments(self.boundary.span.end, self.end.span().start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.boundary.span.end, self.end.span().start), + )) .append(self.end.doc(ctx)) } } @@ -251,28 +279,34 @@ impl<'s> DocGen<'s> for SassForward<'s> { if let Some(prefix) = &self.prefix { docs.reserve(2); docs.push(Doc::space()); - docs.extend( - ctx.end_spaced_comments(mem::replace(&mut pos, prefix.span.end), prefix.span.start), - ); + docs.extend(ctx.end_spaced_comments( + ctx.get_comments_between( + mem::replace(&mut pos, prefix.span.end), + prefix.span.start, + ), + )); docs.push(prefix.doc(ctx)); } if let Some(visibility) = &self.visibility { docs.reserve(2); docs.push(Doc::space()); - docs.extend(ctx.end_spaced_comments( + docs.extend(ctx.end_spaced_comments(ctx.get_comments_between( mem::replace(&mut pos, visibility.span.end), visibility.span.start, - )); + ))); docs.push(visibility.doc(ctx)); } if let Some(config) = &self.config { docs.reserve(2); docs.push(Doc::space()); - docs.extend( - ctx.end_spaced_comments(mem::replace(&mut pos, config.span.end), config.span.start), - ); + docs.extend(ctx.end_spaced_comments( + ctx.get_comments_between( + mem::replace(&mut pos, config.span.end), + config.span.start, + ), + )); docs.push(config.doc(ctx)); } @@ -292,7 +326,9 @@ impl<'s> DocGen<'s> for SassForwardMember<'s> { impl<'s> DocGen<'s> for SassForwardPrefix<'s> { fn doc(&self, ctx: &Ctx<'_, 's>) -> Doc<'s> { Doc::text("as ") - .concat(ctx.end_spaced_comments(self.as_span.end, self.name.span.start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.as_span.end, self.name.span.start), + )) .append(self.name.doc(ctx)) .append(Doc::text("*")) } @@ -329,7 +365,9 @@ impl<'s> DocGen<'s> for SassFunction<'s> { impl<'s> DocGen<'s> for SassIfAtRule<'s> { fn doc(&self, ctx: &Ctx<'_, 's>) -> Doc<'s> { let mut docs = vec![Doc::text("@if ")]; - docs.extend(ctx.end_spaced_comments(self.span.start, self.if_clause.span.start)); + docs.extend(ctx.end_spaced_comments( + ctx.get_comments_between(self.span.start, self.if_clause.span.start), + )); docs.push(self.if_clause.doc(ctx)); let mut pos = self.if_clause.span.end; @@ -340,15 +378,15 @@ impl<'s> DocGen<'s> for SassIfAtRule<'s> { .scan(&mut pos, |pos, (clause, elseif_span)| { Some( iter::once(Doc::space()) - .chain(ctx.end_spaced_comments( + .chain(ctx.end_spaced_comments(ctx.get_comments_between( mem::replace(*pos, elseif_span.end), elseif_span.start, - )) + ))) .chain(iter::once(Doc::text("@else if "))) - .chain(ctx.end_spaced_comments( + .chain(ctx.end_spaced_comments(ctx.get_comments_between( mem::replace(*pos, clause.span.end), clause.span.start, - )) + ))) .chain(iter::once(clause.doc(ctx))), ) }) @@ -360,7 +398,7 @@ impl<'s> DocGen<'s> for SassIfAtRule<'s> { { docs.reserve(3); docs.push(Doc::space()); - docs.extend(ctx.end_spaced_comments(pos, else_span.start)); + docs.extend(ctx.end_spaced_comments(ctx.get_comments_between(pos, else_span.start))); docs.push(Doc::text("@else")); docs.push(helpers::format_space_before_block( else_span.end, @@ -388,20 +426,20 @@ impl<'s> DocGen<'s> for SassInclude<'s> { let mut pos = self.name.span().end; if let Some(arguments) = &self.arguments { - docs.extend(ctx.end_spaced_comments( + docs.extend(ctx.end_spaced_comments(ctx.get_comments_between( mem::replace(&mut pos, arguments.span.end), arguments.span.start, - )); + ))); docs.push(arguments.doc(ctx)); } if let Some(content_block_params) = &self.content_block_params { docs.reserve(2); docs.push(Doc::space()); - docs.extend(ctx.end_spaced_comments( + docs.extend(ctx.end_spaced_comments(ctx.get_comments_between( mem::replace(&mut pos, content_block_params.span.end), content_block_params.span.start, - )); + ))); docs.push(content_block_params.doc(ctx)); } @@ -434,7 +472,9 @@ impl<'s> DocGen<'s> for SassIncludeArgs<'s> { impl<'s> DocGen<'s> for SassIncludeContentBlockParams<'s> { fn doc(&self, ctx: &Ctx<'_, 's>) -> Doc<'s> { Doc::text("using ") - .concat(ctx.end_spaced_comments(self.using_span.end, self.params.span.start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.using_span.end, self.params.span.start), + )) .append(self.params.doc(ctx)) } } @@ -453,14 +493,18 @@ impl<'s> DocGen<'s> for SassInterpolatedIdent<'s> { SassInterpolatedIdentElement::Expression(expr) => { let expr_span = expr.span(); docs.push(Doc::text("#{")); - docs.extend(ctx.end_spaced_comments(pos, expr_span.start)); + docs.extend( + ctx.end_spaced_comments(ctx.get_comments_between(pos, expr_span.start)), + ); docs.push(expr.doc(ctx)); docs.extend( ctx.start_spaced_comments( - expr_span.end, - iter.peek() - .map(|element| element.span().start) - .unwrap_or(self.span.end), + ctx.get_comments_between( + expr_span.end, + iter.peek() + .map(|element| element.span().start) + .unwrap_or(self.span.end), + ), ), ); docs.push(Doc::text("}")); @@ -502,14 +546,18 @@ impl<'s> DocGen<'s> for SassInterpolatedStr<'s> { SassInterpolatedStrElement::Expression(expr) => { let expr_span = expr.span(); docs.push(Doc::text("#{")); - docs.extend(ctx.end_spaced_comments(pos, expr_span.start)); + docs.extend( + ctx.end_spaced_comments(ctx.get_comments_between(pos, expr_span.start)), + ); docs.push(expr.doc(ctx)); docs.extend( ctx.start_spaced_comments( - expr_span.end, - iter.peek() - .map(|element| element.span().start) - .unwrap_or(self.span.end), + ctx.get_comments_between( + expr_span.end, + iter.peek() + .map(|element| element.span().start) + .unwrap_or(self.span.end), + ), ), ); docs.push(Doc::text("}")); @@ -543,14 +591,18 @@ impl<'s> DocGen<'s> for SassInterpolatedUrl<'s> { SassInterpolatedUrlElement::Expression(expr) => { let expr_span = expr.span(); docs.push(Doc::text("#{")); - docs.extend(ctx.end_spaced_comments(pos, expr_span.start)); + docs.extend( + ctx.end_spaced_comments(ctx.get_comments_between(pos, expr_span.start)), + ); docs.push(expr.doc(ctx)); docs.extend( ctx.start_spaced_comments( - expr_span.end, - iter.peek() - .map(|element| element.span().start) - .unwrap_or(self.span.end), + ctx.get_comments_between( + expr_span.end, + iter.peek() + .map(|element| element.span().start) + .unwrap_or(self.span.end), + ), ), ); docs.push(Doc::text("}")); @@ -566,9 +618,13 @@ impl<'s> DocGen<'s> for SassKeywordArgument<'s> { fn doc(&self, ctx: &Ctx<'_, 's>) -> Doc<'s> { self.name .doc(ctx) - .concat(ctx.start_spaced_comments(self.name.span.start, self.colon_span.start)) + .concat(ctx.start_spaced_comments( + ctx.get_comments_between(self.name.span.start, self.colon_span.start), + )) .append(Doc::text(": ")) - .concat(ctx.end_spaced_comments(self.colon_span.end, self.value.span().start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.colon_span.end, self.value.span().start), + )) .append(self.value.doc(ctx)) } } @@ -602,9 +658,13 @@ impl<'s> DocGen<'s> for SassMapItem<'s> { fn doc(&self, ctx: &Ctx<'_, 's>) -> Doc<'s> { self.key .doc(ctx) - .concat(ctx.start_spaced_comments(self.key.span().end, self.colon_span.start)) + .concat(ctx.start_spaced_comments( + ctx.get_comments_between(self.key.span().end, self.colon_span.start), + )) .append(Doc::text(": ")) - .concat(ctx.end_spaced_comments(self.colon_span.end, self.value.span().start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.colon_span.end, self.value.span().start), + )) .append(self.value.doc(ctx)) } } @@ -623,7 +683,9 @@ impl<'s> DocGen<'s> for SassMixin<'s> { impl<'s> DocGen<'s> for SassModuleConfig<'s> { fn doc(&self, ctx: &Ctx<'_, 's>) -> Doc<'s> { Doc::text("with ") - .concat(ctx.end_spaced_comments(self.with_span.end, self.lparen_span.start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.with_span.end, self.lparen_span.start), + )) .append(helpers::format_parenthesized( helpers::SeparatedListFormatter::new( ",", @@ -646,9 +708,13 @@ impl<'s> DocGen<'s> for SassModuleConfigItem<'s> { let value_span = self.value.span(); self.variable .doc(ctx) - .concat(ctx.start_spaced_comments(self.variable.span.end, self.colon_span.start)) + .concat(ctx.start_spaced_comments( + ctx.get_comments_between(self.variable.span.end, self.colon_span.start), + )) .append(Doc::text(": ")) - .concat(ctx.end_spaced_comments(self.colon_span.end, value_span.start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.colon_span.end, value_span.start), + )) .append(self.value.doc(ctx)) .concat( self.flags @@ -656,10 +722,10 @@ impl<'s> DocGen<'s> for SassModuleConfigItem<'s> { .scan(value_span.end, |pos, flag| { Some( iter::once(Doc::soft_line()) - .chain(ctx.end_spaced_comments( + .chain(ctx.end_spaced_comments(ctx.get_comments_between( mem::replace(pos, flag.span.end), flag.span.start, - )) + ))) .chain(iter::once(flag.doc(ctx))) .collect::>() .into_iter(), @@ -689,8 +755,10 @@ impl<'s> DocGen<'s> for SassParameter<'s> { fn doc(&self, ctx: &Ctx<'_, 's>) -> Doc<'s> { let name = self.name.doc(ctx); if let Some(default_value) = &self.default_value { - name.concat(ctx.start_spaced_comments(self.name.span.end, default_value.span.start)) - .append(default_value.doc(ctx)) + name.concat(ctx.start_spaced_comments( + ctx.get_comments_between(self.name.span.end, default_value.span.start), + )) + .append(default_value.doc(ctx)) } else { name } @@ -700,7 +768,9 @@ impl<'s> DocGen<'s> for SassParameter<'s> { impl<'s> DocGen<'s> for SassParameterDefaultValue<'s> { fn doc(&self, ctx: &Ctx<'_, 's>) -> Doc<'s> { Doc::text(": ") - .concat(ctx.end_spaced_comments(self.colon_span.end, self.value.span().start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.colon_span.end, self.value.span().start), + )) .append(self.value.doc(ctx)) } } @@ -763,7 +833,7 @@ impl<'s> DocGen<'s> for SassParenthesizedExpression<'s> { let expr_span = self.expr.span(); helpers::format_parenthesized( Doc::list( - ctx.end_spaced_comments(self.span.start, expr_span.start) + ctx.end_spaced_comments(ctx.get_comments_between(self.span.start, expr_span.start)) .collect(), ) .append(self.expr.doc(ctx)), @@ -819,16 +889,16 @@ impl<'s> DocGen<'s> for SassUse<'s> { if let Some(namespace) = &self.namespace { docs.push(Doc::space()); - docs.extend(ctx.end_spaced_comments( + docs.extend(ctx.end_spaced_comments(ctx.get_comments_between( mem::replace(&mut pos, namespace.span.end), namespace.span.start, - )); + ))); docs.push(namespace.doc(ctx)); } if let Some(config) = &self.config { docs.push(Doc::space()); - docs.extend(ctx.end_spaced_comments(pos, config.span.start)); + docs.extend(ctx.end_spaced_comments(ctx.get_comments_between(pos, config.span.start))); docs.push(config.doc(ctx)); } @@ -839,7 +909,9 @@ impl<'s> DocGen<'s> for SassUse<'s> { impl<'s> DocGen<'s> for SassUseNamespace<'s> { fn doc(&self, ctx: &Ctx<'_, 's>) -> Doc<'s> { Doc::text("as ") - .concat(ctx.end_spaced_comments(self.as_span.end, self.kind.span().start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.as_span.end, self.kind.span().start), + )) .append(match &self.kind { SassUseNamespaceKind::Named(named) => named.doc(ctx), SassUseNamespaceKind::Unnamed(unnamed) => unnamed.doc(ctx), @@ -864,7 +936,9 @@ impl<'s> DocGen<'s> for SassVariableDeclaration<'s> { } docs.push(self.name.doc(ctx)); - docs.extend(ctx.start_spaced_comments(self.name.span.end, self.colon_span.start)); + docs.extend(ctx.start_spaced_comments( + ctx.get_comments_between(self.name.span.end, self.colon_span.start), + )); docs.push(Doc::text(":")); let should_group = match &self.value { @@ -875,7 +949,9 @@ impl<'s> DocGen<'s> for SassVariableDeclaration<'s> { .. }) => { docs.push(Doc::line_or_space()); - docs.extend(ctx.end_spaced_comments(self.colon_span.end, value_span.start)); + docs.extend(ctx.end_spaced_comments( + ctx.get_comments_between(self.colon_span.end, value_span.start), + )); docs.push( helpers::SeparatedListFormatter::new(",", Doc::line_or_space()) .with_trailing() @@ -888,13 +964,17 @@ impl<'s> DocGen<'s> for SassVariableDeclaration<'s> { } ComponentValue::SassList(sass_list) => { docs.push(Doc::space()); - docs.extend(ctx.end_spaced_comments(self.colon_span.end, value_span.start)); + docs.extend(ctx.end_spaced_comments( + ctx.get_comments_between(self.colon_span.end, value_span.start), + )); docs.push(sass_list.doc(ctx).nest(ctx.indent_width)); false } _ => { docs.push(Doc::space()); - docs.extend(ctx.end_spaced_comments(self.colon_span.end, value_span.start)); + docs.extend(ctx.end_spaced_comments( + ctx.get_comments_between(self.colon_span.end, value_span.start), + )); docs.push(self.value.doc(ctx)); false } @@ -906,10 +986,10 @@ impl<'s> DocGen<'s> for SassVariableDeclaration<'s> { .scan(value_span.end, |pos, flag| { Some( iter::once(Doc::soft_line().nest(ctx.indent_width)) - .chain(ctx.end_spaced_comments( + .chain(ctx.end_spaced_comments(ctx.get_comments_between( mem::replace(pos, flag.span.end), flag.span.start, - )) + ))) .chain(iter::once(flag.doc(ctx))) .collect::>() .into_iter(), @@ -938,7 +1018,7 @@ impl<'s> DocGen<'s> for UnknownSassAtRule<'s> { if let Some(prelude) = &self.prelude { docs.push(Doc::space()); let span = prelude.span(); - docs.extend(ctx.end_spaced_comments(pos, span.start)); + docs.extend(ctx.end_spaced_comments(ctx.get_comments_between(pos, span.start))); docs.push(prelude.doc(ctx)); pos = span.end; } diff --git a/malva/src/doc_gen/selector.rs b/malva/src/doc_gen/selector.rs index 6793846..b632f2b 100644 --- a/malva/src/doc_gen/selector.rs +++ b/malva/src/doc_gen/selector.rs @@ -25,36 +25,38 @@ impl<'s> DocGen<'s> for AttributeSelector<'s> { fn doc(&self, ctx: &Ctx<'_, 's>) -> Doc<'s> { let mut docs = Vec::with_capacity(5); docs.push(Doc::text("[")); - docs.extend(ctx.end_spaced_comments(self.span.start, self.name.span.start)); + docs.extend( + ctx.end_spaced_comments( + ctx.get_comments_between(self.span.start, self.name.span.start), + ), + ); docs.push(self.name.doc(ctx)); let mut pos = self.name.span.end; if let Some((matcher, value)) = self.matcher.as_ref().zip(self.value.as_ref()) { - docs.extend( - ctx.end_spaced_comments( - mem::replace(&mut pos, matcher.span.end), - matcher.span.start, - ), - ); + docs.extend(ctx.end_spaced_comments(ctx.get_comments_between( + mem::replace(&mut pos, matcher.span.end), + matcher.span.start, + ))); docs.push(matcher.doc(ctx)); let value_span = value.span(); - docs.extend( - ctx.end_spaced_comments(mem::replace(&mut pos, value_span.end), value_span.start), - ); + docs.extend(ctx.end_spaced_comments( + ctx.get_comments_between(mem::replace(&mut pos, value_span.end), value_span.start), + )); docs.push(value.doc(ctx)); if let Some(modifier) = &self.modifier { docs.reserve(2); docs.push(Doc::space()); - docs.extend(ctx.end_spaced_comments( + docs.extend(ctx.end_spaced_comments(ctx.get_comments_between( mem::replace(&mut pos, modifier.span.end), modifier.span.start, - )); + ))); docs.push(modifier.doc(ctx)); } } - docs.extend(ctx.start_spaced_comments(pos, self.span.end)); + docs.extend(ctx.start_spaced_comments(ctx.get_comments_between(pos, self.span.end))); docs.push(Doc::text("]")); Doc::list(docs) } @@ -134,7 +136,9 @@ impl<'s> DocGen<'s> for ComplexSelector<'s> { children .fold((docs, pos), |(mut docs, pos), child| match child { ComplexSelectorChild::CompoundSelector(selector) => { - docs.extend(ctx.end_spaced_comments(pos, selector.span.start)); + docs.extend(ctx.end_spaced_comments( + ctx.get_comments_between(pos, selector.span.start), + )); docs.push(selector.doc(ctx).nest(ctx.indent_width)); (docs, selector.span.end) } @@ -147,7 +151,9 @@ impl<'s> DocGen<'s> for ComplexSelector<'s> { } ComplexSelectorChild::Combinator(combinator) => { docs.push(Doc::line_or_space().nest(ctx.indent_width)); - docs.extend(ctx.end_spaced_comments(pos, combinator.span.start)); + docs.extend(ctx.end_spaced_comments( + ctx.get_comments_between(pos, combinator.span.start), + )); docs.push(combinator.doc(ctx)); docs.push(Doc::space()); (docs, combinator.span.end) @@ -168,10 +174,10 @@ impl<'s> DocGen<'s> for CompoundSelector<'s> { let selector_span = selector.span(); Some( Doc::list( - ctx.unspaced_comments( + ctx.unspaced_comments(ctx.get_comments_between( mem::replace(pos, selector_span.end), selector_span.start, - ) + )) .collect(), ) .append(selector.doc(ctx)), @@ -236,7 +242,9 @@ impl<'s> DocGen<'s> for NsPrefix<'s> { let bar = Doc::text("|"); if let Some(kind) = &self.kind { kind.doc(ctx) - .concat(ctx.unspaced_comments(kind.span().end, self.span.end)) + .concat( + ctx.unspaced_comments(ctx.get_comments_between(kind.span().end, self.span.end)), + ) .append(bar) } else { bar @@ -289,7 +297,11 @@ impl<'s> DocGen<'s> for NthMatcher<'s> { impl<'s> DocGen<'s> for PseudoClassSelector<'s> { fn doc(&self, ctx: &Ctx<'_, 's>) -> Doc<'s> { let mut docs = vec![Doc::text(":")]; - docs.extend(ctx.unspaced_comments(self.span.start, self.name.span().start)); + docs.extend( + ctx.unspaced_comments( + ctx.get_comments_between(self.span.start, self.name.span().start), + ), + ); docs.push(helpers::ident_to_lowercase(&self.name, ctx)); if let Some(arg) = &self.arg { @@ -310,7 +322,9 @@ impl<'s> DocGen<'s> for PseudoClassSelector<'s> { } } - arg_doc.extend(ctx.end_spaced_comments(arg.l_paren.end, arg_span.start)); + arg_doc.extend( + ctx.end_spaced_comments(ctx.get_comments_between(arg.l_paren.end, arg_span.start)), + ); arg_doc.push(match &arg.kind { PseudoClassSelectorArgKind::CompoundSelector(compound_selector) => { compound_selector.doc(ctx) @@ -357,7 +371,11 @@ impl<'s> DocGen<'s> for PseudoClassSelector<'s> { } }); - arg_doc.extend(ctx.start_spaced_comments(arg_span.end, arg.r_paren.start)); + arg_doc.extend( + ctx.start_spaced_comments( + ctx.get_comments_between(arg_span.end, arg.r_paren.start), + ), + ); if ctx.options.linebreak_in_pseudo_parens { docs.push( Doc::list(arg_doc) @@ -378,7 +396,11 @@ impl<'s> DocGen<'s> for PseudoClassSelector<'s> { impl<'s> DocGen<'s> for PseudoElementSelector<'s> { fn doc(&self, ctx: &Ctx<'_, 's>) -> Doc<'s> { let mut docs = vec![Doc::text("::")]; - docs.extend(ctx.unspaced_comments(self.span.start, self.name.span().start)); + docs.extend( + ctx.unspaced_comments( + ctx.get_comments_between(self.span.start, self.name.span().start), + ), + ); docs.push(helpers::ident_to_lowercase(&self.name, ctx)); if let Some(arg) = &self.arg { @@ -391,7 +413,9 @@ impl<'s> DocGen<'s> for PseudoElementSelector<'s> { arg_doc.push(Doc::line_or_nil()); } - arg_doc.extend(ctx.end_spaced_comments(arg.l_paren.end, arg_span.start)); + arg_doc.extend( + ctx.end_spaced_comments(ctx.get_comments_between(arg.l_paren.end, arg_span.start)), + ); arg_doc.push(match &arg.kind { PseudoElementSelectorArgKind::CompoundSelector(compound_selector) => { compound_selector.doc(ctx) @@ -407,7 +431,11 @@ impl<'s> DocGen<'s> for PseudoElementSelector<'s> { } }); - arg_doc.extend(ctx.start_spaced_comments(arg_span.end, arg.r_paren.start)); + arg_doc.extend( + ctx.start_spaced_comments( + ctx.get_comments_between(arg_span.end, arg.r_paren.start), + ), + ); if ctx.options.linebreak_in_pseudo_parens { docs.push( Doc::list(arg_doc) @@ -485,7 +513,9 @@ impl<'s> DocGen<'s> for TagNameSelector<'s> { if let Some(prefix) = &self.name.prefix { prefix .doc(ctx) - .concat(ctx.unspaced_comments(prefix.span.end, self.name.name.span().start)) + .concat(ctx.unspaced_comments( + ctx.get_comments_between(prefix.span.end, self.name.name.span().start), + )) .append(name) } else { name @@ -508,7 +538,9 @@ impl<'s> DocGen<'s> for UniversalSelector<'s> { if let Some(prefix) = &self.prefix { prefix .doc(ctx) - .concat(ctx.unspaced_comments(prefix.span.end, self.span.end)) + .concat( + ctx.unspaced_comments(ctx.get_comments_between(prefix.span.end, self.span.end)), + ) .append(asterisk) } else { asterisk @@ -522,7 +554,9 @@ impl<'s> DocGen<'s> for WqName<'s> { if let Some(prefix) = &self.prefix { prefix .doc(ctx) - .concat(ctx.unspaced_comments(prefix.span.end, self.name.span().start)) + .concat(ctx.unspaced_comments( + ctx.get_comments_between(prefix.span.end, self.name.span().start), + )) .append(name) } else { name @@ -542,7 +576,7 @@ fn format_pseudo_selector_arg_tokens<'a, 's: 'a>( let mut docs = Vec::with_capacity(token_seq.tokens.len() * 2); let mut iter = token_seq.tokens.iter().peekable(); while let Some(token) = iter.next() { - docs.extend(ctx.end_spaced_comments(pos, token.span.start)); + docs.extend(ctx.end_spaced_comments(ctx.get_comments_between(pos, token.span.start))); docs.push(token.doc(ctx)); if let TokenWithSpan { @@ -565,7 +599,7 @@ fn format_pseudo_selector_arg_tokens<'a, 's: 'a>( pos = token.span.end; } - docs.extend(ctx.start_spaced_comments(pos, to)); + docs.extend(ctx.start_spaced_comments(ctx.get_comments_between(pos, to))); Doc::list(docs) } diff --git a/malva/src/doc_gen/stmt.rs b/malva/src/doc_gen/stmt.rs index 8d2c6eb..781d973 100644 --- a/malva/src/doc_gen/stmt.rs +++ b/malva/src/doc_gen/stmt.rs @@ -16,11 +16,13 @@ impl<'s> DocGen<'s> for Declaration<'s> { if let Some(less_property_merge) = &self.less_property_merge { docs.push(less_property_merge.doc(ctx)); - docs.extend( - ctx.start_spaced_comments(less_property_merge.span.end, self.colon_span.start), - ); + docs.extend(ctx.start_spaced_comments( + ctx.get_comments_between(less_property_merge.span.end, self.colon_span.start), + )); } else { - docs.extend(ctx.start_spaced_comments(self.name.span().end, self.colon_span.start)); + docs.extend(ctx.start_spaced_comments( + ctx.get_comments_between(self.name.span().end, self.colon_span.start), + )); } docs.push(Doc::text(":")); @@ -53,8 +55,11 @@ impl<'s> DocGen<'s> for Declaration<'s> { while let Some(value) = iter.next() { let span = value.span(); docs.push( - Doc::list(ctx.end_spaced_comments(pos, span.start).collect()) - .nest(ctx.indent_width), + Doc::list( + ctx.end_spaced_comments(ctx.get_comments_between(pos, span.start)) + .collect(), + ) + .nest(ctx.indent_width), ); docs.push(value.doc(ctx)); @@ -84,7 +89,9 @@ impl<'s> DocGen<'s> for Declaration<'s> { .enumerate() .fold(pos, |pos, (index, value)| { let span = value.span(); - let comments = ctx.end_spaced_comments(pos, span.start).collect::>(); + let comments = ctx + .end_spaced_comments(ctx.get_comments_between(pos, span.start)) + .collect::>(); if !comments.is_empty() { docs.push(Doc::space()); @@ -108,8 +115,11 @@ impl<'s> DocGen<'s> for Declaration<'s> { while let Some(value) = iter.next() { let span = value.span(); docs.push( - Doc::list(ctx.end_spaced_comments(pos, span.start).collect()) - .nest(ctx.indent_width), + Doc::list( + ctx.end_spaced_comments(ctx.get_comments_between(pos, span.start)) + .collect(), + ) + .nest(ctx.indent_width), ); docs.push(value.doc(ctx)); @@ -152,8 +162,11 @@ impl<'s> DocGen<'s> for Declaration<'s> { if let Some(important) = &self.important { docs.push(Doc::soft_line().nest(ctx.indent_width)); docs.push( - Doc::list(ctx.end_spaced_comments(pos, important.span.start).collect()) - .nest(ctx.indent_width), + Doc::list( + ctx.end_spaced_comments(ctx.get_comments_between(pos, important.span.start)) + .collect(), + ) + .nest(ctx.indent_width), ); docs.push(important.doc(ctx)); } diff --git a/malva/src/doc_gen/token_seq.rs b/malva/src/doc_gen/token_seq.rs index a876827..7b43046 100644 --- a/malva/src/doc_gen/token_seq.rs +++ b/malva/src/doc_gen/token_seq.rs @@ -14,7 +14,7 @@ impl<'s> DocGen<'s> for TokenSeq<'s> { let mut iter = self.tokens.iter().peekable(); while let Some(token) = iter.next() { let span = token.span(); - docs.extend(ctx.start_spaced_comments(pos, span.start)); + docs.extend(ctx.start_spaced_comments(ctx.get_comments_between(pos, span.start))); docs.push(token.doc(ctx)); if let TokenWithSpan { diff --git a/malva/src/doc_gen/value.rs b/malva/src/doc_gen/value.rs index b07ab21..2958c28 100644 --- a/malva/src/doc_gen/value.rs +++ b/malva/src/doc_gen/value.rs @@ -122,10 +122,14 @@ impl<'s> DocGen<'s> for Calc<'s> { }; left.append(helpers::format_operator_prefix_space(ctx)) - .concat(ctx.end_spaced_comments(self.left.span().end, self.op.span.start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.left.span().end, self.op.span.start), + )) .append(self.op.doc(ctx)) .append(helpers::format_operator_suffix_space(ctx)) - .concat(ctx.end_spaced_comments(self.op.span.end, self.right.span().start)) + .concat(ctx.end_spaced_comments( + ctx.get_comments_between(self.op.span.end, self.right.span().start), + )) .append(right) .group() } @@ -284,10 +288,10 @@ impl<'s> DocGen<'s> for Function<'s> { group.iter().map(|arg| { let arg_span = arg.span(); Doc::list( - ctx.end_spaced_comments( + ctx.end_spaced_comments(ctx.get_comments_between( mem::replace(pos, arg_span.end), arg_span.start, - ) + )) .collect(), ) .append(arg.doc(ctx)) @@ -313,10 +317,10 @@ impl<'s> DocGen<'s> for Function<'s> { )] = group { format_group(group, &mut pos, separator.clone(), ctx) - .concat(ctx.start_spaced_comments( + .concat(ctx.start_spaced_comments(ctx.get_comments_between( mem::replace(&mut pos, delimiter_span.end), delimiter_span.start, - )) + ))) .append(delimiter.doc(ctx)) } else { format_group(group, &mut pos, separator.clone(), ctx) @@ -327,8 +331,7 @@ impl<'s> DocGen<'s> for Function<'s> { let mut has_last_line_comment = false; arg_docs.extend(ctx.start_spaced_comments_without_last_hard_line( - pos, - self.span.end, + ctx.get_comments_between(pos, self.span.end), &mut has_last_line_comment, ));