1
0
Fork 0
dotfiles/config/nvim/nifoc/formatting.fnl
Daniel Kempkens 6f237fea8c nvim: Remove legendary
... Telescope can do roughly the same thing already
2022-04-23 20:51:50 +02:00

28 lines
1.1 KiB
Fennel

(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]
(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)