Skip to content

Commit

Permalink
Run core/fmt.sh from VSCode on save (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergekh2 authored Jul 1, 2024
1 parent ecff2c0 commit ec1ee32
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
7 changes: 5 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"[makefile]": {
"editor.renderWhitespace": "all"
},
"go.formatTool": "gofumpt",
"go.formatTool": "custom",
"go.testFlags": [
"-v",
"-tags",
Expand All @@ -36,5 +36,8 @@
"tailwindCSS.experimental.classRegex": [
["cva\\(([^)]*)\\)", "[\"'`]([^\"'`]*).*?[\"'`]"],
["cn\\(([^)]*)\\)", "(?:'|\"|`)([^']*)(?:'|\"|`)"]
]
],
"go.alternateTools": {
"customFormatter": "${workspaceFolder}/core/fmt.sh",
}
}
23 changes: 19 additions & 4 deletions core/fmt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,29 @@
set -euo pipefail
cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")"

# In addition to running gofumpt this script also limits line length to 120 characters
# There is no good way to run golines for single file from .vscode
# due to this bug: https://github.com/golang/vscode-go/issues/2582
if ! command -v "gofumpt" >/dev/null 2>&1; then
go install mvdan.cc/gofumpt@latest >/dev/null 2>&1
fi
if ! command -v "golines" >/dev/null 2>&1; then
go install github.com/segmentio/golines@latest >/dev/null 2>&1
fi

# Read all lines from stdin into the variable 'input'
INPUT=$(cat)

FMT_CMD="golines --base-formatter=gofumpt --max-len=120"

# If arguments are empty and stdin is not empty, run formatter on stdin to stdout
if [[ -z "$*" && -n "${INPUT}" ]]
then
echo "${INPUT}" | ${FMT_CMD}
exit 0
fi

# Set ARGS to -w if not set, otherwie to cmd line args
ARGS=${@:-"-w"}

OUTPUT=$(go list -f '{{.Dir}}' ./... | grep -v /contracts | grep -v /protocol | grep -v /mocks | xargs golines --base-formatter=gofumpt --max-len=120 $ARGS)
OUTPUT=$(go list -f '{{.Dir}}' ./... | grep -v /contracts | grep -v /protocol | grep -v /mocks | xargs ${FMT_CMD} $ARGS)
if [ -n "$OUTPUT" ]
then
echo "$OUTPUT"
Expand Down

0 comments on commit ec1ee32

Please sign in to comment.