Merge pull request #12396 from XuehaiPan/vscode-shell-formatter

.vscode: auto fix shell script code style on save or on demand
This commit is contained in:
Mike McQuaid 2021-11-08 14:21:00 +00:00 committed by GitHub
commit 64fb3c86f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 19 deletions

View File

@ -5,6 +5,7 @@
"rebornix.ruby", "rebornix.ruby",
"wingrunr21.vscode-ruby", "wingrunr21.vscode-ruby",
"timonwong.shellcheck", "timonwong.shellcheck",
"foxundermoon.shell-format" "foxundermoon.shell-format",
"editorconfig.editorconfig"
] ]
} }

12
.vscode/settings.json vendored
View File

@ -13,5 +13,15 @@
"--external-sources", "--external-sources",
"--source-path=${workspaceFolder}/Library" "--source-path=${workspaceFolder}/Library"
], ],
"shellformat.flag": "-i 2 -ci" "shellformat.effectLanguages": [
"shellscript"
],
"shellformat.path": "${workspaceFolder}/Library/Homebrew/utils/shfmt.sh",
"shellformat.flag": "-i 2 -ci -ln bash",
"[shellscript]": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.shellcheck": true,
}
}
} }

View File

@ -35,6 +35,10 @@ then
if [[ -x "$(PATH="${HOMEBREW_PATH}" command -v diff)" ]] if [[ -x "$(PATH="${HOMEBREW_PATH}" command -v diff)" ]]
then then
DIFF="$(PATH="${HOMEBREW_PATH}" command -v diff)" # fall back to `diff` in PATH without coloring DIFF="$(PATH="${HOMEBREW_PATH}" command -v diff)" # fall back to `diff` in PATH without coloring
elif [[ -z "${HOMEBREW_PATH}" && -x "$(command -v diff)" ]]
then
# HOMEBREW_PATH may unset if shfmt.sh is called by vscode
DIFF="$(command -v diff)" # fall back to `diff` in PATH without coloring
else else
odie "${0##*/}: Please install diff by running \`brew install diffutils\`." odie "${0##*/}: Please install diff by running \`brew install diffutils\`."
fi fi
@ -81,9 +85,11 @@ do
done done
unset file unset file
STDIN=''
if [[ "${#FILES[@]}" == 0 ]] if [[ "${#FILES[@]}" == 0 ]]
then then
exit FILES=(/dev/stdin)
STDIN=1
fi fi
### ###
@ -334,6 +340,11 @@ align_multiline_switch_cases() {
format() { format() {
local file="$1" local file="$1"
local tempfile local tempfile
if [[ -n "${STDIN}" ]]
then
tempfile="$(mktemp)"
else
if [[ ! -f "${file}" || ! -r "${file}" ]] if [[ ! -f "${file}" || ! -r "${file}" ]]
then then
onoe "File \"${file}\" is not readable." onoe "File \"${file}\" is not readable."
@ -341,21 +352,30 @@ format() {
fi fi
tempfile="$(dirname "${file}")/.${file##*/}.temp" tempfile="$(dirname "${file}")/.${file##*/}.temp"
trap 'rm -f "${tempfile}" 2>/dev/null' RETURN
cp -af "${file}" "${tempfile}" cp -af "${file}" "${tempfile}"
fi
trap 'rm -f "${tempfile}" 2>/dev/null' RETURN
# Format with `shfmt` first
if [[ -z "${STDIN}" ]]
then
if [[ ! -f "${tempfile}" || ! -w "${tempfile}" ]] if [[ ! -f "${tempfile}" || ! -w "${tempfile}" ]]
then then
onoe "File \"${tempfile}\" is not writable." onoe "File \"${tempfile}\" is not writable."
return 1 return 1
fi fi
# Format with `shfmt` first
if ! "${SHFMT}" -w "${SHFMT_ARGS[@]}" "${tempfile}" if ! "${SHFMT}" -w "${SHFMT_ARGS[@]}" "${tempfile}"
then then
onoe "Failed to run \`shfmt\` for file \"${file}\"." onoe "Failed to run \`shfmt\` for file \"${file}\"."
return 2 return 2
fi fi
else
if ! "${SHFMT}" "${SHFMT_ARGS[@]}" >"${tempfile}"
then
onoe "Failed to run \`shfmt\` for file \"${file}\"."
return 2
fi
fi
# Fail fast when forbidden styles detected # Fail fast when forbidden styles detected
no_forbidden_styles "${file}" "${tempfile}" || return 3 no_forbidden_styles "${file}" "${tempfile}" || return 3
@ -364,6 +384,12 @@ format() {
wrap_then_do "${file}" "${tempfile}" wrap_then_do "${file}" "${tempfile}"
align_multiline_switch_cases "${file}" "${tempfile}" align_multiline_switch_cases "${file}" "${tempfile}"
if [[ -n "${STDIN}" ]]
then
cat "${tempfile}"
return 0
fi
if ! "${DIFF}" -q "${file}" "${tempfile}" &>/dev/null if ! "${DIFF}" -q "${file}" "${tempfile}" &>/dev/null
then then
if [[ -n "${INPLACE}" ]] if [[ -n "${INPLACE}" ]]