1
0
Fork 0

nvim: Change out formatter ... again

This commit is contained in:
Daniel Kempkens 2023-07-19 14:48:38 +02:00
parent e16fccbfd0
commit e19653ef02
Signed by: daniel
SSH key fingerprint: SHA256:Ks/MyhQYcPRQiwMKLAKquWCdCPe3JXlb1WttgnAoSeM
6 changed files with 52 additions and 98 deletions

View file

@ -1,42 +1,21 @@
(let [mod {} (let [mod {}
b vim.b b vim.b
cmd vim.cmd
api vim.api api vim.api
set-bufvar vim.api.nvim_buf_set_var set-bufvar vim.api.nvim_buf_set_var]
treefmt-exists (vim.fn.executable :treefmt)]
(fn mod.setup [] (fn mod.setup []
(let [usercmd vim.api.nvim_create_user_command (let [usercmd vim.api.nvim_create_user_command]
augroup (vim.api.nvim_create_augroup :NifocFormatting {:clear true})
aucmd vim.api.nvim_create_autocmd]
(usercmd :FormatDisableBuffer mod.disable-for-buffer (usercmd :FormatDisableBuffer mod.disable-for-buffer
{:desc "Disable Formatting for the current buffer"}) {:desc "Disable Formatting for the current buffer"})
(usercmd :FormatEnableBuffer mod.enable-for-buffer (usercmd :FormatEnableBuffer mod.enable-for-buffer
{:desc "Enable Formatting for the current buffer"}) {:desc "Enable Formatting for the current buffer"})))
(aucmd :BufWritePre
{:callback mod.maybe-format-buffer-pre (fn notify [msg]
:group augroup (vim.notify msg vim.log.levels.INFO {:title :Formatter}))
:desc "Run Formatter before saving"})
(aucmd :BufWritePost
{:callback mod.maybe-format-buffer-post
:group augroup
:desc "Run Formatter after saving"})))
(fn has-formatter-config? [ft] (fn has-formatter-config? [ft]
(let [formatter-fts (vim.tbl_keys (. (require :formatter.config) :values (let [cfg (require :formatter.config)
:filetype))] fts (cfg.get :filetype)]
(vim.tbl_contains formatter-fts ft))) (not= (. fts ft) nil)))
(fn has-treefmt-config? [ft]
(and (= treefmt-exists 1) (has-formatter-config? ft)))
(fn run-formatter-exe [] (cmd :FormatWriteLock))
(fn run-lsp-format []
(if (not= b.nifoc_formatter_filter_lsp_client nil)
(vim.lsp.buf.format {:filter #(= $1.name
b.nifoc_formatter_filter_lsp_client)
:timeout_ms 1000})
(vim.lsp.buf.format {:timeout_ms 1000})))
(fn mod.enable-for-buffer [] (fn mod.enable-for-buffer []
(set-bufvar 0 :nifoc_formatter_disabled 0)) (set-bufvar 0 :nifoc_formatter_disabled 0))
@ -47,28 +26,11 @@
(fn mod.active? [] (fn mod.active? []
(let [ft vim.bo.filetype] (let [ft vim.bo.filetype]
(if (= b.nifoc_formatter_disabled 1) false (if (= b.nifoc_formatter_disabled 1) false
(= b.nifoc_lsp_formatter_enabled 1) true (has-formatter-config? ft))))
(has-formatter-config? ft) true
false)))
(fn mod.maybe-enable-lsp [client bufnr] (fn mod.maybe-enable-lsp [client bufnr]
(when (client.supports_method :textDocument/rangeFormatting) (when (client.supports_method :textDocument/rangeFormatting)
(api.nvim_buf_set_option bufnr :formatexpr "v:lua.vim.lsp.formatexpr()")) (api.nvim_buf_set_option bufnr :formatexpr "v:lua.vim.lsp.formatexpr()")))
(when (client.supports_method :textDocument/formatting)
(set-bufvar bufnr :nifoc_lsp_formatter_enabled 1)))
(fn mod.maybe-format-buffer-pre []
(let [ft vim.bo.filetype]
(if (= b.nifoc_formatter_disabled 1) nil
(has-treefmt-config? ft) nil
(= b.nifoc_lsp_formatter_enabled 1) (run-lsp-format))))
(fn mod.maybe-format-buffer-post []
(let [ft vim.bo.filetype]
(if (= b.nifoc_formatter_disabled 1) nil
(has-treefmt-config? ft) (run-formatter-exe)
(= b.nifoc_lsp_formatter_enabled 1) nil
(has-formatter-config? ft) (run-formatter-exe))))
mod) mod)

View file

