1
0
Fork 0

nvim: Start integrating treefmt

This commit is contained in:
Daniel Kempkens 2023-07-21 12:29:50 +02:00
parent d97ba0b62b
commit 4a80ad30fa
Signed by: daniel
SSH key fingerprint: SHA256:Ks/MyhQYcPRQiwMKLAKquWCdCPe3JXlb1WttgnAoSeM
3 changed files with 49 additions and 8 deletions

View file

@ -160,6 +160,8 @@
config.treefmt.build.devShell config.treefmt.build.devShell
config.pre-commit.devShell config.pre-commit.devShell
]; ];
TREEFMT_CONFIG_FILE = config.treefmt.build.configFile;
}; };
}; };
}; };

View file

@ -1,5 +1,39 @@
(let [formatter (require :formatter) (let [M {}
api vim.api] 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 []]
(each [i v (ipairs opts)]
(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)}))))
(fn buffer-filename [] (api.nvim_buf_get_name 0)) (fn buffer-filename [] (api.nvim_buf_get_name 0))
(fn args-prettier [parser] (fn args-prettier [parser]
@ -10,9 +44,14 @@
{:args [:-i shiftwidth]})) {:args [:-i shiftwidth]}))
(fn do-format [exe opts] (fn do-format [exe opts]
(if (= (vim.fn.executable exe) 1) (let [treefmt-formatter (. treefmt-formatters exe)]
(vim.tbl_extend :keep opts {: exe}) (if (not= treefmt-formatter nil)
{:exe :ls :cond #false})) 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)
(formatter.setup {:format_on_save #(not= vim.b.nifoc_formatter_disabled 1) (formatter.setup {:format_on_save #(not= vim.b.nifoc_formatter_disabled 1)
:filetype {:css #(do-format :prettier (args-prettier :css)) :filetype {:css #(do-format :prettier (args-prettier :css))
@ -29,5 +68,5 @@
:typescript #(do-format :prettier :typescript #(do-format :prettier
(args-prettier :typescript)) (args-prettier :typescript))
:yaml #(do-format :yamlfmt {:args [:-in]})} :yaml #(do-format :yamlfmt {:args [:-in]})}
:lsp [:elixirls :nil_ls]})) :lsp [:elixirls :nil_ls]})
M)

View file

@ -15,7 +15,7 @@ in
withRuby = false; withRuby = false;
withPython3 = false; withPython3 = false;
extraLuaPackages = luaPkgs: with luaPkgs; [ jsregexp readline ]; extraLuaPackages = luaPkgs: with luaPkgs; [ jsregexp lua-toml readline ];
extraPackages = with pkgs; [ extraPackages = with pkgs; [
git git