Skip to content

Instantly share code, notes, and snippets.

@rodrigore
Created December 15, 2024 13:36
Show Gist options
  • Select an option

  • Save rodrigore/5546632a5e13e67f461ca4c0f5fe92ae to your computer and use it in GitHub Desktop.

Select an option

Save rodrigore/5546632a5e13e67f461ca4c0f5fe92ae to your computer and use it in GitHub Desktop.

Revisions

  1. rodrigore created this gist Dec 15, 2024.
    46 changes: 46 additions & 0 deletions nvim-cmp.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    local cmp = require("cmp")
    return {
    "hrsh7th/nvim-cmp",
    enabled = false,
    opts = function(_, opts)
    local has_words_before = function()
    unpack = unpack or table.unpack
    local line, col = unpack(vim.api.nvim_win_get_cursor(0))
    return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
    end

    local cmp = require("cmp")

    opts.completion = vim.tbl_extend("force", opts.completion, {
    autocomplete = false,
    })

    opts.mapping = vim.tbl_extend("force", opts.mapping, {
    ["<Tab>"] = cmp.mapping(function(fallback)
    local copilot_ok, copilot_suggestion = pcall(require, "copilot.suggestion")
    if vim.snippet.active({ direction = 1 }) then
    vim.schedule(function()
    vim.snippet.jump(1)
    end)
    elseif copilot_ok and copilot_suggestion.is_visible() then
    copilot_suggestion.accept()
    elseif has_words_before() then
    cmp.complete()
    else
    fallback()
    end
    end, { "i", "s" }),
    ["<S-Tab>"] = cmp.mapping(function(fallback)
    local copilot_ok, copilot_suggestion = pcall(require, "copilot.suggestion")
    if vim.snippet.active({ direction = -1 }) then
    vim.schedule(function()
    vim.snippet.jump(-1)
    end)
    elseif copilot_ok and copilot_suggestion.is_visible() then
    copilot_suggestion.next()
    else
    fallback()
    end
    end, { "i", "s" }),
    })
    end,