1
0
Fork 0
dotfiles/config/nvim/nifoc/formatting.fnl

28 lines
1.1 KiB
Plaintext
Raw Normal View History

(let [mod {}
b vim.b
cmd vim.cmd
api vim.api]
(fn mod.setup []
(let [augroup (vim.api.nvim_create_augroup :NifocFormatting {:clear true})
aucmd vim.api.nvim_create_autocmd]
2022-04-19 16:07:34 +00:00
(aucmd :BufWritePre {:callback mod.maybe-format-buffer :group augroup})))
(fn mod.maybe-enable-lsp [client bufnr]
(when client.server_capabilities.documentRangeFormattingProvider
(api.nvim_buf_set_option bufnr :formatexpr "v:lua.vim.lsp.formatexpr()"))
(when client.server_capabilities.documentFormattingProvider
(api.nvim_buf_set_var bufnr :nifoc_lsp_formatter_enabled 1)))
(fn mod.maybe-format-buffer []
(let [formatprg (vim.opt_local.formatprg:get)
formatprg-exe (-> formatprg (vim.split " " {:trimempty true}) (. 1))
neoformat (.. "Neoformat " formatprg-exe)]
(if (= b.nifoc_lsp_formatter_enabled 1)
(vim.lsp.buf.formatting_sync nil 1000)
(not= formatprg-exe nil)
(cmd (.. "try | undojoin | " neoformat " | catch /E790/ | " neoformat
" | endtry")))))
mod)