2022-04-19 12:56:22 +00:00
|
|
|
(let [mod {}
|
|
|
|
b vim.b
|
2022-04-27 20:17:59 +00:00
|
|
|
api vim.api
|
2023-07-23 20:40:54 +00:00
|
|
|
set-bufvar vim.api.nvim_buf_set_var
|
2023-10-09 13:05:57 +00:00
|
|
|
conform (require :conform)]
|
2022-04-19 12:56:22 +00:00
|
|
|
(fn mod.setup []
|
2023-10-09 13:05:57 +00:00
|
|
|
(let [usercmd api.nvim_create_user_command
|
|
|
|
augroup (api.nvim_create_augroup :NifocFormatting {:clear true})
|
|
|
|
aucmd 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
|
2023-07-23 20:40:54 +00:00
|
|
|
{:desc "Enable Formatting for the current buffer"})
|
2023-10-09 13:05:57 +00:00
|
|
|
(aucmd :BufWritePre
|
2023-10-15 20:08:36 +00:00
|
|
|
{:callback #(mod.maybe-format-buffer $1.buf)
|
2023-10-09 13:05:57 +00:00
|
|
|
: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-10-09 13:05:57 +00:00
|
|
|
(let [fts conform.formatters_by_ft]
|
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)))
|
|
|
|
|
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? []
|
2023-07-12 18:39:50 +00:00
|
|
|
(let [ft vim.bo.filetype]
|
|
|
|
(if (= b.nifoc_formatter_disabled 1) false
|
2023-10-09 13:05:57 +00:00
|
|
|
(or (has-formatter-config? ft) (format-with-lsp? ft)))))
|
2022-04-19 12:56:22 +00:00
|
|
|
|
2023-10-09 19:59:19 +00:00
|
|
|
(fn mod.maybe-format-buffer [bufnr]
|
2023-07-23 20:40:54 +00:00
|
|
|
(let [ft vim.bo.filetype]
|
|
|
|
(if (= b.nifoc_formatter_disabled 1) nil
|
2023-10-14 17:55:46 +00:00
|
|
|
(conform.format {: bufnr
|
|
|
|
:async false
|
|
|
|
:timeout_ms 1000
|
2023-10-15 20:08:36 +00:00
|
|
|
:lsp_fallback (format-with-lsp? ft)}))
|
|
|
|
nil))
|
2022-04-19 12:56:22 +00:00
|
|
|
|
2023-07-23 20:40:54 +00:00
|
|
|
mod)
|