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

refactor(keys): use allowlist for valid opts #1792

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

devxpain
Copy link

Summary

This pull request refactors the opts function in LazyKeysHandler to use an allowlist for valid options instead of a blacklist. This change enhances code clarity and maintainability by explicitly defining accepted options, reducing the risk of unintentional errors from unsupported keys.

Changes Made

  • Replaced the previous blacklist approach with an allowlist for valid keys in the opts function of LazyKeysHandler.

Reason

I provided this pull request to allow the addition of which-key's key specifications to each plugin's keys attribute. The previous blacklist method restricted the addition of attributes that weren't explicitly allowed, such as the icon attribute, resulting in errors from Neovim. By using an allowlist, we can avoid these issues and freely include all necessary attributes in the key specifications. Following is an example of what we can do after applying the pull request.

In nvim/lua/plugins/notify.lua

return {
  "rcarriga/nvim-notify",
  dependencies = {
    "nvim-telescope/telescope.nvim",
  },
  keys = {
    { "<leader>n", group = "Notifications", icon = "󰨄" },
    { "<leader>nm", ":Notifications<CR>", desc = "Messages" },
    { "<leader>ng", ":Telescope notify<CR>", desc = "Grep", icon = "" },
    { "<leader>nd", ':lua require"notify".dismiss()<CR>', desc = "Dismiss", icon = "󰱝" },
  },
  init = function()
    vim.notify = require("notify")
  end,
}

In nvim/lua/plugins/which-key.lua

return {
  -- Provides keybindings for all your plugins
  "folke/which-key.nvim",
  event = "VeryLazy",
  init = function()
    vim.o.timeout = true
    vim.o.timeoutlen = 500
  end,
  config = function(_, opts)
    local wk = require("which-key")
    wk.setup(opts)

    local specs = {
      -- custom specs
    }
    ---@cast specs wk.Spec[]
    for _, plugin in pairs(require("lazy.core.config").plugins) do
      local keys = plugin.keys
      if keys then
        keys = vim.deepcopy(keys)
        if plugin.lazy then
          -- When plugin.lazy is true, we set key[2] to nil for each key mapping.
          -- This allows lazy.nvim to use the key mappings as triggers for loading the plugin,
          -- rather than executing the original RHS (right-hand side) function,
          -- which may not be available until the plugin is loaded.
          for _, key in ipairs(keys) do
            key[2] = nil
          end
        end
        vim.list_extend(specs, keys)
      end
    end
    wk.add(specs)
  end,
}

Instead of using a blacklist for specific keys, use an allowlist for valid keys in the `opts` function of `LazyKeysHandler`.
@devxpain devxpain changed the title Refactor keys: Use allowlist for valid options in LazyKeysHandler refactor(keys): use allowlist for valid opts Oct 28, 2024
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

Successfully merging this pull request may close these issues.

1 participant