-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnvim_cmp.lua
104 lines (96 loc) · 3.85 KB
/
nvim_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
-- ╭─────────────────────────────────────────────────────────╮
-- │ Also see: lsp │
-- ╰─────────────────────────────────────────────────────────╯
local snip_engine = require("ak.util").snippets
local cmp = require("cmp")
-- Formatting:
local formatting_style = {
format = function(_, item)
local icon = MiniIcons and MiniIcons.get("lsp", item.kind)
icon = icon and (" " .. icon .. " ") or icon
if icon then item.kind = string.format("%s %s", icon, item.kind) end
return item
end,
}
-- Mappings
local mapping_override = {
["<C-b>"] = cmp.mapping.scroll_docs(-4),
["<C-f>"] = cmp.mapping.scroll_docs(4),
["<C-Space>"] = cmp.mapping.complete(), -- invoke cmp manually
-- Cmp assigns: ctrl-y and ctrl-e.
["<C-j>"] = cmp.mapping.confirm({ select = true }), -- like c-y, easier to type
}
local snippet_jump = { -- not for mini, has its own mapping
luasnip = {
-- TODO: problem on forward when text is "l"?
["<c-l>"] = function()
local luasnip = require("luasnip")
if luasnip.expand_or_locally_jumpable() then luasnip.expand_or_jump() end
end,
["<c-h>"] = function()
local luasnip = require("luasnip")
if luasnip.locally_jumpable(-1) then luasnip.jump(-1) end
end,
},
none = {
["<c-l>"] = function()
if vim.snippet.active({ direction = 1 }) then vim.snippet.jump(1) end
end,
["<c-h>"] = function()
if vim.snippet.active({ direction = -1 }) then vim.snippet.jump(-1) end
end,
},
}
local tmp = snippet_jump[snip_engine]
if tmp then
mapping_override["<c-l>"] = cmp.mapping(tmp["<c-l>"], { "i", "s" })
mapping_override["<c-h>"] = cmp.mapping(tmp["<c-h>"], { "i", "s" })
end
local mapping = cmp.mapping.preset.insert(mapping_override)
-- Sources:
local sources = cmp.config.sources({ -- NOTE: only luasnip has a source
{ name = "nvim_lsp" },
snip_engine == "luasnip" and { name = "luasnip" } or nil,
{ name = "buffer" },
{ name = "path" },
})
-- Snippet expansion:
local snippet = {
expand = function(args)
---@diagnostic disable-next-line: undefined-global
local insert = MiniSnippets.config.expand.insert or MiniSnippets.default_insert
insert({ body = args.body }) -- Insert at cursor
end,
}
if snip_engine == "luasnip" then
snippet["expand"] = function(args) require("luasnip").lsp_expand(args.body) end
elseif snip_engine == "none" then
snippet["expand"] = nil -- defaults to native without expand
end
-- Window
local window = {
-- Default winhighlight = 'Normal:Pmenu,FloatBorder:Pmenu,CursorLine:PmenuSel,Search:None',
completion = cmp.config.window.bordered({
-- Default winhighlight: "Normal:Normal,FloatBorder:FloatBorder,CursorLine:Visual,Search:None"
winhighlight = "Normal:Normal,FloatBorder:None,CursorLine:PmenuSel,Search:None",
}),
-- Default winhighlight = 'FloatBorder:NormalFloat',
documentation = cmp.config.window.bordered({
-- Default winhighlight: "Normal:Normal,FloatBorder:FloatBorder,CursorLine:Visual,Search:None"
winhighlight = "Normal:Normal,FloatBorder:None,CursorLine:PmenuSel,Search:None",
}),
}
-- ---@type cmp.ConfigSchema
local opts = {
formatting = formatting_style,
mapping = mapping,
snippet = snippet,
sources = sources,
completion = { completeopt = "menu,menuone,noinsert" },
experimental = { ghost_text = { hl_group = "CmpGhostText" } },
window = window,
-- performance = { max_view_entries = 15 }, --- there is also sources.max_item_count
-- view = { entries = { follow_cursor = true, }, }, --docs_auto_open
}
cmp.setup(opts)
vim.api.nvim_set_hl(0, "CmpGhostText", { link = "Comment", default = true })