Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add nvim-cmp integration #345

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,30 @@ Requires [fzf-lua](https://github.com/ibhagwan/fzf-lua) plugin to be installed.

</details>

<details>
<summary>nvim-cmp integration</summary>

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 <tab> 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)

</details>

## Roadmap (Wishlist)

- Use indexed vector database with current workspace for better context selection
Expand Down
45 changes: 45 additions & 0 deletions lua/CopilotChat/integrations/cmp.lua
Original file line number Diff line number Diff line change
@@ -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