Add ruby-syntax-tree format support (#224)

Add [ruby-syntax-tree](https://github.com/ruby-syntax-tree/syntax_tree)
as an available formatter. Include support for finding a [`.streerc`
file](https://github.com/ruby-syntax-tree/syntax_tree#configuration).

---------

Co-authored-by: Radon Rosborough <radon@intuitiveexplanations.com>
This commit is contained in:
Nolan 2023-12-21 19:34:11 -08:00 committed by GitHub
parent 3aa747856a
commit 93f7480c96
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 90 additions and 23 deletions

View file

@ -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")

View file

@ -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)

View file

@ -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 "$@"

View file

@ -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 "$@"

View file

@ -0,0 +1,4 @@
# Need ruby for gem
apt-get install -y ruby
gem install syntax_tree

View file

@ -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
}

View file

@ -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