Skip to content

fang2hou/blink-copilot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Completion Sample

blink-copilot

⚙️ Configurable GitHub Copilot blink.cmp source

Table of Contents

📋 Requirements

  • Neovim >= 0.9.0
  • GitHub Copilot LSP Provider (choose one of the following)
    • copilot.vim - Official GitHub Copilot Vim/Neovim Plugin
    • copilot.lua - Third-party GitHub Copilot written in Lua

🌟 Features

  1. Developed according to the blink.cmp async specification.
  2. Automatically detects LSP client on buffer switching, ensuring seamless functionality even if the Copilot LSP client isn't attached on the first file opening.
  3. Supports multiple completion candidates and automatically retries if a request fails.
  4. Automatically refreshes Copilot suggestion items when the cursor moves.
  5. Add debounce to avoid excessive requests to Copilot.
  6. Utilizes the latest GitHub Copilot LSP API, resulting in less preprocessing and better performance compared to similar plugins.
  7. Offers superior performance over copilot.lua with rewritten native LSP interaction, even when using the official Vim plugin.
  8. Features enhanced preview with smart deindentation formatting.
  9. Supports both copilot.lua and the official copilot.vim as backend LSP providers.
  10. Easily register and customize the completion kind and icon.

⚙️ Configuration

blink-copilot seamlessly integrates with both blink.cmp source options and Neovim plugin configurations. For most users, simply configuring the options within sources.provider.copilot.opts is sufficient.

Explore the configuration in detail
{
  "saghen/blink.cmp",
  optional = true,
  dependencies = {
    "fang2hou/blink-copilot",
    opts = {
      max_completions = 1,  -- Global default for max completions
      max_attempts = 2,     -- Global default for max attempts
      -- `kind` is not set, so the default value is "Copilot"
    }
  },
  opts = {
    sources = {
      default = { "copilot" },
      providers = {
        copilot = {
          name = "copilot",
          module = "blink-copilot",
          score_offset = 100,
          async = true,
          opts = {
            -- Local options override global ones
            -- Final settings: max_completions = 3, max_attempts = 2, kind = "Copilot"
            max_completions = 3,  -- Override global max_completions
          }
        },
      },
    },
  },
}

Here is the default configuration for blink-copilot:

{
  max_completions = 3,
  max_attempts = 4,
  kind = "Copilot",
  debounce = 750, ---@type integer | false
  auto_refresh = {
    backward = true,
    forward = true,
  },
}

max_completions

Maximum number of completions to show.

Note

Sometimes Copilot do not provide any completions, even you set max_completions to a large number. This is a limitation of Copilot itself, not the plugin.

Default: 3

max_attempts

Maximum number of attempts to fetch completions.

Note

Each attempt will fetch 0 ~ 10 completions. Considering the possibility of failure, it is generally recommended to set it to max_completions+1.

Default: 4

kind

Specifies the type of completion item to display.

Note

If the provided kind does not exist, blink-copilot will automatically register it in CompletionItemKind.
To display a custom icon for the kind, you need to define the kind_icon in the blink.cmp options.
Refer to the recipes for examples.

Default: "Copilot"

debounce

Note

Debounce is a feature that limits the number of requests sent to Copilot.
You can customize the debounce time in milliseconds or set it to false to disable it.

Important

If you disable debounce and enable auto_refresh, the copilot suggestion items will be refreshed every time the cursor moves.
Excessive refreshing may temporarily block your Copilot.

Default: 750

auto_refresh

Automatically refreshes the completion list when the cursor moves.

Note

If you enable backward, the completion list will be refreshed when the cursor moves backward. If you enable forward, the completion list will be refreshed when the cursor moves forward.

Default: { backward = true, forward = true }

🥘 Recipes

Here are some example configuration for using blink-copilot with lazy.nvim.

Not Using LazyVim?

