使用我当前的
lspconfig
插件的 nvim 设置:
return {
{
"williamboman/mason.nvim",
config = function()
require("mason").setup({})
end
},
{
"williamboman/mason-lspconfig.nvim",
config = function()
require("mason-lspconfig").setup({
ensure_installed = { "lua_ls", "clangd" }
})
end
},
{
"neovim/nvim-lspconfig",
config = function()
local cmp_lsp = require("cmp_nvim_lsp")
local capabilities = vim.tbl_deep_extend(
"force",
{},
vim.lsp.protocol.make_client_capabilities(),
cmp_lsp.default_capabilities()
)
local lspconfig = require("lspconfig")
lspconfig.lua_ls.setup({
capabilities = capabilities
})
lspconfig.clangd.setup({
capabilities = capabilities
})
vim.keymap.set('n', 'K', vim.lsp.buf.hover, {})
vim.keymap.set('n', 'gd', vim.lsp.buf.definition, {})
vim.keymap.set({ 'n', 'v' }, '<space>ca', vim.lsp.buf.code_action, {})
end
}
对于完成:
return {
{
"hrsh7th/cmp-nvim-lsp"
},
{
'L3MON4D3/LuaSnip',
dependencies = {
'saadparwaiz1/cmp_luasnip',
"rafamadriz/friendly-snippets",
},
},
{
'hrsh7th/nvim-cmp',
config = function()
local cmp = require'cmp'
require("luasnip.loaders.from_vscode").lazy_load()
cmp.setup({
snippet = {
expand = function(args)
require('luasnip').lsp_expand(args.body)
end,
},
window = {
completion = cmp.config.window.bordered(),
documentation = cmp.config.window.bordered(),
},
mapping = cmp.mapping.preset.insert({
['<C-b>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4),
['<C-Space>'] = cmp.mapping.complete(),
['<C-e>'] = cmp.mapping.abort(),
['<CR>'] = cmp.mapping.confirm({ select = true }),
}),
sources = cmp.config.sources({
{ name = 'nvim-lsp' },
{ name = 'luasnip' },
{ name = 'buffer' },
})
})
end
}
}
问题是自动补全显示片段,不显示任何错误,并且工作得很好,除了它不自动补全我自己定义的变量和函数这一事实。
我尝试使用
:LspInfo
和 :LspLog
检查其中是否存在隐藏的错误,但没有真正显示出来。语言服务器已连接并且工作正常。
更改此行:
{ name = 'nvim-lsp' },
到
{ name = 'nvim_lsp' },