nvim: Improve diagnostics and formatting
This commit is contained in:
parent
2ea793d106
commit
9f3e43db4a
3 changed files with 57 additions and 26 deletions
|
@ -1,7 +1,9 @@
|
||||||
(let [mod {}
|
(let [mod {}
|
||||||
cmd vim.cmd
|
cmd vim.cmd
|
||||||
api vim.api
|
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]
|
(fn maybe-refresh-codelens [client]
|
||||||
(when client.server_capabilities.codeLensProvider
|
(when client.server_capabilities.codeLensProvider
|
||||||
(vim.lsp.codelens.refresh)))
|
(vim.lsp.codelens.refresh)))
|
||||||
|
@ -18,21 +20,25 @@
|
||||||
(cmd "sign define DiagnosticSignInfo text= texthl=DiagnosticSignInfo linehl= numhl=DiagnosticSignInfo")
|
(cmd "sign define DiagnosticSignInfo text= texthl=DiagnosticSignInfo linehl= numhl=DiagnosticSignInfo")
|
||||||
(cmd "sign define DiagnosticSignHint text= texthl=DiagnosticSignHint linehl= numhl=DiagnosticSignHint"))
|
(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]
|
(fn mod.maybe-enable-lsp [client bufnr]
|
||||||
(when (= vim.b.nifoc_lsp_enabled nil)
|
(when (= vim.b.nifoc_lsp_enabled nil)
|
||||||
(api.nvim_buf_set_var bufnr :nifoc_lsp_enabled 1)
|
(api.nvim_buf_set_var bufnr :nifoc_lsp_enabled 1)
|
||||||
(keymap.lsp-attach client bufnr)
|
(keymap.lsp-attach client bufnr)
|
||||||
(let [augroup (vim.api.nvim_create_augroup :NifocDiagnostic {:clear true})
|
(mod.maybe-enable-diagnostics bufnr)
|
||||||
aucmd vim.api.nvim_create_autocmd]
|
(aucmd [:CursorHold :CursorHoldI :InsertLeave]
|
||||||
(aucmd [:CursorHold :CursorHoldI]
|
{:callback #(maybe-refresh-codelens client)
|
||||||
{:callback (fn []
|
:buffer bufnr
|
||||||
(vim.diagnostic.open_float nil {:focus false})
|
:group augroup
|
||||||
(maybe-refresh-codelens client))
|
:desc "Refresh Codelens"})))
|
||||||
:buffer bufnr
|
|
||||||
:group augroup})
|
|
||||||
(aucmd :InsertLeave {:callback #(maybe-refresh-codelens client)
|
|
||||||
:buffer bufnr
|
|
||||||
:group augroup}))))
|
|
||||||
|
|
||||||
mod)
|
mod)
|
||||||
|
|
||||||
|
|
|
@ -1,27 +1,43 @@
|
||||||
(let [mod {}
|
(let [mod {}
|
||||||
b vim.b
|
b vim.b
|
||||||
cmd vim.cmd
|
cmd vim.cmd
|
||||||
api vim.api]
|
api vim.api
|
||||||
|
set-bufvar vim.api.nvim_buf_set_var]
|
||||||
(fn mod.setup []
|
(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 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]
|
(fn mod.maybe-enable-lsp [client bufnr]
|
||||||
(when client.server_capabilities.documentRangeFormattingProvider
|
(when client.server_capabilities.documentRangeFormattingProvider
|
||||||
(api.nvim_buf_set_option bufnr :formatexpr "v:lua.vim.lsp.formatexpr()"))
|
(api.nvim_buf_set_option bufnr :formatexpr "v:lua.vim.lsp.formatexpr()"))
|
||||||
(when client.server_capabilities.documentFormattingProvider
|
(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 []
|
(fn mod.maybe-format-buffer []
|
||||||
(let [formatprg (vim.opt_local.formatprg:get)
|
(let [formatprg (vim.opt_local.formatprg:get)
|
||||||
formatprg-exe (-> formatprg (vim.split " " {:trimempty true}) (. 1))]
|
formatprg-exe (-> formatprg (vim.split " " {:trimempty true}) (. 1))]
|
||||||
(if (= b.nifoc_lsp_formatter_enabled 1)
|
(if (= b.nifoc_formatter_disabled 1) nil
|
||||||
(vim.lsp.buf.formatting_sync nil 1000)
|
(= b.nifoc_lsp_formatter_enabled 1) (vim.lsp.buf.formatting_sync nil
|
||||||
(not= formatprg-exe nil)
|
1000)
|
||||||
(let [neoformat (.. "Neoformat " formatprg-exe)]
|
(not= formatprg-exe nil) (let [neoformat (.. "Neoformat "
|
||||||
(cmd (.. "try | undojoin | " neoformat " | catch /E790/ | "
|
formatprg-exe)]
|
||||||
neoformat " | endtry"))))))
|
(cmd (.. "try | undojoin | " neoformat
|
||||||
|
" | catch /E790/ | " neoformat
|
||||||
|
" | endtry"))))))
|
||||||
|
|
||||||
mod)
|
mod)
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
(let [lint (require :lint)
|
(let [lint (require :lint)
|
||||||
|
diagnostic (require :nifoc.diagnostic)
|
||||||
augroup (vim.api.nvim_create_augroup :NifocLint {:clear true})
|
augroup (vim.api.nvim_create_augroup :NifocLint {:clear true})
|
||||||
aucmd vim.api.nvim_create_autocmd]
|
aucmd vim.api.nvim_create_autocmd]
|
||||||
;; Custom Linters
|
;; Custom Linters
|
||||||
(set lint.linters.deadnix
|
(set lint.linters.deadnix
|
||||||
{:cmd :deadnix
|
{:cmd :deadnix
|
||||||
:stdin false
|
:stdin true
|
||||||
:args [:--output-format :json]
|
:args [:--output-format :json :/dev/stdin]
|
||||||
:stream :stdout
|
:stream :stdout
|
||||||
:ignore_exitcode false
|
:ignore_exitcode false
|
||||||
:parser (fn [output]
|
:parser (fn [output]
|
||||||
|
@ -27,14 +28,22 @@
|
||||||
(set lint.linters_by_ft {:dockerfile [:hadolint]
|
(set lint.linters_by_ft {:dockerfile [:hadolint]
|
||||||
:elixir [:credo]
|
:elixir [:credo]
|
||||||
:fennel [:fennel]
|
:fennel [:fennel]
|
||||||
|
:fish [:fish]
|
||||||
:nix [:deadnix :nix :statix]
|
:nix [:deadnix :nix :statix]
|
||||||
:sh [:shellcheck]})
|
:sh [:shellcheck]})
|
||||||
|
|
||||||
(fn setup-linting [opts]
|
(fn setup-linting [opts]
|
||||||
|
(diagnostic.maybe-enable-diagnostics opts.buf)
|
||||||
(lint.try_lint)
|
(lint.try_lint)
|
||||||
(aucmd [:BufWinEnter :BufWritePost :InsertLeave]
|
(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)]
|
(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"})))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue