Skip to content

Commit

Permalink
fix: keep trailing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jac3km4 committed Jan 16, 2025
1 parent a3c251e commit 6641c0e
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 14 deletions.
38 changes: 27 additions & 11 deletions crates/syntax/formatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,17 @@ pub fn format_document<'a>(

let mut collector = PrefixCollector::new(&ws);
collector.visit_module(&module).ok();
let (prefixes, remainder) = collector.into_inner();
let remainder = remainder
.iter()
.filter(|(t, _)| !matches!(t, Token::LineFeed))
.map(|(t, _)| t.clone())
.collect::<Vec<_>>();

let display = DisplayFn(move |f: &mut fmt::Formatter<'_>| {
let ctx = FormatCtx::new(settings, &collector.prefixes);
write!(f, "{}", module.as_fmt(ctx))
let ctx = FormatCtx::new(settings, &prefixes);
writeln!(f, "{}", module.as_fmt(ctx))?;
remainder.iter().try_for_each(|t| writeln!(f, "{t}"))
});
(Some(display), errors)
}
Expand Down Expand Up @@ -1303,23 +1310,32 @@ pub enum Prefix<'src> {
#[derive(Debug)]
struct PrefixCollector<'ctx, 'src> {
prefixes: HashMap<NodeId, Vec<Prefix<'src>>>,
tokens: &'ctx [Spanned<Token<'src>>],
remainder: &'ctx [Spanned<Token<'src>>],
}

impl<'ctx, 'src> PrefixCollector<'ctx, 'src> {
fn new(tokens: &'ctx [Spanned<Token<'src>>]) -> Self {
Self {
prefixes: HashMap::new(),
tokens,
remainder: tokens,
}
}

fn into_inner(
self,
) -> (
HashMap<NodeId, Vec<Prefix<'src>>>,
&'ctx [Spanned<Token<'src>>],
) {
(self.prefixes, self.remainder)
}

fn skip_until(&mut self, span: Span) {
while let [(Token::LineFeed, s), rest @ ..] = self.tokens {
while let [(Token::LineFeed, s), rest @ ..] = self.remainder {
if s.start > span.start {
break;
}
self.tokens = rest;
self.remainder = rest;
}
}
}
Expand All @@ -1331,7 +1347,7 @@ impl<'src> AstVisitor<'src, WithSpan> for PrefixCollector<'_, 'src> {
let mut prefixes = vec![];
let mut is_lf_seq = false;

while let [(fst, span), rest @ ..] = self.tokens {
while let [(fst, span), rest @ ..] = self.remainder {
if span.start > node.span().start {
break;
}
Expand All @@ -1341,14 +1357,14 @@ impl<'src> AstVisitor<'src, WithSpan> for PrefixCollector<'_, 'src> {
if matches!(node, AstNode::Stmt(_)) && span.start <= node.span().start =>
{
is_lf_seq = true;
self.tokens = rest;
self.remainder = rest;
}
_ => {
if is_lf_seq {
prefixes.push(Prefix::LineBreak);
is_lf_seq = false;
}
self.tokens = rest;
self.remainder = rest;
}
}
match fst {
Expand All @@ -1367,11 +1383,11 @@ impl<'src> AstVisitor<'src, WithSpan> for PrefixCollector<'_, 'src> {
}

fn post_visit_node(&mut self, node: AstNode<'_, 'src, WithSpan>) -> Result<(), Self::Error> {
while let [(Token::LineFeed, span), rest @ ..] = self.tokens {
while let [(Token::LineFeed, span), rest @ ..] = self.remainder {
if span.start >= node.span().end {
break;
}
self.tokens = rest;
self.remainder = rest;
}

Ok(())
Expand Down
1 change: 1 addition & 0 deletions crates/syntax/formatter/tests/data/commented.reds
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// the entire file is commented out
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,5 @@ func Test() {
};
let f3 = (a: Int32) -> a;
}

// trailing comment
7 changes: 7 additions & 0 deletions crates/syntax/formatter/tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: crates/syntax/formatter/tests/formatted.rs
expression: module
input_file: crates/syntax/formatter/tests/data/commented.reds
snapshot_kind: text
---
// the entire file is commented out
4 changes: 3 additions & 1 deletion [email protected][email protected]
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
source: crates/syntax/formatter/tests/formatted.rs
expression: module
input_file: crates/syntax/formatter/tests/data/ControlFlow.reds
input_file: crates/syntax/formatter/tests/data/control-flow.reds
snapshot_kind: text
---
func Test() {
Expand Down Expand Up @@ -77,3 +77,5 @@ func Test() {
};
let f3 = (a: Int32) -> a;
}

// trailing comment
2 changes: 1 addition & 1 deletion [email protected][email protected]
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
source: crates/syntax/formatter/tests/formatted.rs
expression: module
input_file: crates/syntax/formatter/tests/data/Module.reds
input_file: crates/syntax/formatter/tests/data/module.reds
snapshot_kind: text
---
module Test.Module
Expand Down
2 changes: 1 addition & 1 deletion [email protected][email protected]
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
source: crates/syntax/formatter/tests/formatted.rs
expression: module
input_file: crates/syntax/formatter/tests/data/Operators.reds
input_file: crates/syntax/formatter/tests/data/operators.reds
snapshot_kind: text
---
func Test1() {
Expand Down

0 comments on commit 6641c0e

Please sign in to comment.