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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
local M = {}
M.methods = {}
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
M.methods.has_words_before = has_words_before
---@deprecated use M.methods.has_words_before instead
M.methods.check_backspace = function()
return not has_words_before()
end
local T = function(str)
return vim.api.nvim_replace_termcodes(str, true, true, true)
end
local function feedkeys(key, mode)
vim.api.nvim_feedkeys(T(key), mode, true)
end
M.methods.feedkeys = feedkeys
---when inside a snippet, seeks to the nearest luasnip field if possible, and checks if it is jumpable
---@param dir number 1 for forward, -1 for backward; defaults to 1
---@return boolean true if a jumpable luasnip field is found while inside a snippet
local function jumpable(dir)
local luasnip_ok, luasnip = pcall(require, "luasnip")
if not luasnip_ok then
return false
end
local win_get_cursor = vim.api.nvim_win_get_cursor
local get_current_buf = vim.api.nvim_get_current_buf
---sets the current buffer's luasnip to the one nearest the cursor
---@return boolean true if a node is found, false otherwise
local function seek_luasnip_cursor_node()
-- TODO(kylo252): upstream this
-- for outdated versions of luasnip
if not luasnip.session.current_nodes then
return false
end
local node = luasnip.session.current_nodes[get_current_buf()]
if not node then
return false
end
local snippet = node.parent.snippet
local exit_node = snippet.insert_nodes[0]
local pos = win_get_cursor(0)
pos[1] = pos[1] - 1
-- exit early if we're past the exit node
if exit_node then
local exit_pos_end = exit_node.mark:pos_end()
if (pos[1] > exit_pos_end[1]) or (pos[1] == exit_pos_end[1] and pos[2] > exit_pos_end[2]) then
snippet:remove_from_jumplist()
luasnip.session.current_nodes[get_current_buf()] = nil
return false
end
end
node = snippet.inner_first:jump_into(1, true)
while node ~= nil and node.next ~= nil and node ~= snippet do
local n_next = node.next
local next_pos = n_next and n_next.mark:pos_begin()
local candidate = n_next ~= snippet and next_pos and (pos[1] < next_pos[1])
or (pos[1] == next_pos[1] and pos[2] < next_pos[2])
-- Past unmarked exit node, exit early
if n_next == nil or n_next == snippet.next then
snippet:remove_from_jumplist()
luasnip.session.current_nodes[get_current_buf()] = nil
return false
end
if candidate then
luasnip.session.current_nodes[get_current_buf()] = node
return true
end
local ok
ok, node = pcall(node.jump_from, node, 1, true) -- no_move until last stop
if not ok then
snippet:remove_from_jumplist()
luasnip.session.current_nodes[get_current_buf()] = nil
return false
end
end
-- No candidate, but have an exit node
if exit_node then
-- to jump to the exit node, seek to snippet
luasnip.session.current_nodes[get_current_buf()] = snippet
return true
end
-- No exit node, exit from snippet
snippet:remove_from_jumplist()
luasnip.session.current_nodes[get_current_buf()] = nil
return false
end
if dir == -1 then
return luasnip.in_snippet() and luasnip.jumpable(-1)
else
return luasnip.in_snippet() and seek_luasnip_cursor_node() and luasnip.jumpable(1)
end
end
M.methods.jumpable = jumpable
M.config = function()
local status_cmp_ok, cmp = pcall(require, "cmp")
if not status_cmp_ok then
return
end
local status_luasnip_ok, luasnip = pcall(require, "luasnip")
if not status_luasnip_ok then
return
end
lvim.builtin.cmp = {
confirm_opts = {
behavior = cmp.ConfirmBehavior.Replace,
select = false,
},
completion = {
---@usage The minimum length of a word to complete on.
keyword_length = 1,
},
experimental = {
ghost_text = false,
native_menu = false,
},
formatting = {
fields = { "kind", "abbr", "menu" },
max_width = 0,
kind_icons = lvim.icons.kind,
source_names = {
nvim_lsp = "(LSP)",
emoji = "(Emoji)",
path = "(Path)",
calc = "(Calc)",
cmp_tabnine = "(Tabnine)",
vsnip = "(Snippet)",
luasnip = "(Snippet)",
buffer = "(Buffer)",
tmux = "(TMUX)",
copilot = "(Copilot)",
},
duplicates = {
buffer = 1,
path = 1,
nvim_lsp = 0,
luasnip = 1,
},
duplicates_default = 0,
format = function(entry, vim_item)
local max_width = lvim.builtin.cmp.formatting.max_width
if max_width ~= 0 and #vim_item.abbr > max_width then
vim_item.abbr = string.sub(vim_item.abbr, 1, max_width - 1) .. lvim.icons.ui.Ellipsis
end
if lvim.use_icons then
vim_item.kind = lvim.builtin.cmp.formatting.kind_icons[vim_item.kind]
-- TODO: not sure why I can't put this anywhere else
vim.api.nvim_set_hl(0, "CmpItemKindCopilot", { fg = "#6CC644" })
if entry.source.name == "copilot" then
vim_item.kind = lvim.icons.git.Octoface
vim_item.kind_hl_group = "CmpItemKindCopilot"
end
vim.api.nvim_set_hl(0, "CmpItemKindTabnine", { fg = "#CA42F0" })
if entry.source.name == "cmp_tabnine" then
vim_item.kind = lvim.icons.misc.Robot
vim_item.kind_hl_group = "CmpItemKindTabnine"
end
vim.api.nvim_set_hl(0, "CmpItemKindCrate", { fg = "#F64D00" })
if entry.source.name == "crates" then
vim_item.kind = lvim.icons.misc.Package
vim_item.kind_hl_group = "CmpItemKindCrate"
end
if entry.source.name == "lab.quick_data" then
vim_item.kind = lvim.icons.misc.CircuitBoard
vim_item.kind_hl_group = "CmpItemKindConstant"
end
vim.api.nvim_set_hl(0, "CmpItemKindEmoji", { fg = "#FDE030" })
if entry.source.name == "emoji" then
vim_item.kind = lvim.icons.misc.Smiley
vim_item.kind_hl_group = "CmpItemKindEmoji"
end
end
vim_item.menu = lvim.builtin.cmp.formatting.source_names[entry.source.name]
vim_item.dup = lvim.builtin.cmp.formatting.duplicates[entry.source.name]
or lvim.builtin.cmp.formatting.duplicates_default
return vim_item
end,
},
snippet = {
expand = function(args)
require("luasnip").lsp_expand(args.body)
end,
},
window = {
completion = cmp.config.window.bordered(),
documentation = cmp.config.window.bordered(),
},
sources = {
{
name = "copilot",
-- keyword_length = 0,
max_item_count = 3,
trigger_characters = {
{
".",
":",
"(",
"'",
'"',
"[",
",",
"#",
"*",
"@",
"|",
"=",
"-",
"{",
"/",
"\\",
"+",
"?",
" ",
-- "\t",
-- "\n",
},
},
},
{ name = "nvim_lsp" },
{ name = "path" },
{ name = "luasnip" },
{ name = "cmp_tabnine" },
{ name = "nvim_lua" },
{ name = "buffer" },
{ name = "calc" },
{ name = "emoji" },
{ name = "treesitter" },
{ name = "crates" },
{ name = "tmux" },
},
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 { behavior = cmp.SelectBehavior.Select }, { "i" }),
["<Up>"] = cmp.mapping(cmp.mapping.select_prev_item { behavior = cmp.SelectBehavior.Select }, { "i" }),
["<C-d>"] = cmp.mapping.scroll_docs(-4),
["<C-f>"] = cmp.mapping.scroll_docs(4),
["<C-y>"] = cmp.mapping {
i = cmp.mapping.confirm { behavior = cmp.ConfirmBehavior.Replace, select = false },
c = function(fallback)
if cmp.visible() then
cmp.confirm { behavior = cmp.ConfirmBehavior.Replace, select = false }
else
fallback()
end
end,
},
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif luasnip.expand_or_locally_jumpable() then
luasnip.expand_or_jump()
elseif jumpable(1) then
luasnip.jump(1)
elseif has_words_before() then
-- cmp.complete()
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" }),
["<C-Space>"] = cmp.mapping.complete(),
["<C-e>"] = cmp.mapping.abort(),
["<CR>"] = cmp.mapping(function(fallback)
if cmp.visible() then
local confirm_opts = vim.deepcopy(lvim.builtin.cmp.confirm_opts) -- avoid mutating the original opts below
local is_insert_mode = function()
return vim.api.nvim_get_mode().mode:sub(1, 1) == "i"
end
if is_insert_mode() then -- prevent overwriting brackets
confirm_opts.behavior = cmp.ConfirmBehavior.Insert
end
if cmp.confirm(confirm_opts) then
return -- success, exit early
end
end
if jumpable(1) and luasnip.jump(1) then
return -- success, exit early
end
fallback() -- if not exited early, always fallback
end),
},
}
end
function M.setup()
local cmp = require "cmp"
cmp.setup(lvim.builtin.cmp)
cmp.setup.cmdline(":", {
mapping = cmp.mapping.preset.cmdline(),
sources = {
{ name = "path" },
},
})
cmp.setup.cmdline({ "/", "?" }, {
mapping = cmp.mapping.preset.cmdline(),
sources = {
{ name = "buffer" },
},
})
end
return M
|