From cf3493c9c5b38827468a37c0f6aabdbb779995b0 Mon Sep 17 00:00:00 2001 From: Michael Wolf Date: Fri, 22 May 2020 10:44:52 -0500 Subject: [PATCH] Initial import --- _dir-locals.el | 70 +++++++++++++++++++++++++++++++++++++++++++++ _reformat-common.sh | 21 ++++++++++++++ do-reformat.sh | 38 ++++++++++++++++++++++++ would-reformat.sh | 54 ++++++++++++++++++++++++++++++++++ 4 files changed, 183 insertions(+) create mode 100644 _dir-locals.el create mode 100644 _reformat-common.sh create mode 100755 do-reformat.sh create mode 100755 would-reformat.sh diff --git a/_dir-locals.el b/_dir-locals.el new file mode 100644 index 0000000..dca168c --- /dev/null +++ b/_dir-locals.el @@ -0,0 +1,70 @@ +((nil . ((eval . + (progn (add-hook + 'after-save-hook + (lambda () + (let* ((file-name (buffer-file-name)) + (would-reformat-cmd + (format "%s/bin/would-reformat.sh %s" + (projectile-project-root) + file-name)) + (res (shell-command-to-string would-reformat-cmd))) + (progn (message (format "doing %s" file-name)) + (message (format "running command: %s" would-reformat-cmd)) + (cond ((string-equal res "would change") + (message (format "file `%s' would change (%s)" file-name res))) + ((string-equal res "would not change") + (message (format "file `%s' would not change (%s)" file-name res))) + ;; ((string-equal "ignoring") t) + )))) + + nil + t) + (global-set-key + [f1] + (lambda () + (interactive) + (if (buffer-modified-p) + (message "save buffer first") + + (let* ((file-name (buffer-file-name)) + ;; tx https://stackoverflow.com/a/23299809 + (do-reformat-cmd + (format "%s/bin/do-reformat.sh" (projectile-project-root))) + (result (funcall + (lambda (program &rest args) + "Run PROGRAM with ARGS and return the exit code and output in a list." + ;; (interactive) + (with-temp-buffer + (list (apply 'call-process program nil (current-buffer) nil args) + (buffer-string)))) + do-reformat-cmd + file-name)) + (status (car result)) + (output (car (cdr result)))) + (cond ((= status 0) + (progn (revert-buffer nil t) + (message (format "%s" output)))) + ((/= status 0) + (message (format "%s" output))))))) + ) + (global-set-key + [(shift f1)] + (lambda () + (interactive) + (let* ((file-name (buffer-file-name)) + (would-reformat-cmd + (format "%s/bin/would-reformat.sh %s" + (projectile-project-root) + file-name)) + (res (shell-command-to-string would-reformat-cmd))) + (progn (message (format "doing %s" file-name)) + (message (format "running command: %s" would-reformat-cmd)) + (cond ((string-equal res "would change") + (message (format "file `%s' would change (%s)" file-name res))) + ((string-equal res "would not change") + (message (format "file `%s' would not change (%s)" file-name res))) + ((string-equal res "syntax error") + (message (format "file `%s' has a syntax error (%s)" file-name res))) + ;; ((string-equal "ignoring") t) + ))))) + ))))) diff --git a/_reformat-common.sh b/_reformat-common.sh new file mode 100644 index 0000000..0e2fcd9 --- /dev/null +++ b/_reformat-common.sh @@ -0,0 +1,21 @@ +function sniff_file_type () { + ff=$1 + shift + file_type= + + if [[ $ff == *.py ]] ; then + file_type="python" + elif [[ $ff == *.js ]] ; then + file_type="javascript" + elif [[ $ff == *.ts ]] ; then + file_type="typescript" + elif [[ $ff == *.vue ]] ; then + file_type="vue" + elif [[ $ff == *.css ]] ; then + file_type="css" + elif [[ $ff == *.php ]] ; then + file_type="php" + fi + + echo $file_type +} diff --git a/do-reformat.sh b/do-reformat.sh new file mode 100755 index 0000000..4a74938 --- /dev/null +++ b/do-reformat.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +set -uo pipefail +IFS=$'\n\t' + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +source "$DIR/_reformat-common.sh" + +file="$1" + +file_type=$(sniff_file_type $file) + +if [[ $file_type == "javascript" || \ + $file_type == "vue" || \ + $file_type == "css" || \ + $file_type == "typescript" || \ + $file_type == "php" ]] ; then + out=$(npx prettier --write $file) + retval="$?" + success_retval=0 +elif [[ $file_type == "python" ]] ; then + out=$(pipx run black $file 2>/dev/null) + retval="$?" + success_retval=0 +else + echo -n "ignoring" + exit 0 +fi + +if [[ x"$retval" == x"$success_retval" ]] ; then + echo "$out" + exit 0 +else + exit $retval +fi + +exit 0 diff --git a/would-reformat.sh b/would-reformat.sh new file mode 100755 index 0000000..e3dda2d --- /dev/null +++ b/would-reformat.sh @@ -0,0 +1,54 @@ +#!/bin/bash + +set -uo pipefail +IFS=$'\n\t' + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +file="$1" + +source "$DIR/_reformat-common.sh" + +file_type=$(sniff_file_type $file) + +retval=0 +success_retval=0 +if [[ $file_type == "javascript" || \ + $file_type == "vue" || \ + $file_type == "typescript" || \ + $file_type == "css" || \ + $file_type == "php" ]] ; then + out=$(npx prettier --check $file 2>&1 > /dev/null) + retval="$?" + success_retval=0 + would_reformat_retval=1 + syntax_error_retval=2 +elif [[ $file_type == "python" ]] ; then + out=$(pipx run black --check $file 2>/dev/null) + retval="$?" + success_retval=0 + would_reformat_retval=1 + syntax_error_retval=123 +else + echo -n "ignoring" + exit 0 + +fi + +if [[ x"$retval" == x"$success_retval" ]] ; then + echo -n "would not change" + exit 0 +elif [[ x"$retval" == x"$would_reformat_retval" ]] ; then + echo -n "would change" + exit 1 +elif [[ x"$retval" == x"$syntax_error_retval" ]] ; then + echo -n "syntax error" + exit 2 +else + echo -n "problem running reformatter" + + exit 3 +fi + +echo -n "ok" +exit 0