From 039313b250960b2ae0db4cfb8646110f320123e4 Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Fri, 30 Jan 2026 12:22:18 -0600 Subject: [PATCH] don't break inline param comment formatting --- src/formatting.rs | 36 +++++++++++++++++-- .../fixtures/expected/inline_param_comment.nu | 3 ++ tests/fixtures/input/inline_param_comment.nu | 3 ++ tests/ground_truth.rs | 12 +++++++ 4 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/expected/inline_param_comment.nu create mode 100644 tests/fixtures/input/inline_param_comment.nu diff --git a/src/formatting.rs b/src/formatting.rs index 562740a..d799388 100644 --- a/src/formatting.rs +++ b/src/formatting.rs @@ -300,7 +300,24 @@ impl<'a> Formatter<'a> { self.write_expr_span(expr); } - Expr::Signature(sig) => self.format_signature(sig), + Expr::Signature(sig) => { + let has_comments = self + .comments + .iter() + .any(|(span, _)| span.start >= expr.span.start && span.end <= expr.span.end); + if has_comments { + self.write_expr_span(expr); + self.last_pos = expr.span.end; + // Mark comments within the span as written + for (i, (span, _)) in self.comments.iter().enumerate() { + if span.start >= expr.span.start && span.end <= expr.span.end { + self.written_comments[i] = true; + } + } + } else { + self.format_signature(sig); + } + } Expr::Call(call) => self.format_call(call), Expr::ExternalCall(head, args) => self.format_external_call(head, args), @@ -443,7 +460,22 @@ impl<'a> Formatter<'a> { fn format_def_argument(&mut self, positional: &Expression) { match &positional.expr { Expr::String(_) => self.format_expression(positional), - Expr::Signature(sig) => self.format_signature(sig), + Expr::Signature(sig) => { + let has_comments = self.comments.iter().any(|(span, _)| { + span.start >= positional.span.start && span.end <= positional.span.end + }); + if has_comments { + self.write_expr_span(positional); + // Mark comments within the span as written + for (i, (span, _)) in self.comments.iter().enumerate() { + if span.start >= positional.span.start && span.end <= positional.span.end { + self.written_comments[i] = true; + } + } + } else { + self.format_signature(sig); + } + } Expr::Closure(block_id) | Expr::Block(block_id) => { self.format_block_expression(*block_id, positional.span, true); } diff --git a/tests/fixtures/expected/inline_param_comment.nu b/tests/fixtures/expected/inline_param_comment.nu new file mode 100644 index 0000000..07a0d6e --- /dev/null +++ b/tests/fixtures/expected/inline_param_comment.nu @@ -0,0 +1,3 @@ +def fun1 [ + text: string # param comment + ] { $text } \ No newline at end of file diff --git a/tests/fixtures/input/inline_param_comment.nu b/tests/fixtures/input/inline_param_comment.nu new file mode 100644 index 0000000..07a0d6e --- /dev/null +++ b/tests/fixtures/input/inline_param_comment.nu @@ -0,0 +1,3 @@ +def fun1 [ + text: string # param comment + ] { $text } \ No newline at end of file diff --git a/tests/ground_truth.rs b/tests/ground_truth.rs index df45cce..1559a0e 100644 --- a/tests/ground_truth.rs +++ b/tests/ground_truth.rs @@ -736,3 +736,15 @@ fn issue76_test() { let test_binary = get_test_binary(); run_ground_truth_test(&test_binary, "issue76"); } + +#[test] +fn ground_truth_inline_param_comment_issue77() { + let test_binary = get_test_binary(); + run_ground_truth_test(&test_binary, "inline_param_comment"); +} + +#[test] +fn idempotency_inline_param_comment_issue77() { + let test_binary = get_test_binary(); + run_idempotency_test(&test_binary, "inline_param_comment"); +}