sh icon indicating copy to clipboard operation
sh copied to clipboard

v4: syntax: indent multiline switch cases on same level

Open scop opened this issue 4 years ago • 4 comments

When a switch case spans multiple lines using continuation, I think it would be good to have the begin indent for all those lines be the same, rather than increased one step for the second onwards. So instead of this:

case foo in
bar | quux | \
	baz)
	echo hello
	;;
esac

...I'd at least personally prefer

case foo in
bar | quux | \
baz)
	echo hello
	;;
esac

...and similarly with indented cases, not this:

case foo in
	bar | quux | \
		baz)
		echo hello
		;;
esac

...but this instead:

case foo in
	bar | quux | \
	baz)
		echo hello
		;;
esac

My rationale is that if the lines following the first one are indented one step further, they tend to be hard to distinguish from the case's script body which is indented using the same step. Keeping the indent same for all lines in a case is easier to read.

scop avatar Dec 26 '20 20:12 scop

This is an interesting suggestion, which we could consider for a future v4. We can't change this in v3 as it would be a breaking change, and adding a flag in v3 just for this edge case feels overkill.

To give some background on the current formatting: the idea is that any opening of a node/block should increase the indentation level by 1. We do not allow increasing the indentation level by more than 1 in a single line, to prevent "jumps".

This means that, looking at the next line, it's easy to see that it's a continuation to the previous, and not an entirely separate one.

It's true that it can then become difficult to separate "continuation" versus "contents of the inner block", but you can remedy this in two ways:

  1. leave an empty line, if the continuation is long enough:
case foo in
bar | quux | \
	baz)

	echo hello
	;;
esac
  1. don't use a continuation line, if it's short enough:
case foo in
bar | quux | baz)
	echo hello
	;;
esac

mvdan avatar Dec 30 '20 17:12 mvdan

Thanks for considering this. Not using continuation is the obvious choice where applicable, but I have a bunch of cases in bash-completion with quite a few items in them and using looooong lines would be worse than just living with the current state of affairs.

I just discovered another workaround currently is to (ab)use fallthrough to avoid continuation which I'm not that much a fan of, but anyway:

case foo in
	bar | quux) ;&
	baz)
		echo hello
		;;
esac

scop avatar Dec 30 '20 19:12 scop

Forgive me if this is the wrong time/place, and I'm relatively new to Bash so if this is just plain wrong syntax just let me know... but I prefer switch cases indented no matter if they're multi-line

Preferred:

case $input in
        "a") va=a;;
        "v") var=b;;
esac

What I'm currently getting after formatting with shell-format in VS Code:

case $input in
"a") va=a;;
"v") var=b;;
esac

blakegearin avatar Feb 19 '21 07:02 blakegearin

What you want is already supported via -ci. This is about single cases which span multiple lines before ) with backslashes, like in the original post.

mvdan avatar Feb 19 '21 07:02 mvdan