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

View file

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

View file

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

View file

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

View file

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

View file

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