diff --git a/home/vim/after/plugin/mappings/unimpaired.lua b/home/vim/after/plugin/mappings/unimpaired.lua index f39a8c0..f502056 100644 --- a/home/vim/after/plugin/mappings/unimpaired.lua +++ b/home/vim/after/plugin/mappings/unimpaired.lua @@ -1,5 +1,7 @@ local wk = require("which-key") +local lsp = require("ambroisie.lsp") + local keys = { -- Edition and navigation mappins ["["] = { @@ -30,7 +32,7 @@ local keys = { x = "XML encode", y = "C string encode", -- Custom - d = { vim.diagnostic.goto_prev, "Previous diagnostic" }, + d = { lsp.goto_prev_diagnostic, "Previous diagnostic" }, }, ["]"] = { name = "Next", @@ -60,7 +62,7 @@ local keys = { x = "XML decode", y = "C string decode", -- Custom - d = { vim.diagnostic.goto_next, "Next diagnostic" }, + d = { lsp.goto_next_diagnostic, "Next diagnostic" }, }, -- Option mappings diff --git a/home/vim/lua/ambroisie/lsp.lua b/home/vim/lua/ambroisie/lsp.lua index b682d27..05869b7 100644 --- a/home/vim/lua/ambroisie/lsp.lua +++ b/home/vim/lua/ambroisie/lsp.lua @@ -3,6 +3,43 @@ local M = {} -- Simplified LSP formatting configuration local lsp_format = require("lsp-format") +-- Move to the next/previous diagnostic, automatically showing the diagnostics +-- float if necessary. +-- @param forward whether to go forward or backwards +local function goto_diagnostic(forward) + vim.validate({ + forward = { forward, "boolean" }, + }) + + local opts = { + float = false, + } + + -- Only show floating diagnostics if they are otherwise not displayed + local config = vim.diagnostic.config() + if not (config.virtual_text or config.virtual_lines) then + opts.float = true + end + + if forward then + vim.diagnostic.goto_next(opts) + else + vim.diagnostic.goto_prev(opts) + end +end + +-- Move to the next diagnostic, automatically showing the diagnostics float if +-- necessary. +M.goto_next_diagnostic = function() + goto_diagnostic(true) +end + +-- Move to the previous diagnostic, automatically showing the diagnostics float +-- if necessary. +M.goto_prev_diagnostic = function() + goto_diagnostic(false) +end + -- shared LSP configuration callback -- @param client native client configuration -- @param bufnr int? buffer number of the attched client