From ce634e496caadb155feb4b870aae6c8424461d53 Mon Sep 17 00:00:00 2001 From: Michael Wolf Date: Sat, 30 Sep 2023 15:33:25 -0600 Subject: [PATCH] Add initial support for custom sniffing --- _reformat-common.bash | 23 +++++++++++++++++++++++ examples/custom-sniffer | 18 ++++++++++++++++++ tests/project-root1.sh | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100755 examples/custom-sniffer diff --git a/_reformat-common.bash b/_reformat-common.bash index 4721b45..10cfb56 100644 --- a/_reformat-common.bash +++ b/_reformat-common.bash @@ -68,6 +68,21 @@ function resolve_symlink() { 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() { root="$1" file_type="$2" @@ -88,6 +103,12 @@ function sniff_file_type() { ff=$1 shift + maybe_custom=$(custom_sniff "$ff") + if [[ "$maybe_custom" != "" ]]; then + echo "$maybe_custom" + return 0 + fi + file_type= if [[ $ff == *.py ]]; then @@ -118,6 +139,8 @@ function sniff_file_type() { file_type="golang" elif [[ $ff = *.dart ]]; then file_type="dart" + elif [[ $ff = *.pl ]]; then + file_type="perl" fi echo $file_type diff --git a/examples/custom-sniffer b/examples/custom-sniffer new file mode 100755 index 0000000..99e9a2e --- /dev/null +++ b/examples/custom-sniffer @@ -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 diff --git a/tests/project-root1.sh b/tests/project-root1.sh index 6861520..449822f 100755 --- a/tests/project-root1.sh +++ b/tests/project-root1.sh @@ -74,5 +74,41 @@ testWfRoot() { 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 source shunit2