Skip to content

Toggle copilot-cmp #404

@adityaprakash-work

Description

@adityaprakash-work

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions