generated from nix-community/kickstart-nix.nvim
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
completion.lua.bkp
163 lines (153 loc) · 4.76 KB
/
completion.lua.bkp
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
if vim.g.did_load_completion_plugin then
return
end
vim.g.did_load_completion_plugin = true
local cmp = require('cmp')
local lspkind = require('lspkind')
local luasnip = require('luasnip')
vim.opt.completeopt = { 'menu', 'menuone', 'noselect' }
local function has_words_before()
local unpack_ = unpack or table.unpack
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
---@param source string|table
local function complete_with_source(source)
if type(source) == 'string' then
cmp.complete { config = { sources = { { name = source } } } }
elseif type(source) == 'table' then
cmp.complete { config = { sources = { source } } }
end
end
cmp.setup {
completion = {
completeopt = 'menu,menuone,noinsert',
-- autocomplete = false,
},
formatting = {
format = lspkind.cmp_format {
mode = 'symbol_text',
with_text = true,
maxwidth = 50, -- prevent the popup from showing more than provided characters (e.g 50 will not show more than 50 characters)
ellipsis_char = '...', -- when popup menu exceed maxwidth, the truncated part would show ellipsis_char instead (must define maxwidth first)
menu = {
buffer = '[BUF]',
nvim_lsp = '[LSP]',
nvim_lsp_signature_help = '[LSP]',
nvim_lsp_document_symbol = '[LSP]',
nvim_lua = '[API]',
path = '[PATH]',
luasnip = '[SNIP]',
},
},
},
snippet = {
expand = function(args)
require('luasnip').lsp_expand(args.body) -- For `luasnip` users.
end,
},
mapping = {
['<C-b>'] = cmp.mapping(function(_)
if cmp.visible() then
cmp.scroll_docs(-4)
else
complete_with_source('buffer')
end
end, { 'i', 'c', 's' }),
['<C-f>'] = cmp.mapping(function(_)
if cmp.visible() then
cmp.scroll_docs(4)
else
complete_with_source('path')
end
end, { 'i', 'c', 's' }),
['<C-n>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
-- expand_or_jumpable(): Jump outside the snippet region
-- expand_or_locally_jumpable(): Only jump inside the snippet region
elseif luasnip.expand_or_locally_jumpable() then
luasnip.expand_or_jump()
elseif has_words_before() then
cmp.complete()
else
fallback()
end
end, { 'i', 'c', 's' }),
['<C-p>'] = 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', 'c', 's' }),
-- toggle completion
['<C-e>'] = cmp.mapping(function(_)
if cmp.visible() then
cmp.close()
else
cmp.complete()
end
end, { 'i', 'c', 's' }),
['<C-y>'] = cmp.mapping.confirm {
select = true,
},
},
sources = cmp.config.sources {
-- The insertion order influences the priority of the sources
{ name = 'nvim_lsp', keyword_length = 3 },
{ name = 'nvim_lsp_signature_help', keyword_length = 3 },
{ name = 'buffer' },
{ name = 'path' },
},
enabled = function()
return vim.bo[0].buftype ~= 'prompt'
end,
experimental = {
native_menu = false,
ghost_text = true,
},
}
cmp.setup.filetype('lua', {
sources = cmp.config.sources {
{ name = 'nvim_lua' },
{ name = 'nvim_lsp', keyword_length = 3 },
{ name = 'path' },
},
})
-- Use buffer source for `/` and `?` (if you enabled `native_menu`, this won't work anymore).
cmp.setup.cmdline({ '/', '?' }, {
mapping = cmp.mapping.preset.cmdline(),
sources = {
{ name = 'nvim_lsp_document_symbol', keyword_length = 3 },
{ name = 'buffer' },
{ name = 'cmdline_history' },
},
view = {
entries = { name = 'wildmenu', separator = '|' },
},
})
-- Use cmdline & path source for ':' (if you enabled `native_menu`, this won't work anymore).
cmp.setup.cmdline(':', {
mapping = cmp.mapping.preset.cmdline(),
sources = cmp.config.sources {
{ name = 'cmdline' },
{ name = 'cmdline_history' },
{ name = 'path' },
},
})
vim.keymap.set({ 'i', 'c', 's' }, '<C-n>', cmp.complete, { noremap = false, desc = '[cmp] complete' })
vim.keymap.set({ 'i', 'c', 's' }, '<C-f>', function()
complete_with_source('path')
end, { noremap = false, desc = '[cmp] path' })
vim.keymap.set({ 'i', 'c', 's' }, '<C-o>', function()
complete_with_source('nvim_lsp')
end, { noremap = false, desc = '[cmp] lsp' })
vim.keymap.set({ 'c' }, '<C-h>', function()
complete_with_source('cmdline_history')
end, { noremap = false, desc = '[cmp] cmdline history' })
vim.keymap.set({ 'c' }, '<C-c>', function()
complete_with_source('cmdline')
end, { noremap = false, desc = '[cmp] cmdline' })