I am new to lua and LSP and just copied the suggested configuration from GitHub - neovim/nvim-lspconfig: Quickstart configs for Nvim LSP.
Problem: The on_attach
handler has not been invoked. After an hour of investigation, I found out, the cause is the local
scope of the on_attach
handler function.
In other words, this worked:
function on_attach(client, bufnr)
...
end
This not:
local function on_attach(client, bufnr)
...
end
Minimal example:
-- ~/.config/nvim/lua/plugins.lua
local lsp_flags = {
debounce_text_changes = 150,
}
function on_attach(client, bufnr)
local bufopts = { noremap=true, silent=true, buffer=bufnr }
vim.keymap.set('n', 'gr', vim.lsp.buf.references, bufopts)
-- ... more keybindings and configuration omitted here
end
return require('packer').startup(function(use)
use 'wbthomason/packer.nvim'
use {
'neovim/nvim-lspconfig',
config = function()
require'lspconfig'.pyright.setup ({
on_attach = on_attach,
flags = lsp_flags,
})
end
}
end)
Might be anybody so kind to explain, what the difference is in this context?