sh icon indicating copy to clipboard operation
sh copied to clipboard

request: allow `; shift` to remain at the end of lines

Open andreykaipov opened this issue 4 years ago • 3 comments

I figured since there's already some options for indenting switch cases or putting a space after redirect operators, you also might be open to supporting the following pattern:

f() {
  a="$1"; shift
  b="$1"; shift
  c="$1"; shift
  echo "$a $b $c"
}

Here's a few examples from shell scripts of popular projects where this pattern could be found:

https://github.com/ohmybash/oh-my-bash/blob/master/themes/colours.theme.sh https://github.com/sonph/onehalf/blob/master/gnome-terminal/onehalfdark.sh#L84-L106 https://github.com/kohler/click/blob/master/click-compile.in#L43-L85

I'm going to look through the code to see if it's something I could tackle, but in the meantime figured I'd open this request. If you don't think it's something you'd like to support, that's okay. I still really appreciate the tool!

Thanks!

andreykaipov avatar Feb 26 '21 23:02 andreykaipov

You might be interested in https://github.com/mvdan/sh/issues/564. It's a similar kind of feature, but for a good reason; since Bash parses lines one by one, spliting foo; exit $? into multiple lines could result in corrupted code being run.

Other people have asked for ways to have "one-liners", such as https://github.com/mvdan/sh/issues/591 https://github.com/mvdan/sh/issues/602 https://github.com/mvdan/sh/issues/261. The problem is - where do we draw the line? I certainly don't want to allow multiple commands on a single line in general, as that would make the formatting too inconsistent.

So my default stance is to only allow the exit case, where it actually affects the behavior of a script. All other cases seem to be about personal preference, and at the end of the day the formatting tool is always a compromise between consistency and everyone's personal preferences.

I will say that I find this case more commonly useful than the other cases which have been brought up, since this is a useful (and the only) way to have named parameters in a function declaration. Maybe it would be reasonable to pattern-match local NAME=VALUE; shift and allow that to be on a single line.

mvdan avatar Mar 04 '21 11:03 mvdan

I too want to avoid this formatting Shift in multiline

@mvdan are you planning to work on this ?

I think the never-split function introduce here is something similar, although I haven't checked it as that repository is a lot behind this and may have bugs/less features too.

devkapilbansal avatar Aug 11 '21 15:08 devkapilbansal

Please don't paste large screenshots of text. Just paste the text.

We'll definitely not add a feature like never-split. You're free to use forks, but those are different projects entirely.

My thinking is still leaning in favor of doing this, see my last comment.

mvdan avatar Aug 11 '21 15:08 mvdan