summaryrefslogtreecommitdiff
path: root/lua/blink/cmp/sources/lib/init.lua
blob: 9b56cf5ecc20a265045bf14df95c241833e42b68 (plain)
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
local async = require('blink.cmp.lib.async')
local config = require('blink.cmp.config')

--- @class blink.cmp.Sources
--- @field completions_queue blink.cmp.SourcesQueue | nil
--- @field current_signature_help blink.cmp.Task | nil
--- @field sources_registered boolean
--- @field providers table<string, blink.cmp.SourceProvider>
--- @field completions_emitter blink.cmp.EventEmitter<blink.cmp.SourceCompletionsEvent>
---
--- @field get_all_providers fun(): blink.cmp.SourceProvider[]
--- @field get_enabled_provider_ids fun(mode: blink.cmp.Mode): string[]
--- @field get_enabled_providers fun(mode: blink.cmp.Mode): table<string, blink.cmp.SourceProvider>
--- @field get_provider_by_id fun(id: string): blink.cmp.SourceProvider
--- @field get_trigger_characters fun(mode: blink.cmp.Mode): string[]
---
--- @field emit_completions fun(context: blink.cmp.Context, responses: table<string, blink.cmp.CompletionResponse>)
--- @field request_completions fun(context: blink.cmp.Context)
--- @field cancel_completions fun()
--- @field apply_max_items_for_completions fun(context: blink.cmp.Context, items: blink.cmp.CompletionItem[]): blink.cmp.CompletionItem[]
--- @field listen_on_completions fun(callback: fun(context: blink.cmp.Context, items: blink.cmp.CompletionItem[]))
--- @field resolve fun(context: blink.cmp.Context, item: blink.cmp.CompletionItem): blink.cmp.Task
--- @field execute fun(context: blink.cmp.Context, item: blink.cmp.CompletionItem): blink.cmp.Task
---
--- @field get_signature_help_trigger_characters fun(mode: blink.cmp.Mode): { trigger_characters: string[], retrigger_characters: string[] }
--- @field get_signature_help fun(context: blink.cmp.SignatureHelpContext, callback: fun(signature_help: lsp.SignatureHelp | nil))
--- @field cancel_signature_help fun()
---
--- @field reload fun(provider?: string)
--- @field get_lsp_capabilities fun(override?: lsp.ClientCapabilities, include_nvim_defaults?: boolean): lsp.ClientCapabilities

--- @class blink.cmp.SourceCompletionsEvent
--- @field context blink.cmp.Context
--- @field items table<string, blink.cmp.CompletionItem[]>

--- @type blink.cmp.Sources
--- @diagnostic disable-next-line: missing-fields
local sources = {
  completions_queue = nil,
  providers = {},
  completions_emitter = require('blink.cmp.lib.event_emitter').new('source_completions', 'BlinkCmpSourceCompletions'),
}

function sources.get_all_providers()
  local providers = {}
  for provider_id, _ in pairs(config.sources.providers) do
    providers[provider_id] = sources.get_provider_by_id(provider_id)
  end
  return providers
end

function sources.get_enabled_provider_ids(mode)
  local enabled_providers = mode ~= 'default' and config.sources[mode]
    or config.sources.per_filetype[vim.bo.filetype]
    or config.sources.default
  if type(enabled_providers) == 'function' then return enabled_providers() end
  --- @cast enabled_providers string[]
  return enabled_providers
end

function sources.get_enabled_providers(mode)
  local mode_providers = sources.get_enabled_provider_ids(mode)

  --- @type table<string, blink.cmp.SourceProvider>
  local providers = {}
  for _, provider_id in ipairs(mode_providers) do
    local provider = sources.get_provider_by_id(provider_id)
    if provider:enabled() then providers[provider_id] = sources.get_provider_by_id(provider_id) end
  end
  return providers
end

function sources.get_provider_by_id(provider_id)
  -- TODO: remove in v1.0
  if not sources.providers[provider_id] and provider_id == 'luasnip' then
    error(
      "Luasnip has been moved to the `snippets` source, alongside a new preset system (`snippets.preset = 'luasnip'`). See the documentation for more information."
    )
  end

  assert(
    sources.providers[provider_id] ~= nil or config.sources.providers[provider_id] ~= nil,
    'Requested provider "'
      .. provider_id
      .. '" has not been configured. Available providers: '
      .. vim.fn.join(vim.tbl_keys(sources.providers), ', ')
  )

  -- initialize the provider if it hasn't been initialized yet
  if not sources.providers[provider_id] then
    local provider_config = config.sources.providers[provider_id]
    sources.providers[provider_id] = require('blink.cmp.sources.lib.provider').new(provider_id, provider_config)
  end

  return sources.providers[provider_id]
end

--- Completion ---

function sources.get_trigger_characters(mode)
  local providers = sources.get_enabled_providers(mode)
  local trigger_characters = {}
  for _, provider in pairs(providers) do
    vim.list_extend(trigger_characters, provider:get_trigger_characters())
  end
  return trigger_characters
end

function sources.emit_completions(context, _items_by_provider)
  local items_by_provider = {}
  for id, items in pairs(_items_by_provider) do
    if sources.providers[id]:should_show_items(context, items) then items_by_provider[id] = items end
  end
  sources.completions_emitter:emit({ context = context, items = items_by_provider })
end

