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

76 lines
3.4 KiB
Text
Raw Normal View History

2023-07-21 10:29:50 +00:00
(let [M {}
2023-07-23 20:40:54 +00:00
format (require :format)
format-core (require :core)
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 []
level vim.log.levels]
(var latest-used-formatter :unknown)
(fn notify [msg lvl]
(vim.notify (.. msg " [" latest-used-formatter "]") lvl {:title :Format}))
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-07-23 20:40:54 +00:00
(fn treefmt-or-fallback [file-path fallback]
(let [ext (format-core.file.extension file-path)
ext-glob (.. "*." ext)]
(if (vim.list_contains treefmt-exts ext-glob)
2023-08-09 18:33:03 +00:00
(do
(set latest-used-formatter :treefmt)
[{:cmd :treefmt
:args [file-path]
:ignore_err (fn [err data]
(and (= err nil) (not (string.find data :Error))))}])
2023-07-23 20:40:54 +00:00
(= (vim.fn.executable fallback.cmd) 1)
2023-08-09 18:33:03 +00:00
(do
(set latest-used-formatter fallback.cmd)
[fallback])
2023-07-23 20:40:54 +00:00
[])))
2023-07-23 20:40:54 +00:00
(fn formatter-prettier [file-path]
(treefmt-or-fallback file-path {:cmd :prettier :args [:--write file-path]}))
2023-07-21 10:29:50 +00:00
2023-07-23 20:40:54 +00:00
(fn M.treefmt-extensions [] treefmt-exts)
2023-07-19 12:48:38 +00:00
2023-07-23 20:40:54 +00:00
(format.setup {:filetypes {:css formatter-prettier
:fennel (fn [file-path]
(treefmt-or-fallback file-path
{:cmd :fnlfmt
:args [:--fix
file-path]}))
:fish (fn [file-path]
{:cmd :fish_indent
:args [:--write file-path]})
:html formatter-prettier
:javascript formatter-prettier
:json formatter-prettier
:nix (fn [file-path]
(treefmt-or-fallback file-path
{:cmd :nixpkgs-fmt
:args [file-path]}))
:sh (fn [file-path]
(treefmt-or-fallback file-path
{:cmd :shfmt
:args [:-i
:2
:-s
:-w
file-path]}))
:typescript formatter-prettier
:yaml (fn [file-path]
(treefmt-or-fallback file-path
{:cmd :yamlfmt
2023-08-09 18:33:03 +00:00
:args [file-path]}))}
:hooks {:on_success #(notify "Formatting Succeed" level.INFO)
:on_err #(notify "Formatting Failed" level.ERROR)
:on_timeout #(notify "Formatting Timeout" level.ERROR)}})
2023-07-21 10:29:50 +00:00
M)