Added lsp capabilities of nvim cmp. It can now do much more as LSPs see

nvim has more functionalities (Show unimported functions etc). Also
added better sorting to cmp entries. Added a notification when LSP
finished loading.
This commit is contained in:
Afonso Franco 2022-12-15 17:43:06 +00:00
parent 2f72604e89
commit 3c2807b83c
Signed by: afonso
GPG key ID: C459E0BB3DCEE899
3 changed files with 154 additions and 126 deletions

View file

@ -1,135 +1,140 @@
local cmp_status_ok, cmp = pcall(require, "cmp")
if not cmp_status_ok then
return
return
end
local snip_status_ok, luasnip = pcall(require, "luasnip")
if not snip_status_ok then
return
return
end
require("luasnip/loaders/from_vscode").lazy_load()
local check_backspace = function()
local col = vim.fn.col "." - 1
return col == 0 or vim.fn.getline("."):sub(col, col):match "%s"
local col = vim.fn.col "." - 1
return col == 0 or vim.fn.getline("."):sub(col, col):match "%s"
end
--   פּ ﯟ   some other good icons
local kind_icons = {
Text = "",
Method = "m",
Function = "",
Constructor = "",
Field = "",
Variable = "",
Class = "",
Interface = "",
Module = "",
Property = "",
Unit = "",
Value = "",
Enum = "",
Keyword = "",
Snippet = "",
Color = "",
File = "",
Reference = "",
Folder = "",
EnumMember = "",
Constant = "",
Struct = "",
Event = "",
Operator = "",
TypeParameter = "",
Text = "",
Method = "m",
Function = "",
Constructor = "",
Field = "",
Variable = "",
Class = "",
Interface = "",
Module = "",
Property = "",
Unit = "",
Value = "",
Enum = "",
Keyword = "",
Snippet = "",
Color = "",
File = "",
Reference = "",
Folder = "",
EnumMember = "",
Constant = "",
Struct = "",
Event = "",
Operator = "",
TypeParameter = "",
}
-- find more here: https://www.nerdfonts.com/cheat-sheet
cmp.setup {
snippet = {
expand = function(args)
luasnip.lsp_expand(args.body) -- For `luasnip` users.
end,
},
mapping = {
["<C-k>"] = cmp.mapping.select_prev_item(),
snippet = {
expand = function(args)
luasnip.lsp_expand(args.body) -- For `luasnip` users.
end,
},
mapping = {
["<C-k>"] = cmp.mapping.select_prev_item(),
["<C-j>"] = cmp.mapping.select_next_item(),
["<C-b>"] = cmp.mapping(cmp.mapping.scroll_docs(-1), { "i", "c" }),
["<C-f>"] = cmp.mapping(cmp.mapping.scroll_docs(1), { "i", "c" }),
["<C-Space>"] = cmp.mapping(cmp.mapping.complete(), { "i", "c" }),
["<C-y>"] = cmp.config.disable, -- Specify `cmp.config.disable` if you want to remove the default `<C-y>` mapping.
["<C-e>"] = cmp.mapping {
i = cmp.mapping.abort(),
c = cmp.mapping.close(),
},
-- Accept currently selected item. If none selected, `select` first item.
-- Set `select` to `false` to only confirm explicitly selected items.
["<CR>"] = cmp.mapping.confirm { select = true },
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif luasnip.expandable() then
luasnip.expand()
elseif luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
elseif check_backspace() then
fallback()
else
fallback()
end
end, {
"i",
"s",
}),
["<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",
}),
},
formatting = {
fields = { "kind", "abbr", "menu" },
format = function(entry, vim_item)
-- Kind icons
vim_item.kind = string.format("%s", kind_icons[vim_item.kind])
-- vim_item.kind = string.format('%s %s', kind_icons[vim_item.kind], vim_item.kind) -- This concatonates the icons with the name of the item kind
vim_item.menu = ({
nvim_lsp = "(LSP)",
luasnip = "(Snippet)",
buffer = "(Text)",
path = "(Path)",
})[entry.source.name]
return vim_item
end,
},
sources = {
{ name = "nvim_lsp" },
{ name = "luasnip" },
{ name = "buffer" },
{ name = "path" },
},
confirm_opts = {
behavior = cmp.ConfirmBehavior.Replace,
select = false,
},
window = {
documentation = cmp.config.window.bordered(),
completion = cmp.config.window.bordered({
winhighlight = "Normal:Pmenu,FloatBorder:Pmenu,CursorLine:PmenuSel,Search:None"
})
["<C-b>"] = cmp.mapping(cmp.mapping.scroll_docs(-1), { "i", "c" }),
["<C-f>"] = cmp.mapping(cmp.mapping.scroll_docs(1), { "i", "c" }),
["<C-Space>"] = cmp.mapping(cmp.mapping.complete(), { "i", "c" }),
["<C-y>"] = cmp.config.disable, -- Specify `cmp.config.disable` if you want to remove the default `<C-y>` mapping.
["<C-e>"] = cmp.mapping {
i = cmp.mapping.abort(),
c = cmp.mapping.close(),
},
-- Accept currently selected item. If none selected, `select` first item.
-- Set `select` to `false` to only confirm explicitly selected items.
["<CR>"] = cmp.mapping.confirm { select = true },
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif luasnip.expandable() then
luasnip.expand()
elseif luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
elseif check_backspace() then
fallback()
else
fallback()
end
end, {
"i",
"s",
}),
["<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",
}),
},
formatting = {
fields = { "kind", "abbr", "menu" },
format = function(entry, vim_item)
-- Kind icons
vim_item.kind = string.format("%s", kind_icons[vim_item.kind])
-- vim_item.kind = string.format('%s %s', kind_icons[vim_item.kind], vim_item.kind) -- This concatonates the icons with the name of the item kind
vim_item.menu = ({
nvim_lsp = "(LSP)",
luasnip = "(Snippet)",
buffer = "(Text)",
path = "(Path)",
})[entry.source.name]
return vim_item
end,
},
sorting = {
comparators = {
cmp.config.compare.exact,
cmp.config.compare.offset,
cmp.config.compare.recently_used,
}
},
sources = {
{ name = "nvim_lsp" },
{ name = "luasnip" },
{ name = "buffer" },
{ name = "path" },
},
confirm_opts = {
behavior = cmp.ConfirmBehavior.Replace,
select = false,
},
window = {
documentation = cmp.config.window.bordered(),
completion = cmp.config.window.bordered({
winhighlight = "Normal:Pmenu,FloatBorder:Pmenu,CursorLine:PmenuSel,Search:None"
})
},
experimental = {
ghost_text = false,
native_menu = false,
},
},
experimental = {
ghost_text = false,
native_menu = false,
},
}

