folke / which-key.nvim

💥 Create key bindings that stick. WhichKey helps you remember your Neovim keymaps, by showing available keybindings in a popup as you type.
Apache License 2.0
5.38k stars 177 forks source link

bug: How do I launch which-key with a specific list of bindings from a which-key binding? #523

Closed JPDucky closed 3 months ago

JPDucky commented 1 year ago

Did you check docs and existing issues?

Neovim version (nvim -v)

v0.10.0-dev-530+g8376e8700, neovim-nightly snap

Operating system/version

fedora 37

Describe the bug

I am trying to have which-key show a specific set of bindings when I press a keymap. Currently the example I'm working on is I have ['wp'] set to launch the telescope-project window. I want when that window launches, for which-key to display the keys that I have defined for that window. Right now I have my mappings in another file with my defaults working, and in my plugin file I have wk.register(mappings), which again works. The next line I have wk.register({ ['<leader>wk'] = { project_keys } }), where project keys is a function I've imported as project_keys from the mappings file (don't worry about naming, I've shortened things a bit for explanantion):

local function telescope_project_keys()
  require('telescope').extensions.project.project{ display_type = 'full' }
  wk.show({
    d = { "Delete Project" },
    r = { "Rename Project" },
    c = { "Add Project" },
    C = { "Add Project CWD" },
    f = { "Find Project" },
    b = { "Browse Project Files" },
    s = { "Search in Project Files" },
    R = { "Recent Project Files" },
    w = { "Change Working Directory" },
  }, "Project" )
end

I know that this isn't quite right, but I haven't been able to figure out how to properly do this. Any help is appreciated.

Steps To Reproduce

  1. Press ['<leader>wp']
  2. Neovim errors with
    Invalid mapping for {
    opts = {
    prefix = "<leader>wp"
    },
    value = { <function 1> }
    }

Expected Behavior

  1. Press ['<leader>wp']
  2. telescope-project launches, with the which-key keys displayed as well

Repro

-- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
  vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", lazypath, })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
  "folke/tokyonight.nvim",
  { "folke/which-key.nvim", config = true },
  -- add any other plugins here
  {
    'nvim-telescope/telescope.nvim',
    version = false,
    dependencies = {
      'nvim-lua/plenary.nvim',
      {
        'nvim-telescope/telescope-fzf-native.nvim',
        build = 'make',
        cond = function()
          return vim.fn.executable 'make' == 1
        end,
      },
      'debugloop/telescope-undo.nvim',
      {
        'nvim-telescope/telescope-project.nvim',
        dependencies = {
          'nvim-telescope/telescope-file-browser.nvim'
        },
      },
    },
    opts = {
      extensions = {
        undo = {},
      },
      pickers = {
      },
    },
    config = function()
      require('telescope').setup({
        extensions = {
          undo = {
            side_by_side = true,
            layout_strategy = "vertical",
            layout_config = {
              preview_height = 0.85,
            }
          },
          fzf = {},
          project = {},
        }
      })
    end,
  },
}

require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
-- add anything else here

local wk = require 'which-key'

local function telescope_project_keys()
  require('telescope').extensions.project.project{ display_type = 'full' }
  wk.show({
    d = { "Delete Project" },
    r = { "Rename Project" },
    c = { "Add Project" },
    C = { "Add Project CWD" },
    f = { "Find Project" },
    b = { "Browse Project Files" },
    s = { "Search in Project Files" },
    R = { "Recent Project Files" },
    w = { "Change Working Directory" },
  }, "Project" )
end

local mappings = {
    ['<leader>'] = {
      a = {
        name = '+Actions',
      },
    },
    -- ... rest of config ... --
  }

wk.register(mappings)
wk.register({
  ['<leader>'] = {
    wp = { telescope_project_keys },
  },
})
github-actions[bot] commented 4 months ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.