blob: 2d48683c8a572815318505cde783dd94a68d303b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
#!/usr/bin/env bash
# PostToolUse (Edit|Write) hook: format the just-written file with the single
# formatter that matches its type. Reads the hook payload as JSON on stdin and
# dispatches on the file's path/extension. The `case` selects exactly one branch,
# so a file is never run through more than one formatter; unknown types are left
# untouched. Each formatter reads its own config from the repo/XDG as usual.
file_path=$(jq --raw-output '.tool_input.file_path' 2>/dev/null)
[[ -n $file_path && -f $file_path ]] || exit 0
case $file_path in
*.lua) stylua --search-parent-directories "$file_path" ;;
*.c | *.h | *.cpp | *.hpp) clang-format -i "$file_path" ;;
*.md | *.markdown)
mdformat --number --extensions tables --extensions frontmatter \
--extensions wikilink "$file_path"
;;
*.cmake | */CMakeLists.txt | CMakeLists.txt) cmake-format --in-place "$file_path" ;;
*.qml) qmlformat --inplace "$file_path" ;;
*.py) black --quiet "$file_path" ;;
*.php) php-cs-fixer fix "$file_path" 2>/dev/null || true ;;
*.sh | *.bash) shfmt --write "$file_path" ;;
*)
# Extensionless scripts: format as shell when the shebang names bash or sh.
if head -1 "$file_path" 2>/dev/null | grep -q '^#!/.*\(bash\|sh\)'; then
shfmt --write "$file_path"
fi
;;
esac
|