Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
22 changes: 20 additions & 2 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ impl<'a> Formatter<'a> {
self.format_closure_expression(*block_id, expr.span);
}
Expr::Subexpression(block_id) => {
self.format_subexpression(*block_id);
self.format_subexpression(*block_id, expr.span);
}

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

/// Format a subexpression
fn format_subexpression(&mut self, block_id: nu_protocol::BlockId) {
fn format_subexpression(&mut self, block_id: nu_protocol::BlockId, span: Span) {
let block = self.working_set.get_block(block_id);
// Check if the subexpression has explicit parentheses in the source
let has_explicit_parens = self.source.get(span.start) == Some(&b'(')
&& self.source.get(span.end - 1) == Some(&b')');
if !has_explicit_parens {
self.format_block(block);
return;
}
// Special case: subexpressions containing only a string interpolation don't need parentheses
if block.pipelines.len() == 1 && block.pipelines[0].elements.len() == 1 {
if let Expr::StringInterpolation(_) = &block.pipelines[0].elements[0].expr.expr {
self.format_block(block);
return;
}
}
// Special case: subexpressions containing a pipeline starting with $in don't need parentheses
if block.pipelines.len() == 1 && !block.pipelines[0].elements.is_empty() {
let first_element = &block.pipelines[0].elements[0];
let element_text = String::from_utf8_lossy(
&self.source[first_element.expr.span.start..first_element.expr.span.end],
);
if element_text == "$in" {
self.format_block(block);
return;
}
}

self.write("(");
let is_simple = block.pipelines.len() == 1 && block.pipelines[0].elements.len() <= 3;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def in-lang [ln] { $in | items {|k v| {$k: ($v | get $ln | default ($v | get 'en')) } } | reduce -f {} {|it acc| $acc | merge $it } }
2 changes: 1 addition & 1 deletion tests/fixtures/expected/issue76.nu
Original file line number Diff line number Diff line change
@@ -1 +1 @@
def pretty-print-command [] { ($"`(ansi default_dimmed)(ansi default_italic)($in)(ansi reset)`") }
def pretty-print-command [] { $"`(ansi default_dimmed)(ansi default_italic)($in)(ansi reset)`" }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def in-lang [ln] { ($in | items {|k v| {$k: ($v | get $ln | default ($v | get 'en')) } } | reduce -f {} {|it acc| $acc | merge $it }) }
6 changes: 6 additions & 0 deletions tests/ground_truth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ fn ground_truth_def_statement() {
run_ground_truth_test(&test_binary, "def_statement");
}

#[test]
fn ground_truth_def_with_pipeline() {
let test_binary = get_test_binary();
run_ground_truth_test(&test_binary, "def_with_pipeline_double_parens_issue82");
}

// ============================================================================
// Ground Truth Tests - Control Flow
// ============================================================================
Expand Down
2 changes: 1 addition & 1 deletion toolkit.nu
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# (**2**) catch classical flaws in the new changes with *clippy* and (**3**)
# make sure all the tests pass.
# print the pipe input inside backticks, dimmed and italic, as a pretty command
def pretty-print-command [] { ($"`(ansi default_dimmed)(ansi default_italic)($in)(ansi reset)`") }
def pretty-print-command [] { $"`(ansi default_dimmed)(ansi default_italic)($in)(ansi reset)`" }
# check standard code formatting and apply the changes
export def fmt [--check, --verbose] {
# do not apply the format changes, only check the syntax
Expand Down