Skip to content

Help with cmp completion + context input #1056

@paulodiovani

Description

@paulodiovani

Since v3 does not include the [cmp integration])https://github.com/CopilotC-Nvim/CopilotChat.nvim/blob/v2.16.0/lua/CopilotChat/integrations/cmp.lua anymore, I created my own complete source as below, but I also wanted to use the same <C-Space> mapping to autocomplete or prompt #context options.

e.g.

#files:<C-Space>

Setting <C-Space> in the mappings does not work, neither does <C-@>.
e.g.

copilot_chat.setup({
  chat_autocomplete = false,
  mappings = {
    complete = {
      insert = '<C-Space>',
    },
 -- ...

It would be nice to be able to use cmp for some of the contexts, such as #buffers:, but I didn't managed to do that either.

CMP Complete source for CopilotChat

-- disable original autocomplete
copilot_chat.setup({
  chat_autocomplete = false,
})
local copilot_chat = require('CopilotChat')
local cmp = require('cmp')

-- Configure cmp completion
local source = {}

function source:get_trigger_characters()
  local info = copilot_chat.complete_info()
  return info['triggers']
end

function source:get_keyword_pattern()
  local info = copilot_chat.complete_info()
  return info['pattern']
end

function source:complete(_, callback)
  local items = copilot_chat.complete_items() or {}
  local completion_kinds = vim.lsp.protocol.CompletionItemKind

  local mapped_items = vim.tbl_map(function(item)
    return {
      label = item.word,
      kind = completion_kinds[item.kind] or completion_kinds.Text,
      detail = item.info,
      documentation = item.menu,
    }
  end, items)

  callback(mapped_items)
end

function source:execute(completion_item, callback)
  callback(completion_item)
end

cmp.register_source('copilot_chat', source)

cmp.setup.filetype('copilot-chat', {
  completion = {
    autocomplete = false,
    completeopt = table.concat(vim.opt.completeopt:get(), ","),
  },
  sources = {
    { name = 'copilot_chat' },
  },
})

Note: this works for current main branch, more precisely for commit f84727f, but will not work for the v3.9.1 or older, because the complete_items() changed from receiving a callback to returning the list of items. If someone intend to copy these and use with a released version, you'll need to update the code. I'm using main because the version with the callback was much slower.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions