Initial import
This commit is contained in:
70
_dir-locals.el
Normal file
70
_dir-locals.el
Normal file
@@ -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)
|
||||
)))))
|
||||
)))))
|
||||
21
_reformat-common.sh
Normal file
21
_reformat-common.sh
Normal file
@@ -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
|
||||
}
|
||||
38
do-reformat.sh
Executable file
38
do-reformat.sh
Executable file
@@ -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
|
||||
54
would-reformat.sh
Executable file
54
would-reformat.sh
Executable file
@@ -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
|
||||
Reference in New Issue
Block a user