Closed srearl closed 9 months ago
I am not sure if the problem was nvim-cmp or lsp but have got this close to working. Thanks for your consideration.
@srearl Would be cool if you write in the issue how you solved the problem. :D
Hi @steinbrueckri - I was twisting so many knobs that I cannot be certain of the actual fix but things really seemed to start clicking when I moved the setup to an after
directory. It is still not perfect as now I have lost some treesitter functionality and I have to use separate sources for autocomplete and linting in R but at least autocomplete is (mostly) working.
lua/lsp.lua
return {
"neovim/nvim-lspconfig",
dependencies = {
"hrsh7th/cmp-nvim-lsp"
},
config = function()
-- Change the Diagnostic symbols in the sign column (gutter)
local signs = {
Error = " ",
Warn = " ",
Hint = " ",
Info = " "
}
for type, icon in pairs(signs) do
local hl = "DiagnosticSign" .. type
vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = "" })
end
end,
}
lua/nvim-cmp,lua
return {
"hrsh7th/nvim-cmp",
event = {
"InsertEnter",
"CmdlineEnter"
},
dependencies = {
"hrsh7th/cmp-buffer", -- source for text in buffer
"hrsh7th/cmp-path", -- source for file system paths
"hrsh7th/cmp-nvim-lua", -- lua NEW
"hrsh7th/cmp-nvim-lsp", -- lsp NEW
"L3MON4D3/LuaSnip", -- snippet engine
"saadparwaiz1/cmp_luasnip", -- for autocompletion
"rafamadriz/friendly-snippets", -- useful snippets
"onsails/lspkind.nvim", -- vs-code like pictograms,
},
} -- close return
after/plugin/lsp.lua
-- SETUP NVIM-CMP
local cmp = require("cmp")
local util = require("lspconfig.util")
local luasnip = require("luasnip")
local lspkind = require("lspkind")
local has_words_before = function()
unpack = unpack or table.unpack
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
end
require("luasnip.loaders.from_vscode").lazy_load()
require("luasnip.loaders.from_snipmate").lazy_load()
require("luasnip.loaders.from_lua").load({paths = "~/.config/nvim/snippets/"})
cmp.setup({
completion = {
completeopt = "menu,menuone,preview,noselect",
},
snippet = {
-- REQUIRED: you must specify a snippet engine
expand = function(args)
require('luasnip').lsp_expand(args.body) -- For `luasnip` users.
-- vim.fn["vsnip#anonymous"](args.body) -- For `vsnip` users.
-- require('snippy').expand_snippet(args.body) -- For `snippy` users.
-- vim.fn["UltiSnips#Anon"](args.body) -- For `ultisnips` users.
end,
},
window = {
completion = cmp.config.window.bordered(),
documentation = cmp.config.window.bordered(),
},
mapping = cmp.mapping.preset.insert({
["<C-k>"] = cmp.mapping.select_prev_item(), -- previous suggestion
["<C-j>"] = cmp.mapping.select_next_item(), -- next suggestion
['<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 = false }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items.
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
-- You could replace the expand_or_jumpable() calls with expand_or_locally_jumpable()
-- they way you will only jump inside the snippet region
elseif luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
elseif has_words_before() then
cmp.complete()
else
fallback()
end
end, { "i", "s" }), -- end tab
["<S-Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif luasnip.jumpable(-1) then
luasnip.jump(-1)
else
fallback()
end
end, { "i", "s" }), -- end shift tab
}),
sources = cmp.config.sources({
{ name = 'nvim_lsp' },
{ name = 'luasnip' }, -- For luasnip users.
{ name = "cmp_nvim_r" }, -- R
-- { name = 'vsnip' }, -- For vsnip users.
-- { name = 'ultisnips' }, -- For ultisnips users.
-- { name = 'snippy' }, -- For snippy users.
{
name = "buffer",
option = {
get_bufnrs = function() return { vim.api.nvim_get_current_buf() } end
}
}, -- text within current buffer
{ name = "path",
option = {
trailing_slash = true
}
}, -- file system paths
}), -- end sources
-- configure lspkind for vs-code like pictograms in completion menu
formatting = {
fields = { "abbr", "kind", "menu"},
format = lspkind.cmp_format({
mode = "symbol",
maxwidth = 50,
ellipsis_char = "...",
before = function(entry, item)
local menu_icon = {
nvim_lsp = '',
vsnip = '',
path = '',
cmp_nvim_r = 'R'
}
item.menu = menu_icon[entry.source.name]
return item
end,
})
}, -- end formatting
}) -- end cmp.setup
-- Set configuration for specific filetype.
cmp.setup.filetype('gitcommit', {
sources = cmp.config.sources({
{ name = 'cmp_git' }, -- You can specify the `cmp_git` source if you were installed it.
}, {
{ name = 'buffer' },
})
})
-- Use buffer source for `/` and `?` (if you enabled `native_menu`, this won't
-- work anymore).
cmp.setup.cmdline({ '/', '?' }, {
mapping = cmp.mapping.preset.cmdline(),
sources = {
{ name = 'buffer' }
}
})
-- Use cmdline & path source for ':' (if you enabled `native_menu`, this won't
-- work anymore).
cmp.setup.cmdline(':', {
mapping = cmp.mapping.preset.cmdline(),
sources = cmp.config.sources({
{ name = 'path' }
}, {
{ name = 'cmdline' }
})
})
local _border = "single"
vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(
vim.lsp.handlers.hover, {
border = _border
}
)
vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(
vim.lsp.handlers.signature_help, {
border = _border
}
)
vim.diagnostic.config{
float={border=_border}
}
require('lspconfig.ui.windows').default_options = {
border = _border
}
local capabilities = require('cmp_nvim_lsp').default_capabilities()
-- SETUP LSPCONFIG
require("lspconfig").r_language_server.setup{ capabilities = capabilities }
require("lspconfig").sqlls.setup {
cmd = { 'sql-language-server', 'up', '--method', 'stdio' },
filetypes = { 'sql', 'pgsql' },
settings = {}
}
FAQ
Announcement
Minimal reproducible full config
Description
I am unable to invoke several sources for autocompletion. Autocompletion does work at least partially as I have autocomplete when coding in the R language, just not any other sources. The resources, such as snippets from LuaSnip, are available, they simply are not available via autocomplete.
Steps to reproduce
Trying many, many different settings within the included nvim-cmp.lua configuration file but to no avail, and I am out of ideas. The problem does not produce an error, it just does not work for most sources, and is not specific to a file type.
Expected behavior
All sources listed in the nvim-cmp.lua configuration file should be available for autocomplete.
Actual behavior
:CmpStatus
Additional context
Thanks for your Herculean contributions to the Neovim community, they are tremendous gifts.