@ -1,35 +1,33 @@
(let [formatter (require :formatter) (let [formatter (require :formatter)
formatter-utils (require :formatter.util) api vim.api]
treefmt-exists (vim.fn.executable :treefmt) (fn buffer-filename [] (api.nvim_buf_get_name 0))
treefmt-formatter #{:exe :treefmt :stdin false :tempfile_prefix :treefmt}]
(fn builtin-formatter [ft formatter]
(let [file (.. :formatter.filetypes. ft)]
(. (require file) formatter)))
(fn other-formatter [ft-or-fn formatter] (fn args-prettier [parser]
(if (= (type ft-or-fn) :function) (ft-or-fn) {:args [:--stdin-filepath (buffer-filename) :--parser parser]})
((builtin-formatter ft-or-fn formatter))))
(fn prefer-treefmt [ft-or-fn formatter] (fn args-shfmt []
(if (= treefmt-exists 1) (let [shiftwidth (vim.opt.shiftwidth:get)]
(treefmt-formatter) {:args [:-i shiftwidth]}))
(other-formatter ft-or-fn formatter)))
(formatter.setup {:logging true (fn do-format [exe opts]
:log_level vim.log.levels.WARN (if (= (vim.fn.executable exe) 1)
:filetype {:css [(prefer-treefmt :css :prettier)] (vim.tbl_extend :keep opts {: exe})
:fennel [#{:exe :fnlfmt :args ["-"] :stdin true}] {:exe :ls :cond #false}))
:fish [(builtin-formatter :fish :fishindent)]
:html [(prefer-treefmt :html :prettier)] (formatter.setup {:format_on_save #(not= vim.b.nifoc_formatter_disabled 1)
:javascript [(prefer-treefmt :javascript :filetype {:css #(do-format :prettier (args-prettier :css))
:prettier)] :fennel #(do-format :fnlfmt {:args ["-"]})
:json [(prefer-treefmt :json :prettier)] :fish #(do-format :fish_indent {})
:nix [(prefer-treefmt :nix :nixpkgs_fmt)] :html #(do-format :prettier
:sh [(prefer-treefmt :sh :shfmt)] (args-prettier :html))
:toml [(prefer-treefmt :toml :taplo)] :javascript #(do-format :prettier
:typescript [(prefer-treefmt :typescript (args-prettier :javascript))
:prettier)] :json #(do-format :prettier
:yaml [(prefer-treefmt #{:exe :yamlfmt (args-prettier :json))
:args [:-in] :sh #(do-format :shfmt (args-shfmt))
:stdin true})]}})) :toml #(do-format :taplo {:args [:fmt "-"]})
:typescript #(do-format :prettier
(args-prettier :typescript))
:yaml #(do-format :yamlfmt {:args [:-in]})}
:lsp [:elixirls :nil_ls]}))

View file

@ -39,8 +39,7 @@
:fish [:fish] :fish [:fish]
:java [:checkstyle] :java [:checkstyle]
:nix [:deadnix :nix :statix] :nix [:deadnix :nix :statix]
:sh [:shellcheck] :sh [:shellcheck]})
:yaml [:yamllint]})
(fn setup-linting [opts] (fn setup-linting [opts]
(diagnostic.maybe-enable-diagnostics opts.buf) (diagnostic.maybe-enable-diagnostics opts.buf)

View file

@ -43,13 +43,8 @@ in
nodePackages.eslint_d nodePackages.eslint_d
hadolint hadolint
luajitPackages.fennel luajitPackages.fennel
fnlfmt
nixpkgs-fmt
shellcheck shellcheck
shfmt
statix statix
yamllint
yamlfmt
] ++ optionals isDarwin [ ] ++ optionals isDarwin [
xcbuild xcbuild
]; ];
@ -259,7 +254,7 @@ in
# Formatting # Formatting
{ {
plugin = formatter-nvim; plugin = nvim-formatter;
config = builtins.readFile ../../config/nvim/plugins/formatter.fnl; config = builtins.readFile ../../config/nvim/plugins/formatter.fnl;
type = "fennel"; type = "fennel";
} }

View file

@ -427,14 +427,14 @@ in
fetchSubmodules = false; fetchSubmodules = false;
}; };
}; };
formatter-nvim = buildVimPluginFrom2Nix { nvim-formatter = buildVimPluginFrom2Nix {
pname = "formatter.nvim"; pname = "nvim-formatter";
version = "2023-07-13"; version = "2023-07-10";
src = fetchFromGitHub { src = fetchFromGitHub {
owner = "mhartington"; owner = "seblj";
repo = "formatter.nvim"; repo = "nvim-formatter";
rev = "9bf2e7e294b00bac87c6123c889828ee08dc9b46"; rev = "a77c61eb3967668839a0ae91f4f8f43938bfae82";
sha256 = "0hmlh6qcra7sfq0i989cxs5jmgk6774bljzvq9m17ybwj3imb14f"; sha256 = "1d5q3xg7bf10xfipml42ji9pwp0cl9rnjs12xqdjac8hm6sq469a";
fetchSubmodules = false; fetchSubmodules = false;
}; };
}; };
@ -583,12 +583,12 @@ in
}; };
noice-nvim = buildVimPluginFrom2Nix { noice-nvim = buildVimPluginFrom2Nix {
pname = "noice.nvim"; pname = "noice.nvim";
version = "2023-07-18"; version = "2023-07-19";
src = fetchFromGitHub { src = fetchFromGitHub {
owner = "folke"; owner = "folke";
repo = "noice.nvim"; repo = "noice.nvim";
rev = "74f9155f2ff2f7172d5709c8df9357ba69b834db"; rev = "dba8ac8e1239f541df1bba7d177eb09e51262ac4";
sha256 = "0nlql9pg7a2fav6hgsd1s1wj90hvh5giy55j33x1249zsk3yrbaq"; sha256 = "19jnp8jdslrfhafwqxm7sx4rpnl7lqxbz2r1chbpq6xlfzj57wv0";
fetchSubmodules = false; fetchSubmodules = false;
}; };
}; };

View file

@ -49,7 +49,7 @@
- src: hrsh7th/cmp-cmdline - src: hrsh7th/cmp-cmdline
- src: hrsh7th/cmp-nvim-lsp-document-symbol - src: hrsh7th/cmp-nvim-lsp-document-symbol
# Formatting # Formatting
- src: mhartington/formatter.nvim - src: seblj/nvim-formatter
# Pairs # Pairs
- src: windwp/nvim-autopairs - src: windwp/nvim-autopairs
- src: windwp/nvim-ts-autotag - src: windwp/nvim-ts-autotag