diff --git a/config/nvim/lua/fschauen/diagnostic.lua b/config/nvim/lua/fschauen/diagnostic.lua index 8314a55..f8d9fbe 100644 --- a/config/nvim/lua/fschauen/diagnostic.lua +++ b/config/nvim/lua/fschauen/diagnostic.lua @@ -54,25 +54,81 @@ end ---@param bufnr integer|nil: Buffer number (0 for current buffer, nil for all buffers. M.hide = function(bufnr) vim.diagnostic.hide(nil, bufnr or 0) end -M.select_virtual_text_severity = function() - vim.ui.select( - { "ERROR", "WARN", "INFO", "HINT" }, - { prompt = "Min. severity for virtual text:" }, - function(choice) - if choice then - M.severity = vim.diagnostic.severity[choice] or M.severity - vim.diagnostic.config { - virtual_text = { - severity = { min = M.severity }, - }, - } - end - end - ) -end - local icons = require("fschauen.util.icons") +---Select minimum diagnostic severity for which to show virtual text. +M.select_virtual_text_severity = function() + if not pcall(require, "telescope") then + vim.notify("Telescope not available!", vim.log.levels.ERROR) + return + end + + local display_row = require("telescope.pickers.entry_display").create { + separator = "", + items = { { width = 3 }, { remaining = true } }, + } + + local make_display = function(entry) + return display_row { + { entry.value.icon, entry.value.highlight }, + { entry.value.title, entry.value.highlight }, + } + end + + local opts = require("telescope.themes").get_dropdown() + require("telescope.pickers") + .new(opts, { + prompt_title = "Min. severity for virtual text:", + finder = require("telescope.finders").new_table { + results = { + { + title = "Error", + severity = vim.diagnostic.severity.ERROR, + icon = icons.diagnostics_bold.Error, + highlight = "DiagnosticError", + }, + { + title = "Warning", + severity = vim.diagnostic.severity.WARN, + icon = icons.diagnostics_bold.Warn, + highlight = "DiagnosticWarn", + }, + { + title = "Info", + severity = vim.diagnostic.severity.INFO, + icon = icons.diagnostics_bold.Info, + highlight = "DiagnosticInfo", + }, + { + title = "Hint", + severity = vim.diagnostic.severity.HINT, + icon = icons.diagnostics_bold.Hint, + highlight = "DiagnosticHint", + }, + }, + entry_maker = function(entry) + return { value = entry, ordinal = entry.title, display = make_display } + end, + }, + sorter = require("telescope.config").values.generic_sorter(opts), + attach_mappings = function(prompt_bufnr, _) + local actions = require("telescope.actions") + actions.select_default:replace(function() + actions.close(prompt_bufnr) + local selection = require("telescope.actions.state").get_selected_entry() + M.severity = selection.value.severity or M.severity + vim.diagnostic.config { + virtual_text = { + severity = { min = M.severity }, + }, + } + end) + return true + end, + }) + :find() +end + ---Customize nvim's diagnostics display. M.setup = function() vim.diagnostic.config {