Skip to content

Commit 27cbcdd

Browse files
authored
fix double parens issue (#78)
## Description of changes closes #76 - added a test - fixed some testing code - fixed bug
1 parent c03e166 commit 27cbcdd

7 files changed

Lines changed: 242 additions & 141 deletions

File tree

src/formatting.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,8 +674,16 @@ impl<'a> Formatter<'a> {
674674

675675
/// Format a subexpression
676676
fn format_subexpression(&mut self, block_id: nu_protocol::BlockId) {
677-
self.write("(");
678677
let block = self.working_set.get_block(block_id);
678+
// Special case: subexpressions containing only a string interpolation don't need parentheses
679+
if block.pipelines.len() == 1 && block.pipelines[0].elements.len() == 1 {
680+
if let Expr::StringInterpolation(_) = &block.pipelines[0].elements[0].expr.expr {
681+
self.format_block(block);
682+
return;
683+
}
684+
}
685+
686+
self.write("(");
679687
let is_simple = block.pipelines.len() == 1 && block.pipelines[0].elements.len() <= 3;
680688

681689
if is_simple {

tests/fixtures/expected/issue76.nu

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def pretty-print-command [] { ($"`(ansi default_dimmed)(ansi default_italic)($in)(ansi reset)`") }

tests/fixtures/expected/subexpression.nu

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88
let result = (1 + 2) * 3
99
let value = ($x + (($y * 2)))
1010
print (echo "hello")
11+
let msg = $"Hello ($name)"
1112
if (true) { print "yes" }

tests/fixtures/input/issue76.nu

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def pretty-print-command [] { ($"`(ansi default_dimmed)(ansi default_italic)($in)(ansi reset)`") }

tests/fixtures/input/subexpression.nu

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88
let result = (1 + 2) * 3
99
let value = ($x + (($y * 2)))
1010
print (echo "hello")
11+
let msg = ($"Hello ($name)")
1112
if (true) { print "yes" }

0 commit comments

Comments
 (0)