summaryrefslogtreecommitdiff
path: root/start/cmp/lua/cmp_nvim_lsp/source.lua
diff options
context:
space:
mode:
authorMichael Smith <mikesmiffy128@gmail.com>2022-05-22 22:47:23 +0100
committerMichael Smith <mikesmiffy128@gmail.com>2022-05-22 22:47:23 +0100
commit95aea6b533e71e478d61d18fac71cca116c56a4d (patch)
tree1a1f42f723772258cc54551495826768f374474a /start/cmp/lua/cmp_nvim_lsp/source.lua
Add all the plugins I currently use
Diffstat (limited to 'start/cmp/lua/cmp_nvim_lsp/source.lua')
-rw-r--r--start/cmp/lua/cmp_nvim_lsp/source.lua117
1 files changed, 117 insertions, 0 deletions
diff --git a/start/cmp/lua/cmp_nvim_lsp/source.lua b/start/cmp/lua/cmp_nvim_lsp/source.lua
new file mode 100644
index 0000000..d4dd587
--- /dev/null
+++ b/start/cmp/lua/cmp_nvim_lsp/source.lua
@@ -0,0 +1,117 @@
+local source = {}
+
+source.new = function(client)
+ local self = setmetatable({}, { __index = source })
+ self.client = client
+ self.request_ids = {}
+ return self
+end
+
+source.get_debug_name = function(self)
+ return table.concat({ 'nvim_lsp', self.client.name }, ':')
+end
+
+source.is_available = function(self)
+ -- client is stopped.
+ if self.client.is_stopped() then
+ return false
+ end
+
+ -- client is not attached to current buffer.
+ if not vim.lsp.buf_get_clients(vim.api.nvim_get_current_buf())[self.client.id] then
+ return false
+ end
+
+ -- client has no completion capability.
+ if not self:_get(self.client.server_capabilities, { 'completionProvider' }) then
+ return false
+ end
+ return true;
+end
+
+source.get_trigger_characters = function(self)
+ return self:_get(self.client.server_capabilities, { 'completionProvider', 'triggerCharacters' }) or {}
+end
+
+source.complete = function(self, request, callback)
+ local params = vim.lsp.util.make_position_params(0, self.client.offset_encoding)
+ params.context = {}
+ params.context.triggerKind = request.completion_context.triggerKind
+ params.context.triggerCharacter = request.completion_context.triggerCharacter
+
+ self:_request('textDocument/completion', params, function(_, response)
+ callback(response)
+ end)
+end
+
+source.resolve = function(self, completion_item, callback)
+ -- client is stopped.
+ if self.client.is_stopped() then
+ return callback()
+ end
+
+ -- client has no completion capability.
+ if not self:_get(self.client.server_capabilities, { 'completionProvider', 'resolveProvider' }) then
+ return callback()
+ end
+
+ self:_request('completionItem/resolve', completion_item, function(_, response)
+ callback(response or completion_item)
+ end)
+end
+
+source.execute = function(self, completion_item, callback)
+ -- client is stopped.
+ if self.client.is_stopped() then
+ return callback()
+ end
+
+ -- completion_item has no command.
+ if not completion_item.command then
+ return callback()
+ end
+
+ self:_request('workspace/executeCommand', completion_item.command, function(_, _)
+ callback()
+ end)
+end
+
+source._get = function(_, root, paths)
+ local c = root
+ for _, path in ipairs(paths) do
+ c = c[path]
+ if not c then
+ return nil
+ end
+ end
+ return c
+end
+
+source._request = function(self, method, params, callback)
+ if self.request_ids[method] ~= nil then
+ self.client.cancel_request(self.request_ids[method])
+ self.request_ids[method] = nil
+ end
+ local _, request_id
+ _, request_id = self.client.request(method, params, function(arg1, arg2, arg3)
+ if self.request_ids[method] ~= request_id then
+ return
+ end
+ self.request_ids[method] = nil
+
+ -- Text changed, retry
+ if arg1 and arg1.code == -32801 then
+ self:_request(method, params, callback)
+ return
+ end
+
+ if method == arg2 then
+ callback(arg1, arg3) -- old signature
+ else
+ callback(arg1, arg2) -- new signature
+ end
+ end)
+ self.request_ids[method] = request_id
+end
+
+return source