repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

neovim/lua/bugabinga/plugins/cmp.lua

Raw
local icon = require 'std.icon'

return {
	'hrsh7th/nvim-cmp',
	lazy = false,
	dependencies = {
		'hrsh7th/cmp-nvim-lsp',
		'hrsh7th/cmp-buffer',
		'hrsh7th/cmp-path',
		'hrsh7th/cmp-nvim-lua',
		'saadparwaiz1/cmp_luasnip',
		'onsails/lspkind.nvim',
		'L3MON4D3/LuaSnip',
	},
	config = function ()
		local cmp = require 'cmp'
		local luasnip = require 'luasnip'
		local MAX_INDEX_FILE_SIZE = 4000

		local snippet_next = function ( fallback )
			if luasnip.expand_or_jumpable() then
				luasnip.expand_or_jump()
			elseif cmp.visible() then
				cmp.select_next_item()
			else
				fallback()
			end
		end

		local snippet_prev = function ( fallback )
			if luasnip.jumpable( -1 ) then
				luasnip.jump( -1 )
			elseif cmp.visible() then
				cmp.select_prev_item()
			else
				fallback()
			end
		end

		local mapping = cmp.mapping.preset.insert {
			['<C-d>'] = cmp.mapping.scroll_docs( -4 ),
			['<C-f>'] = cmp.mapping.scroll_docs( 4 ),
			['<C-e>'] = cmp.mapping.close(),
			['<C-y>'] = cmp.mapping.confirm { behavior = cmp.ConfirmBehavior.Replace, select = true, },
			['<C-space>'] = cmp.mapping.complete(),
			['<Tab>'] = cmp.mapping( snippet_next, { 'i', 's', } ),
			['<S-Tab>'] = cmp.mapping( snippet_prev, { 'i', 's', } ),
		}

		local formatting = {}
		local ok, lspkind = pcall( require, 'lspkind' )
		if ok then
			formatting.format = lspkind.cmp_format {
				mode = 'symbol',
				symbol_map = {
					Copilot = icon.copilot,
					Class = icon.class,
					Color = icon.color,
					Constant = icon.constant,
					Constructor = icon.constructor,
					Enum = icon.enum,
					EnumMember = icon.enum_member,
					Event = icon.event,
					Field = icon.field,
					File = icon.file,
					Folder = icon.folder,
					Function = icon['function'],
					Interface = icon.interface,
					Keyword = icon.keyword,
					Method = icon.method,
					Module = icon.module,
					Operator = icon.operator,
					Property = icon.property,
					Reference = icon.reference,
					Snippet = icon.snippet,
					Struct = icon.struct,
					Text = icon.text,
					TypeParameter = icon.type_parameter,
					Unit = icon.unit,
					Value = icon.value,
					Variable = icon.variable,
				},
				menu = {
					buffer = '[buf]',
					nvim_lsp = '[LSP]',
					nvim_lua = '[nvim]',
					path = '[path]',
					luasnip = '[snip]',
				},
			}
		end

		local sources = {
			{ name = 'neodev', },
			{ name = 'nvim_lua', },
			{ name = 'nvim_lsp', },
			{ name = 'path', },
			{ name = 'luasnip', },
			{ name = 'neorg', },
			{
				name = 'buffer',
				keyword_length = 4,
				options = {
					get_bufnrs = function ()
						local bufs = {}
						for _, bufnr in ipairs( vim.api.nvim_list_bufs() ) do
							-- Don't index giant files
							if vim.api.nvim_buf_is_loaded( bufnr ) and vim.api.nvim_buf_line_count( bufnr ) < MAX_INDEX_FILE_SIZE then
								table.insert( bufs, bufnr )
							end
						end
						return bufs
					end,
				},
			},
		}

		cmp.setup {
			mapping = mapping,
			formatting = formatting,
			sources = sources,

			completion = { autocomplete = false, },
			snippet = {
				expand = function ( args ) luasnip.lsp_expand( args.body ) end,
			},

			-- experimental = { native_menu = false, },
		}
	end,
}