Skip to content

Commit

Permalink
Better Copilot integration.
Browse files Browse the repository at this point in the history
  • Loading branch information
miltonllera committed May 13, 2024
1 parent f9bcf4a commit 7c37401
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 48 deletions.
29 changes: 15 additions & 14 deletions lazy-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,39 @@
"cmp-nvim-lua": { "branch": "main", "commit": "f12408bdb54c39c23e67cab726264c10db33ada8" },
"cmp-path": { "branch": "main", "commit": "91ff86cd9c29299a64f968ebb45846c485725f23" },
"cmp_luasnip": { "branch": "master", "commit": "05a9ab28b53f71d1aece421ef32fee2cb857a843" },
"copilot.vim": { "branch": "release", "commit": "b603990a639bb4b8651d054ef8d5a8fe5db56e0c" },
"friendly-snippets": { "branch": "main", "commit": "fa36367422da5a38560892e3db6d090a635d9d41" },
"copilot-cmp": { "branch": "master", "commit": "72fbaa03695779f8349be3ac54fa8bd77eed3ee3" },
"copilot.lua": { "branch": "master", "commit": "f7612f5af4a7d7615babf43ab1e67a2d790c13a6" },
"friendly-snippets": { "branch": "main", "commit": "3e9a3f5a0cfcef1741e352c37bda4e82e5eb846a" },
"gitsigns.nvim": { "branch": "main", "commit": "805610a9393fa231f2c2b49cb521bfa413fadb3d" },
"glow.nvim": { "branch": "main", "commit": "238070a686c1da3bccccf1079700eb4b5e19aea4" },
"lazy.nvim": { "branch": "main", "commit": "d3974346b6cef2116c8e7b08423256a834cb7cbc" },
"lsp_signature.nvim": { "branch": "master", "commit": "c6aeb2f1d2538bbdfdaab1664d9d4c3c75aa9db8" },
"lazy.nvim": { "branch": "main", "commit": "e44636a43376e8a1e851958f7e9cbe996751d59f" },
"lsp_signature.nvim": { "branch": "master", "commit": "aed5d1162b0f07bb3af34bedcc5f70a2b6466ed8" },
"lualine.nvim": { "branch": "master", "commit": "0a5a66803c7407767b799067986b4dc3036e1983" },
"mason-lspconfig.nvim": { "branch": "main", "commit": "05744f0f1967b5757bd05c08df4271ab8ec990aa" },
"mason.nvim": { "branch": "main", "commit": "751b1fcbf3d3b783fcf8d48865264a9bcd8f9b10" },
"mason-lspconfig.nvim": { "branch": "main", "commit": "9ae570e206360e47d30b4c35a4550c165f4ea7b7" },
"mason.nvim": { "branch": "main", "commit": "1b3d60405d1d720b2c4927f19672e9479703b00f" },
"material.nvim": { "branch": "main", "commit": "772e41a7f33743224f30799a3a887dc7dd853f8d" },
"nvim-cmp": { "branch": "main", "commit": "8f3c541407e691af6163e2447f3af1bd6e17f9a3" },
"nvim-lspconfig": { "branch": "master", "commit": "2beb3e9cac3ed9335f8972eb6082f380a4f1f124" },
"nvim-tree.lua": { "branch": "master", "commit": "64f61e4c913047a045ff90bd188dd3b54ee443cf" },
"nvim-treesitter": { "branch": "master", "commit": "da0efd7398713a3177da5ce8690f0e5c5415763d" },
"nvim-cmp": { "branch": "main", "commit": "24122371810089d390847d8ba66325c1f1aa64c0" },
"nvim-lspconfig": { "branch": "master", "commit": "94513a5b246cf32a8f87ca714af50911df63351c" },
"nvim-tree.lua": { "branch": "master", "commit": "edd4e25fd4f8923f9e2816e27b5d1b1b5fff7a85" },
"nvim-treesitter": { "branch": "master", "commit": "d5a1c2b0c8ec5bb377a41c1c414b315d6b3e9432" },
"nvim-web-devicons": { "branch": "master", "commit": "5b9067899ee6a2538891573500e8fd6ff008440f" },
"plenary.nvim": { "branch": "master", "commit": "08e301982b9a057110ede7a735dd1b5285eb341f" },
"python-type-stubs": { "branch": "main", "commit": "2e0624116ea217a06c29a7e84f96a78b9db1d2b2" },
"python-type-stubs": { "branch": "main", "commit": "cd8f8654c3e46a7f771afd044dd8eb48811696c1" },
"tabular": { "branch": "master", "commit": "339091ac4dd1f17e225fe7d57b48aff55f99b23a" },
"telescope-fzf-native.nvim": { "branch": "main", "commit": "9ef21b2e6bb6ebeaf349a0781745549bbb870d27" },
"telescope.nvim": { "branch": "master", "commit": "fac83a556e7b710dc31433dec727361ca062dbe9" },
"telescope.nvim": { "branch": "master", "commit": "02a60a83961cf9c80e471599171982a31d1cc853" },
"tokyonight.nvim": { "branch": "main", "commit": "67afeaf7fd6ebba000633e89f63c31694057edde" },
"trouble.nvim": { "branch": "main", "commit": "b9cf677f20bb2faa2dacfa870b084e568dca9572" },
"vim-black": { "branch": "main", "commit": "e9486a54b63939da162cbbd4f9838f7319842910" },
"vim-commentary": { "branch": "master", "commit": "c4b8f52cbb7142ec239494e5a2c4a512f92c4d07" },
"vim-easy-align": { "branch": "master", "commit": "9815a55dbcd817784458df7a18acacc6f82b1241" },
"vim-fugitive": { "branch": "master", "commit": "ce882460cf3db12e99f8bf579cbf99e331f6dd4f" },
"vim-kitty": { "branch": "main", "commit": "4f3f2901717320f80a1e16a868f706762be8535b" },
"vim-kitty": { "branch": "main", "commit": "f16222cbfd061eda28e832dd407df6d6ec995a67" },
"vim-python-indent-black": { "branch": "main", "commit": "8a08f503f4e501441ad6f478d66fa895bf8cf857" },
"vim-repeat": { "branch": "master", "commit": "24afe922e6a05891756ecf331f39a1f6743d3d5a" },
"vim-startify": { "branch": "master", "commit": "4e089dffdad46f3f5593f34362d530e8fe823dcf" },
"vim-surround": { "branch": "master", "commit": "3d188ed2113431cf8dac77be61b842acb64433d9" },
"vim-toml": { "branch": "main", "commit": "d36caa6b1cf508a4df1c691f915572fc02143258" },
"vim-unimpaired": { "branch": "master", "commit": "6d44a6dc2ec34607c41ec78acf81657248580bf1" },
"vimtex": { "branch": "master", "commit": "5732cd898cc7027e58191fe4be64da8dbc158995" }
"vimtex": { "branch": "master", "commit": "8ca74380935beb4ed5d213bb55b2380cc1a83bd6" }
}
18 changes: 17 additions & 1 deletion lua/plugins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,23 @@ local plugins = {
{ 'junegunn/vim-easy-align' },

-- Copilot
{ "github/copilot.vim" },
-- { "github/copilot.vim" },
-- Use zbirenbaum's version since it is written in lua and is much more efficient
{
"zbirenbaum/copilot.lua",
dependencies = {
"nvim-lua/plenary.nvim",
},
cmd = "Copilot",
event = "InsertEnter",
config = function() require('plugins.copilot') end,
},
{
"zbirenbaum/copilot-cmp",
config = function ()
require("copilot_cmp").setup()
end
},

-- Python formatting
{ "EgZvor/vim-black" },
Expand Down
134 changes: 101 additions & 33 deletions lua/plugins/cmp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ local function get_snippets_rtp()
))
end

