yetone / avante.nvim

Use your Neovim like using Cursor AI IDE!
Apache License 2.0
7.51k stars 279 forks source link

feature: clear diff mappings when diff is done #567

Open apa512 opened 2 months ago

apa512 commented 2 months ago

Feature request

When entering Avante's diff window a few mappings starting with c get added. My issue is that these mappings persist even as the diff is done, making common core bindings such as cc or ct_ unavailable. To clear them, I have to open another file and then go back to the original file.

It would be better if this could somehow be cleared as soon as the diff is done.

Motivation

It would save time and avoid annoyance as my regularly used keybindings don't work unless I remember to switch files.

Other

I could potentially help although I'm not familiar with Lua or Neovim internals.

aarnphm commented 2 months ago

we already clear the mapping once all conflicts are resolved?

https://github.com/yetone/avante.nvim/blob/f9520c4fdfed08e9cc609d6cd319b358e4ea33a5/lua/avante/diff.lua#L400

can you provide a reproducer?

apa512 commented 2 months ago

Sure. I stripped my vim config down to only include avante and I'm still having the same issue. Here's the config:

local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not (vim.uv or vim.loop).fs_stat(lazypath) then
  local lazyrepo = "https://github.com/folke/lazy.nvim.git"
  local out = vim.fn.system({ "git", "clone", "--filter=blob:none", "--branch=stable", lazyrepo, lazypath })
  if vim.v.shell_error ~= 0 then
    vim.api.nvim_echo({
      { "Failed to clone lazy.nvim:\n", "ErrorMsg" },
      { out, "WarningMsg" },
      { "\nPress any key to exit..." },
    }, true, {})
    vim.fn.getchar()
    os.exit(1)
  end
end
vim.opt.rtp:prepend(lazypath)

vim.g.mapleader = ','
vim.g.maplocalleader = ','

require("lazy").setup({
  spec = {
    {
      'yetone/avante.nvim',
      event = 'Lazy',
      lazy = false,
      opts = {
        hints = {
          enabled = false
        },
        windows = {
          width = 50
        }
      },
      build = ':AvanteBuild',
      dependencies = {
        'stevearc/dressing.nvim',
        'nvim-lua/plenary.nvim',
        'MunifTanjim/nui.nvim',
        'nvim-tree/nvim-web-devicons',
        'zbirenbaum/copilot.lua',
        {
          'HakonHarnes/img-clip.nvim',
          event = 'VeryLazy',
          opts = {
            default = {
              embed_image_as_base64 = false,
              prompt_for_file_name = false,
              drag_and_drop = {
                insert_mode = true,
              },
              use_absolute_path = true,
            },
          },
        },
        {
          'MeanderingProgrammer/render-markdown.nvim',
          dependencies = {
            {
              'nvim-treesitter/nvim-treesitter',
              build = ":TSUpdate",
              config = function () 
                local configs = require("nvim-treesitter.configs")

                configs.setup({
                  ensure_installed = { "c", "lua", "vim", "ruby", "vimdoc", "query", "elixir", "json", "heex", "javascript", "html", "typescript", "tsx" },
                  sync_install = false,
                  highlight = { enable = true },
                  indent = { enable = true },
                })
              end
            }
          },
          opts = {
            file_types = { 'markdown', 'Avante' },
          },
          ft = { 'markdown', 'Avante' },
        },
      },
    },
  },
  checker = { enabled = false },
})

The steps outlined above results in the diff being done but with cc and other keys still being mapped to avante diff commands, confirmed by

  1. looking at :map
  2. trying cc and notice that it doesn't do anything
aarnphm commented 2 months ago

after diff is finished, cc is still binded?

Ja-sonYun commented 2 days ago

The same issue occurred to me. Even after the diff is completed, the cc command is still bound until I close the buffer

Ja-sonYun commented 2 days ago

https://github.com/yetone/avante.nvim/blob/f9520c4fdfed08e9cc609d6cd319b358e4ea33a5/lua/avante/diff.lua#L377 In my case, when execute the line vim.fn.hasmapto(mapping, “n”), it returns an empty value(possibly nil), which causes it to skip deleting the keymaps.

Also I couldn’t find the code to unbind the “v” keymaps in diff.lua, so the diff keymap won’t be completely removed.

Isn’t it better to dump the previous keymaps to somewhere, resolve the conflict, and then restore the keymaps from that dump?