-
Notifications
You must be signed in to change notification settings - Fork 3
/
ddc.lua
180 lines (163 loc) · 4.98 KB
/
ddc.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
local M = {}
local function expand_snippet()
local completeditem = vim.api.nvim_get_var('pum#completed_item')
if completeditem.__sourceName == 'ultisnips' and vim.call('UltiSnips#CanExpandSnippet') == 1 then
vim.call('UltiSnips#ExpandSnippet')
end
end
local function autoimport_nvim_lsp(buf)
local completeditem = vim.api.nvim_get_var('pum#completed_item')
local active_clients = vim.lsp.get_active_clients({ bufnr = buf })
if completeditem.__sourceName == 'nvim-lsp' then
for _, client in pairs(active_clients) do
if
client.server_capabilities.completionProvider
and client.server_capabilities.completionProvider.resolveProvider
and completeditem.user_data
and completeditem.user_data.lspitem
then
-- Only if there is lspitem property inside g:pum#completed_item
-- we parse the json data
local lspitem = completeditem.user_data.lspitem
local completed_item = vim.fn.json_decode(lspitem)
-- Apply text edits if it's available
local resolve_fn = function(_, response)
if response and response.additionalTextEdits then
vim.lsp.util.apply_text_edits(response.additionalTextEdits, buf, 'utf-8')
end
end
client.request('completionItem/resolve', completed_item, resolve_fn, buf)
break
end
end
end
end
---Register autoimporting feature from LSP server routed to pum.vim
---@param ev table
---@return nil
local function on_pum_completion(ev)
expand_snippet()
autoimport_nvim_lsp(ev.buf)
end
---Register events related to ddc.vim/pum.vim
---@return nil
local function register_events()
vim.api.nvim_create_autocmd('ColorScheme', {
group = vim.g.user.event,
callback = function()
vim.api.nvim_set_hl(0, 'Pmenu', { bg = 'NONE' })
end,
desc = 'Transparent bg for completion menu',
})
vim.api.nvim_create_autocmd('User', {
pattern = 'PumCompleteDone',
group = vim.g.user.event,
callback = on_pum_completion,
desc = 'Autoimport/Expand snippet via pum.vim',
})
end
---Register keymaps related to ddc.vim/pum.vim
---@return nil
local function register_keymaps()
vim.keymap.set('i', '<C-n>', '<Cmd>call pum#map#insert_relative(1)<CR>')
vim.keymap.set('i', '<C-p>', '<Cmd>call pum#map#insert_relative(-1)<CR>')
vim.keymap.set('i', '<C-e>', '<Cmd>call pum#map#cancel()<CR>')
vim.keymap.set('i', '<C-y>', '<Cmd>call pum#map#confirm()<CR>')
-- Manually open the completion menu
vim.keymap.set(
'i',
'<C-Space>',
'ddc#map#manual_complete()',
{ replace_keycodes = false, expr = true, desc = '[ddc.vim] Manually open popup menu' }
)
end
function M.setup()
vim.call('ddc#custom#patch_global', {
sources = { 'nvim-lsp', 'around', 'buffer', 'ultisnips' },
autoCompleteDelay = 100,
backspaceCompletion = true,
ui = 'pum',
sourceOptions = {
['_'] = {
matchers = { 'matcher_fuzzy' },
sorters = { 'sorter_fuzzy' },
converters = { 'converter_fuzzy' },
},
cmdline = {
mark = 'CMD',
maxItems = 10,
ignoreCase = true,
},
['nvim-lsp'] = {
mark = 'LS',
forceCompletionPattern = [[\.\w*|:\w*|->\w*]],
maxItems = 10,
ignoreCase = true,
minAutoCompleteLength = 1,
},
ultisnips = {
mark = 'S',
maxItems = 5,
ignoreCase = true,
},
around = {
mark = 'A',
maxItems = 5,
ignoreCase = true,
},
buffer = {
mark = 'B',
maxItems = 5,
ignoreCase = true,
},
},
sourceParams = {
['nvim-lsp'] = {
kindLabels = {
Class = ' Class',
Color = ' Color',
Constant = ' Constant',
Constructor = ' New',
Enum = ' Enum',
EnumMember = ' Enum',
Event = ' Event',
Field = ' Field',
File = ' File',
Folder = ' Directory',
Function = ' Function',
Interface = ' Interface',
Keyword = ' Key',
Method = ' Method',
Module = ' Module',
Operator = ' Operator',
Property = ' Property',
Reference = ' Reference',
Snippet = ' Snippet',
Struct = ' Struct',
Text = ' Text',
TypeParameter = '',
Unit = ' Unit',
Value = ' Value',
Variable = ' Variable',
},
},
},
})
-- Markdown FileType completion sources
vim.call('ddc#custom#patch_filetype', 'markdown', { sources = { 'around', 'buffer' } })
-- pum.vim Config
-- ---
vim.call('pum#set_option', {
border = 'rounded',
padding = true,
scrollbar_char = '',
offset_row = vim.opt.cmdheight:get() + 1, -- cmdheight + statusline height
max_height = 15,
max_width = 80,
})
register_keymaps()
register_events()
vim.call('ddc#enable')
vim.call('signature_help#enable')
end
return M