ChatGPT.nvim icon indicating copy to clipboard operation
ChatGPT.nvim copied to clipboard

ChatGPTCompleteCode - Pressing Enter (<CR>) to accept code completion causes the line to be overwritten.

Open danielju91 opened this issue 1 year ago • 0 comments

I am hoping I just have my nvim misconfigured, but when I call ChatGPTCompleteCode code while in insert mode and the cursor on the line with some code, then accept the suggestion, it overwrites the line that the cursor is on.

https://github.com/user-attachments/assets/a80e8ef2-f5c7-4eb3-ad4a-bbdd0b363937

Below is my chatgpt.lua file

return {
  "jackMort/ChatGPT.nvim",
    event = "VeryLazy",
    config = function()
      require("chatgpt").setup({
            api_key_cmd = 'echo $OPENAI_API_KEY',
        })
              -- Define custom leader key mappings
      local map = vim.api.nvim_set_keymap
      local opts = { noremap = true, silent = true }
      -- Map <Leader>ctn to :ChatGPTCompleteCode
      map('n', '<Leader>ctn', ':ChatGPTCompleteCode<CR>', opts)

    end,
    dependencies = {
      "MunifTanjim/nui.nvim",
      "nvim-lua/plenary.nvim",
      "folke/trouble.nvim", -- optional
      "nvim-telescope/telescope.nvim"
    }
}

Below is my nvim-cmp.lua file that I am including in case it is relevant:

return{
    {
        'hrsh7th/nvim-cmp',
        lazy = false,
        priority = 100,
        dependencies = {
            'hrsh7th/cmp-nvim-lsp',
            'hrsh7th/cmp-buffer',
            'hrsh7th/cmp-path',
            'hrsh7th/cmp-nvim-lsp',
            'hrsh7th/cmp-nvim-lsp-signature-help',
            'onsails/lspkind.nvim',
            'ray-x/cmp-treesitter',
            'L3MON4D3/LuaSnip'
        },
        event = "InsertEnter",
        config = function()
        local cmp = require 'cmp'
        local luasnip = require 'luasnip'
        cmp.setup {
            snippet = {
                expand = function(args)
                    luasnip.lsp_expand(args.body)
                end,
            },
            formatting = {
                format = require 'lspkind'.cmp_format {
                    mode = "symbol_text",
                    menu = {
                        nvim_lsp = "[LSP]",
                        buffer = "[Buffer]",
                        latex_symbols = "[Latex]",
                        luasnip = "[LuaSnip]",
                    }
                }
            },
            window = {
                completion = cmp.config.window.bordered(),
                documentation = cmp.config.window.bordered(),
            },
            view = {
                entries = {
                    name = 'custom',
                    selection_order = 'near_cursor'
                }
            },
            mapping = cmp.mapping.preset.insert({
                ['<C-b>'] = cmp.mapping.scroll_docs(-4),
                ['<C-f>'] = cmp.mapping.scroll_docs(4),
                ['<C-Space>'] = cmp.mapping.complete(),
                ['<CR>'] = cmp.mapping.confirm {
                    behavior = cmp.ConfirmBehavior.Replace,
                    select = false,
                },
                ['<Tab>'] = cmp.mapping(function(fallback)
                    if cmp.visible() then
                        cmp.select_next_item()
                    elseif luasnip.expand_or_jumpable() then
                        luasnip.expand_or_jump()
                    else
                        fallback()
                    end
                end, { 'i', 's' }),
                ['<S-Tab>'] = cmp.mapping(function(fallback)
                    if cmp.visible() then
                        cmp.select_prev_item()
                    elseif luasnip.jumpable(-1) then
                        luasnip.jump(-1)
                    else
                        fallback()
                    end
                end, { 'i', 's' }),
            }),
            sources = cmp.config.sources({
                { name = 'nvim_lsp' },
                { name = 'luasnip' },
                { name = 'buffer' },
                { name = 'calc' },
                { name = 'path' },
                { name = 'treesitter' },
            })
        }
    end
    }

}

My Plugins: image

Telescope FG result for <CR>: image

I was hoping that I could use this like Copilot or Cursor's code completion, but because this is happening, I have been unable to take advantage of this feature.

Hopefully I simply missed some configuration that I have to write in.

danielju91 avatar Nov 12 '24 15:11 danielju91