2022-04-19 12:56:22 +00:00
|
|
|
(let [mod {}
|
|
|
|
b vim.b
|
|
|
|
cmd vim.cmd
|
2022-04-27 20:17:59 +00:00
|
|
|
api vim.api
|
|
|
|
set-bufvar vim.api.nvim_buf_set_var]
|
2022-04-19 12:56:22 +00:00
|
|
|
(fn mod.setup []
|
2022-04-27 20:17:59 +00:00
|
|
|
(let [usercmd vim.api.nvim_create_user_command
|
|
|
|
augroup (vim.api.nvim_create_augroup :NifocFormatting {:clear true})
|
2022-04-19 12:56:22 +00:00
|
|
|
aucmd vim.api.nvim_create_autocmd]
|
2022-04-27 20:17:59 +00:00
|
|
|
(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
|
|
|
|
:group augroup
|
|
|
|
:desc "Run Formatter"})))
|
|
|
|
|
2022-06-15 13:02:06 +00:00
|
|
|
(fn run-neoformat [formatprg]
|
2022-09-26 13:23:14 +00:00
|
|
|
(let [neoformat (.. "Neoformat " formatprg)]
|
2022-06-15 13:02:06 +00:00
|
|
|
(cmd (.. "try | undojoin | " neoformat " | catch /E790/ | " neoformat
|
2022-09-26 13:23:14 +00:00
|
|
|
" | endtry"))))
|
2022-06-15 13:02:06 +00:00
|
|
|
|
2023-05-01 21:09:15 +00:00
|
|
|
(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})))
|
|
|
|
|
2022-04-27 20:17:59 +00:00
|
|
|
(fn mod.enable-for-buffer []
|
|
|
|
(set-bufvar 0 :nifoc_formatter_disabled 0))
|
|
|
|
|
|
|
|
(fn mod.disable-for-buffer []
|
|
|
|
(set-bufvar 0 :nifoc_formatter_disabled 1))
|
2022-04-19 12:56:22 +00:00
|
|
|
|
2022-05-31 13:46:32 +00:00
|
|
|
(fn mod.active? []
|
|
|
|
(if (= b.nifoc_formatter_disabled 1) false
|
|
|
|
(= b.nifoc_lsp_formatter_enabled 1) true
|
|
|
|
(not= (vim.opt_local.formatprg:get) "") true
|
|
|
|
false))
|
|
|
|
|
2022-04-19 12:56:22 +00:00
|
|
|
(fn mod.maybe-enable-lsp [client bufnr]
|
2022-05-22 21:05:38 +00:00
|
|
|
(when (client.supports_method :textDocument/rangeFormatting)
|
2022-04-19 12:56:22 +00:00
|
|
|
(api.nvim_buf_set_option bufnr :formatexpr "v:lua.vim.lsp.formatexpr()"))
|
2022-05-22 21:05:38 +00:00
|
|
|
(when (client.supports_method :textDocument/formatting)
|
2022-04-27 20:17:59 +00:00
|
|
|
(set-bufvar bufnr :nifoc_lsp_formatter_enabled 1)))
|
2022-04-19 12:56:22 +00:00
|
|
|
|
|
|
|
(fn mod.maybe-format-buffer []
|
|
|
|
(let [formatprg (vim.opt_local.formatprg:get)
|
2022-04-24 11:30:38 +00:00
|
|
|
formatprg-exe (-> formatprg (vim.split " " {:trimempty true}) (. 1))]
|
2022-04-27 20:17:59 +00:00
|
|
|
(if (= b.nifoc_formatter_disabled 1) nil
|
2022-12-10 23:10:33 +00:00
|
|
|
(= b.nifoc_formatter_force_formatprg 1) (run-neoformat formatprg-exe)
|
2023-05-01 21:09:15 +00:00
|
|
|
(= b.nifoc_lsp_formatter_enabled 1) (run-lsp-format)
|
2022-06-15 13:02:06 +00:00
|
|
|
(not= formatprg-exe nil) (run-neoformat formatprg-exe))))
|
2022-04-19 12:56:22 +00:00
|
|
|
|
|
|
|
mod)
|
|
|
|
|