1
0
Fork 0

nvim: Improve diagnostics and formatting

This commit is contained in:
Daniel Kempkens 2022-04-27 22:17:59 +02:00
parent 2ea793d106
commit 9f3e43db4a
3 changed files with 57 additions and 26 deletions

View file

@ -1,7 +1,9 @@
(let [mod {}
cmd vim.cmd
api vim.api
keymap (require :nifoc.keymap)]
keymap (require :nifoc.keymap)
augroup (vim.api.nvim_create_augroup :NifocDiagnostic {:clear true})
aucmd vim.api.nvim_create_autocmd]
(fn maybe-refresh-codelens [client]
(when client.server_capabilities.codeLensProvider
(vim.lsp.codelens.refresh)))
@ -18,21 +20,25 @@
(cmd "sign define DiagnosticSignInfo text= texthl=DiagnosticSignInfo linehl= numhl=DiagnosticSignInfo")
(cmd "sign define DiagnosticSignHint text= texthl=DiagnosticSignHint linehl= numhl=DiagnosticSignHint"))
(fn mod.maybe-enable-diagnostics [bufnr]
(when (= vim.b.nifoc_diagnostics_enabled nil)
(api.nvim_buf_set_var bufnr :nifoc_diagnostics_enabled 1)
(aucmd [:CursorHold :CursorHoldI]
{:callback #(vim.diagnostic.open_float {:focus false})
:buffer bufnr
:group augroup
:desc "Open Diagnostic Window"})))
(fn mod.maybe-enable-lsp [client bufnr]
(when (= vim.b.nifoc_lsp_enabled nil)
(api.nvim_buf_set_var bufnr :nifoc_lsp_enabled 1)
(keymap.lsp-attach client bufnr)
(let [augroup (vim.api.nvim_create_augroup :NifocDiagnostic {:clear true})
aucmd vim.api.nvim_create_autocmd]
(aucmd [:CursorHold :CursorHoldI]
{:callback (fn []
(vim.diagnostic.open_float nil {:focus false})
(maybe-refresh-codelens client))
(mod.maybe-enable-diagnostics bufnr)
(aucmd [:CursorHold :CursorHoldI :InsertLeave]
{:callback #(maybe-refresh-codelens client)
:buffer bufnr
:group augroup})
(aucmd :InsertLeave {:callback #(maybe-refresh-codelens client)
:buffer bufnr
:group augroup}))))
:group augroup
:desc "Refresh Codelens"})))
mod)

View file

@ -1,27 +1,43 @@
(let [mod {}
b vim.b
cmd vim.cmd
api vim.api]
api vim.api
set-bufvar vim.api.nvim_buf_set_var]
(fn mod.setup []
(let [augroup (vim.api.nvim_create_augroup :NifocFormatting {:clear true})
(let [usercmd vim.api.nvim_create_user_command
augroup (vim.api.nvim_create_augroup :NifocFormatting {:clear true})
aucmd vim.api.nvim_create_autocmd]
(aucmd :BufWritePre {:callback mod.maybe-format-buffer :group augroup})))
(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"})))
(fn mod.enable-for-buffer []
(set-bufvar 0 :nifoc_formatter_disabled 0))
(fn mod.disable-for-buffer []
(set-bufvar 0 :nifoc_formatter_disabled 1))
(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)))
(set-bufvar 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))]
(if (= b.nifoc_lsp_formatter_enabled 1)
(vim.lsp.buf.formatting_sync nil 1000)
(not= formatprg-exe nil)
(let [neoformat (.. "Neoformat " formatprg-exe)]
(cmd (.. "try | undojoin | " neoformat " | catch /E790/ | "
neoformat " | endtry"))))))
(if (= b.nifoc_formatter_disabled 1) nil
(= b.nifoc_lsp_formatter_enabled 1) (vim.lsp.buf.formatting_sync nil
1000)
(not= formatprg-exe nil) (let [neoformat (.. "Neoformat "
formatprg-exe)]
(cmd (.. "try | undojoin | " neoformat
" | catch /E790/ | " neoformat
" | endtry"))))))
mod)

View file

@ -1,11 +1,12 @@
(let [lint (require :lint)
diagnostic (require :nifoc.diagnostic)
augroup (vim.api.nvim_create_augroup :NifocLint {:clear true})
aucmd vim.api.nvim_create_autocmd]
;; Custom Linters
(set lint.linters.deadnix
{:cmd :deadnix
:stdin false
:args [:--output-format :json]
:stdin true
:args [:--output-format :json :/dev/stdin]
:stream :stdout
:ignore_exitcode false
:parser (fn [output]
@ -27,14 +28,22 @@
(set lint.linters_by_ft {:dockerfile [:hadolint]
:elixir [:credo]
:fennel [:fennel]
:fish [:fish]
:nix [:deadnix :nix :statix]
:sh [:shellcheck]})
(fn setup-linting [opts]
(diagnostic.maybe-enable-diagnostics opts.buf)
(lint.try_lint)
(aucmd [:BufWinEnter :BufWritePost :InsertLeave]
{:callback #(lint.try_lint) :buffer opts.buf :group augroup}))
{:callback #(lint.try_lint)
:buffer opts.buf
:group augroup
:desc "Run Linter"}))
(each [ft _ (pairs lint.linters_by_ft)]
(aucmd :FileType {:pattern ft :callback setup-linting :group augroup})))
(aucmd :FileType {:pattern ft
:callback setup-linting
:group augroup
:desc "Setup Linter"})))