Skip to content

Commit

Permalink
Evaluate any non-special args in apheleia-formatters (#55) (#56)
Browse files Browse the repository at this point in the history
* Evaluate any non-special args in apheleia-formatters (#55)

Allows you to place snippets of elisp within a formatter command that
will be evaluated and substituted in place when apheleia decides to run
that command.

* Update apheleia.el

Co-authored-by: Radon Rosborough <[email protected]>
  • Loading branch information
mohkale and raxod502 authored Oct 31, 2021
1 parent 0c88ad9 commit 1b7f2cf
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ The format is based on [Keep a Changelog].
* Support multiple formatters ([#31]). You can now configure a list of
formatters for a major-mode in `apheleia-mode-alist` and they will
be run in sequence.
* Support evaluating items in `apheleia-formatters` to make formatter
commands more dynamic ([#50], [#55]).

### Formatters
* [ClangFormat](https://clang.llvm.org/docs/ClangFormat.html) for
Expand Down Expand Up @@ -43,7 +45,9 @@ The format is based on [Keep a Changelog].
[#39]: https://github.com/raxod502/apheleia/issues/39
[#48]: https://github.com/raxod502/apheleia/pull/48
[#49]: https://github.com/raxod502/apheleia/pull/49
[#50]: https://github.com/raxod502/apheleia/pull/50
[#54]: https://github.com/raxod502/apheleia/pull/54
[#55]: https://github.com/raxod502/apheleia/issues/55

## 1.1.2 (released 2021-02-26)
### Enhancements
Expand Down
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,31 @@ variables:
(setf (alist-get 'black apheleia-formatters)
'("black" "--option" "..." "-"))
```
* There are a list of symbols that are interpreted by apheleia
specially when formatting a command (example: `npx`). Any
non-string entries in a formatter that doesn't equal one of
these symbols is evaluated and replaced in place. This can be
used to pass certain flags to the formatter process depending on
the state of the current buffer. For example:
```elisp
(push '(shfmt . ("beautysh"
"-filename" filepath
(when-let ((indent (bound-and-true-p sh-basic-offset)))
(list "--indent-size" (number-to-string indent)))
(when indent-tabs-mode "--tab")
"-"))
apheleia-formatters)
```
This adds an entry to `apheleia-formatters` for the `beautysh`
formatter. The evaluated entries makes it so that the `--tab`
flag is only passed to `beautysh` when the value of
`indent-tabs-mode` is true. Similarly the indent-size flag is
passed the exact value of the `sh-basic-offset` variable
only when it is bound. Observe that one of these evaluations
returns a list of flags whereas the other returns a single
string. These are substituted into the command as you'd expect.
* `apheleia-mode-alist`: Alist mapping major modes and filename
regexps to names of formatters to use in those modes and files. See
the docstring for more information.
Expand Down
20 changes: 20 additions & 0 deletions apheleia.el
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,26 @@ sequence unless it's first in the sequence"))
(cl-return)))
arg))
command)))
;; Evaluate each element of arg that isn't a string and replace
;; it with the evaluated value. The result of an evaluation should
;; be a string or a list of strings. If the former its replaced as
;; is. If the latter the contents of the list is substituted in
;; place.
(setq command
(cl-loop
for arg in command
with val = nil
do (setq val (if (stringp arg)
arg
(eval arg)))
if val
if (and (consp val)
(cl-every #'stringp val))
append val
else if (stringp val)
collect val
else do (error "Result of command evaluation must be a string \
or list of strings: %S" arg)))
`(,input-fname ,output-fname ,stdin ,@command))))

(defun apheleia--run-formatters (commands buffer callback &optional stdin)
Expand Down

0 comments on commit 1b7f2cf

Please sign in to comment.