dotfiles/home/config/nvim/nifoc/formatting.fnl

52 lines
1.7 KiB
Text
Raw Normal View History

(let [mod {}
b vim.b
api vim.api
2023-07-23 20:40:54 +00:00
set-bufvar vim.api.nvim_buf_set_var
format (require :format)]
(fn mod.setup []
2023-07-23 20:40:54 +00:00
(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
{:desc "Disable Formatting for the current buffer"})
(usercmd :FormatEnableBuffer mod.enable-for-buffer
2023-07-23 20:40:54 +00:00
{:desc "Enable Formatting for the current buffer"})
;; (aucmd :BufWritePre
;; {:callback mod.maybe-format-buffer
;; :group augroup
;; :desc "Run Formatter before saving"})
))
2023-07-13 10:02:14 +00:00
2023-07-19 12:48:38 +00:00
(fn has-formatter-config? [ft]
2023-07-23 20:40:54 +00:00
(let [cfg (. (require :format.static) :config)
fts (. cfg :filetypes)]
2023-07-19 12:48:38 +00:00
(not= (. fts ft) nil)))
2023-05-01 21:09:15 +00:00
2023-07-23 20:40:54 +00:00
(fn format-with-lsp? [ft]
(let [fts [:elixir]]
(vim.list_contains fts ft)))
(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-05-31 13:46:32 +00:00
(fn mod.active? []
(let [ft vim.bo.filetype]
(if (= b.nifoc_formatter_disabled 1) false
2023-07-19 12:48:38 +00:00
(has-formatter-config? ft))))
2022-05-31 13:46:32 +00:00
(fn mod.maybe-enable-lsp [client bufnr]
2022-05-22 21:05:38 +00:00
(when (client.supports_method :textDocument/rangeFormatting)
2023-07-19 12:48:38 +00:00
(api.nvim_buf_set_option bufnr :formatexpr "v:lua.vim.lsp.formatexpr()")))
2023-07-23 20:40:54 +00:00
(fn mod.maybe-format-buffer []
(let [ft vim.bo.filetype]
(if (= b.nifoc_formatter_disabled 1) nil
(format-with-lsp? ft) (vim.lsp.buf.format)
(has-formatter-config? ft) (format.format)
nil)))
2023-07-23 20:40:54 +00:00
mod)