zbirenbaum / copilot-cmp

Lua plugin to turn github copilot into a cmp source
MIT License
1.07k stars 38 forks source link

Copilot doesn't work #75

Closed cxzhou35 closed 10 months ago

cxzhou35 commented 1 year ago

I have viewed the similar issues in this repo, but none of them worked, so I raise a new issue to ask for help.

I have setup both copilot.lua and copilot-cmp plugins, the config is below(based on LazyVim):

return {

  -- copilot
  {
    "zbirenbaum/copilot.lua",
    cmd = "Copilot",
    build = ":Copilot auth",
    opts = {
      suggestion = { enabled = false },
      panel = { enabled = false },
      filetypes = {
        markdown = true,
        help = true,
      },
    },
  },
  {
    "nvim-lualine/lualine.nvim",
    optional = true,
    event = "VeryLazy",
    opts = function(_, opts)
      local Util = require("lazyvim.util")
      local colors = {
        [""] = Util.fg("Special"),
        ["Normal"] = Util.fg("Special"),
        ["Warning"] = Util.fg("DiagnosticError"),
        ["InProgress"] = Util.fg("DiagnosticWarn"),
      }
      table.insert(opts.sections.lualine_x, 2, {
        function()
          local icon = require("lazyvim.config").icons.kinds.Copilot
          local status = require("copilot.api").status.data
          return icon .. (status.message or "")
        end,
        cond = function()
          local ok, clients = pcall(vim.lsp.get_active_clients, { name = "copilot", bufnr = 0 })
          return ok and #clients > 0
        end,
        color = function()
          if not package.loaded["copilot"] then
            return
          end
          local status = require("copilot.api").status.data
          return colors[status.status] or colors[""]
        end,
      })
    end,
  },

  -- copilot cmp source
  {
    "nvim-cmp",
    dependencies = {
      {
        "zbirenbaum/copilot-cmp",
        dependencies = "copilot.lua",
        opts = {},
        config = function(_, opts)
          local copilot_cmp = require("copilot_cmp")
          copilot_cmp.setup(opts)
          -- attach cmp source whenever copilot attaches
          -- fixes lazy-loading issues with the copilot cmp source
          require("lazyvim.util").on_attach(function(client)
            if client.name == "copilot" then
              copilot_cmp._on_insert_enter({})
            end
          end)
        end,
      },
    },
    ---@param opts cmp.ConfigSchema
    opts = function(_, opts)
      local cmp = require("cmp")

      table.insert(opts.sources, 1, { name = "copilot", group_index = 2 })

      opts.sorting = {
        priority_weight = 2,
        comparators = {
          require("copilot_cmp.comparators").prioritize,

          -- Below is the default comparitor list and order for nvim-cmp
          cmp.config.compare.offset,
          -- cmp.config.compare.scopes, --this is commented in nvim-cmp too
          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,
        },
      }
    end,
  },
}

How to reproduce:

  1. Setup LazyVim and install the copilot.lua and copilot-cmp plugins
  2. Auth the github account to use the copilot.
  3. Edit anyone file with neovim.
  4. Copilot doesn't work.

LspInfo result:

image

Lualine Status(The copilot status is online):

image

Anyone can provide the solutions for me? Thanks a lot. :)

zbirenbaum commented 1 year ago

It looked like in the post on copilot.lua you had suggestions in the cmp window correct? If that's the case this is working normally.

But if copilot.lua is working as expected, some texts will appear in the context of my code?

I still can't see any code suggestions provided by copilot.lua without the nvim-cmp popup window.

Copilot.vim/copilot.lua style inline text suggestions are mutually exclusive with copilot-cmp. This is a limitation brought on by nvim-cmp's ghost text which doesn't support multiline suggestions.

cxzhou35 commented 1 year ago

It looked like in the post on copilot.lua you had suggestions in the cmp window correct? If that's the case this is working normally.

But if copilot.lua is working as expected, some texts will appear in the context of my code? I still can't see any code suggestions provided by copilot.lua without the nvim-cmp popup window.

Copilot.vim/copilot.lua style inline text suggestions are mutually exclusive with copilot-cmp. This is a limitation brought on by nvim-cmp's ghost text which doesn't support multiline suggestions.

Sorry, I didn't say it clearly, I disabled the copilot-cmp plugin, and in this case I even can't see any suggestions provided by copilot. Now I can't see any suggestions in both cmp popup window and neovim editor window.

cxzhou35 commented 1 year ago

I just want the suggestions in the nvim-cmp popup window.

zbirenbaum commented 1 year ago

I just want the suggestions in the nvim-cmp popup window.

Can you check what :LspLog reports? Also can you try removing copilot-cmp and enable suggestions for copilot.lua to see if they appear that way?

cxzhou35 commented 1 year ago

I just want the suggestions in the nvim-cmp popup window.

Can you check what :LspLog reports? Also can you try removing copilot-cmp and enable suggestions for copilot.lua to see if they appear that way?

