/
cmp.lua
149 lines (140 loc) · 3.87 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
return {
"hrsh7th/nvim-cmp",
dependencies = {
"hrsh7th/cmp-nvim-lsp",
"hrsh7th/cmp-buffer",
"hrsh7th/cmp-path",
"hrsh7th/cmp-nvim-lua",
"saadparwaiz1/cmp_luasnip",
"onsails/lspkind.nvim",
"L3MON4D3/LuaSnip",
},
cmd = { "CmpInfo" },
event = "InsertEnter *",
config = function()
local p = require("p")
local cmp = require("cmp")
local luasnip = require("luasnip")
local MAX_INDEX_FILE_SIZE = 4000
local has_words_before = function()
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
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(function(fallback)
if luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
elseif cmp.visible() then
cmp.select_next_item()
elseif has_words_before() then
cmp.complete()
else
fallback()
end
end, {
"i",
"s",
}),
["<S-Tab>"] = cmp.mapping(function(fallback)
if luasnip.jumpable(-1) then
luasnip.jump(-1)
elseif cmp.visible() then
cmp.select_prev_item()
else
fallback()
end
end, {
"i",
"s",
}),
})
local formatting = {}
p.require(
"lspkind",
function(lspkind)
formatting.format = lspkind.cmp_format({
mode = "symbol",
symbol_map = {
Copilot = " ",
Class = " ",
Color = " ",
Constant = " ",
Constructor = " ",
Enum = " ",
EnumMember = " ",
Event = "",
Field = " ",
File = " ",
Folder = " ",
Function = " ",
Interface = " ",
Keyword = " ",
Method = " ",
Module = " ",
Operator = " ",
Property = " ",
Reference = " ",
Snippet = " ",
Struct = " ",
Text = " ",
TypeParameter = " ",
Unit = "",
Value = " ",
Variable = " ",
},
menu = {
buffer = "[buf]",
nvim_lsp = "[LSP]",
nvim_lua = "[api]",
path = "[path]",
luasnip = "[snip]",
},
})
end
)
cmp.setup({
mapping = mapping,
formatting = formatting,
sources = {
{ name = "copilot" },
{ name = "crates" },
{ 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,
},
},
},
snippet = {
expand = function(args) luasnip.lsp_expand(args.body) end,
},
experimental = {
native_menu = false,
},
})
vim.api.nvim_create_user_command("CmpInfo", function() cmp.status() end, {})
end,
}