0

My full config is on GitHub.

In my lsp-config.lua I have the following:

buf_set_keymap('n', 'K', '<cmd>lua vim.lsp.buf.hover()<CR>', opts)

However, when I press K on the name of a function, nothing happens. Also, when I run :map! K I get:

No mapping found

What am I doing wrong?

I want to know what the function signature on NewGoFunction is so that I can better understand what it expects.

UPDATE

I've just realised that some other key bindings aren't working either. For example I have:

buf_set_keymap('n', '[d', '<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>', opts)

But when I press that, I get:

E388: Could not find definition

So it is trying to go to definition for some reason!

I'm really confused.

screenshot of code

MORE INFO

I am using gopls as the language server for LSP. Here is the output of :LspInfo

output of :LspInfo

3
  • What lsp-server are you using? Is it installed? Is it running when you open a file? (Check with :LSPInfo.) In other words, give a full repro (reproducible walkthrough) of your situation.
    – 3N4N
    Aug 3, 2022 at 5:25
  • Good question, sorry I should have mentioned that. Here is a screenshot. I'm using gopls.
    – J86
    Aug 3, 2022 at 7:36
  • Thanks. Add that screenshot to the original question, pls.
    – 3N4N
    Aug 3, 2022 at 8:01

1 Answer 1

1

Use this for gopls server:

nvim_lsp.gopls.setup {
  cmd = {"gopls", "serve"},
  on_attach = on_attach,
  capabilities = capabilities,
  settings = {
    gopls = {
      analyses = {
        unusedparams = true,
      },
      staticcheck = true,
    },
  },
}

In your lsp config, you had the following:

local servers = { 'clangd', 'rust_analyzer', 'pyright', 'tsserver', 'elmls' }
for _, lsp in ipairs(servers) do
  nvim_lsp[lsp].setup {
    on_attach = on_attach,
    capabilities = capabilities,
  }
end

nvim_lsp.gopls.setup {
  cmd = {"gopls", "serve"},
  on_attach = on_attach,
  capabilities = capabilities,
  settings = {
    gopls = {
      analyses = {
        unusedparams = true,
      },
      staticcheck = true,
    },
  },
}

Notice that you had a for loop for assigning on_attach and capabilities for all servers in the list/table servers.

But you forgot to add gopls to your servers list, so the on_attach (which contains the keymaps) wasn't assigned to the proper function.

5
  • It worked (screenshot)! Can you please explain what I was doing wrong?
    – J86
    Aug 3, 2022 at 8:42
  • @J86 explanation added. For future ref, read the comments before/after you copy some code. Usually they're easy to understand and lets you know what this codeblock does and whether you need it.
    – 3N4N
    Aug 3, 2022 at 8:58
  • I tried undoing the change and then adding gopls to the list of servers that I had. But that didn't work, in other words, things broke like they were before. I hypothesised that it may be because I was doing the setup of the gopls again, and the second setup block will override any previous setup. When I commented out the bottom block for gopls setup, it started working again. However, this means I now miss the unusedparams = true and the staticcheck = true
    – J86
    Aug 3, 2022 at 9:09
  • It turns out, I can just copy the settings = {} block into the servers for loop. All is good. Thanks very much @kadekai
    – J86
    Aug 3, 2022 at 9:18
  • Hmm, your assumption is probably correct (the second instantiation of gopls setup is resetting the first). I donno much about Lua to confirm it. But there should a better solution for this, something that appends instead of resets. Anyway, I'll remove the second method from my answer as it's wrong. Thanks for confirming.
    – 3N4N
    Aug 3, 2022 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.