View file

@ -1,10 +1,12 @@
local mason_lspconfig = require "mason-lspconfig"
local notify = require "notify"
mason_lspconfig.setup({
ensure_installed = { "sumneko_lua", "rust_analyzer", "texlab", "hls", "yamlls" },
automatic_installation = true
})
local opts = { noremap = true, silent = true }
vim.keymap.set('n', '<space>e', vim.diagnostic.open_float, opts)
vim.keymap.set('n', '[d', vim.diagnostic.goto_prev, opts)
@ -46,15 +48,32 @@ local on_attach = function(client, bufnr)
end
end
local on_init = function (client, initialize_result)
-- Alert user that LSP finished loading
notify("Finished loading " ..client.name, "info", {title = "LSP"})
end
local lspconfig = require "lspconfig"
-- ADD NVIM CMP AS A CAPABILITY
local lsp_defaults = lspconfig.util.default_config
local capabilities = vim.tbl_deep_extend(
'force',
lsp_defaults.capabilities,
require('cmp_nvim_lsp').default_capabilities()
)
mason_lspconfig.setup_handlers {
-- This is a default handler that will be called for each installed server (also for new servers that are installed during a session)
function(server_name)
lspconfig[server_name].setup {
on_attach = on_attach,
on_init = on_init,
flags = lsp_flags,
capabilities = capabilities,
}
end,
}

View file

@ -46,8 +46,21 @@ local plugins = packer.startup({ function(use)
vim.notify = require("notify")
end
}
-------------------------------------------------------LSP----------------------------------------------
--------------------------------------------------SUGGESTION BOX-----------------------------------------
use { "hrsh7th/nvim-cmp",
requires = {
'hrsh7th/cmp-nvim-lsp', -- lsp
'hrsh7th/cmp-buffer', --buffer completions
'hrsh7th/cmp-path', --path completions
'hrsh7th/cmp-cmdline' --cmdline completions
},
config = function()
require "plugins.cmp"
end
}
-------------------------------------------------------LSP----------------------------------------------
use { "williamboman/mason.nvim",
config = function()
require "plugins.mason"
@ -56,6 +69,8 @@ local plugins = packer.startup({ function(use)
use { "williamboman/mason-lspconfig.nvim" }
use { "hrsh7th/cmp-nvim-lsp"}
use { "neovim/nvim-lspconfig",
config = function()
require "plugins.lspconfig"
@ -74,19 +89,8 @@ local plugins = packer.startup({ function(use)
"saadparwaiz1/cmp_luasnip"
},
}
--------------------------------------------------SUGGESTION BOX-----------------------------------------
use { "hrsh7th/nvim-cmp",
requires = {
'hrsh7th/cmp-nvim-lsp', -- lsp
'hrsh7th/cmp-buffer', --buffer completions
'hrsh7th/cmp-path', --path completions
'hrsh7th/cmp-cmdline' --cmdline completions
},
config = function()
require "plugins.cmp"
end
}
------------------------------------------------------------------------------------------
use { 'nvim-tree/nvim-tree.lua',
requires = {