2022-02-10 00:43:30 -08:00
-- Setup nvim-cmp.
2022-04-15 23:21:17 -07:00
local cmp = require ' cmp '
2022-02-10 00:43:30 -08:00
local lspkind = require ( ' lspkind ' )
local capabilities = vim.lsp . protocol.make_client_capabilities ( )
2022-10-27 17:28:20 -07:00
capabilities = require ( ' cmp_nvim_lsp ' ) . default_capabilities ( capabilities )
2022-02-10 00:43:30 -08:00
-- luasnip setup
local luasnip = require ' luasnip '
2022-10-30 12:49:59 -07:00
2022-10-28 12:36:08 -07:00
local has_words_before = function ( )
if vim.api . nvim_buf_get_option ( 0 , " buftype " ) == " prompt " then return false end
local line , col = unpack ( vim.api . nvim_win_get_cursor ( 0 ) )
return col ~= 0 and vim.api . nvim_buf_get_text ( 0 , line - 1 , 0 , line - 1 , col , { } ) [ 1 ] : match ( " ^%s*$ " ) == nil
end
2022-02-10 00:43:30 -08:00
cmp.setup ( {
snippet = {
2022-04-15 23:21:17 -07:00
expand = function ( args )
-- vim.fn["vsnip#anonymous"](args.body) -- For `vsnip` users.
require ( ' luasnip ' ) . lsp_expand ( args.body ) -- For `luasnip` users.
-- require('snippy').expand_snippet(args.body) -- For `snippy` users.
-- vim.fn["UltiSnips#Anon"](args.body) -- For `ultisnips` users.
end ,
2022-02-10 00:43:30 -08:00
} ,
capabilities = capabilities ,
-- mapping = {
-- ['<C-b>'] = cmp.mapping(cmp.mapping.scroll_docs(-4), { 'i', 'c' }),
-- ['<C-f>'] = cmp.mapping(cmp.mapping.scroll_docs(4), { '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(),
-- }),
-- ['<CR>'] = cmp.mapping.confirm({ select = true }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items.
-- },
mapping = {
2022-10-30 12:49:59 -07:00
[ ' <C-p> ' ] = cmp.mapping ( function ( fallback )
if cmp.visible ( ) then
cmp.select_prev_item ( )
elseif luasnip.expand_or_jumpable ( ) then
luasnip.expand_or_jump ( )
elseif has_words_before ( ) then
cmp.complete ( )
else
fallback ( )
end
end , { ' i ' , ' s ' } ) ,
[ ' <C-n> ' ] = cmp.mapping ( function ( fallback )
if cmp.visible ( ) then
cmp.select_next_item ( )
elseif luasnip.expand_or_jumpable ( ) then
luasnip.expand_or_jump ( )
elseif has_words_before ( ) then
cmp.complete ( )
else
fallback ( )
end
end , { ' i ' , ' s ' } ) ,
2022-04-15 23:21:17 -07:00
[ ' <C-d> ' ] = cmp.mapping . scroll_docs ( - 4 ) ,
[ ' <C-f> ' ] = cmp.mapping . scroll_docs ( 4 ) ,
[ ' <C-Space> ' ] = cmp.mapping . complete ( ) ,
[ ' <C-e> ' ] = cmp.mapping . close ( ) ,
[ ' <CR> ' ] = cmp.mapping . confirm {
behavior = cmp.ConfirmBehavior . Replace ,
2022-10-30 12:49:59 -07:00
select = true ,
2022-04-15 23:21:17 -07:00
} ,
2022-10-28 12:36:08 -07:00
[ " <Tab> " ] = cmp.mapping ( function ( fallback )
2022-10-30 12:49:59 -07:00
if has_words_before ( ) then
require ( ' copilot.suggestion ' ) . accept ( )
elseif luasnip.expand_or_jumpable ( ) then
luasnip.expand_or_jump ( )
elseif not has_words_before ( ) then
cmp.complete ( )
else
fallback ( ) -- The fallback function sends a already mapped key. In this case, it's probably `<Tab>`.
end
-- if cmp.visible() then
-- cmp.select_next_item()
-- elseif has_words_before() then
-- cmp.complete()
-- else
-- fallback() -- The fallback function sends a already mapped key. In this case, it's probably `<Tab>`.
-- end
2022-10-28 12:36:08 -07:00
end , { " i " , " s " } ) ,
[ ' <S-Tab> ' ] = cmp.mapping ( function ( )
if cmp.visible ( ) then
cmp.select_prev_item ( )
end
end , { " i " , " s " } ) ,
2022-02-10 00:43:30 -08:00
} ,
formatting = {
2022-04-15 23:21:17 -07:00
format = lspkind.cmp_format ( {
mode = ' symbol ' , -- show only symbol annotations
2022-04-29 17:09:49 -07:00
maxwidth = 20 , -- prevent the popup from showing more than provided characters (e.g 50 will not show more than 50 characters)
2022-02-10 00:43:30 -08:00
2022-04-15 23:21:17 -07:00
-- The function below will be called before any actual modifications from lspkind
-- so that you can provide more controls on popup customization. (See [#30](https://github.com/onsails/lspkind-nvim/pull/30))
-- before = function (entry, vim_item)
-- ...
-- return vim_item
-- end
} )
2022-02-10 00:43:30 -08:00
} ,
sources = cmp.config . sources ( {
2022-04-15 23:21:17 -07:00
{ name = " copilot " , group_index = 2 } ,
{ name = ' nvim_lsp ' , group_index = 2 } ,
-- { name = 'vsnip' }, -- For vsnip users.
{ name = " path " , group_index = 2 } ,
{ name = ' luasnip ' , group_index = 2 } , -- For luasnip users.
-- { name = 'ultisnips' }, -- For ultisnips users.
-- { name = 'snippy' }, -- For snippy users.
2022-02-10 00:43:30 -08:00
} , {
2022-04-15 23:21:17 -07:00
{ name = ' buffer ' } ,
2022-02-10 00:43:30 -08:00
} )
2022-04-15 23:21:17 -07:00
} )
2022-02-10 00:43:30 -08:00
2022-04-15 23:21:17 -07:00
-- Setup lspconfig.
-- local capabilities = require('cmp_nvim_lsp').update_capabilities(vim.lsp.protocol.make_client_capabilities())
-- Replace <YOUR_LSP_SERVER> with each lsp server you've enabled.
-- require('lspconfig')['bashls'].setup {
-- capabilities = capabilities
-- }
-- Enable some language servers with the additional completion capabilities offered by nvim-cmp
local lspconfig = require ( ' lspconfig ' )
2022-05-16 23:52:25 -07:00
local servers = { ' bashls ' , ' jedi_language_server ' , ' sqlls ' , ' jsonls ' , ' yamlls ' , ' vimls ' , ' dotls ' , ' dockerls ' }
2022-04-15 23:21:17 -07:00
for _ , lsp in ipairs ( servers ) do
2022-02-10 00:43:30 -08:00
lspconfig [ lsp ] . setup {
2022-04-15 23:21:17 -07:00
-- on_attach = my_custom_on_attach,
capabilities = capabilities ,
2022-02-10 00:43:30 -08:00
}
2022-04-15 23:21:17 -07:00
end
2022-10-28 12:36:08 -07:00
2022-10-30 12:49:59 -07:00
-- cmp.event:on("menu_opened", function()
-- vim.b.copilot_suggestion_hidden = true
-- end)
-- cmp.event:on("menu_closed", function()
-- vim.b.copilot_suggestion_hidden = false
-- end)