From 6120652fb6d44db238eeedd56a4c353799a03d2d Mon Sep 17 00:00:00 2001 From: Tomas Slusny Date: Tue, 28 May 2024 23:11:07 +0200 Subject: [PATCH] feat: Add nvim-cmp integration --- README.md | 24 +++++++++++++++ lua/CopilotChat/integrations/cmp.lua | 45 ++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 lua/CopilotChat/integrations/cmp.lua diff --git a/README.md b/README.md index 7c60cc84..34efe370 100644 --- a/README.md +++ b/README.md @@ -501,6 +501,30 @@ Requires [fzf-lua](https://github.com/ibhagwan/fzf-lua) plugin to be installed. +
+nvim-cmp integration + +Requires [nvim-cmp](https://github.com/hrsh7th/nvim-cmp) plugin to be installed (and properly configured). + +```lua +-- Registers copilot-chat source and enables it for copilot-chat filetype (so copilot chat window) +require("CopilotChat.integrations.cmp").setup() + +-- You might also want to disable default complete mapping for copilot chat when doing this +require('CopilotChat').setup({ + mappings = { + complete = { + insert = '', + }, + }, + -- rest of your config +}) +``` + +![image](https://github.com/CopilotC-Nvim/CopilotChat.nvim/assets/5115805/063fc99f-a4b2-4187-a065-0fdd287ebee2) + +
+ ## Roadmap (Wishlist) - Use indexed vector database with current workspace for better context selection diff --git a/lua/CopilotChat/integrations/cmp.lua b/lua/CopilotChat/integrations/cmp.lua new file mode 100644 index 00000000..5008395d --- /dev/null +++ b/lua/CopilotChat/integrations/cmp.lua @@ -0,0 +1,45 @@ +local cmp = require('cmp') +local chat = require('CopilotChat') + +local Source = {} + +function Source:get_trigger_characters() + return { '@', '/' } +end + +function Source:complete(params, callback) + local items = {} + local prompts_to_use = chat.prompts() + + if params.completion_context.triggerCharacter == '/' then + for name, _ in pairs(prompts_to_use) do + items[#items + 1] = { + label = '/' .. name, + } + end + else + items[#items + 1] = { + label = '@buffers', + } + + items[#items + 1] = { + label = '@buffer', + } + end + + callback({ items = items }) +end + +local M = {} + +--- Setup the nvim-cmp source for copilot-chat window +function M.setup() + cmp.register_source('copilot-chat', Source) + cmp.setup.filetype('copilot-chat', { + sources = { + { name = 'copilot-chat' }, + }, + }) +end + +return M