blink-copilot + zbirenbaum/copilot.lua
{
  "zbirenbaum/copilot.lua",
  cmd = "Copilot",
  build = ":Copilot auth",
  event = "InsertEnter",
  opts = {
    suggestion = { enabled = false },
    panel = { enabled = false },
    filetypes = {
      markdown = true,
      help = true,
    },
  },
},
{
  "saghen/blink.cmp",
  optional = true,
  dependencies = { "fang2hou/blink-copilot" },
  opts = {
    sources = {
      default = { "copilot" },
      providers = {
        copilot = {
          name = "copilot",
          module = "blink-copilot",
          score_offset = 100,
          async = true,
          opts = {
            max_completions = 3,
            max_attempts = 4,
          }
        },
      },
    },
    appearance = {
      kind_icons = {
        Copilot = "",
      },
    },
  },
}
blink-copilot + github/copilot.vim
{
  "github/copilot.vim",
  cmd = "Copilot",
  build = ":Copilot auth",
  event = "BufWinEnter",
  init = function()
    vim.g.copilot_no_maps = true
  end,
  config = function()
    -- Block the normal Copilot suggestions
    vim.api.nvim_create_augroup("github_copilot", { clear = true })
    for _, event in pairs({ "FileType", "BufUnload", "BufEnter" }) do
      vim.api.nvim_create_autocmd({ event }, {
        group = "github_copilot",
        callback = function()
          vim.fn["copilot#On" .. event]()
        end,
      })
    end
  end,
},
{
  "saghen/blink.cmp",
  dependencies = { "fang2hou/blink-copilot" },
  opts = {
    sources = {
      default = { "copilot" },
      providers = {
        copilot = {
          name = "copilot",
          module = "blink-copilot",
          score_offset = 100,
          async = true,
          opts = {
            max_completions = 3,
            max_attempts = 4,
          }
        },
      },
    },
    appearance = {
      kind_icons = {
        Copilot = "",
      },
    },
  },
}

With LazyVim copilot extra

blink-cmp-copilot ➡️ blink-copilot
{ import = "lazyvim.plugins.extras.ai.copilot" },
{
  "giuxtaposition/blink-cmp-copilot",
  enabled = false,
},
{
  "saghen/blink.cmp",
  dependencies = { "fang2hou/blink-copilot" },
  opts = {
    sources = {
      providers = {
        copilot = {
          module = "blink-copilot",
          opts = {
            max_completions = 3,
            max_attempts = 4,
          }
        },
      },
    },
  },
}
(Optional)copilot.lua ➡️ copilot.vim
{
  "zbirenbaum/copilot.lua",
  enabled = false,
},
{
  "github/copilot.vim",
  cmd = "Copilot",
  build = ":Copilot auth",
  event = "BufWinEnter",
  init = function()
    vim.g.copilot_no_maps = true
  end,
  config = function()
    -- Block the normal Copilot suggestions
    vim.api.nvim_create_augroup("github_copilot", { clear = true })
    for _, event in pairs({ "FileType", "BufUnload", "BufEnter" }) do
      vim.api.nvim_create_autocmd({ event }, {
        group = "github_copilot",
        callback = function()
          vim.fn["copilot#On" .. event]()
        end,
      })
    end
  end,
}

📚 Frequently Asked Questions

The number of completions does not match my settings.

This is because the number of completions provided by Copilot is not fixed. The max_completions setting is only an upper limit, and the actual number of completions may be less than this value.

What's the difference between blink-copilot and blink-cmp-copilot?

  • Completion Preview is now correctly deindented.
  • Support both copilot.lua and copilot.vim as a backend.
  • Support multiple completions, and it is configurable.
  • LSP interaction no longer relies on copilot.lua, ensuring improved performance and compliance with the latest official LSP API specifications.
  • Auto refresh copilot suggestion items when cursor moves.

The completion isn't working after restarting Copilot. What should I do?

The Copilot plugin doesn't automatically attach the new LSP client to buffers. To resolve this, manually reopen your current buffer to attach the LSP client. blink-copilot will automatically detect the new client and resume completions.

Why aren't blink completions showing up?

Blink intentionally pauses completions during macro recording. If you see recording @x in your statusline, press q to stop recording.

🔄 Alternatives and Related Projects

🪪 License

MIT