-- ============================================================================ -- Completion - nvim-cmp Configuration -- ============================================================================ return { { 'hrsh7th/nvim-cmp', event = 'InsertEnter', dependencies = { -- Snippet Engine { 'L3MON4D3/LuaSnip', build = (function() return 'make install_jsregexp' end)(), dependencies = { 'rafamadriz/friendly-snippets', }, }, 'saadparwaiz1/cmp_luasnip', -- Completion Sources 'hrsh7th/cmp-nvim-lsp', 'hrsh7th/cmp-buffer', 'hrsh7th/cmp-path', }, config = function() local cmp = require('cmp') local luasnip = require('luasnip') -- Friendly snippets laden require('luasnip.loaders.from_vscode').lazy_load() -- Custom Snippets für Go luasnip.add_snippets('go', { luasnip.snippet('iferr', { luasnip.text_node('if err != nil {'), luasnip.text_node({ '', '\treturn err' }), luasnip.text_node({ '', '}' }), }), }) cmp.setup({ snippet = { expand = function(args) luasnip.lsp_expand(args.body) end, }, completion = { completeopt = 'menu,menuone,noinsert' }, -- Keymaps mapping = cmp.mapping.preset.insert({ -- Select next/prev item [''] = cmp.mapping.select_next_item(), [''] = cmp.mapping.select_prev_item(), -- Scroll docs [''] = cmp.mapping.scroll_docs(-4), [''] = cmp.mapping.scroll_docs(4), -- Confirm completion [''] = cmp.mapping.confirm({ select = true }), -- Manual trigger [''] = cmp.mapping.complete({}), -- Tab für Snippet forward/backward + completion [''] = cmp.mapping(function(fallback) if cmp.visible() then cmp.select_next_item() elseif luasnip.expand_or_locally_jumpable() then luasnip.expand_or_jump() else fallback() end end, { 'i', 's' }), [''] = cmp.mapping(function(fallback) if cmp.visible() then cmp.select_prev_item() elseif luasnip.locally_jumpable(-1) then luasnip.jump(-1) else fallback() end end, { 'i', 's' }), }), -- Sources (Priorität von oben nach unten) sources = { { name = 'nvim_lsp' }, { name = 'luasnip' }, { name = 'buffer' }, { name = 'path' }, }, -- Window styling window = { completion = cmp.config.window.bordered(), documentation = cmp.config.window.bordered(), }, -- Formatting formatting = { fields = { 'kind', 'abbr', 'menu' }, format = function(entry, item) local icons = { Text = '', Method = '󰆧', Function = '󰊕', Constructor = '', Field = '󰇽', Variable = '󰂡', Class = '󰠱', Interface = '', Module = '', Property = '󰜢', Unit = '', Value = '󰎠', Enum = '', Keyword = '󰌋', Snippet = '', Color = '󰏘', File = '󰈙', Reference = '', Folder = '󰉋', EnumMember = '', Constant = '󰏿', Struct = '', Event = '', Operator = '󰆕', TypeParameter = '󰅲', } item.kind = string.format('%s %s', icons[item.kind] or '', item.kind) item.menu = ({ nvim_lsp = '[LSP]', luasnip = '[Snippet]', buffer = '[Buffer]', path = '[Path]', })[entry.source.name] return item end, }, }) end, }, }