cmp.lua (5145B)
1 if not pcall(require, 'cmp') then 2 return 3 end 4 5 local kind_icons = { 6 Text = '', 7 Method = '', 8 Function = '', 9 Constructor = '', 10 Field = '', 11 Variable = '', 12 Class = 'ﴯ', 13 Interface = '', 14 Module = '', 15 Property = 'ﰠ', 16 Unit = '', 17 Value = '', 18 Enum = '', 19 Keyword = '', 20 Snippet = '', 21 Color = '', 22 File = '', 23 Reference = '', 24 Folder = '', 25 EnumMember = '', 26 Constant = '', 27 Struct = '', 28 Event = '', 29 Operator = '', 30 TypeParameter = '', 31 } 32 33 local cmp = require('cmp') 34 local function close() 35 return function(fallback) 36 -- if Codeium.Completions() then 37 -- fallback = Codeium.Clear 38 -- end 39 40 if not cmp.close() then 41 fallback() 42 end 43 end 44 end 45 46 local register_snippets = true 47 for _, source in ipairs(cmp.core.sources) do 48 if (source.name == 'snippets') then 49 register_snippets = false 50 break 51 end 52 end 53 if register_snippets then 54 local snippet_source = require 'tms.p.cmp.snippet_source' 55 cmp.register_source('snippets', snippet_source) 56 end 57 58 cmp.setup({ 59 window = { 60 -- 61 completion = cmp.config.window.bordered(), 62 documentation = cmp.config.window.bordered(), 63 }, 64 snippet = { 65 expand = function(args) 66 vim.snippet.expand(args.body) 67 end, 68 }, 69 -- view = { entries = { name = 'custom' } }, 70 formatting = { 71 format = function(entry, vim_item) 72 -- Kind icons 73 vim_item.kind = string.format('%s %s', kind_icons[vim_item.kind], vim_item.kind) -- This concatonates the icons with the name of the item kind 74 -- Source 75 vim_item.menu = ({ 76 nvim_lsp = '[LSP]', 77 nvim_lua = '[Lua]', 78 buffer = '[Buffer]', 79 treesitter = '[TS]', 80 latex_symbols = '[LaTeX]', 81 })[entry.source.name] 82 return vim_item 83 end, 84 }, 85 mapping = { 86 ['<c-e>'] = cmp.mapping({ i = close(), c = cmp.mapping.abort() }), 87 ['<c-y>'] = cmp.mapping.confirm({ select = true }), 88 ['<c-u>'] = cmp.mapping(cmp.mapping.scroll_docs(4), { 'i', 'c' }), 89 ['<c-d>'] = cmp.mapping(cmp.mapping.scroll_docs(-4), { 'i', 'c' }), 90 ['<c-n>'] = cmp.mapping({ i = cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Select }) }), 91 ['<c-p>'] = cmp.mapping({ i = cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Select }) }), 92 ['<c-space>'] = cmp.mapping({ i = cmp.mapping.complete() }), 93 }, 94 95 -- = cmp.config.sources({ 96 -- { name = 'nvim_lsp' }, 97 -- { name = 'vsnip' }, -- For vsnip users. 98 -- -- { name = 'snippy' }, -- For snippy users. 99 -- -- { name = 'ultisnips' }, -- For ultisnips users. 100 -- }, { { name = 'buffer' } }), 101 102 sources = cmp.config.sources({ 103 { name = 'snippets', max_item_count = 3 }, 104 { name = 'lazydev', group_index = 0 }, 105 { 106 name = 'nvim_lsp', 107 -- entry_filter = function(entry, ctx) 108 -- local file = io.open('/home/tms/cmp_log', 'a') 109 -- file:write(vim.fn.json_encode { entry.id, entry.score, entry.exact, entry.completion_item }) 110 -- file:close() 111 -- return true 112 -- end, 113 }, 114 { name = 'nvim_lsp_signature_help' }, 115 { name = 'nvim_lua' }, 116 { name = 'path' }, 117 { name = 'spell' }, 118 { name = 'calc' }, 119 -- { name = 'treesitter' }, 120 }, { -- 121 { name = 'buffer' }, 122 }), 123 sorting = { 124 priority_weight = 2, 125 comparators = { 126 -- cmp.config.compare.offset, 127 cmp.config.compare.exact, 128 -- compare.scopes, 129 cmp.config.compare.score, 130 cmp.config.compare.sort_text, 131 -- cmp.config.compare.recently_used, 132 -- cmp.config.compare.locality, 133 -- cmp.config.compare.kind, 134 -- compare.sort_text, 135 -- cmp.config.compare.length, 136 cmp.config.compare.order, 137 }, 138 }, 139 -- experimental = { ghost_text = false }, 140 }) 141 142 cmp.setup.cmdline('/', { 143 -- 144 mapping = { 145 ['<c-e>'] = cmp.mapping { c = cmp.mapping.abort() }, 146 ['<c-y>'] = cmp.mapping { c = cmp.mapping.confirm({ select = true }) }, 147 ['<c-p>'] = cmp.mapping { 148 c = function(fallback) 149 if cmp.visible() then 150 cmp.select_prev_item() 151 else 152 fallback() 153 end 154 end, 155 }, 156 ['<c-n>'] = cmp.mapping { 157 c = function(fallback) 158 if cmp.visible() then 159 cmp.select_next_item() 160 else 161 fallback() 162 end 163 end, 164 }, 165 }, 166 sources = cmp.config.sources({ { name = 'buffer' } }), 167 }) 168 169 cmp.setup.cmdline(':', { 170 mapping = { 171 ['<c-e>'] = cmp.mapping { c = cmp.mapping.abort() }, 172 ['<c-y>'] = cmp.mapping { c = cmp.mapping.confirm({ select = true }) }, 173 ['<c-p>'] = cmp.mapping { 174 c = function(fallback) 175 if cmp.visible() then 176 cmp.select_prev_item() 177 else 178 fallback() 179 end 180 end, 181 }, 182 ['<c-n>'] = cmp.mapping { 183 c = function(fallback) 184 if cmp.visible() then 185 cmp.select_next_item() 186 else 187 fallback() 188 end 189 end, 190 }, 191 }, 192 sources = cmp.config.sources({ -- 193 { name = 'path' }, 194 }, { -- 195 { name = 'cmdline' }, 196 }, { -- 197 { name = 'cmdline_history' }, 198 }), 199 })