Add initial support for custom sniffing

This commit is contained in:
Michael Wolf
2023-09-30 15:33:25 -06:00
parent 3034cd23e8
commit ce634e496c
3 changed files with 77 additions and 0 deletions

View File

@@ -68,6 +68,21 @@ function resolve_symlink() {
echo "$out" echo "$out"
} }
function custom_sniff() {
file="$1"
pr=$(project_root "$file")
if [[ -f "$pr/.would-reformat/custom-sniffer" ]]; then
custom=$("$pr/.would-reformat/custom-sniffer" "$1")
ret="$?"
echo "$custom"
return 0
fi
}
function custom_formatter() { function custom_formatter() {
root="$1" root="$1"
file_type="$2" file_type="$2"
@@ -88,6 +103,12 @@ function sniff_file_type() {
ff=$1 ff=$1
shift shift
maybe_custom=$(custom_sniff "$ff")
if [[ "$maybe_custom" != "" ]]; then
echo "$maybe_custom"
return 0
fi
file_type= file_type=
if [[ $ff == *.py ]]; then if [[ $ff == *.py ]]; then
@@ -118,6 +139,8 @@ function sniff_file_type() {
file_type="golang" file_type="golang"
elif [[ $ff = *.dart ]]; then elif [[ $ff = *.dart ]]; then
file_type="dart" file_type="dart"
elif [[ $ff = *.pl ]]; then
file_type="perl"
fi fi
echo $file_type echo $file_type

18
examples/custom-sniffer Executable file
View File

@@ -0,0 +1,18 @@
#!/usr/bin/env bash
file="$1"
if [[ $file == *.pl ]]; then
# the default is that .pl corresponds to perl, but not here!
echo -n "prolog"
fi
if [[ $file == *.ts ]]; then
# qt translations, who knew
echo -n "qt-translation"
fi
# We're fine with the defaults for other types of files so we don't
# print anything else.
echo ""
exit 0

View File

@@ -74,5 +74,41 @@ testWfRoot() {
assertEquals "$expected" "$got" assertEquals "$expected" "$got"
} }
testNoCustomSniffer001() {
dir=$(mktemp -d --tmpdir would-reformat-testProjectRoot001.XXXXXX)
cd "$dir" || exit
git init >/dev/null 2>&1
"$DIR"/../install.sh 2>&2 >/dev/null
touch perl-file.pl
touch typescript-file.ts
out1=$(sniff_file_type "$(pwd)"/perl-file.pl)
out2=$(sniff_file_type "$(pwd)"/typescript-file.ts)
assertEquals "perl" "$out1"
assertEquals "typescript" "$out2"
}
testCustomSniffer001() {
dir=$(mktemp -d --tmpdir would-reformat-testProjectRoot001.XXXXXX)
cd "$dir" || exit
git init >/dev/null 2>&1
"$DIR"/../install.sh 2>&2 >/dev/null
mkdir -p .would-reformat || exit 1
cp "$(wf_root)"/examples/custom-sniffer ./.would-reformat/custom-sniffer || exit 1
chmod +x ./.would-reformat/custom-sniffer || exit 1
touch prolog-file.pl
touch qt-translation-file.ts
out1=$(sniff_file_type "$(pwd)"/prolog-file.pl)
out2=$(sniff_file_type "$(pwd)"/qt-translation-file.ts)
assertEquals "prolog" "$out1"
assertEquals "qt-translation" "$out2"
}
# shellcheck disable=SC1091 # shellcheck disable=SC1091
source shunit2 source shunit2