local function has_words_before()
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
end


local opts = {
paths = {
fn.stdpath('config')..'/snips/',
Expand All @@ -30,6 +36,15 @@ cmp.setup({
-- Don't preselect an option
preselect = cmp.PreselectMode.None,

-- completion = {
-- autocomplete = false,
-- completeopt = vim.o.completeopt,
-- },

experimental = {
ghost_text = true,
},

-- Snippet engine, required
snippet = {
expand = function(args)
Expand All @@ -40,13 +55,60 @@ cmp.setup({
-- Mappings
mapping = {
-- open/close autocomplete
['<C-Space>'] = function(fallback)
if cmp.visible() then
cmp.close()
else
cmp.complete()
end
end,
["<C-Space>"] = cmp.mapping(cmp.mapping.complete(), { "i", "c" }),

["<Tab>"] = cmp.mapping(function(fallback)
if require("copilot.suggestion").is_visible() then
require("copilot.suggestion").accept()
elseif cmp.visible() then
cmp.select_next_item({ behavior = cmp.SelectBehavior.Insert })
elseif luasnip.expandable() then
luasnip.expand()
elseif has_words_before() then
cmp.complete()
else
fallback()
end
end, {
"i",
"s",
}),

["<S-Tab>"] = cmp.mapping(function()
if cmp.visible() then
cmp.select_prev_item({ behavior = cmp.SelectBehavior.Insert })
end
end, {
"i",
"s",
}),
-- ['<C-Space>'] = function(fallback)
-- if cmp.visible() then
-- cmp.close()
-- else
-- cmp.complete()
-- end
-- end,

-- ['<Tab>'] = function(fallback)
-- if cmp.visible() then
-- cmp.select_next_item()
-- elseif luasnip.expand_or_jumpable() then
-- luasnip.expand_or_jump()
-- else
-- fallback()
-- end
-- end,

-- ['<S-Tab>'] = function(fallback)
-- if cmp.visible() then
-- cmp.select_prev_item()
-- elseif luasnip.jumpable(-1) then
-- luasnip.jump(-1)
-- else
-- fallback()
-- end
-- end,

['<C-c>'] = cmp.mapping.close(),

Expand All @@ -56,39 +118,45 @@ cmp.setup({
select = false,
}),

['<Tab>'] = function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
else
fallback()
end
end,

['<S-Tab>'] = function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif luasnip.jumpable(-1) then
luasnip.jump(-1)
else
fallback()
end
end,

-- Scroll documentation
['<C-d>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4),
},

-- Complete options from the LSP servers and the snippet engine
sources = {
{name = 'nvim_lsp'},
{name = 'luasnip'},
{name = 'nvim_lua'},
{name = 'path'},
{name = 'buffer'},
{name = 'spell'},
{name = 'nvim_lsp', priority = 3},
{name = 'luasnip', priority = 1},
-- {name = "copilot", group_index = 2, priority = 3},

{name = 'path' },
{name = 'buffer' },
-- {name = 'nvim_lua', group_index = 2},
-- {name = 'spell', group_index = 2},
-- {name = 'calc'},
},
})

-- Disable copilot in cmp popup
cmp.event:on("menu_opened", function()
vim.b.copilot_suggestion_hidden = true
end)

cmp.event:on("menu_closed", function()
vim.b.copilot_suggestion_hidden = false
end)

-- Proper sources for buffer and cmdline
cmp.setup.cmdline("/", {
sources = {
{ name = "buffer" },
},
})

cmp.setup.cmdline(":", {
sources = cmp.config.sources({
{ name = "path" },
}, {
{ name = "cmdline" },
}),
})
33 changes: 33 additions & 0 deletions lua/plugins/copilot.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require("copilot").setup({
panel = {
auto_refresh = false,
keymap = {
accept = "<CR>",
jump_prev = "[[",
jump_next = "]]",
refresh = "gr",
open = "<M-CR>",
},
},
suggestion = {
auto_trigger = false,
keymap = {
accept = false,
accept_word = "<M-Right>",
accept_line = "<M-Down>",
prev = "<M-[>",
next = "<M-]>",
dismiss = "<C-]>",
},
},
})

local suggestion = require("copilot.suggestion")

vim.keymap.set("i", "<M-SPACE>", function()
if suggestion.is_visible() then
suggestion.accept()
else
suggestion.next()
end
end, { desc ="[copilot] accept or next suggestion" })

0 comments on commit 7c37401

Please sign in to comment.