Skip to content

Commit e405822

Browse files
authored
Fix 82 (#83)
## Description of changes This PR tries to fix #82 by removing the unnecessary parens ## Relevant Issues closes #82
1 parent f4cc349 commit e405822

7 files changed

Lines changed: 31 additions & 4 deletions

File tree

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto eol=lf

src/formatting.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ impl<'a> Formatter<'a> {
334334
self.format_closure_expression(*block_id, expr.span);
335335
}
336336
Expr::Subexpression(block_id) => {
337-
self.format_subexpression(*block_id);
337+
self.format_subexpression(*block_id, expr.span);
338338
}
339339

340340
Expr::List(items) => self.format_list(items),
@@ -705,15 +705,33 @@ impl<'a> Formatter<'a> {
705705
}
706706

707707
/// Format a subexpression
708-
fn format_subexpression(&mut self, block_id: nu_protocol::BlockId) {
708+
fn format_subexpression(&mut self, block_id: nu_protocol::BlockId, span: Span) {
709709
let block = self.working_set.get_block(block_id);
710+
// Check if the subexpression has explicit parentheses in the source
711+
let has_explicit_parens = self.source.get(span.start) == Some(&b'(')
712+
&& self.source.get(span.end - 1) == Some(&b')');
713+
if !has_explicit_parens {
714+
self.format_block(block);
715+
return;
716+
}
710717
// Special case: subexpressions containing only a string interpolation don't need parentheses
711718
if block.pipelines.len() == 1 && block.pipelines[0].elements.len() == 1 {
712719
if let Expr::StringInterpolation(_) = &block.pipelines[0].elements[0].expr.expr {
713720
self.format_block(block);
714721
return;
715722
}
716723
}
724+
// Special case: subexpressions containing a pipeline starting with $in don't need parentheses
725+
if block.pipelines.len() == 1 && !block.pipelines[0].elements.is_empty() {
726+
let first_element = &block.pipelines[0].elements[0];
727+
let element_text = String::from_utf8_lossy(
728+
&self.source[first_element.expr.span.start..first_element.expr.span.end],
729+
);
730+
if element_text == "$in" {
731+
self.format_block(block);
732+
return;
733+
}
734+
}
717735

718736
self.write("(");
719737
let is_simple = block.pipelines.len() == 1 && block.pipelines[0].elements.len() <= 3;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def in-lang [ln] { $in | items {|k v| {$k: ($v | get $ln | default ($v | get 'en')) } } | reduce -f {} {|it acc| $acc | merge $it } }

tests/fixtures/expected/issue76.nu

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
def pretty-print-command [] { ($"`(ansi default_dimmed)(ansi default_italic)($in)(ansi reset)`") }
1+
def pretty-print-command [] { $"`(ansi default_dimmed)(ansi default_italic)($in)(ansi reset)`" }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def in-lang [ln] { ($in | items {|k v| {$k: ($v | get $ln | default ($v | get 'en')) } } | reduce -f {} {|it acc| $acc | merge $it }) }

tests/ground_truth.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@ fn ground_truth_def_statement() {
180180
run_ground_truth_test(&test_binary, "def_statement");
181181
}
182182

183+
#[test]
184+
fn ground_truth_def_with_pipeline() {
185+
let test_binary = get_test_binary();
186+
run_ground_truth_test(&test_binary, "def_with_pipeline_double_parens_issue82");
187+
}
188+
183189
// ============================================================================
184190
// Ground Truth Tests - Control Flow
185191
// ============================================================================

toolkit.nu

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# (**2**) catch classical flaws in the new changes with *clippy* and (**3**)
77
# make sure all the tests pass.
88
# print the pipe input inside backticks, dimmed and italic, as a pretty command
9-
def pretty-print-command [] { ($"`(ansi default_dimmed)(ansi default_italic)($in)(ansi reset)`") }
9+
def pretty-print-command [] { $"`(ansi default_dimmed)(ansi default_italic)($in)(ansi reset)`" }
1010
# check standard code formatting and apply the changes
1111
export def fmt [--check, --verbose] {
1212
# do not apply the format changes, only check the syntax

0 commit comments

Comments
 (0)