2023-07-21 10:29:50 +00:00
|
|
|
(let [M {}
|
|
|
|
formatter (require :formatter)
|
|
|
|
api vim.api
|
|
|
|
toml (require :toml)
|
|
|
|
treefmt-config-file vim.env.TREEFMT_CONFIG_FILE
|
|
|
|
treefmt-formatters {}]
|
|
|
|
(fn read-file [file]
|
|
|
|
(with-open [f (io.open file :rb)]
|
|
|
|
(f:read :*all)))
|
|
|
|
|
|
|
|
(fn add-option [opts addable]
|
|
|
|
(table.insert opts addable)
|
|
|
|
opts)
|
|
|
|
|
|
|
|
(fn remove-option [opts removeable]
|
|
|
|
(let [new-opts []]
|
2023-07-21 15:29:40 +00:00
|
|
|
(each [_ v (ipairs opts)]
|
2023-07-21 10:29:50 +00:00
|
|
|
(when (not= v removeable) (table.insert new-opts v)))
|
|
|
|
new-opts))
|
|
|
|
|
|
|
|
(when (not= treefmt-config-file nil)
|
|
|
|
(let [cfg (toml.parse (read-file treefmt-config-file))]
|
|
|
|
(when (not= cfg.formatter.fnlfmt nil)
|
|
|
|
(set treefmt-formatters.fnlfmt
|
|
|
|
{:exe cfg.formatter.fnlfmt.command
|
|
|
|
:args (-> cfg.formatter.fnlfmt.options (remove-option :--fix)
|
|
|
|
(add-option "-"))}))
|
|
|
|
(when (not= cfg.formatter.shfmt nil)
|
|
|
|
(set treefmt-formatters.shfmt
|
|
|
|
{:exe cfg.formatter.shfmt.command
|
|
|
|
:args (remove-option cfg.formatter.shfmt.options :-w)}))
|
|
|
|
(when (not= cfg.formatter.yamlfmt nil)
|
|
|
|
(set treefmt-formatters.yamlfmt
|
|
|
|
{:exe cfg.formatter.yamlfmt.command
|
|
|
|
:args (add-option cfg.formatter.yamlfmt.options :-in)}))))
|
|
|
|
|
2023-07-19 12:48:38 +00:00
|
|
|
(fn buffer-filename [] (api.nvim_buf_get_name 0))
|
2023-07-12 18:39:50 +00:00
|
|
|
|
2023-07-19 12:48:38 +00:00
|
|
|
(fn args-prettier [parser]
|
|
|
|
{:args [:--stdin-filepath (buffer-filename) :--parser parser]})
|
2023-07-13 17:57:12 +00:00
|
|
|
|
2023-07-19 12:48:38 +00:00
|
|
|
(fn args-shfmt []
|
|
|
|
(let [shiftwidth (vim.opt.shiftwidth:get)]
|
|
|
|
{:args [:-i shiftwidth]}))
|
2023-07-12 18:39:50 +00:00
|
|
|
|
2023-07-19 12:48:38 +00:00
|
|
|
(fn do-format [exe opts]
|
2023-07-21 10:29:50 +00:00
|
|
|
(let [treefmt-formatter (. treefmt-formatters exe)]
|
|
|
|
(if (not= treefmt-formatter nil)
|
|
|
|
treefmt-formatter
|
|
|
|
(if (= (vim.fn.executable exe) 1)
|
|
|
|
(vim.tbl_extend :keep opts {: exe})
|
|
|
|
{:exe :ls :cond #false}))))
|
|
|
|
|
|
|
|
(fn M.treefmt-based-formatters [] treefmt-formatters)
|
2023-07-19 12:48:38 +00:00
|
|
|
|
|
|
|
(formatter.setup {:format_on_save #(not= vim.b.nifoc_formatter_disabled 1)
|
|
|
|
:filetype {:css #(do-format :prettier (args-prettier :css))
|
|
|
|
:fennel #(do-format :fnlfmt {:args ["-"]})
|
|
|
|
:fish #(do-format :fish_indent {})
|
|
|
|
:html #(do-format :prettier
|
|
|
|
(args-prettier :html))
|
|
|
|
:javascript #(do-format :prettier
|
|
|
|
(args-prettier :javascript))
|
|
|
|
:json #(do-format :prettier
|
|
|
|
(args-prettier :json))
|
|
|
|
:sh #(do-format :shfmt (args-shfmt))
|
|
|
|
:toml #(do-format :taplo {:args [:fmt "-"]})
|
|
|
|
:typescript #(do-format :prettier
|
|
|
|
(args-prettier :typescript))
|
|
|
|
:yaml #(do-format :yamlfmt {:args [:-in]})}
|
2023-07-21 10:29:50 +00:00
|
|
|
:lsp [:elixirls :nil_ls]})
|
|
|
|
M)
|