From 33b51e5dea1b3776b2190b319fd3bbde28bc73b1 Mon Sep 17 00:00:00 2001 From: Bruno Carlin Date: Tue, 11 Feb 2025 15:23:05 +0100 Subject: [PATCH] feat(nvim): enable lsp inlay hints --- nvim/.config/nvim/lua/lsp/gopls.lua | 130 +++++++++++++------------ nvim/.config/nvim/lua/settings/lsp.lua | 9 ++ 2 files changed, 75 insertions(+), 64 deletions(-) diff --git a/nvim/.config/nvim/lua/lsp/gopls.lua b/nvim/.config/nvim/lua/lsp/gopls.lua index e1d87aa..4fdf927 100644 --- a/nvim/.config/nvim/lua/lsp/gopls.lua +++ b/nvim/.config/nvim/lua/lsp/gopls.lua @@ -23,69 +23,71 @@ vim.api.nvim_create_autocmd("BufWritePre", { require('lspconfig').gopls.setup { - flags = { - debounce_text_changes = 150, - }, - cmd = {'gopls', '--remote=auto'}, - settings = { - gopls = { - vulncheck = "Imports", - gofumpt = true, - usePlaceholders = true, - completeUnimported = true, - completeFunctionCalls = true, - experimentalPostfixCompletions = true, - allExperiments = true, - semanticTokens = true, - analyses = { - assign = true, - atomic = true, - atomicalign = true, - bools = true, - buildtag = true, - composites = true, - copylocks = true, - erroras = true, - httpresponse = true, - ifaceassert = true, - infertypeargs = true, - loopclosure = true, - lostcancel = true, - nilfunc = true, - nilness = true, - printf = true, - shadow = true, - simplifyrange = true, - simplifyslice = true, - sortslice = true, - stdmethods = true, - stringintconv = true, - testinggoroutine = true, - tests = true, - unmarshal = true, - unreachable = true, - unsafeptr = true, - unusedparams = true, - unusedresult = true, - unusedwrite = true, - useany = true, - nonewvars = true, - }, - codelenses = { - generate = true, - gc_details = true, - run_govulncheck = true, - }, - hints = { - assignVariableTypes = true, - compositeLiteralFields = true, - constantValues = true, - functionTypeParameters = true, - parameterNames = true, - rangeVariableTypes = true, - }, - }, + flags = { + debounce_text_changes = 150, + }, + cmd = {'gopls', '--remote=auto'}, + settings = { + gopls = { + vulncheck = "Imports", + gofumpt = true, + usePlaceholders = true, + completeUnimported = true, + completeFunctionCalls = true, + experimentalPostfixCompletions = true, + allExperiments = true, + semanticTokens = true, + analyses = { + appends = true, + assign = true, + atomic = true, + atomicalign = true, + bools = true, + buildtag = true, + composites = true, + copylocks = true, + erroras = true, + httpresponse = true, + ifaceassert = true, + infertypeargs = true, + loopclosure = true, + lostcancel = true, + nilfunc = true, + nilness = true, + printf = true, + shadow = true, + simplifyrange = true, + simplifyslice = true, + sortslice = true, + stdmethods = true, + stringintconv = true, + testinggoroutine = true, + tests = true, + unmarshal = true, + unreachable = true, + unsafeptr = true, + unusedparams = true, + unusedresult = true, + unusedwrite = true, + useany = true, + nonewvars = true, + }, + codelenses = { + generate = true, + gc_details = true, + run_govulncheck = true, + tidy = true, + upgrade_dependency = true, + }, + hints = { + assignVariableTypes = true, + compositeLiteralFields = true, + compositeLiteralTypes = true, + constantValues = true, + functionTypeParameters = true, + parameterNames = true, + rangeVariableTypes = true, + }, }, + }, } - --- Change local setting to separate 3rd-party and local imports with go.mod prefix diff --git a/nvim/.config/nvim/lua/settings/lsp.lua b/nvim/.config/nvim/lua/settings/lsp.lua index 66deef8..51088ec 100644 --- a/nvim/.config/nvim/lua/settings/lsp.lua +++ b/nvim/.config/nvim/lua/settings/lsp.lua @@ -41,6 +41,12 @@ vim.keymap.set('n', 'dl', 'lua vim.diagnostic.setloclist()', opt vim.api.nvim_create_autocmd('LspAttach', { group = vim.api.nvim_create_augroup('UserLspConfig', {}), callback = function(ev) + -- Enable inlay hints + local client = vim.lsp.get_client_by_id(ev.data.client_id) + if client.server_capabilities.inlayHintProvider then + vim.lsp.inlay_hint.enable(true, { bufnr = ev.buf }) + end + -- Enable completion triggered by vim.bo[ev.buf].omnifunc = 'v:lua.vim.lsp.omnifunc' @@ -57,6 +63,9 @@ vim.api.nvim_create_autocmd('LspAttach', { vim.keymap.set('n', 'wl', function() print(vim.inspect(vim.lsp.buf.list_workspace_folders())) end, opts) + vim.keymap.set('n', 'i', function() + vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled()) + end, opts) vim.keymap.set('n', 'D', vim.lsp.buf.type_definition, opts) vim.keymap.set('n', 'rn', vim.lsp.buf.rename, opts) vim.keymap.set({ 'n', 'v' }, 'ca', vim.lsp.buf.code_action, opts)