function sources.request_completions(context)
  -- create a new context if the id changed or if we haven't created one yet
  if sources.completions_queue == nil or context.id ~= sources.completions_queue.id then
    if sources.completions_queue ~= nil then sources.completions_queue:destroy() end
    sources.completions_queue =
      require('blink.cmp.sources.lib.queue').new(context, sources.get_all_providers(), sources.emit_completions)
  -- send cached completions if they exist to immediately trigger updates
  elseif sources.completions_queue:get_cached_completions() ~= nil then
    sources.emit_completions(
      context,
      --- @diagnostic disable-next-line: param-type-mismatch
      sources.completions_queue:get_cached_completions()
    )
  end

  sources.completions_queue:get_completions(context)
end

function sources.cancel_completions()
  if sources.completions_queue ~= nil then
    sources.completions_queue:destroy()
    sources.completions_queue = nil
  end
end

--- Limits the number of items per source as configured
function sources.apply_max_items_for_completions(context, items)
  -- get the configured max items for each source
  local total_items_for_sources = {}
  local max_items_for_sources = {}
  for id, source in pairs(sources.providers) do
    max_items_for_sources[id] = source.config.max_items(context, items)
    total_items_for_sources[id] = 0
  end

  -- no max items configured, return as-is
  if #vim.tbl_keys(max_items_for_sources) == 0 then return items end

  -- apply max items
  local filtered_items = {}
  for _, item in ipairs(items) do
    local max_items = max_items_for_sources[item.source_id]
    total_items_for_sources[item.source_id] = total_items_for_sources[item.source_id] + 1
    if max_items == nil or total_items_for_sources[item.source_id] <= max_items then
      table.insert(filtered_items, item)
    end
  end
  return filtered_items
end

--- Resolve ---

function sources.resolve(context, item)
  --- @type blink.cmp.SourceProvider?
  local item_source = nil
  for _, source in pairs(sources.providers) do
    if source.id == item.source_id then
      item_source = source
      break
    end
  end
  if item_source == nil then
    return async.task.new(function(resolve) resolve(item) end)
  end

  return item_source
    :resolve(context, item)
    :catch(function(err) vim.print('failed to resolve item with error: ' .. err) end)
end

--- Execute ---

function sources.execute(context, item)
  local item_source = nil
  for _, source in pairs(sources.providers) do
    if source.id == item.source_id then
      item_source = source
      break
    end
  end
  if item_source == nil then
    return async.task.new(function(resolve) resolve() end)
  end

  return item_source
    :execute(context, item)
    :catch(function(err) vim.print('failed to execute item with error: ' .. err) end)
end

--- Signature help ---

function sources.get_signature_help_trigger_characters(mode)
  local trigger_characters = {}
  local retrigger_characters = {}

  -- todo: should this be all sources? or should it follow fallbacks?
  for _, source in pairs(sources.get_enabled_providers(mode)) do
    local res = source:get_signature_help_trigger_characters()
    vim.list_extend(trigger_characters, res.trigger_characters)
    vim.list_extend(retrigger_characters, res.retrigger_characters)
  end
  return { trigger_characters = trigger_characters, retrigger_characters = retrigger_characters }
end

function sources.get_signature_help(context, callback)
  local tasks = {}
  for _, source in pairs(sources.providers) do
    table.insert(tasks, source:get_signature_help(context))
  end

  sources.current_signature_help = async.task.await_all(tasks):map(function(signature_helps)
    signature_helps = vim.tbl_filter(function(signature_help) return signature_help ~= nil end, signature_helps)
    callback(signature_helps[1])
  end)
end

function sources.cancel_signature_help()
  if sources.current_signature_help ~= nil then
    sources.current_signature_help:cancel()
    sources.current_signature_help = nil
  end
end

--- Misc ---

--- For external integrations to force reloading the source
function sources.reload(provider)
  -- Reload specific provider
  if provider ~= nil then
    assert(type(provider) == 'string', 'Expected string for provider')
    assert(
      sources.providers[provider] ~= nil or config.sources.providers[provider] ~= nil,
      'Source ' .. provider .. ' does not exist'
    )
    if sources.providers[provider] ~= nil then sources.providers[provider]:reload() end
    return
  end

  -- Reload all providers
  for _, source in pairs(sources.providers) do
    source:reload()
  end
end

function sources.get_lsp_capabilities(override, include_nvim_defaults)
  return vim.tbl_deep_extend('force', include_nvim_defaults and vim.lsp.protocol.make_client_capabilities() or {}, {
    textDocument = {
      completion = {
        completionItem = {
          snippetSupport = true,
          commitCharactersSupport = false, -- todo:
          documentationFormat = { 'markdown', 'plaintext' },
          deprecatedSupport = true,
          preselectSupport = false, -- todo:
          tagSupport = { valueSet = { 1 } }, -- deprecated
          insertReplaceSupport = true, -- todo:
          resolveSupport = {
            properties = {
              'documentation',
              'detail',
              'additionalTextEdits',
              -- todo: support more properties? should test if it improves latency
            },
          },
          insertTextModeSupport = {
            -- todo: support adjustIndentation
            valueSet = { 1 }, -- asIs
          },
          labelDetailsSupport = true,
        },
        completionList = {
          itemDefaults = {
            'commitCharacters',
            'editRange',
            'insertTextFormat',
            'insertTextMode',
            'data',
          },
        },

        contextSupport = true,
        insertTextMode = 1, -- asIs
      },
    },
  }, override or {})
end

return sources