Defining a formatter for Cabal files
For Haskell code I can use lsp-format-buffer
and lsp-format-region
to keep
my file looking nice, but I've never found a function for doing the same for
Cabal files. There's a nice command line tool, cabal-fmt
, for doing it, but it
means having to jump to a terminal. It would of course be nicer to satisfy my
needs for aesthetics directly from Emacs. A few times I've thought of writing
the function myself, I mean how hard can it be? But then I've forgotten about it
until then next time I'm editing a Cabal file.
A few days ago I noticed emacs-reformatter popping up in my feeds. That removed all reasons to procrastinate. It turned out to be very easy to set up.
The package doesn't have a recipe for straight.el so it needs a :straight
section. Also, the naming of the file in the package doesn't fit the package
name, hence the slightly different name in the use-package
declaration:1
(use-package reformatter :straight (:host github :repo "purcell/emacs-reformatter"))
Now the formatter can be defined
(reformatter-define cabal-format :program "cabal-fmt" :args '("/dev/stdin"))
in order to create functions for formatting, cabal-format-buffer
and
cabal-format-region
, as well as a minor mode for formatting on saving a Cabal
file.
Footnotes:
I'm sure it's possible to use :files
to deal with this, but I'm not sure
how and my naive guess failed. It's OK to be like this until I figure it out
properly.