2022-11-02 22:50:13 -07:00
-- Setup nvim-cmp.
local cmp = require ' cmp '
local lspkind = require ( ' lspkind ' )
local lspconfig = require ( ' lspconfig ' )
-- luasnip setup
local luasnip = require ' luasnip '
local highlight = require ( ' cmp.utils.highlight ' )
2023-08-16 19:29:40 -07:00
local capabilities = require ( ' cmp_nvim_lsp ' ) . default_capabilities ( )
2023-08-16 02:29:56 -07:00
2022-11-02 22:50:13 -07:00
local has_words_before = function ( )
2023-08-11 18:12:35 -07:00
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
2023-09-06 16:22:30 -07:00
vim.api . nvim_buf_get_text ( 0 , line - 1 , 0 , line - 1 , col , { } ) [ 1 ] : match (
" ^%s*$ " ) == nil
2022-11-02 22:50:13 -07:00
end
2023-09-06 16:22:30 -07:00
lspkind.init ( { symbol_map = { Copilot = " " } } )
2022-11-02 22:50:13 -07:00
2023-09-06 16:22:30 -07:00
vim.api . nvim_set_hl ( 0 , " CmpItemKindCopilot " , { fg = " #6CC644 " } )
2022-11-02 22:50:13 -07:00
cmp.setup ( {
snippet = {
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.
2023-08-11 18:12:35 -07:00
end
2022-11-02 22:50:13 -07:00
} ,
capabilities = capabilities ,
mapping = {
[ ' <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
2023-09-06 16:22:30 -07:00
end , { ' i ' , ' s ' } ) ,
2022-11-02 22:50:13 -07:00
[ ' <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
2023-09-06 16:22:30 -07:00
end , { ' i ' , ' s ' } ) ,
2022-11-02 22:50:13 -07:00
[ ' <C-d> ' ] = cmp.mapping . scroll_docs ( - 4 ) ,
[ ' <C-f> ' ] = cmp.mapping . scroll_docs ( 4 ) ,
[ ' <C-Space> ' ] = cmp.mapping . complete ( ) ,
2023-03-10 11:42:54 -08:00
[ ' <C-e> ' ] = cmp.mapping . abort ( ) ,
2022-11-02 22:50:13 -07:00
[ ' <CR> ' ] = cmp.mapping . confirm {
behavior = cmp.ConfirmBehavior . Replace ,
2023-08-11 18:12:35 -07:00
select = false
2022-11-02 22:50:13 -07:00
} ,
-- ["<Tab>"] = cmp.mapping(function(fallback)
-- if cmp.visible() then
-- cmp.select_next_item()
-- elseif luasnip.expand_or_jumpable() then
-- luasnip.expand_or_jump()
-- else
-- fallback()
-- end
2023-08-11 18:12:35 -07:00
-- end, { "i", "s" }),
2022-11-02 22:50:13 -07:00
[ " <Tab> " ] = vim.schedule_wrap ( function ( fallback )
2023-08-11 18:12:35 -07:00
if cmp.visible ( ) and has_words_before ( ) then
2023-09-06 16:22:30 -07:00
cmp.select_next_item ( { behavior = cmp.SelectBehavior . Select } )
2023-08-11 18:12:35 -07:00
elseif luasnip.expand_or_jumpable ( ) then
luasnip.expand_or_jump ( )
else
fallback ( )
end
2022-11-02 22:50:13 -07:00
end ) ,
[ ' <S-Tab> ' ] = cmp.mapping ( function ( )
2023-08-11 18:12:35 -07:00
if cmp.visible ( ) then cmp.select_prev_item ( ) end
2023-09-06 16:22:30 -07:00
end , { " i " , " s " } )
2022-11-02 22:50:13 -07:00
} ,
window = {
completion = {
2023-08-11 18:12:35 -07:00
winhighlight = " Normal:Pmenu,FloatBorder:Pmenu,Search:None " ,
col_offset = - 3 ,
side_padding = 0 ,
border = " rounded " ,
borderchars = {
" ─ " , " │ " , " ─ " , " │ " , " ╭ " , " ╮ " , " ╯ " , " ╰ "
}
2022-11-02 22:50:13 -07:00
} ,
documentation = {
2023-08-11 18:12:35 -07:00
border = " rounded " ,
borderchars = {
" ─ " , " │ " , " ─ " , " │ " , " ╭ " , " ╮ " , " ╯ " , " ╰ "
}
-- padding = 15,
2022-11-02 22:50:13 -07:00
}
} ,
formatting = {
2023-05-03 23:59:17 -07:00
-- options: 'text', 'text_symbol', 'symbol_text', 'symbol'
-- mode = 'symbol_text',
2023-09-06 16:22:30 -07:00
fields = { " kind " , " abbr " , " menu " } ,
2022-11-02 22:50:13 -07:00
format = function ( entry , vim_item )
2023-08-11 18:12:35 -07:00
local kind = require ( " lspkind " ) . cmp_format ( {
mode = " symbol_text " ,
maxwidth = 75 ,
symbol_map = {
Copilot = " " ,
Function = " " ,
Text = " " ,
Method = " " ,
Operator = " " ,
Keyword = " " ,
Variable = " " ,
Field = " " ,
Class = " " ,
Interface = " " ,
Module = " " ,
Property = " " ,
Unit = " " ,
Value = " " ,
Enum = " " ,
Snippet = " " ,
Color = " " ,
File = " " ,
Reference = " " ,
Folder = " " ,
EnumMember = " " ,
Constant = " " ,
Struct = " " ,
Event = " " ,
TypeParameter = " "
}
} ) ( entry , vim_item )
2023-09-06 16:22:30 -07:00
local strings = vim.split ( kind.kind , " %s " , { trimempty = true } )
2023-08-11 18:12:35 -07:00
kind.kind = " " .. strings [ 1 ] .. " "
kind.menu = " ( " .. strings [ 2 ] .. " ) "
2022-11-02 22:50:13 -07:00
2023-08-11 18:12:35 -07:00
return kind
end
2022-11-02 22:50:13 -07:00
-- format = lspkind.cmp_format({
-- mode = 'symbol_text', -- show only symbol annotations
-- menu = ({
-- buffer = "[Buffer]",
-- nvim_lsp = "[LSP]",
-- luasnip = "[LuaSnip]",
-- nvim_lua = "[Lua]",
-- latex_symbols = "[Latex]",
-- })
-- })
} ,
sources = cmp.config . sources ( {
2023-09-06 16:22:30 -07:00
{ name = " copilot " , group_index = 2 } , { name = " path " , group_index = 2 } ,
{ name = ' nvim_lsp ' , group_index = 2 } ,
{ name = ' nvim_lsp_signature_help ' , group_index = 2 } ,
{ name = ' nvim_lsp_document_symbol ' , group_index = 2 } ,
{ name = ' vim-dadbod-completion ' , group_index = 2 } ,
{ name = ' neorg ' , group_index = 2 } , -- For luasnip users.
{ name = ' luasnip ' , group_index = 2 } , -- For luasnip users.
2022-11-02 22:50:13 -07:00
{
2023-08-11 18:12:35 -07:00
name = ' buffer ' ,
option = {
get_bufnrs = function ( )
local bufs = { }
for _ , win in ipairs ( vim.api . nvim_list_wins ( ) ) do
bufs [ vim.api . nvim_win_get_buf ( win ) ] = true
end
return vim.tbl_keys ( bufs )
end
}
2022-11-02 22:50:13 -07:00
}
-- { name = 'ultisnips' }, -- For ultisnips users.
-- { name = 'snippy' }, -- For snippy users.
2023-09-06 16:22:30 -07:00
} , { { name = ' buffer ' } } ) ,
2022-11-02 22:50:13 -07:00
sorting = {
priority_weight = 2 ,
comparators = {
2023-08-11 18:12:35 -07:00
require ( " copilot_cmp.comparators " ) . prioritize ,
require ( " copilot_cmp.comparators " ) . score ,
require ( " copilot_cmp.comparators " ) . recently_used ,
require ( " copilot_cmp.comparators " ) . kind ,
require ( " copilot_cmp.comparators " ) . sort_text ,
require ( " copilot_cmp.comparators " ) . length ,
require ( " copilot_cmp.comparators " ) . order ,
2023-05-04 09:15:14 -07:00
2023-08-11 18:12:35 -07:00
-- Below is the default comparitor list and order for nvim-cmp
cmp.config . compare.offset ,
-- cmp.config.compare.scopes, --this is commented in nvim-cmp too
cmp.config . compare.exact , cmp.config . compare.score ,
cmp.config . compare.recently_used , cmp.config . compare.locality ,
cmp.config . compare.kind , cmp.config . compare.sort_text ,
cmp.config . compare.length , cmp.config . compare.order
}
}
2022-11-02 22:50:13 -07:00
} )
2023-03-10 11:42:54 -08:00
cmp.setup . cmdline ( ' / ' , {
2023-08-11 18:12:35 -07:00
mapping = cmp.mapping . preset.cmdline ( ) ,
2023-09-06 16:22:30 -07:00
sources = { { name = ' buffer ' } }
2023-03-10 11:42:54 -08:00
} )
cmp.setup . cmdline ( ' : ' , {
2023-08-11 18:12:35 -07:00
mapping = cmp.mapping . preset.cmdline ( ) ,
2023-09-06 16:22:30 -07:00
sources = cmp.config . sources ( { { name = ' path ' } } , {
{ name = ' cmdline ' , option = { ignore_cmds = { ' Man ' , ' ! ' } } }
2023-08-11 18:12:35 -07:00
} )
2023-03-10 11:42:54 -08:00
} )
2023-08-11 18:12:35 -07:00
local servers = {
2023-09-06 16:21:01 -07:00
' bashls ' , ' pyright ' , ' jsonls ' , ' yamlls ' , ' vimls ' , ' dotls ' , ' dockerls ' ,
2025-02-13 00:38:12 -08:00
' html ' , ' cssls ' , ' lua_ls ' , ' eslint ' , ' ts_ls ' , ' angularls ' , ' ansiblels '
2023-08-11 18:12:35 -07:00
}
2023-09-06 16:22:08 -07:00
2022-11-02 22:50:13 -07:00
for _ , lsp in ipairs ( servers ) do
2023-08-15 04:18:31 -07:00
if lsp == ' lua_ls ' then
lspconfig [ lsp ] . setup {
2025-02-13 01:51:51 -08:00
on_attach = require ( " lsp-format " ) . on_attach ,
on_init = function ( client )
if client.workspace_folders then
local path = client.workspace_folders [ 1 ] . name
if path ~= vim.fn . stdpath ( ' config ' ) and ( vim.loop . fs_stat ( path .. ' /.luarc.json ' ) or vim.loop . fs_stat ( path .. ' /.luarc.jsonc ' ) ) then
return
end
end
client.config . settings.Lua = vim.tbl_deep_extend ( ' force ' , client.config . settings.Lua , {
2023-09-06 16:21:01 -07:00
runtime = {
2025-02-13 01:51:51 -08:00
-- Tell the language server which version of Lua you're using
-- (most likely LuaJIT in the case of Neovim)
version = ' LuaJIT '
2023-09-06 16:21:01 -07:00
} ,
2025-02-13 01:51:51 -08:00
-- Make the server aware of Neovim runtime files
2023-09-06 16:21:01 -07:00
workspace = {
2025-02-13 01:51:51 -08:00
checkThirdParty = false ,
library = {
vim.env . VIMRUNTIME
-- Depending on the usage, you might want to add additional paths here.
-- "${3rd}/luv/library"
-- "${3rd}/busted/library",
}
-- or pull in all of 'runtimepath'. NOTE: this is a lot slower and will cause issues when working on your own configuration (see https://github.com/neovim/nvim-lspconfig/issues/3189)
-- library = vim.api.nvim_get_runtime_file("", true)
2023-09-06 16:21:01 -07:00
}
2025-02-13 01:51:51 -08:00
} )
end ,
capabilities = capabilities ,
callSnippet = " Replace " ,
settings = {
Lua = { }
2023-09-06 16:21:01 -07:00
}
2023-08-15 04:18:31 -07:00
}
else
2025-02-13 01:51:51 -08:00
lspconfig [ lsp ] . setup { on_attach = require ( " lsp-format " ) . on_attach , capabilities = capabilities }
2023-08-15 04:18:31 -07:00
end
2022-11-02 22:50:13 -07:00
end
2025-02-13 01:51:51 -08: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)