zbirenbaum / zbirenbaum/copilot.lua

Toggle copilot-cmp

Open
#404 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

help wanted
Dominant language
Lua
Stars
4.1k
Forks
161
Avg merge
11h 54m
Merged PRs (30d)
13

Description

Hey, I was trying to create script to toggle cmp based completions vs ghost text based completions. Here is my code

-- Single-file approach:
--  - Returns a plugin spec for Lazy (copilot.lua + copilot-cmp).
--  - Also includes Copilot setup logic with two modes (ghost text vs. cmp).
--  - Exposes a global toggle function, CopilotToggle(), to switch modes.

local ghost_text_enabled = true

-- LLM
local MODEL = "gpt-4o-copilot"

-- The main setup function
local function setupCopilot()
  if ghost_text_enabled then
    -- Ghost text (inline suggestions) mode
    require("copilot").setup({
      suggestion = {
        enabled = true,
        auto_trigger = true,
        debounce = 75,
        keymap = {
          accept      = "<C-f>", -- accept the entire suggestion
          accept_word = "<C-w>",
          accept_line = "<C-l>",
          next        = "<C-n>",
          prev        = "<C-p>",
          dismiss     = "<C-x>",
        },
      },
      panel = { enabled = false },
      filetypes = {
        ["*"] = true,
      },
      copilot_model = MODEL,
    })

    -- nvim-cmp WITHOUT Copilot as a source
    local cmp = require("cmp")
    cmp.setup({
      sources = cmp.config.sources({
        { name = "nvim_lsp", group_index = 2 },
        { name = "luasnip",  group_index = 2 },
        { name = "buffer",   group_index = 2 },
        { name = "nvim_lua", group_index = 2 },
        { name = "path",     group_index = 2 },
      }),
    })

  else
    -- CMP mode (Copilot as a completion source)
    require("copilot").setup({
      suggestion = {
        enabled = false,
        auto_trigger = true,
        debounce = 75,
        keymap = {
          accept  = false,    -- rely on nvim-cmp or external mapping
          next    = "<M-]>",  -- cycle forward
          prev    = "<M-[>",  -- cycle backward
          dismiss = "<C-]>",  -- dismiss suggestion
        },
      },
      panel = { enabled = false },
      filetypes = {
        ["*"] = true,
      },
      copilot_model = MODEL,
    })

    -- Setup copilot-cmp
    require("copilot_cmp").setup()

    -- Include Copilot as a source for nvim-cmp
    local cmp = require("cmp")
    cmp.setup({
      sources = cmp.config.sources({
        { name = "copilot",  group_index = 1, priority = 100 },
        { name = "nvim_lsp", group_index = 2 },
        { name = "luasnip",  group_index = 2 },
        { name = "buffer",   group_index = 2 },
        { name = "nvim_lua", group_index = 2 },
        { name = "path",     group_index = 2 },
      }),
    })
  end
end

-- Define a global function so you can call it from a keymap or command
_G.CopilotModeToggle = function()
  ghost_text_enabled = not ghost_text_enabled
  vim.notify("Copilot Mode changed to " ..
    (ghost_text_enabled and "Ghost Text" or "CMP"))
  setupCopilot()
end


-- Return a Lazy plugin spec
return {
  {
    "zbirenbaum/copilot.lua",
    event = "InsertEnter",
    config = function()
      -- When copilot.lua is loaded, set up the current mode
      setupCopilot()
    end,
  },
  {
    "zbirenbaum/copilot-cmp",
    dependencies = "zbirenbaum/copilot.lua",
    config = function()
      -- If you're in CMP mode, you'll want this set up;
      -- If in Ghost mode, it won't interfere.
      require("copilot_cmp").setup()
    end,
  },
}

When ghost_text_enabled is set to true or false, both cases work as expected. The problem is when I toggle, the ghost text based completion doesn't seem to go. Similarly, in the reverse case, the cmp based completions are turned off but the ghost text completions are not turned on. Can someone help me with this utility. Thanks

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the single-file Lazy plugin spec and trace setupCopilot() and CopilotModeToggle() in both modes. Reproduce the toggle in Neovim, checking how the existing ghost-text and nvim-cmp configurations behave after switching. Done means changing modes disables the previous completion path and enables the selected one.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.