r/lunarvim • LunarVim Dev • May 05 '23

A Guide to Integrating LunarVim & GitHub Copilot

https://medium.com/@chrisatmachine/a-guide-to-integrating-lunarvim-github-copilot-61d92f764913
19 Upvotes

9 comments sorted by

3

u/rebelrexx858 May 05 '23

You're just crushing articles today

2

u/tlonny Nov 07 '23

if you add the below to your $HOME/.config/lvim/config.lua you should be all set. This should merge copilot suggestions in with suggestions from LSP. The extra gubbins at the bottom is because Copilot sometimes wants to give you suggestions before you've actually written anything. This becomes problematic if you're trying to use tab to indent the line - hence the fix. Hope this helps someone!

-- Copilot plugins are defined below:
lvim.plugins = {
{
"zbirenbaum/copilot.lua",
cmd = "Copilot",
event = "InsertEnter",
config = function()
require("copilot").setup({})
end,
},
{
"zbirenbaum/copilot-cmp",
config = function ()
require("copilot_cmp").setup({
suggestion = { enabled = false },
panel = { enabled = false }
})
end
}
}
-- Below config is required to prevent copilot overriding Tab with a suggestion
-- when you're just trying to indent!
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
local on_tab = vim.schedule_wrap(function(fallback)
local cmp = require("cmp")
if cmp.visible() and has_words_before() then
cmp.select_next_item({ behavior = cmp.SelectBehavior.Select })
else
fallback()
end
end)
lvim.builtin.cmp.mapping["<Tab>"] = on_tab

1

u/ZealousidealLaugh125 Nov 10 '23

This really helped me thank you!

1

u/itsmeciao Feb 15 '24 edited May 12 '24

Thanks, here is the same code formatted for your copying-pasting convenience:

-- Copilot plugins are defined below:
lvim.plugins = {
    {
        "zbirenbaum/copilot.lua",
        cmd = "Copilot",
        event = "InsertEnter",
        config = function()
            require("copilot").setup({})
        end,
    },
    
    {
        "zbirenbaum/copilot-cmp",
        config = function()
            require("copilot_cmp").setup({
                suggestion = { enabled = false },
                panel = { enabled = false }
            })
        end
    }
}

-- Below config is required to prevent copilot overriding Tab with a suggestion
-- when you're just trying to indent!
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
local on_tab = vim.schedule_wrap(function(fallback)
    local cmp = require("cmp")
    if cmp.visible() and has_words_before() then
        cmp.select_next_item({ behavior = cmp.SelectBehavior.Select })
    else
        fallback()
    end
end)
lvim.builtin.cmp.mapping["<Tab>"] = on_tab

1

u/muggafugga May 12 '24

There's a typo in your example

require("copilot-cmp").setup({

should be

require("copilot_cmp").setup({

1

u/itsmeciao May 12 '24

Fixed, thanks!

1

u/eduardolinux May 10 '24

I can't access the full article without paying a subscription. Is this some kind of joke?

1

u/datagangster May 13 '23

Thanks for putting this together