2023-11-27 06:31:45 +00:00
-- Configure nvim-cmp with "supertab"-like behavior for LuaSnip integration: https://www.lazyvim.org/configuration/recipes#supertab
-- Also, prevent autocomplete from automatically selecting the first item in the list on <CR>, and instead require the user to press <Tab> to select the first item.
return {
" hrsh7th/nvim-cmp " ,
dependencies = {
" hrsh7th/cmp-calc " ,
" hrsh7th/cmp-emoji " ,
" hrsh7th/cmp-cmdline " ,
" petertriho/cmp-git " ,
} ,
---@param opts cmp.ConfigSchema
opts = function ( _ , opts )
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
2022-10-06 04:44:41 +00:00
2023-11-27 06:31:45 +00:00
local luasnip = require ( " luasnip " )
local cmp = require ( " cmp " )
2023-02-21 04:33:06 +00:00
2023-11-27 06:31:45 +00:00
opts.mapping = vim.tbl_extend ( " force " , opts.mapping , {
[ " <Tab> " ] = cmp.mapping ( function ( fallback )
-- This bit is for preventing auto-completion auto-triggering from copilot-cmp:
-- https://github.com/zbirenbaum/copilot-cmp?tab=readme-ov-file#tab-completion-configuration-highly-recommended
if cmp.visible ( ) and has_words_before ( ) then
-- You could replace select_next_item() with confirm({ select = true }) to get VS Code autocompletion behavior
cmp.select_next_item ( { behavior = cmp.SelectBehavior . Select } )
-- You could replace the expand_or_jumpable() calls with expand_or_locally_jumpable()
-- this 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 " } ) ,
[ " <S-Tab> " ] = cmp.mapping ( function ( fallback )
if cmp.visible ( ) then
2022-10-06 04:44:41 +00:00
cmp.select_prev_item ( )
2023-11-27 06:31:45 +00:00
elseif luasnip.jumpable ( - 1 ) then
luasnip.jump ( - 1 )
else
fallback ( )
end
end , { " i " , " s " } ) ,
[ " <CR> " ] = cmp.mapping . confirm ( { select = false } ) , -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items.
2022-10-24 16:04:40 +00:00
} )
2022-10-06 04:44:41 +00:00
2023-11-27 06:31:45 +00:00
-- Do not auto-select the first auto-complete item, require a <Tab> to select the first item.
opts.completion . completeopt = " menu,menuone,noinsert,noselect "
2022-10-06 04:44:41 +00:00
2023-11-27 06:31:45 +00:00
opts.sources = cmp.config . sources ( vim.list_extend ( opts.sources , {
{ name = " emoji " } ,
{ name = " git " } ,
{ name = " calc " } ,
} ) )
2022-10-06 04:44:41 +00:00
2023-11-27 06:31:45 +00:00
-- `/` cmdline setup.
cmp.setup . cmdline ( " / " , {
mapping = cmp.mapping . preset.cmdline ( ) ,
sources = {
{ name = " buffer " } ,
} ,
} )
2022-10-06 04:44:41 +00:00
2023-11-27 06:31:45 +00:00
-- `:` cmdline setup.
cmp.setup . cmdline ( " : " , {
mapping = cmp.mapping . preset.cmdline ( ) ,
sources = cmp.config . sources ( {
{ name = " path " } ,
} , {
{
name = " cmdline " ,
option = {
ignore_cmds = { " Man " , " ! " } ,
} ,
} ,
} ) ,
} )
end ,
2022-10-06 04:44:41 +00:00
}