mirror of
https://github.com/fish-shell/fish-shell.git
synced 2026-06-05 16:21:15 -03:00
fish_indent: Allow semicolons for and and or
As mentioned in #2900, something like ```fish test -n "$var"; and set -l foo $var ``` is sufficiently idiomatic that it should be allowable. Also fixes some additional weirdness with semicolons.
This commit is contained in:
@@ -224,17 +224,27 @@ void prettifier_t::prettify_node(const parse_node_tree_t &tree, node_offset_t no
|
|||||||
// instead of the semicolons. Semicolons are just ignored, unless they are followed by a
|
// instead of the semicolons. Semicolons are just ignored, unless they are followed by a
|
||||||
// command. So `echo;` removes the semicolon, but `echo; echo` removes it and adds a
|
// command. So `echo;` removes the semicolon, but `echo; echo` removes it and adds a
|
||||||
// newline.
|
// newline.
|
||||||
|
last_was_semicolon = false;
|
||||||
if (node.get_source(source) == L"\n") {
|
if (node.get_source(source) == L"\n") {
|
||||||
append_newline();
|
append_newline();
|
||||||
} else {
|
} else if (!has_new_line) {
|
||||||
|
// The semicolon is only useful if we haven't just had a newline.
|
||||||
last_was_semicolon = true;
|
last_was_semicolon = true;
|
||||||
}
|
}
|
||||||
} else if ((node_type >= FIRST_PARSE_TOKEN_TYPE && node_type <= LAST_PARSE_TOKEN_TYPE) ||
|
} else if ((node_type >= FIRST_PARSE_TOKEN_TYPE && node_type <= LAST_PARSE_TOKEN_TYPE) ||
|
||||||
node_type == parse_special_type_parse_error) {
|
node_type == parse_special_type_parse_error) {
|
||||||
if (last_was_semicolon) {
|
if (last_was_semicolon) {
|
||||||
append_newline();
|
// We keep the semicolon for `; and` and `; or`,
|
||||||
|
// others we turn into newlines.
|
||||||
|
if (node.keyword != parse_keyword_and
|
||||||
|
&& node.keyword != parse_keyword_or) {
|
||||||
|
append_newline();
|
||||||
|
} else {
|
||||||
|
output.push_back(L';');
|
||||||
|
}
|
||||||
last_was_semicolon = false;
|
last_was_semicolon = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (node.keyword != parse_keyword_none) {
|
if (node.keyword != parse_keyword_none) {
|
||||||
append_whitespace(node_indent);
|
append_whitespace(node_indent);
|
||||||
output.append(keyword_description(node.keyword));
|
output.append(keyword_description(node.keyword));
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
set -l indent ../test/root/bin/fish_indent
|
||||||
|
|
||||||
echo Test1
|
echo Test1
|
||||||
echo -n '
|
echo -n '
|
||||||
begin
|
begin
|
||||||
@@ -5,7 +7,7 @@ echo hi
|
|||||||
|
|
||||||
|
|
||||||
end | cat | cat | begin ; echo hi ; end | begin ; begin ; echo hi ; end ; end arg
|
end | cat | cat | begin ; echo hi ; end | begin ; begin ; echo hi ; end ; end arg
|
||||||
' | ../test/root/bin/fish_indent
|
' | $indent
|
||||||
|
|
||||||
echo \nTest2
|
echo \nTest2
|
||||||
echo -n '
|
echo -n '
|
||||||
@@ -18,7 +20,7 @@ switch aloha
|
|||||||
echo hi
|
echo hi
|
||||||
|
|
||||||
end
|
end
|
||||||
' | ../test/root/bin/fish_indent
|
' | $indent
|
||||||
|
|
||||||
echo \nTest3
|
echo \nTest3
|
||||||
echo -n '
|
echo -n '
|
||||||
@@ -33,7 +35,7 @@ function hello_world
|
|||||||
|
|
||||||
echo hello
|
echo hello
|
||||||
end
|
end
|
||||||
' | ../test/root/bin/fish_indent
|
' | $indent
|
||||||
|
|
||||||
echo \nTest4
|
echo \nTest4
|
||||||
echo -n '
|
echo -n '
|
||||||
@@ -53,7 +55,7 @@ switch foo #abc
|
|||||||
qqq
|
qqq
|
||||||
case "*"
|
case "*"
|
||||||
echo sup
|
echo sup
|
||||||
end' | ../test/root/bin/fish_indent
|
end' | $indent
|
||||||
|
|
||||||
echo \nTest5
|
echo \nTest5
|
||||||
echo -n '
|
echo -n '
|
||||||
@@ -65,7 +67,7 @@ switch beta
|
|||||||
echo delta
|
echo delta
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
' | ../test/root/bin/fish_indent -i
|
' | $indent -i
|
||||||
|
|
||||||
echo \nTest6
|
echo \nTest6
|
||||||
# Test errors
|
# Test errors
|
||||||
@@ -75,20 +77,20 @@ echo hi
|
|||||||
else
|
else
|
||||||
echo bye
|
echo bye
|
||||||
end; echo alpha "
|
end; echo alpha "
|
||||||
' | ../test/root/bin/fish_indent
|
' | $indent
|
||||||
|
|
||||||
echo \nTest7
|
echo \nTest7
|
||||||
# issue 1665
|
# issue 1665
|
||||||
echo -n '
|
echo -n '
|
||||||
if begin ; false; end; echo hi ; end
|
if begin ; false; end; echo hi ; end
|
||||||
while begin ; false; end; echo hi ; end
|
while begin ; false; end; echo hi ; end
|
||||||
' | ../test/root/bin/fish_indent
|
' | $indent
|
||||||
|
|
||||||
echo \nTest redir formatting
|
echo \nTest redir formatting
|
||||||
# issue 2899
|
# issue 2899
|
||||||
echo -n '
|
echo -n '
|
||||||
echo < stdin >>appended yes 2>&1 no > stdout maybe 2>& 4 | cat 2>| cat
|
echo < stdin >>appended yes 2>&1 no > stdout maybe 2>& 4 | cat 2>| cat
|
||||||
' | ../test/root/bin/fish_indent
|
' | $indent
|
||||||
|
|
||||||
echo \nTest normalization of keywords
|
echo \nTest normalization of keywords
|
||||||
# issue 2921
|
# issue 2921
|
||||||
@@ -122,7 +124,13 @@ echo abc;end
|
|||||||
echo hi |
|
echo hi |
|
||||||
|
|
||||||
echo bye
|
echo bye
|
||||||
' | ../test/root/bin/fish_indent
|
' | $indent
|
||||||
|
|
||||||
echo 'a;;;;;;' | ../test/root/bin/fish_indent
|
echo 'a;;;;;;' | $indent
|
||||||
echo 'echo; echo' | ../test/root/bin/fish_indent
|
echo 'echo; echo' | $indent
|
||||||
|
|
||||||
|
# Check that we keep semicolons for and and or, but only on the same line.
|
||||||
|
printf '%s\n' "a; and b" | $indent
|
||||||
|
printf '%s\n' "a;" "and b" | $indent
|
||||||
|
printf '%s\n' "a" "; and b" | $indent
|
||||||
|
printf '%s\n' "a; b" | $indent
|
||||||
|
|||||||
@@ -36,7 +36,6 @@ function hello_world
|
|||||||
echo sup
|
echo sup
|
||||||
echo hello
|
echo hello
|
||||||
|
|
||||||
|
|
||||||
echo hello
|
echo hello
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -128,3 +127,10 @@ echo bye
|
|||||||
a
|
a
|
||||||
echo
|
echo
|
||||||
echo
|
echo
|
||||||
|
a; and b
|
||||||
|
a
|
||||||
|
and b
|
||||||
|
a
|
||||||
|
and b
|
||||||
|
a
|
||||||
|
b
|
||||||
|
|||||||
Reference in New Issue
Block a user