diff --git a/apheleia-formatters.el b/apheleia-formatters.el index abdb009..606e3e2 100644 --- a/apheleia-formatters.el +++ b/apheleia-formatters.el @@ -89,7 +89,8 @@ (lisp-indent . apheleia-indent-lisp-buffer) (ktlint . ("ktlint" "--log-level=none" "--stdin" "-F" "-")) (latexindent . ("latexindent" "--logfile=/dev/null")) - (mix-format . ("apheleia-mix" "format" "-")) + (mix-format . ("apheleia-from-project-root" + ".formatter.exs" "mix" "format" "-")) (nixfmt . ("nixfmt")) (ocamlformat . ("ocamlformat" "-" "--name" filepath "--enable-outside-detected-project")) @@ -159,6 +160,8 @@ "--stderr" "--format" "quiet" "--fail-level" "fatal")) (ruby-standard . ("standardrb" "--stdin" filepath "--fix" "--stderr" "--format" "quiet" "--fail-level" "fatal")) + (ruby-syntax-tree . ("apheleia-from-project-root" + ".streerc" "stree" "format" filepath)) (ruff . ("ruff" "format" "--silent" (apheleia-formatters-fill-column "--line-length") diff --git a/apheleia-utils.el b/apheleia-utils.el index 7e3820f..842da81 100644 --- a/apheleia-utils.el +++ b/apheleia-utils.el @@ -92,6 +92,17 @@ FILE-NAME." (when-let ((file (locate-dominating-file default-directory file-name))) (list file-flag (concat (expand-file-name file) file-name)))) +(defun apheleia-formatters-args-from-file (file-name) + "Extract arguments from a text file. +Look for a file up recursively from the current directory until FILE-NAME is +found. If found, read the file and return an Alist of lines in the file." + (when-let ((file (locate-dominating-file default-directory file-name))) + (with-temp-buffer + (insert-file-contents (concat (expand-file-name file) file-name)) + (cl-loop for line in (split-string (buffer-string) "\n" t) + collect line))) + ) + (defun apheleia-formatters-extension-p (&rest exts) "Assert whether current buffer has an extension in EXTS." (when-let ((name buffer-file-name) diff --git a/scripts/formatters/apheleia-from-project-root b/scripts/formatters/apheleia-from-project-root new file mode 100755 index 0000000..1c22bed --- /dev/null +++ b/scripts/formatters/apheleia-from-project-root @@ -0,0 +1,40 @@ +#!/usr/bin/env bash + +# USAGE: apheleia-from-project-root ROOT-FNAME CMD... +# +# If there is a file called ROOT-FNAME in the current directory or any +# parent directory up to the root of the filesystem, first change to +# that directory. If not, no worries, proceed without error. Then +# execute CMD with provided args. +# +# This can be used to make sure that a formatter is executed in the +# root directory of a project, if the file is within a project. For +# example, ROOT-FNAME could be ".git" or ".formatter.exs" or +# "package.json" or "pyproject.toml". + +if (( "$#" <= 1 )); then + echo >&2 "usage: apheleia-from-project-root ROOT-FNAME CMD..." + exit 1 +fi + +# This function prints the name of the current directory if it +# contains a file or directory named after the first argument, or the +# parent directory if it contains such a file, or the parent's parent, +# and so on. If no such file is found it returns nonzero. +# https://unix.stackexchange.com/a/22215 +find_upwards() { + fname="$1" + + path="${PWD}" + while [[ -n "${path}" && ! -e "${path}/${fname}" ]]; do + path="${path%/*}" + done + [[ -n "${path}" ]] && echo "${path}" +} + +if dir="$(find_upwards "$1")"; then + cd -- "${dir}" || exit +fi + +shift +exec "$@" diff --git a/scripts/formatters/apheleia-mix b/scripts/formatters/apheleia-mix deleted file mode 100755 index d2038e1..0000000 --- a/scripts/formatters/apheleia-mix +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/env bash - -# This function prints the name of the current directory if it -# contains a file or directory named after the first argument, or the -# parent directory if it contains such a file, or the parent's parent, -# and so on. If no such file is found it returns nonzero. -# https://unix.stackexchange.com/a/22215 -find_upwards() { - fname="$1" - - path="${PWD}" - while [[ -n "${path}" && ! -e "${path}/${fname}" ]]; do - path="${path%/*}" - done - [[ -n "${path}" ]] && echo "${path}" -} - -if dir="$(find_upwards .formatter.exs)"; then - cd -- "${dir}" || exit -fi - -exec mix "$@" diff --git a/test/formatters/installers/ruby-syntax-tree.bash b/test/formatters/installers/ruby-syntax-tree.bash new file mode 100644 index 0000000..9468c07 --- /dev/null +++ b/test/formatters/installers/ruby-syntax-tree.bash @@ -0,0 +1,4 @@ +# Need ruby for gem +apt-get install -y ruby + +gem install syntax_tree diff --git a/test/formatters/samplecode/ruby-syntax-tree/in.rb b/test/formatters/samplecode/ruby-syntax-tree/in.rb new file mode 100644 index 0000000..2a2f35a --- /dev/null +++ b/test/formatters/samplecode/ruby-syntax-tree/in.rb @@ -0,0 +1,13 @@ + d=[30644250780,9003106878, + 30636278846,66641217692,4501790980, + 671_24_603036,131_61973916,66_606629_920, + 30642677916,30643069058];a,s=[],$*[0] + s.each_byte{|b|a<<("%036b"%d[b. + chr.to_i]).scan(/\d{6}/)} + a.transpose.each{ |a| + a.join.each_byte{\ + |i|print i==49?\ + ($*[1]||"#")\ + :32.chr} + puts + } diff --git a/test/formatters/samplecode/ruby-syntax-tree/out.rb b/test/formatters/samplecode/ruby-syntax-tree/out.rb new file mode 100644 index 0000000..3bf1506 --- /dev/null +++ b/test/formatters/samplecode/ruby-syntax-tree/out.rb @@ -0,0 +1,18 @@ +d = [ + 30_644_250_780, + 9_003_106_878, + 30_636_278_846, + 66_641_217_692, + 4_501_790_980, + 671_24_603036, + 131_61973916, + 66_606629_920, + 30_642_677_916, + 30_643_069_058 +] +a, s = [], $*[0] +s.each_byte { |b| a << ("%036b" % d[b.chr.to_i]).scan(/\d{6}/) } +a.transpose.each do |a| + a.join.each_byte { |i| print i == 49 ? ($*[1] || "#") : 32.chr } + puts +end