1
0
Fork 0
dotfiles/home/config/nvim/plugins/formatter.fnl

59 lines
3.2 KiB
Plaintext
Raw Normal View History

2023-10-09 13:05:57 +00:00
(let [conform (require :conform)
conform-util (require :conform.util)
2023-07-21 10:29:50 +00:00
toml (require :toml)
treefmt-config-file vim.env.TREEFMT_CONFIG_FILE
2023-08-09 18:33:03 +00:00
treefmt-exts []
tmp-filename (.. "$" :FILENAME)]
2023-07-21 10:29:50 +00:00
(fn read-file [file]
(with-open [f (io.open file :rb)]
(f:read :*all)))
(when (not= treefmt-config-file nil)
(let [cfg (toml.parse (read-file treefmt-config-file))]
2023-07-23 20:40:54 +00:00
(each [_ opts (pairs cfg.formatter)]
(vim.list_extend treefmt-exts opts.includes))))
2023-07-13 17:57:12 +00:00
2023-10-09 13:05:57 +00:00
(fn treefmt-or-fallback [fallback]
(let [ext (vim.fn.expand "%:e")
2023-07-23 20:40:54 +00:00
ext-glob (.. "*." ext)]
(if (vim.list_contains treefmt-exts ext-glob)
2023-10-09 13:05:57 +00:00
{:command :treefmt
:args [tmp-filename]
:stdin false
:cwd (conform-util.root_file [:flake.nix :flake.lock])
:require_cwd true}
(= (vim.fn.executable fallback.command) 1)
fallback
{:command fallback.command
:available false
:available_msg "Executable not found"})))
2023-10-09 13:05:57 +00:00
(conform.setup {:formatters_by_ft {:css [:treefmt_or_prettier]
2023-10-09 16:07:32 +00:00
:elixir [:mix]
2023-10-09 13:05:57 +00:00
:fennel [:treefmt_or_fnlfmt]
:fish [:fish_indent]
:html [:treefmt_or_prettier]
:javascript [:treefmt_or_prettier]
:json [:treefmt_or_prettier]
:nix [:treefmt_or_nixpkgs_fmt]
:sh [:treefmt_or_shfmt]
:typescript [:treefmt_or_prettier]
:yaml [:treefmt_or_yamlfmt]}
:notify_on_error true
:formatters {:treefmt_or_fnlfmt #(treefmt-or-fallback {:command :fnlfmt
:args ["-"]})
:treefmt_or_nixpkgs_fmt #(treefmt-or-fallback {:command :nixpkgs-fmt})
:treefmt_or_prettier #(treefmt-or-fallback {:command :prettier
:args [:--stdin-filepath
tmp-filename]
:cwd (conform-util.root_file [:.prettierrc
:package.json])})
:treefmt_or_shfmt #(treefmt-or-fallback {:command :shfmt
:args [:-filename
tmp-filename
:-i
:2
:-s]})
:treefmt_or_yamlfmt #(treefmt-or-fallback {:command :yamlfmt
:args ["-"]})}}))