cmd/shfmt: option to prefer properties from editorconfig
My use case is the following: I want to define a set of fallback options for shfmt in my editor. When an .editorconfig applies, however, I want the properties there to take precedence. As far as I understand, there is no way to do this currently.
To solve this, I'd like to suggest the following feature:
Simple solution
A minimal way of supporting this use case might be to introduce a generic flag --prefer-editorconfig. I could then, for example, configure my editor to call shfmt with the flags --indent=2 --case-indent --prefer-editorconfig. When no .editorconfig is present, shfmt would produce an indentation of 2 spaces and indented switch cases. Now, suppose I work in a repository with the following .editorconfig:
[*.{sh,bash}]
indent_style = tabThis would result in tabs and indented switch cases (i.e. it's not "all or nothing", but the properties from .editorconfig are overlaid over the settings given as command line flags).
I also came up with two other variants that are more explicit and more flexible:
Alternative A
Add a generic flag --editorconfig which can take the following values:
ignore: don't honor.editorconfigat allprefer: combine settings from the command line and from.editorconfig, giving precedence to the latter (this is what I would use)fallback: combine settings from the command line and from.editorconfig, giving precedence to the formerautoorlegacy: current behavior (honor.editorconfigonly if no settings are given as command line flags, if I understood correctly)
(I am unsure how useful the last value really is, with the "all or nothing" behavior. Maybe all use cases can already be satisfied with the first three?)
Alternative B
Add a printer flag --options-from-editorconfig which acts as a stand-in for the (possibly empty) set of options from .editorconfig. The precedence would then be according to the order of occurrence (ascending, as is already the case today when specifying the same flag twice). Contrived example:
In shfmt --case-indent --options-from-editorconfig --indent=2, the option --case-indent is active only if .editorconfig doesn't override it with switch_case_indent = false, but the indentation is always 2 spaces, regardless of the contents of .editorconfig.
If no .editorconfig applies, --options-from-editorconfig does nothing. If --options-from-editorconfig is omitted, .editorconfig is ignored.
This variant is the most flexible, as it allows to specify both fallback options and options "stronger than .editorconfig" at the same time.
Would you be open to a feature like this? I'd be willing to try implementing this in a PR. Which variant would you prefer? Or is there another solution that fits better with the existing flags?
Source: mvdan/sh