-
Notifications
You must be signed in to change notification settings - Fork 53
/
init.lua
138 lines (122 loc) · 4.18 KB
/
init.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
local source = require('cmp_nvim_lsp.source')
local M = {}
---Registered client and source mapping.
M.client_source_map = {}
---Setup cmp-nvim-lsp source.
M.setup = function()
vim.api.nvim_create_autocmd('InsertEnter', {
group = vim.api.nvim_create_augroup('cmp_nvim_lsp', { clear = true }),
pattern = '*',
callback = M._on_insert_enter
})
end
local if_nil = function(val, default)
if val == nil then return default end
return val
end
-- Backported from vim.deprecate (0.9.0+)
local function deprecate(name, alternative, version, plugin, backtrace)
local message = name .. ' is deprecated'
plugin = plugin or 'Nvim'
message = alternative and (message .. ', use ' .. alternative .. ' instead.') or message
message = message
.. ' See :h deprecated\nThis function will be removed in '
.. plugin
.. ' version '
.. version
if vim.notify_once(message, vim.log.levels.WARN) and backtrace ~= false then
vim.notify(debug.traceback('', 2):sub(2), vim.log.levels.WARN)
end
end
M.default_capabilities = function(override)
override = override or {}
return {
textDocument = {
completion = {
dynamicRegistration = if_nil(override.dynamicRegistration, false),
completionItem = {
snippetSupport = if_nil(override.snippetSupport, true),
commitCharactersSupport = if_nil(override.commitCharactersSupport, true),
deprecatedSupport = if_nil(override.deprecatedSupport, true),
preselectSupport = if_nil(override.preselectSupport, true),
tagSupport = if_nil(override.tagSupport, {
valueSet = {
1, -- Deprecated
}
}),
insertReplaceSupport = if_nil(override.insertReplaceSupport, true),
resolveSupport = if_nil(override.resolveSupport, {
properties = {
"documentation",
"additionalTextEdits",
"insertTextFormat",
"insertTextMode",
"command",
},
}),
insertTextModeSupport = if_nil(override.insertTextModeSupport, {
valueSet = {
1, -- asIs
2, -- adjustIndentation
}
}),
labelDetailsSupport = if_nil(override.labelDetailsSupport, true),
},
contextSupport = if_nil(override.snippetSupport, true),
insertTextMode = if_nil(override.insertTextMode, 1),
completionList = if_nil(override.completionList, {
itemDefaults = {
'commitCharacters',
'editRange',
'insertTextFormat',
'insertTextMode',
'data',
}
})
},
},
}
end
---Backwards compatibility
M.update_capabilities = function(_, override)
local _deprecate = vim.deprecate or deprecate
_deprecate('cmp_nvim_lsp.update_capabilities', 'cmp_nvim_lsp.default_capabilities', '1.0.0', 'cmp-nvim-lsp')
return M.default_capabilities(override)
end
---Refresh sources on InsertEnter.
M._on_insert_enter = function()
local cmp = require('cmp')
local allowed_clients = {}
local get_clients = (
vim.lsp.get_clients ~= nil and vim.lsp.get_clients -- nvim 0.10+
or vim.lsp.get_active_clients
)
-- register all active clients.
for _, client in ipairs(get_clients()) do
allowed_clients[client.id] = client
if not M.client_source_map[client.id] then
local s = source.new(client)
if s:is_available() then
M.client_source_map[client.id] = cmp.register_source('nvim_lsp', s)
end
end
end
-- register all buffer clients (early register before activation)
for _, client in ipairs(get_clients({ bufnr = 0 })) do
allowed_clients[client.id] = client
if not M.client_source_map[client.id] then
local s = source.new(client)
if s:is_available() then
M.client_source_map[client.id] = cmp.register_source('nvim_lsp', s)
end
end
end
-- unregister stopped/detached clients.
for client_id, source_id in pairs(M.client_source_map) do
if not allowed_clients[client_id] or allowed_clients[client_id]:is_stopped() then
cmp.unregister_source(source_id)
M.client_source_map[client_id] = nil
end
end
end
return M