From f87c6cb390bbedefe73468da0c49b957c5ecc589 Mon Sep 17 00:00:00 2001 From: Fernando Schauenburg Date: Tue, 5 Sep 2023 13:04:27 +0200 Subject: [PATCH] vim/diagnostic: only navigate items with visible virtual text --- config/nvim/lua/fschauen/diagnostic.lua | 30 ++++++++++++++++--------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/config/nvim/lua/fschauen/diagnostic.lua b/config/nvim/lua/fschauen/diagnostic.lua index ffe37ae..adb4beb 100644 --- a/config/nvim/lua/fschauen/diagnostic.lua +++ b/config/nvim/lua/fschauen/diagnostic.lua @@ -1,20 +1,32 @@ local M = {} -local diag_opts = { - wrap = false, -- don't wrap around the begin/end of file -} +-- Show/navigate warning and errors by default. +M.severity = vim.diagnostic.severity.WARN + +-- Make options to pass to goto_next()/goto_prev(). This needs to be a function +-- rather than just a table bevause M.severity can change and we always want the +-- *current* value. +local make_opts = function(opts) + local defaults = { + wrap = false, -- don't wrap around the begin/end of file + severity = { -- only navigate items with visible virtual text + min = M.severity + }, + } + return vim.tbl_extend('keep', opts or {}, defaults) +end ---Move to the next diagnostic. ---@param opts table\nil: options passed along to `vim.diagnostic.goto_next`. M.goto_next= function(opts) - vim.diagnostic.goto_next(vim.tbl_extend('keep', opts or {}, diag_opts)) + vim.diagnostic.goto_next(make_opts(opts)) vim.cmd 'normal zz' end ---Move to the previous diagnostic. ---@param opts table|nil: options passed along to `vim.diagnostic.goto_prev`. M.goto_prev= function(opts) - vim.diagnostic.goto_prev(vim.tbl_extend('keep', opts or {}, diag_opts)) + vim.diagnostic.goto_prev(make_opts(opts)) vim.cmd 'normal zz' end @@ -41,18 +53,16 @@ M.hide = function(bufnr) vim.diagnostic.hide(nil, bufnr or 0) end -local default_severity = vim.diagnostic.severity.WARN - M.select_virtual_text_severity = function() vim.ui.select( { 'ERROR', 'WARN', 'INFO', 'HINT' }, { prompt = 'Min. severity for virtual text:' }, function(choice, --[[index]]_) if choice then - local severity = vim.diagnostic.severity[choice] or default_severity + M.severity = vim.diagnostic.severity[choice] or M.severity vim.diagnostic.config { virtual_text = { - severity = { min = severity } + severity = { min = M.severity } }, } end @@ -67,7 +77,7 @@ M.setup = function() spacing = 6, prefix = '●', severity = { - min = default_severity, + min = M.severity, } }, float = {