-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmp.lua
251 lines (241 loc) · 8.23 KB
/
cmp.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
local M = {
"hrsh7th/nvim-cmp",
dependencies = {
{
"hrsh7th/cmp-nvim-lsp",
},
{
"hrsh7th/cmp-emoji",
},
{
"hrsh7th/cmp-buffer",
},
{
"hrsh7th/cmp-path",
},
{
"iteratee/cmp-cmdline",
branch = "kb/file-name-completion",
},
{
"saadparwaiz1/cmp_luasnip",
},
{
"L3MON4D3/LuaSnip",
dependencies = {
"rafamadriz/friendly-snippets",
},
},
{
"hrsh7th/cmp-nvim-lua",
},
{
"hrsh7th/cmp-nvim-lsp-signature-help",
},
{
"hrsh7th/cmp-calc",
},
{
"FoamScience/cmp-nvim-lsp-document-symbol",
branch = "override_kinds",
},
{
"SergioRibera/cmp-dotenv",
},
{
"p00f/clangd_extensions.nvim",
},
},
event = { "LspAttach", "InsertCharPre" },
}
function M.config()
vim.api.nvim_set_hl(0, "CmpItemKindCopilot", { fg = "#6CC644" })
vim.api.nvim_set_hl(0, "CmpItemKindCrate", { fg = "#F64D00" })
vim.api.nvim_set_hl(0, "CmpItemKindEmoji", { fg = "#FDE030" })
local cmp = require("cmp")
local luasnip = require("luasnip")
require("luasnip/loaders/from_vscode").lazy_load()
require("luasnip").filetype_extend("typescriptreact", { "html" })
local check_backspace = function()
local col = vim.fn.col(".") - 1
return col == 0 or vim.fn.getline("."):sub(col, col):match("%s")
end
local icons = require("user.lspicons")
local source_display = {
dotenv = { icon = icons.misc.Tag, hl_group = "CmpItemKindDefault" },
copilot = { icon = icons.git.Octoface, hl_group = "CmpItemKindCopilot" },
nvim_lsp_signature_help = { icon = icons.ui.SignIn, hl_group = "CmpItemKindInterface" },
buffer = { icon = icons.kind.File, hl_group = "CmpItemKindAbbr" },
path = { icon = icons.ui.Files, hl_group = "CmpItemKindFile" },
emoji = { icon = ":) ", hl_group = "CmpItemKindEmoji" },
treesitter = { icon = icons.ui.Tree, hl_group = "CmpItemKindClass" },
crates = { icon = icons.ui.Package, hl_group = "CmpItemKindCrate" },
tmux = { icon = icons.misc.Dos, hl_group = "CmpItemKindUnit" },
}
cmp.setup({
preselect = cmp.PreselectMode.None,
snippet = {
expand = function(args)
luasnip.lsp_expand(args.body) -- For `luasnip` users.
end,
},
mapping = cmp.mapping.preset.insert({
["<C-k>"] = cmp.mapping(cmp.mapping.select_prev_item(), { "i", "c" }),
["<C-j>"] = cmp.mapping(cmp.mapping.select_next_item(), { "i", "c" }),
["<Down>"] = cmp.mapping(cmp.mapping.select_next_item(), { "i", "c" }),
["<Up>"] = cmp.mapping(cmp.mapping.select_prev_item(), { "i", "c" }),
["<C-b>"] = cmp.mapping(cmp.mapping.scroll_docs(-1), { "i", "c" }),
["<C-f>"] = cmp.mapping(cmp.mapping.scroll_docs(1), { "i", "c" }),
["<C-Space>"] = cmp.mapping(cmp.mapping.complete(), { "i", "c" }),
["<C-e>"] = cmp.mapping({
i = cmp.mapping.abort(),
c = cmp.mapping.close(),
}),
-- Accept currently selected item. If none selected, `select` first item.
-- Set `select` to `false` to only confirm explicitly selected items.
["<CR>"] = cmp.mapping.confirm({ select = true }),
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif luasnip.expandable() then
luasnip.expand()
elseif luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
elseif check_backspace() then
fallback()
else
fallback()
end
end, {
"i",
"s",
}),
["<S-Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif luasnip.jumpable(-1) then
luasnip.jump(-1)
else
fallback()
end
end, {
"i",
"s",
}),
}),
formatting = {
fields = { "kind", "abbr", "menu" },
format = function(entry, vim_item)
if source_display[entry.source.name] ~= nil then
vim_item.kind = source_display[entry.source.name].icon
vim_item.kind_hl_group = source_display[entry.source.name].hl_group
else
vim_item.kind = icons.kind[vim_item.kind]
end
return vim_item
end,
},
sources = {
{ name = "path" },
{ name = "copilot" },
{ name = "luasnip" },
{ name = "nvim_lua" },
{ name = "quick_data" },
{ name = "emoji" },
{ name = "treesitter" },
{ name = "crates" },
{ name = "tmux" },
{ name = "dotenv" },
{ name = "nvim_lsp" },
{ name = "nvim_lsp_document_symbol" },
{ name = "nvim_lsp_signature_help" },
{ name = "buffer" },
},
confirm_opts = {
behavior = cmp.ConfirmBehavior.Replace,
select = false,
},
window = {
completion = cmp.config.window.bordered(),
documentation = cmp.config.window.bordered(),
},
experimental = {
ghost_text = false,
},
sorting = {
comparators = {
cmp.config.compare.offset,
cmp.config.compare.exact,
cmp.config.compare.recently_used,
function(e1, e2)
local clangd_ext_ok, clangd_ext_scores = pcall(require, "clangd_extensions.cmp_scores")
if not clangd_ext_ok then return nil end
return clangd_ext_scores(e1, e2)
end,
cmp.config.compare.kind,
cmp.config.compare.sort_text,
cmp.config.compare.length,
cmp.config.compare.order,
},
},
})
cmp.setup.cmdline({ "/", "?" }, {
mapping = cmp.mapping.preset.cmdline(),
sources = {
{ name = "buffer" },
{
name = "nvim_lsp_document_symbol",
option = {
kinds_to_show = {
foam = {
"Variable",
"Constant",
"Number",
"Boolean",
"Array",
"Object",
"Key",
"Struct",
},
},
},
},
},
})
cmp.setup.cmdline(":", {
mapping = cmp.mapping.preset.cmdline(),
sources = cmp.config.sources({
{ name = "path" },
}, {
{
name = "cmdline",
option = {
ignore_cmds = { "!", "x", "w" },
},
},
}),
})
cmp.setup.cmdline(":'<,'>", {
mapping = cmp.mapping.preset.cmdline(),
sources = cmp.config.sources({
{ name = "path" },
}, {
{
name = "cmdline",
option = {
ignore_cmds = { "!", "x", "w" },
},
},
}),
})
pcall(function()
local function on_confirm_done(...)
local autopairs_cmp_ok, autopairs_cmp = pcall(require, "nvim-autopairs.completion.cmp")
if not autopairs_cmp_ok then return nil end
autopairs_cmp.on_confirm_done()(...)
end
require("cmp").event:off("confirm_done", on_confirm_done)
require("cmp").event:on("confirm_done", on_confirm_done)
end)
end
return M