mvdan / sh

A shell parser, formatter, and interpreter with bash support; includes shfmt
https://pkg.go.dev/mvdan.cc/sh/v3
BSD 3-Clause "New" or "Revised" License
6.97k stars 332 forks source link

emulate beautysh `--force-function-style` #1064

Open williamspatrick opened 4 months ago

williamspatrick commented 4 months ago

Would it be possible to easily emulate the --force-function-style behavior of beautysh? That seems to be the only missing feature to use this as a replacement.

  --force-function-style FORCE_FUNCTION_STYLE, -s FORCE_FUNCTION_STYLE
                        Force a specific Bash function formatting. See below for more info.

...
Bash function styles that can be specified via --force-function-style are:
  fnpar: function keyword, open/closed parentheses, e.g.      function foo()
  fnonly: function keyword, no open/closed parentheses, e.g.  function foo
  paronly: no function keyword, open/closed parentheses, e.g. foo()

I usually set this to fnpar.

mvdan commented 4 months ago

shfmt supports all three of these forms, but it's true that it doesn't support enforcing any of the three forms consistently. I agree that being able to do that would be nice for Bash. It's worth pointing out that this is not a problem for POSIX Shell, where foo() is the only option.

That said, the bar for new formatting options is high - I really want to avoid a situation like https://linux.die.net/man/1/indent with dozens of formatting options. I'm not sure whether we can support this without a new option, because I suspect people have different preferences for Bash function declarations.

Perhaps the answer is to repurpose the existing --func-next-line in v4 so that it becomes a --func-style flag with a string, with options like:

This "blows up" the option into a 2x3 matrix, but I think that's fine as I don't suspect it will need to grow any further.