I notice that you also reply in the copilot.lua, you can find my solution in this comment, now the copilot is work, but the integration of cmp still failed.

return {
  {
    "zbirenbaum/copilot.lua",
    cmd = "Copilot",
    event = "InsertEnter",
    build = ":Copilot auth",
    module = "copilot",
    opts = {
      suggestion = { enabled = false },
      panel = { enabled = false },
      filetypes = {
        yaml = false,
        markdown = false,
        help = false,
        gitcommit = false,
        gitrebase = false,
        hgcommit = false,
        svn = false,
        cvs = false,
      },
    },
  },
  {
    "zbirenbaum/copilot-cmp",
    dependencies = "copilot.lua",
    event = { "InsertEnter", "LspAttach" },
    fix_pairs = true,
    config = function(_, opts)
      local copilot_cmp = require("copilot_cmp")
      copilot_cmp.setup(opts)
      -- attach cmp source whenever copilot attaches
      -- fixes lazy-loading issues with the copilot cmp source
      require("lazyvim.util").on_attach(function(client)
        if client.name == "copilot" then
          copilot_cmp._on_insert_enter({})
        end
      end)
    end,
  },
}
techcaotri commented 11 months ago

Not sure if it's similar to your issue but I had a similar symptom with 'clangd' and 'ccls' active at the same time: Copilot suggestion was working but the integration of cmp was failed, suggestion not showing the cmp menu.

After digging a bit, I think it's because of the wrong check here

I made a quick fix and PR here. Now I can see the copilot suggestion in cmp menu: image

Could you check if it also resolves your issue @cxzhou35

My Neovim version: NVIM v0.10.0-dev-604+gd191bdf9d

P/S: I guess it's also related to issue #78

I just want the suggestions in the nvim-cmp popup window.

Can you check what :LspLog reports? Also can you try removing copilot-cmp and enable suggestions for copilot.lua to see if they appear that way?

I notice that you also reply in the copilot.lua, you can find my solution in this comment, now the copilot is work, but the integration of cmp still failed.

return {
  {
    "zbirenbaum/copilot.lua",
    cmd = "Copilot",
    event = "InsertEnter",
    build = ":Copilot auth",
    module = "copilot",
    opts = {
      suggestion = { enabled = false },
      panel = { enabled = false },
      filetypes = {
        yaml = false,
        markdown = false,
        help = false,
        gitcommit = false,
        gitrebase = false,
        hgcommit = false,
        svn = false,
        cvs = false,
      },
    },
  },
  {
    "zbirenbaum/copilot-cmp",
    dependencies = "copilot.lua",
    event = { "InsertEnter", "LspAttach" },
    fix_pairs = true,
    config = function(_, opts)
      local copilot_cmp = require("copilot_cmp")
      copilot_cmp.setup(opts)
      -- attach cmp source whenever copilot attaches
      -- fixes lazy-loading issues with the copilot cmp source
      require("lazyvim.util").on_attach(function(client)
        if client.name == "copilot" then
          copilot_cmp._on_insert_enter({})
        end
      end)
    end,
  },
}
cxzhou35 commented 11 months ago

Not sure if it's similar to your issue but I had a similar symptom with 'clangd' and 'ccls' active at the same time: Copilot suggestion was working but the integration of cmp was failed, suggestion not showing the cmp menu.

After digging a bit, I think it's because of the wrong check here

I made a quick fix and PR here. Now I can see the copilot suggestion in cmp menu: image

Could you check if it also resolves your issue @cxzhou35

My Neovim version: NVIM v0.10.0-dev-604+gd191bdf9d

P/S: I guess it's also related to issue #78

I just want the suggestions in the nvim-cmp popup window.

Can you check what :LspLog reports? Also can you try removing copilot-cmp and enable suggestions for copilot.lua to see if they appear that way?

I notice that you also reply in the copilot.lua, you can find my solution in this comment, now the copilot is work, but the integration of cmp still failed.

return {
  {
    "zbirenbaum/copilot.lua",
    cmd = "Copilot",
    event = "InsertEnter",
    build = ":Copilot auth",
    module = "copilot",
    opts = {
      suggestion = { enabled = false },
      panel = { enabled = false },
      filetypes = {
        yaml = false,
        markdown = false,
        help = false,
        gitcommit = false,
        gitrebase = false,
        hgcommit = false,
        svn = false,
        cvs = false,
      },
    },
  },
  {
    "zbirenbaum/copilot-cmp",
    dependencies = "copilot.lua",
    event = { "InsertEnter", "LspAttach" },
    fix_pairs = true,
    config = function(_, opts)
      local copilot_cmp = require("copilot_cmp")
      copilot_cmp.setup(opts)
      -- attach cmp source whenever copilot attaches
      -- fixes lazy-loading issues with the copilot cmp source
      require("lazyvim.util").on_attach(function(client)
        if client.name == "copilot" then
          copilot_cmp._on_insert_enter({})
        end
      end)
    end,
  },
}

Thanks to your detailed reply, I will try your solution and give my results as soon as possible.