Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix which-key errors #483

Open
NeilGirdhar opened this issue Jul 31, 2024 · 12 comments
Open

Fix which-key errors #483

NeilGirdhar opened this issue Jul 31, 2024 · 12 comments

Comments

@NeilGirdhar
Copy link

I was forced to do this to suppress the which-key warnings:

    -- Commenting
    {
        'numToStr/Comment.nvim',
        config = function()
            require('Comment').setup()
            vim.keymap.del("n", "gc")
            vim.keymap.del("n", "gb")
            local wk = require('which-key')
            wk.add({
                { "gb", group = "Comment toggle blockwise" },
                { "gc", group = "Comment toggle linewise" },
            })
        end
    },
@thomastthai
Copy link

I landed here when I found which-key.nvim was causing Comment.nvim gcc to not work. I'm still trying to solve the issue.

Which warning did you get from which-key? How does your snippet remove the warning? I see that "gc" and "gb" keymaps in normal mode are removed. Those keymaps are added back via wk.add()?

@NeilGirdhar
Copy link
Author

Yes because the the mappings in Comment.nvim are ordinary mappings rather than groups. This conflicts with nested mappings. This snippet removes the bad mappings and replaces them with groups, which doesn't conflict.

@thomastthai
Copy link

thomastthai commented Oct 2, 2024

Perhaps I misunderstood something. require('Comment').setup() sets the default keymaps for Comment.nvim that include gc and gb. Then vim.keymap.del("n", "gc") and vim.keymap.del("n", "gb") delete those default keymaps. For those keymaps to work again, wouldn't each keymap in wk.add() need additional RHS parameters?

Some RHS parameters from Comment.txt at around line 267:

Usage: ~
>lua
    -- Toggle current line or with count
    vim.keymap.set('n', 'gcc', function()
        return vim.v.count == 0
            and '<Plug>(comment_toggle_linewise_current)'
            or '<Plug>(comment_toggle_linewise_count)'
    end, { expr = true })

    -- Toggle in Op-pending mode
    vim.keymap.set('n', 'gc', '<Plug>(comment_toggle_linewise)')

    -- Toggle in VISUAL mode
    vim.keymap.set('x', 'gc', '<Plug>(comment_toggle_linewise_visual)')
<

@NeilGirdhar
Copy link
Author

NeilGirdhar commented Oct 2, 2024

For those keymaps to work again,

Those keymaps don't need to "work" since they're supposed to be group-keymaps. We don't want to run those plug-commands. We want to run nothing and set the which-key heading. gc should do this:

Screenshot 2024-10-02 at 4 39 26 PM

Some RHS parameters from Comment.txt at around line 267:

Yes, those look like the problematic lines.

@thomastthai
Copy link

For those coming here from the future, the which-key warnings @NeilGirdhar may be referring to are from :checkhealth which-key are similar to:

- WARNING In mode `n`, <gb> overlaps with <gbc>:
  - <gb>: Comment toggle blockwise
  - <gbc>: Comment toggle current block
- WARNING In mode `n`, <gc> overlaps with <gco>, <gcA>, <gcc>, <gcO>:
  - <gc>: Comment toggle linewise
  - <gco>: Comment insert below
  - <gcA>: Comment insert end of line
  - <gcc>: Comment toggle current line
  - <gcO>: Comment insert above
- WARNING In mode `n`, <<Space>gc> overlaps with <<Space>gco>, <<Space>gcA>, <<Space>gcc>, <<Space>gcO>:
  - <<Space>gc>: Comment toggle linewise
  - <<Space>gco>: Comment insert below
  - <<Space>gcA>: Comment insert end of line
  - <<Space>gcc>: Comment toggle current line
  - <<Space>gcO>: Comment insert above
- WARNING In mode `n`, <<Space>gb> overlaps with <<Space>gbc>:
  - <<Space>gb>: Comment toggle blockwise
  - <<Space>gbc>: Comment toggle current block

I changed my keymaps for Comment.nvim to use my key which is , which may be different than yours.

@thomastthai
Copy link

thomastthai commented Oct 2, 2024

@NeilGirdhar , thanks for taking the time to explain. I'll summarize to help others new to which-key, nvim, or Comment.nvim save time down the rabbit hole.

I made many configuration changes during my experiments, including using gb and gc as keymaps, not just group-keymaps! So in my head, I kept thinking if they get deleted, they can't be used again. For the default settings, Comment.nvim sets gc and gb as "ordinary mappings rather than groups." gcc and gbc are "ordinary" keymaps. Therefore, deleting gc and gb, and re-adding them as group-keymap via wk.add() removes the warnings, and gcc and gbc continue to work.

@NeilGirdhar
Copy link
Author

You got it! Do you feel like trying to get a pull request through to remove these from Comment.nvim? Maybe even (somehow, I have no idea how) make it add the group bindings if which-key is installed? That would clean this up for all users 😄

@thomastthai
Copy link

Having Comment.nvim detect if which-key is in use and addressing the issue within the Comment.nvim's source code would be idea. I am still new to Lua and nvim to be able to get a pull request though to fix this.

@bak
Copy link

bak commented Oct 6, 2024

I'm curious if either of you updated your which-key config for v3 — I found the :checkhealth which-key warnings for all my mappings (it was complaining about others besides Comment.nvim) went away after I updated the config as described here: https://github.com/folke/which-key.nvim#-installation.

@NeilGirdhar
Copy link
Author

@bak Which installation option are you talking about? I get

checking for overlapping keymaps ~
- WARNING In mode `n`, <gc> overlaps with <gcO>, <gcc>, <gco>, <gcA>:
  - <gc>: Comment toggle linewise
  - <gcO>: Comment insert above
  - <gcc>: Comment toggle current line
  - <gco>: Comment insert below
  - <gcA>: Comment insert end of line
- WARNING In mode `n`, <gb> overlaps with <gbc>:
  - <gb>: Comment toggle blockwise
  - <gbc>: Comment toggle current block
- OK Overlapping keymaps are only reported for informational purposes.
  This doesn't necessarily mean there is a problem with your config.

without the above configuration.

@bak
Copy link

bak commented Oct 8, 2024

Yeah sorry I don't know exactly the change that fixed things, but I was getting the same output as you, and found this thread, but noticed that which-key released 3.0 about the time of your first post and went to check out their README to see what might have changed. I replaced what I had with their suggested config:

{
  "folke/which-key.nvim",
  event = "VeryLazy",
  opts = {
    -- your configuration comes here
    -- or leave it empty to use the default settings
    -- refer to the configuration section below
  },
  keys = {
    {
      "<leader>?",
      function()
        require("which-key").show({ global = false })
      end,
      desc = "Buffer Local Keymaps (which-key)",
    },
  },
}

and haven't had a problem since

@NeilGirdhar
Copy link
Author

I don't think that works. Try removing the config and see if the errors reappear. If not, then this isn't really a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants