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

feature: Stable API for retrieving keymap definitions #1133

Closed
1 task done
mrjones2014 opened this issue Oct 17, 2023 · 5 comments
Closed
1 task done

feature: Stable API for retrieving keymap definitions #1133

mrjones2014 opened this issue Oct 17, 2023 · 5 comments
Labels
enhancement New feature or request

Comments

@mrjones2014
Copy link

Did you check the docs?

  • I have read all the lazy.nvim docs

Is your feature request related to a problem? Please describe.

Hi, feel free to close if you feel its out of scope or not planned etc.

I was just wondering if you'd consider supporting a stable, public API for retrieving keymap tables from plugin specs.

In legendary.nvim I've got an extension that auto-loads all keymaps defined via lazy.nvim into legendary. I got the original implementation from you in GitHub Discussions.

However, yesterday, there was a breaking change in the API, so I fixed it with this PR. It seems like this is a private API which doesn't necessarily remain stable.

Describe the solution you'd like

A simple, public API for grabbing all keymaps defined in plugin specs, maybe something like this?

local lazy = require('lazy')
for plugin, spec in pairs(lazy.get_specs()) do
  local keys = lazy.get_keymaps(plugin, spec)
  -- do whatever with the keys tables
end

Describe alternatives you've considered

PR to fix when there's breaking changes. Which is fine if you decide that's the answer.

Additional context

No response

@folke
Copy link
Owner

folke commented Oct 17, 2023

I just responded to your PR. You can get the plugin's keymaps with:

local keys = plugin._.handlers.keys or {}

So you no longer need to use the internal Handler apis.

I can't promise this will always remain where it is now.

Plugin._ contains state information about a plugin in lazy.

@folke folke closed this as not planned Won't fix, can't repro, duplicate, stale Oct 17, 2023
@mrjones2014
Copy link
Author

Sounds good. Thanks for the context and PR comment!

@folke
Copy link
Owner

folke commented Oct 17, 2023

One caveat. plugin._.handlers can actually be nil as well.
But it should always hold a value at the time legendary loads.

But maybe better to add an extra check there just in case :)

@mrjones2014
Copy link
Author

It looks like, at the time legendary is loaded, it's nill for lazy.nvim's own plugin spec, but none others. I used vim.tbl_get to safely get the value. Thanks!

@folke
Copy link
Owner

folke commented Oct 17, 2023

Ah right, yes, lazy.nvim's spec is a special case. No handlers for that one.

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

No branches or pull requests

2 participants