Skip to content

Commit

Permalink
Support multiple formatters (radian-software#31)
Browse files Browse the repository at this point in the history
Closes radian-software#31

This commit makes it so apheleia can run multiple formatters one after
the other and use the resultant output to format the current buffer.
This works somewhat like a pipeline. The output of one formatter becomes
the input to the next formatter until all formatters have run and then
an RCS patch is built from the resultant output and applied to the
current buffer.

Note: For convenience we internally represent the users configuration as
a list of formatters even when it may only be one. For example if the
user has configured `(python-mode . black)` in apheleia-mode-alist then
internally we interpret that as a formatter list of `(black)` instead of
`black` as we did previously.
  • Loading branch information
mohkale committed Oct 24, 2021
1 parent 8b9d576 commit c636ffe
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 98 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog].

## Unreleased
### Enhancements
* 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.

### Formatters
* [ClangFormat](https://clang.llvm.org/docs/ClangFormat.html) for
C/C++
Expand All @@ -29,6 +34,7 @@ The format is based on [Keep a Changelog].

[#24]: https://github.com/raxod502/apheleia/pull/24
[#30]: https://github.com/raxod502/apheleia/issues/30
[#31]: https://github.com/raxod502/apheleia/issues/31
[#32]: https://github.com/raxod502/apheleia/pull/32
[#39]: https://github.com/raxod502/apheleia/issues/39
[#48]: https://github.com/raxod502/apheleia/pull/48
Expand Down
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,30 @@ variables:
(setf (alist-get 'black apheleia-formatters)
'("black" "--option" "..." "-"))
```
* `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.
* You can use this variable to configure multiple formatters for
the same buffer by setting the `cdr` of an entry to a list of
formatters to run instead of a single formatter. For example you
may want to run `isort` and `black` one after the other.
```elisp
(setf (alist-get 'isort apheleia-formatters)
'("isort" "--stdout" "-"))
(setf (alist-get 'python-mode apheleia-mode-alist)
'(isort black))
```
This will make apheleia run `isort` on the current buffer and then
`black` on the result of `isort` and then use the final output to
format the current buffer.
**Warn**: At the moment there's no smart or configurable error
handling in place. This means if one of the configured
formatters fail (for example if `isort` isn't installed) then
apheleia just doesn't format the buffer at all, even if `black`
is installed.
* `apheleia-formatter`: Optional buffer-local variable specifying the
formatter to use in this buffer. Overrides `apheleia-mode-alist`.
Expand Down
Loading

0 comments on commit c636ffe

Please sign in to comment.