debugloop / telescope-undo.nvim

A telescope extension to view and search your undo tree šŸŒ“
MIT License
634 stars 11 forks source link

Expected behavior of key mappings seems like they are not working. #38

Closed freddiehaddad closed 10 months ago

freddiehaddad commented 1 year ago

How are the key maps supposed to work with this extension?

Regardless of whether I press Shift+Enter, Ctrl+Enter, or just Enter, the behavior is the same. The addition gets added to the clipboard as can be seen by pressing paste.

If I have an undo snapshot that looks like this:

--- lua/plugins/telescope.lua
+++ lua/plugins/telescope.lua
@@ -109,7 +109,7 @@
-  mappings = {
-    i = {
-      ['<cr>'] = function() vim.notify('enter') end,
-      ['<S-cr>'] = function() vim.notify('shift enter') end,
-      ['<C-cr>'] = function() vim.notify('ctrl enter') end,
-    },
-  },
+  --mappings = {
+  --      i = {
+  --              ['<cr>'] = function() vim.notify('enter') end,
+  --              ['<S-cr>'] = function() vim.notify('shift enter') end,
+  --              ['<C-cr>'] = function() vim.notify('ctrl enter') end,
+  --      },
+  --},

If i'm in INSERT mode (which I am when opening Telescope undo), I would expect Shift+Enter to yank:

mappings = {
  i = {
    ['<cr>'] = function() vim.notify('enter') end,
    ['<S-cr>'] = function() vim.notify('shift enter') end,
    ['<C-cr>'] = function() vim.notify('ctrl enter') end,
  },
},

And Ctrl+Enter to yank:

--mappings = {
--      i = {
--              ['<cr>'] = function() vim.notify('enter') end,
--              ['<S-cr>'] = function() vim.notify('shift enter') end,
--              ['<C-cr>'] = function() vim.notify('ctrl enter') end,
--      },
--},

Neither of these things happen and the addition is always yanked regardless of if I press Enter, Shift+Enter, or Ctrl+Enter. Same is true in normal mode with y, Y, and Enter.

As you can see from the example, I tried overriding the key mappings with a function call that prints a message. However, they don't get printed on key press. When I open telescope undo, I do see the 'enter' message which seems strange.

Here's the relevant parts of my lazy.nvim config:

https://github.com/freddiehaddad/nvim/blob/main/lua/plugins/telescope.lua

freddiehaddad commented 1 year ago

FWIW: I changed the default mappings to:

i = {
    ['<cr>'] = function(...) return require('telescope-undo.actions').yank_additions(...) end,
    ['<c-y>'] = function(...) return require('telescope-undo.actions').yank_deletions(...) end,
    ['<c-r>'] = function(...) return require('telescope-undo.actions').restore(...) end,
 },
 n = {
    ['y'] = function(...) return require('telescope-undo.actions').yank_additions(...) end,
    ['Y'] = function(...) return require('telescope-undo.actions').yank_deletions(...) end,
    ['r'] = function(...) return require('telescope-undo.actions').restore(...) end,
},

Now things are are working as expected. It seems the Ctrl+Enter and Shift+Enter keymaps aren't working for me.

freddiehaddad commented 1 year ago

Attaching my Telescope config in lazy.nvim for convenience:

return {
    {
        'nvim-telescope/telescope.nvim',
        dependencies = { { 'nvim-lua/plenary.nvim' }, { 'EdenEast/nightfox.nvim' } },
        init = function()
            local hl = vim.api.nvim_set_hl
            local p = require('nightfox.palette').load('carbonfox')

            hl(0, 'TelescopePreviewBorder', { fg = p.blue.dim })
            hl(0, 'TelescopePreviewTitle', { fg = p.blue.base })

            hl(0, 'TelescopePromptBorder', { fg = p.cyan.dim })
            hl(0, 'TelescopePromptTitle', { fg = p.cyan.bright })

            hl(0, 'TelescopeResultsBorder', { fg = p.orange.dim })
            hl(0, 'TelescopeResultsTitle', { fg = p.orange.bright })
        end,
        keys = {
            -- f
            {
                '<leader>ff',
                function() require('telescope.builtin').find_files() end,
                desc = 'Find files',
            },
            {
                '<leader>fF',
                function() require('telescope.builtin').find_files({ hidden = true, no_ignore = true }) end,
                desc = 'Find files (hidden)',
            },
            {
                '<leader>fb',
                function() require('telescope.builtin').buffers({ ignore_current_buffer = true, sort_mru = true }) end,
                desc = 'Find buffers',
            },

            -- s
            {
                '<leader>sb',
                function() require('telescope.builtin').current_buffer_fuzzy_find() end,
                desc = 'Search buffer',
            },
            {
                '<leader>sg',
                function() require('telescope.builtin').live_grep() end,
                desc = 'Grep files',
            },
            {
                '<leader>sG',
                function() require('telescope.builtin').live_grep({ grep_open_files = true }) end,
                desc = 'Grep files (Open files)',
            },
            {
                '<leader>sh',
                function() require('telescope.builtin').help_tags() end,
                desc = 'Search help tags',
            },
            {
                '<leader>sH',
                function() require('telescope.builtin').highlights() end,
                desc = 'Search highlights',
            },
            {
                '<leader>sw',
                function() require('telescope.builtin').grep_string({ word_match = '-w' }) end,
                desc = 'Search word',
            },
            {
                '<leader>sw',
                function() require('telescope.builtin').grep_string({ word_match = '-w' }) end,
                mode = 'v',
                desc = 'Search selection',
            },

            {
                '<leader>R',
                function() require('telescope.builtin').resume() end,
                desc = 'Resume',
            },
        },
        opts = {
            defaults = {
                -- use square corners
                borderchars = { 'ā”€', 'ā”‚', 'ā”€', 'ā”‚', 'ā”Œ', 'ā”', 'ā”˜', 'ā””' },
            },
        },
    },

    -- native fuzzy search for telescope
    {
        'nvim-telescope/telescope-fzf-native.nvim',
        build = 'make',
        dependencies = {
            'nvim-telescope/telescope.nvim',
            opts = { extensions = { fzf = {} } },
        },
        init = function() require('telescope').load_extension('fzf') end,
    },

    -- telescope undo extension
    {
        'debugloop/telescope-undo.nvim',
        dependencies = {
            'nvim-telescope/telescope.nvim',
            opts = {
                extensions = {
                    undo = {
                        use_delta = false,
                        layout_config = {
                            preview_width = 0.75,
                        },
                        mappings = {
                            i = {
                                ['<cr>'] = function(...) return require('telescope-undo.actions').yank_additions(...) end,
                                ['<c-y>'] = function(...) return require('telescope-undo.actions').yank_deletions(...) end,
                                ['<c-r>'] = function(...) return require('telescope-undo.actions').restore(...) end,
                            },
                            n = {
                                ['y'] = function(...) return require('telescope-undo.actions').yank_additions(...) end,
                                ['Y'] = function(...) return require('telescope-undo.actions').yank_deletions(...) end,
                                ['r'] = function(...) return require('telescope-undo.actions').restore(...) end,
                            },
                        },
                    },
                },
            },
        },
        init = function() require('telescope').load_extension('undo') end,
        keys = {
            { '<leader>su', function() require('telescope').extensions.undo.undo() end, desc = 'Search undo history' },
        },
    },
}
debugloop commented 11 months ago

