Lua plugin to turn github copilot into a cmp source
| Files | Last commit | Last update |
|---|---|---|
| 2 years ago | ||
| 2 years ago | ||
| 4 years ago | ||
| 3 years ago | ||
| 2 years ago |
copilot-cmp
该仓库将 https://github.com/zbirenbaum/copilot.lua 转换为一个 cmp 源。
Copilot 的建议会自动作为片段加载到你的 cmp 菜单中,并在悬停 Copilot 建议时显示完整内容。

设置
如果你已经安装了 copilot.lua,你可以像下面这样使用 packer 安装此插件:
安装
延迟加载
{
"zbirenbaum/copilot-cmp",
config = function ()
require("copilot_cmp").setup()
end
}
Packer
use {
"zbirenbaum/copilot-cmp",
after = { "copilot.lua" },
config = function ()
require("copilot_cmp").setup()
end
}
如果没有安装 copilot.lua,请先访问 https://github.com/zbirenbaum/copilot.lua 并按照说明安装。
推荐禁用 copilot.lua 的建议和面板模块,因为它们可能干扰 copilot-cmp 中正确出现的补全。要在 copilot.lua 配置文件中实现这一点,请添加以下代码:
require("copilot").setup({
suggestion = { enabled = false },
panel = { enabled = false },
})
配置:
nvim-cmp:
源定义
要将 cmp 与这个源链接,只需进入你的 cmp 配置文件并在 sources 下包含 { name = "copilot" }。
示例如下:
cmp.setup {
...
sources = {
-- Copilot 源
{ name = "copilot", group_index = 2 },
-- 其他源
{ name = "nvim_lsp", group_index = 2 },
{ name = "path", group_index = 2 },
{ name = "luasnip", group_index = 2 },
},
...
}
高亮及图标
Copilot 的 cmp 源现在有一个内建的高亮组 CmpItemKindCopilot。要为 lspkind 添加 Copilot 图标,只需将 copilot 添加到你的 lspkind 符号映射中。
-- lspkind.lua
local lspkind = require("lspkind")
lspkind.init({
symbol_map = {
Copilot = "",
},
})
vim.api.nvim_set_hl(0, "CmpItemKindCopilot", {fg ="#6CC644"})
或者,你也可以在 cmp 格式函数内将 Copilot 添加到 lspkind 的 symbol_map。
-- cmp.lua
cmp.setup {
...
formatting = {
format = lspkind.cmp_format({
mode = "symbol",
max_width = 50,
symbol_map = { Copilot = "" }
})
}
...
}
如果不使用 lspkind,只需按正常处理 kind 格式的方式添加自定义图标,它会像其他正常的 lsp 补全类型一样集成。
Tab 补全配置(强烈推荐)
与其他补全源不同,Copilot 可以使用空行上方或下方的其他行来提供补全。这可能会对使用 <TAB> 选择菜单条目的人造成问题。通过 cmp 的配置,以下代码可以让菜单正常显示,但只有当实际输入了非空白字符时,<TAB> 才会回退到缩进。
local has_words_before = function()
if vim.api.nvim_buf_get_option(0, "buftype") == "prompt" then return false end
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
return col ~= 0 and vim.api.nvim_buf_get_text(0, line-1, 0, line-1, col, {})[1]:match"^%s*$" == nil
end
cmp.setup({
mapping = {
["<Tab>"] = vim.schedule_wrap(function(fallback)
if cmp.visible() and has_words_before() then
cmp.select_next_item({ behavior = cmp.SelectBehavior.Select })
else
fallback()
end
end),
},
})
比较器
提供了一个定制比较器 prioritize 来排序 cmp 项。prioritize 比较器使 Copilot 项出现在 cmp 菜单的较高位置。建议保持优先级权重为 2,或将 exact 比较器置于 Copilot 之上,以免更好的 lsp 匹配被较差的 Copilot 匹配困在下面。
示例:
cmp.setup {
...
sorting = {
priority_weight = 2,
comparators = {
require("copilot_cmp.comparators").prioritize,
-- 下面是 nvim-cmp 的默认比较器列表和顺序
cmp.config.compare.offset,
-- cmp.config.compare.scopes, -- 这里在 nvim-cmp 中也是注释掉的
cmp.config.compare.exact,
cmp.config.compare.score,
cmp.config.compare.recently_used,
cmp.config.compare.locality,
cmp.config.compare.kind,
cmp.config.compare.sort_text,
cmp.config.compare.length,
cmp.config.compare.order,
},
},
...
}
copilot-cmp:
注意:现在强烈不推荐修改默认设置,除非遇到特定问题需要这样做。
此插件的可配置选项如下:
{
event = { "InsertEnter", "LspAttach" },
fix_pairs = true,
}
event
event 参数配置何时注册源。除非你的配置中有特殊问题,否则通常不需要更改此设置。
fix_pairs
假设你有以下代码:
print('h')
Copilot 可能会考虑到 ' 和 ),并完成为这样:
print('hello
从一致性角度来说,这不是好的行为,因为最终会导致删除结尾的两个字符。此选项可以解决这个问题。除非遇到括号相关的问题并且怀疑可能是由它引起的,否则不要关闭此功能。