Skip to content

Commit f4cc349

Browse files
authored
don't break inline param comment formatting (#80)
## Description of changes This PR tries to fix the inline parameter comment formatting identified in #77 Closes #77
1 parent cdc2b28 commit f4cc349

4 files changed

Lines changed: 52 additions & 2 deletions

File tree

src/formatting.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,24 @@ impl<'a> Formatter<'a> {
300300
self.write_expr_span(expr);
301301
}
302302

303-
Expr::Signature(sig) => self.format_signature(sig),
303+
Expr::Signature(sig) => {
304+
let has_comments = self
305+
.comments
306+
.iter()
307+
.any(|(span, _)| span.start >= expr.span.start && span.end <= expr.span.end);
308+
if has_comments {
309+
self.write_expr_span(expr);
310+
self.last_pos = expr.span.end;
311+
// Mark comments within the span as written
312+
for (i, (span, _)) in self.comments.iter().enumerate() {
313+
if span.start >= expr.span.start && span.end <= expr.span.end {
314+
self.written_comments[i] = true;
315+
}
316+
}
317+
} else {
318+
self.format_signature(sig);
319+
}
320+
}
304321

305322
Expr::Call(call) => self.format_call(call),
306323
Expr::ExternalCall(head, args) => self.format_external_call(head, args),
@@ -443,7 +460,22 @@ impl<'a> Formatter<'a> {
443460
fn format_def_argument(&mut self, positional: &Expression) {
444461
match &positional.expr {
445462
Expr::String(_) => self.format_expression(positional),
446-
Expr::Signature(sig) => self.format_signature(sig),
463+
Expr::Signature(sig) => {
464+
let has_comments = self.comments.iter().any(|(span, _)| {
465+
span.start >= positional.span.start && span.end <= positional.span.end
466+
});
467+
if has_comments {
468+
self.write_expr_span(positional);
469+
// Mark comments within the span as written
470+
for (i, (span, _)) in self.comments.iter().enumerate() {
471+
if span.start >= positional.span.start && span.end <= positional.span.end {
472+
self.written_comments[i] = true;
473+
}
474+
}
475+
} else {
476+
self.format_signature(sig);
477+
}
478+
}
447479
Expr::Closure(block_id) | Expr::Block(block_id) => {
448480
self.format_block_expression(*block_id, positional.span, true);
449481
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def fun1 [
2+
text: string # param comment
3+
] { $text }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def fun1 [
2+
text: string # param comment
3+
] { $text }

tests/ground_truth.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,3 +736,15 @@ fn issue76_test() {
736736
let test_binary = get_test_binary();
737737
run_ground_truth_test(&test_binary, "issue76");
738738
}
739+
740+
#[test]
741+
fn ground_truth_inline_param_comment_issue77() {
742+
let test_binary = get_test_binary();
743+
run_ground_truth_test(&test_binary, "inline_param_comment");
744+
}
745+
746+
#[test]
747+
fn idempotency_inline_param_comment_issue77() {
748+
let test_binary = get_test_binary();
749+
run_idempotency_test(&test_binary, "inline_param_comment");
750+
}

0 commit comments

Comments
 (0)