Hi, yes indeed <c-cr> and <s-cr> are kinda known to cause problems in various terminal emulators, especially with a multiplexer in the mix. Tbh, I mostly use normal mode with the undo telescope... I think I'll adopt yours as an additional default for insert mode in a future version šŸ‘šŸ»

haizaar commented 11 months ago

Hitting this issue as well - out of the default mappings only <cr> is working. Somehow @freddiehaddad's workaround doesn't work for me.

My config is here (loading via NvChad): https://github.com/zarmory/NvChad/blob/v2.0/lua/custom/plugins.lua#L51

The plugin is still useful (thank you!), just not as useful as it could've been.

freddiehaddad commented 11 months ago

@haizaar Can you post your config with the key map changes as well? I'm curious as to why my suggested modifications aren't working. It's either not being configured correctly or conflicting key maps.

If you notice in my example, I'm setting telescope as a dependency of the undo extension. This way I configured the key maps in telescope prior to loading the extension which I'm fairly sure is necessary.

debugloop commented 11 months ago

@freddiehaddad is right, the way Telescope works you'll always have to set telescope as a dependency. The other trick is to realize that you can call Telescope's setup as often as you'd like, it will merge configs (including additional keymaps) from consecutive calls. The second thing is, you wont be able to set keymaps in Lazy's opts field, as the require is likely not available yet due to lazy loading of some form (you could get that right, but...), so you best do that from config.

Looking at your config, you're never calling load_extension which telescope requires you to do.

I suggest doing this in addition to whatever you use as a standalone telescope plugin spec:

{
  "debugloop/telescope-undo.nvim",
  dependencies = {
    {
      "nvim-telescope/telescope.nvim",
      dependencies = {
        { "nvim-lua/plenary.nvim" },
      },
    },
  },
  keys = {
    {
      "<leader>u",
      "<cmd>Telescope undo<cr>",
      desc = "undo history",
    },
  },
  opts = {
    extensions = {
      undo = {
        side_by_side = true,
        layout_strategy = "vertical",
        layout_config = {
          preview_height = 0.8,
        },
      },
    },
  },
  config = function(_, opts)
    opts.defaults.mappings = {
      i = {
        ['<c-y>'] = require('telescope-undo.actions').yank_deletions,
        ['<c-r>'] = require('telescope-undo.actions').restore,
      },
    }
    require("telescope").setup(opts)
    require("telescope").load_extension("undo")
  end,
}

I'll put the two keymaps in asap, I should have never assumed proper handling of keycodes in people's terminals šŸ™ƒ

debugloop commented 10 months ago

The additional defaults are in now, I also added some general clarifications on configuring, mapping, and so on. Hope it helps, thanks for your feedback šŸ„‡

haizaar commented 10 months ago

Thanks for the quick response. Looks like the opts table doesn't have defaults member:

Failed to run `config` for telescope-undo.nvim                                                                                                                                                                                                                                  
/home/.../.config/nvim/lua/custom/plugins.lua:96: attempt to index field 'defaults' (a nil value)
# stacktrace:
  - lua/custom/plugins.lua:96 _in_ **config**
  - /telescope.nvim/lua/telescope/_extensions/init.lua:8 _in_ **load_extension**
  - /telescope.nvim/lua/telescope/_extensions/init.lua:62
  - /telescope.nvim/lua/telescope/command.lua:197 _in_ **run_command**
  - /telescope.nvim/lua/telescope/command.lua:259 _in_ **load_command**
  - /telescope.nvim/plugin/telescope.lua:108

My config is here: https://github.com/zarmory/NvChad/commit/fa9bb4a5705af6b39215bf503c2a147aa961343b

haizaar commented 10 months ago

After removing the mapping definitions, it all works fine (since they are now defaults - thank you for adding those).

P.S. Telescope itself is initialized by NvChad here, in case it's related.

debugloop commented 10 months ago

Yup, makes sense, I've actually had a small error in above snippet (the assignment should not have been to default). If you keep to the readme it should work alright :+1: