Skip to content

Commit f93a3e9

Browse files
committed
fish_indent: Collapse successive newlines
This makes it so code like ```fish echo foo echo bar ``` is collapsed into ```fish echo foo echo bar ``` One empty line is allowed, more is overkill. We could also allow more than one for e.g. function endings.
1 parent 73d7605 commit f93a3e9

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

src/bin/fish_indent.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ impl<'source, 'ast> PrettyPrinter<'source, 'ast> {
127127
},
128128
output: WString::default(),
129129
current_indent: 0,
130-
gap_text_mask_newline: false,
130+
// Start with true to ignore leading empty lines.
131+
gap_text_mask_newline: true,
131132
gaps: vec![],
132133
preferred_semi_locations: vec![],
133134
errors: None,
@@ -430,15 +431,16 @@ impl<'source, 'ast> PrettyPrinterState<'source, 'ast> {
430431
if needs_nl {
431432
self.emit_newline();
432433
needs_nl = false;
434+
self.gap_text_mask_newline = false;
433435
if tok_text == "\n" {
434436
continue;
435437
}
436438
} else if self.gap_text_mask_newline {
437-
// We only respect mask_newline the first time through the loop.
438-
self.gap_text_mask_newline = false;
439-
if tok_text == "\n" {
439+
// When told to mask newlines, we do it as long as we get semicolon or newline.
440+
if tok.type_ == TokenType::end {
440441
continue;
441442
}
443+
self.gap_text_mask_newline = false;
442444
}
443445

444446
if tok.type_ == TokenType::comment {
@@ -451,6 +453,8 @@ impl<'source, 'ast> PrettyPrinterState<'source, 'ast> {
451453
// Newlines are preserved unless mask_newline is set.
452454
if tok_text == "\n" {
453455
self.emit_newline();
456+
// Ignore successive ends.
457+
self.gap_text_mask_newline = true;
454458
}
455459
} else {
456460
// Anything else we write a space.
@@ -620,11 +624,6 @@ impl<'source, 'ast> PrettyPrinterState<'source, 'ast> {
620624
} else {
621625
self.emit_newline();
622626
}
623-
624-
// If it was a semi but we emitted a newline, swallow a subsequent newline.
625-
if !prefer_semi && self.substr(range) == ";" {
626-
self.gap_text_mask_newline = true;
627-
}
628627
}
629628
}
630629

tests/checks/indent.fish

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ end | cat | cat | begin ; echo hi ; end | begin ; begin ; echo hi ; end ; end ar
7474
#CHECK: begin
7575
#CHECK: {{ }}echo hi
7676
#CHECK:
77-
#CHECK:
7877
#CHECK: end | cat | cat | begin
7978
#CHECK: {{ }}echo hi
8079
#CHECK: end | begin
@@ -96,10 +95,8 @@ end
9695
' | $fish_indent
9796

9897
#CHECK: switch aloha
99-
#CHECK:
10098
#CHECK: {{ }}case alpha
10199
#CHECK: {{ }}{{ }}echo sup
102-
#CHECK:
103100
#CHECK: {{ }}case beta gamma
104101
#CHECK: {{ }}{{ }}echo hi
105102
#CHECK:
@@ -150,7 +147,6 @@ qqq
150147
case "*"
151148
echo sup
152149
end' | $fish_indent
153-
#CHECK:
154150
#CHECK: echo alpha #comment1
155151
#CHECK: #comment2
156152
#CHECK:
@@ -324,7 +320,6 @@ echo bye
324320
#CHECK: end
325321
#CHECK:
326322
#CHECK: echo hi |
327-
#CHECK:
328323
#CHECK: {{ }}echo bye
329324

330325
echo 'a;;;;;;' | $fish_indent
@@ -449,3 +444,23 @@ echo "\'\\\\\x00\'" | string unescape | $fish_indent | string escape
449444

450445
echo '\"\"\|\x00' | string unescape | $fish_indent | string unescape
451446
# CHECK: |
447+
448+
echo 'a
449+
450+
451+
;
452+
453+
454+
b
455+
' | $fish_indent
456+
#CHECK: a
457+
#CHECK:
458+
#CHECK: b
459+
460+
echo "
461+
462+
463+
464+
echo this file starts late
465+
" | $fish_indent
466+
#CHECK: echo this file starts late

0 commit comments

Comments
 (0)