Skip to content

0xC0FFEE/copilot-lsp

 
 

Repository files navigation

Copilot LSP Configuration for Neovim

Features

Done

  • TextDocument Focusing

In Progress

  • Inline Completion
  • Next Edit Suggestion
  • Uses native LSP Binary

To Do

  • Sign In Flow
  • Status Notification

Usage

To use the plugin, add the following to your Neovim configuration:

return {
    "copilotlsp-nvim/copilot-lsp",
    init = function()
        vim.g.copilot_nes_debounce = 500
        vim.lsp.enable("copilot_ls")
        vim.keymap.set("n", "<tab>", function()
            local bufnr = vim.api.nvim_get_current_buf()
            local state = vim.b[bufnr].nes_state
            if state then
                -- Try to jump to the start of the suggestion edit.
                -- If already at the start, then apply the pending suggestion and jump to the end of the edit.
                local _ = require("copilot-lsp.nes").walk_cursor_start_edit()
                    or (
                        require("copilot-lsp.nes").apply_pending_nes()
                        and require("copilot-lsp.nes").walk_cursor_end_edit()
                    )
                return nil
            end

            -- optionally: try to re-show the most recent NES suggestion for this buffer
            if require("copilot-lsp.nes").restore_last_nes(bufnr) then
                return nil
            end

            -- Resolving the terminal's inability to distinguish between `TAB` and `<C-i>` in normal mode
            return "<C-i>"
        end, { desc = "Accept Copilot NES suggestion", expr = true })
    end,
}

Clearing suggestions with Escape

You can map the <Esc> key to clear suggestions while preserving its other functionality:

-- Clear copilot suggestion with Esc if visible, otherwise preserve default Esc behavior
vim.keymap.set("n", "<esc>", function()
    if not require("copilot-lsp.nes").clear() then
        -- fallback to other functionality
    end
end, { desc = "Clear Copilot suggestion or fallback" })

Re-showing the last cleared suggestion

require("copilot-lsp.nes").restore_last_nes(bufnr) returns true when it re-shows the most recent cleared NES suggestion for the bufnr buffer; defaults to current buffer. It returns false if there is already an active suggestion, if nothing has been cleared yet, or if the saved suggestion is stale.

Default Configuration

NES (Next Edit Suggestion) Smart Clearing

You don’t need to configure anything, but you can customize the defaults: move_count_threshold is the most important. It controls how many cursor moves happen before suggestions are cleared. Higher = slower to clear.

require('copilot-lsp').setup({
  nes = {
    move_count_threshold = 3,   -- Clear after 3 cursor movements
  }
})

Blink Integration

return {
    keymap = {
        preset = "super-tab",
        ["<Tab>"] = {
            function(cmp)
                if vim.b[vim.api.nvim_get_current_buf()].nes_state then
                    cmp.hide()
                    return (
                        require("copilot-lsp.nes").apply_pending_nes()
                        and require("copilot-lsp.nes").walk_cursor_end_edit()
                    )
                end
                if cmp.snippet_active() then
                    return cmp.accept()
                else
                    return cmp.select_and_accept()
                end
            end,
            "snippet_forward",
            "fallback",
        },
    },
}

It can also be combined with fang2hou/blink-copilot to get inline completions. Just add the completion source to your Blink configuration and it will integrate

Requirements

  • Copilot LSP installed via Mason or system and on PATH
  • Optional but recommended for the highest-fidelity same-line NES previews: codediff.nvim (provides the public codediff.diff API backed by libvscode-diff). If it is unavailable, copilot-lsp falls back to canonical same-line highlighting.

Screenshots

NES

JS Correction Go Insertion

433686050-807042bc-1ecb-4a5f-8928-418293e7999b.mp4

About

Copilot LSP: A lightweight and extensible Neovim plugin for integrating GitHub Copilot's AI-powered code suggestions via Language Server Protocol (LSP).

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Lua 99.9%
  • Makefile 0.1%