From 9810b20e0b5be2fc8361c6a7aed35950c5ec9a5d Mon Sep 17 00:00:00 2001 From: Frank Zechert Date: Sun, 28 Jun 2026 20:52:55 +0200 Subject: [PATCH] configure LSP, codelens, and trouble --- lua/config/autocommands.lua | 38 ++++++++++++++++++++----------------- lua/config/keymaps.lua | 14 +++++++++++++- lua/config/ui.lua | 4 ++-- lua/lsp/init.lua | 7 ++++++- lua/plugins/trouble.lua | 25 ++++++++++++++++++++++++ lua/plugins/whichkey.lua | 8 ++++---- 6 files changed, 71 insertions(+), 25 deletions(-) diff --git a/lua/config/autocommands.lua b/lua/config/autocommands.lua index a7b6e98..c84bf08 100644 --- a/lua/config/autocommands.lua +++ b/lua/config/autocommands.lua @@ -46,9 +46,10 @@ create_cmd("TextYankPost", { create_cmd("FileType", { desc = "Disable list characters for some filetypes", group = create_group("cmd-disable-listchars", { clear = true }), - pattern = { "netrw" }, + pattern = { "netrw", "trouble" }, callback = function(args) vim.opt_local.list = false + vim.opt.textwidth = 0 end }) @@ -124,21 +125,24 @@ create_cmd("QuickFixCmdPost", { }) -- Automatically open diagnostic float when hovering with cursor -vim.api.nvim_create_autocmd("CursorHold", { - callback = function() - if not vim.api.nvim_get_current_win() then return end - vim.diagnostic.open_float(nil, { focus = false, scope = "cursor" }) - end, -}) - -vim.api.nvim_create_autocmd("CursorMoved", { - callback = function() - for _, win in ipairs(vim.api.nvim_list_wins()) do - local config = vim.api.nvim_win_get_config(win) - if config.relative ~= "" then - vim.api.nvim_win_close(win, false) - end - end - end, +create_cmd("CursorHold", { + desc = "Automatically open Diagnostic Float", + group = create_group("cmd-auto-open-diagnostic-float", { clear = true }), + callback = function() + -- If any floating window exists, do nothing + for _, win in ipairs(vim.api.nvim_list_wins()) do + local cfg = vim.api.nvim_win_get_config(win) + if cfg.relative ~= "" then + return + end + end + -- Only open if there is a diagnostic under cursor + local diags = vim.diagnostic.get(0, { lnum = vim.fn.line('.') - 1 }) + if #diags == 0 then + return + end + + vim.diagnostic.open_float(nil, { focus = false, scope = "cursor" }) + end, }) diff --git a/lua/config/keymaps.lua b/lua/config/keymaps.lua index 5204d6e..617f038 100644 --- a/lua/config/keymaps.lua +++ b/lua/config/keymaps.lua @@ -114,5 +114,17 @@ map("n", "grr", [[Trouble lsp_references toggle]], { desc = "Show LSP r map("n", "xq", [[Trouble qflist toggle focus=false]], { desc = "Show Quickfix List" }) map("n", "xl", [[Trouble loclist toggle focus=false]], { desc = "Show Location List" }) map("n", "grn", vim.lsp.buf.rename, { desc = "Rename", silent = true, noremap = true }) -map("n", "gra", vim.lsp.buf.code_action, { desc = "Action", silent = true, noremap = true }) +map({"n", "v"}, "gra", vim.lsp.buf.code_action, { desc = "Action", silent = true, noremap = true }) map("n", "gl", vim.diagnostic.open_float, { desc = "Open Diagnostic Float" }) +map("n", "grx", vim.lsp.codelens.run, { desc = "Run Codelens Operation" }) + +-- Toggle Codelense +vim.keymap.set("n", "tc", function() + vim.g.codelens_enabled = not vim.g.codelens_enabled + vim.lsp.codelens.enable(vim.g.codelens_enabled) +end, { desc = "Toggle Code Lens" }) + +-- Open Signature Help +vim.keymap.set("i", "", function() + vim.lsp.buf.signature_help() +end, { desc = "Show signature help" }) diff --git a/lua/config/ui.lua b/lua/config/ui.lua index ea0766d..b2982b4 100644 --- a/lua/config/ui.lua +++ b/lua/config/ui.lua @@ -50,12 +50,12 @@ vim.g.ui = { linebreak = "↘", windowborder = "single", popupborder = "single", - custom_highlights = function(colors, utils) + custom_highlights = function(colors, _) return { whitespace = { fg = colors.overlay0 }, TrailingWhitespace = { fg = colors.red }, LineNr = { fg = colors.overlay0 }, - CursorLineNR = { fg = colors.peach }, + CursorLineNr = { fg = colors.peach }, } end, symbols = { diff --git a/lua/lsp/init.lua b/lua/lsp/init.lua index d0e078e..8429738 100644 --- a/lua/lsp/init.lua +++ b/lua/lsp/init.lua @@ -198,6 +198,11 @@ local function show_lsp_details() end, { buffer = popup_buffer, nowait = true, desc = "Close Window" }) end -vim.keymap.set("n", "li", show_lsp_details, { +vim.keymap.set("n", "xi", show_lsp_details, { desc = "Show detailed LSP info in a float" }) + +vim.g.codelens_enabled = true +vim.lsp.codelens.enable(vim.g.codelens_enable) + + diff --git a/lua/plugins/trouble.lua b/lua/plugins/trouble.lua index 11bf0f3..8911175 100644 --- a/lua/plugins/trouble.lua +++ b/lua/plugins/trouble.lua @@ -40,6 +40,7 @@ trouble.setup({ auto_preview = true, auto_refresh = true, auto_jump = false, + auto_close = true, modes = { diagnostics = { preview = { @@ -49,5 +50,29 @@ trouble.setup({ size = 0.4 }, }, + lsp_declarations = { + preview = { + type = "split", + relative = "win", + position = "right", + size = 0.4 + }, + }, + lsp_definitions = { + preview = { + type = "split", + relative = "win", + position = "right", + size = 0.4 + }, + }, + lsp_references = { + preview = { + type = "split", + relative = "win", + position = "right", + size = 0.4 + }, + }, }, }) diff --git a/lua/plugins/whichkey.lua b/lua/plugins/whichkey.lua index bc8f925..b84ab76 100644 --- a/lua/plugins/whichkey.lua +++ b/lua/plugins/whichkey.lua @@ -40,8 +40,8 @@ wk.setup({ preset = "modern" }) wk.add({ - { "b", group = "Buffers" }, - { "x", group = "Trouble" }, - { "c", group = "Code" }, - { "gr", group = "LSP" }, + { "b", group = "Buffers..." }, + { "x", group = "Trouble..." }, + { "gr", group = "LSP...", mode = {"n","v"} }, + { "t", group = "Toggle..." }, })