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

feat(integrations): Optionally autoload keymaps from lazy.nvim plugin specs #382

Merged
merged 1 commit into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Define your keymaps, commands, and autocommands as simple Lua tables, building a

- Define your keymaps, commands, `augroup`/`autocmd`s, and even arbitrary Lua functions to run on the fly, as simple Lua tables, then bind them with `legendary.nvim`
- Integration with [which-key.nvim](https://github.com/folke/which-key.nvim), use your existing `which-key.nvim` tables with `legendary.nvim`
- Integration with [lazy.nvim](https://github.com/folke/lazy.nvim), automatically load keymaps defined via `lazy.nvim`'s `keys` property on plugin specs
- Execute normal, insert, and visual mode keymaps, commands, autocommands, and Lua functions when you select them
- Show your most recently executed items at the top when triggered via `legendary.nvim` (can be disabled via config)
- Uses `vim.ui.select()` so it can be hooked up to a fuzzy finder using something like [dressing.nvim](https://github.com/stevearc/dressing.nvim) for a VS Code command palette like interface
Expand Down Expand Up @@ -65,12 +66,20 @@ With `lazy.nvim`:
{
'mrjones2014/legendary.nvim',
version = 'v2.1.0',
-- since legendary.nvim handles all your keymaps/commands,
-- its recommended to load legendary.nvim before other plugins
priority = 10000,
lazy = false,
-- sqlite is only needed if you want to use frecency sorting
-- dependencies = { 'kkharji/sqlite.lua' }
}
-- or, to get rolling updates
{
'mrjones2014/legendary.nvim',
-- since legendary.nvim handles all your keymaps/commands,
-- its recommended to load legendary.nvim before other plugins
priority = 10000,
lazy = false,
-- sqlite is only needed if you want to use frecency sorting
-- dependencies = { 'kkharji/sqlite.lua' }
}
Expand All @@ -90,7 +99,41 @@ Plug "mrjones2014/legendary.nvim"

## Quickstart

Register keymaps through setup:
If you use [lazy.nvim](https://github.com/folke/lazy.nvim) for your plugin manager, `legendary.nvim` can automatically
register keymaps defined via the `keys` property of `lazy.nvim` plugin specs. This lets you keep your plugin-specific
keymaps where you define the plugin, and `legendary.nvim` automatically detects them. For example:

```lua
-- in a plugin spec:
return {
'folke/flash.nvim',
keys = {
{
's',
function()
require('flash').jump()
end,
mode = { 'n', 'x', 'o' },
desc = 'Jump forwards',
},
{
'S',
function()
require('flash').jump({ search = { forward = false } })
end,
mode = { 'n', 'x', 'o' },
desc = 'Jump backwards',
},
},
}

-- where you set up legendary.nvim
-- now the keymaps from the `flash.nvim` plugin spec will be automatically loaded
require('legendary').setup({ lazy_nvim = { auto_register = true } })
```

Otherwise, register keymaps, commands, autocmds, and functions through setup, including
opting into _extensions_ which can automatically load keymaps and commands from other plugins:

```lua
require('legendary').setup({
Expand Down Expand Up @@ -357,6 +400,11 @@ require('legendary').setup({
max_timestamps = 10,
},
},
lazy_nvim = {
-- Automatically register keymaps that are defined on lazy.nvim plugin specs
-- using the `keys = {}` property.
auto_register = false,
},
which_key = {
-- Automatically add which-key tables to legendary
-- see ./doc/WHICH_KEY.md for more details
Expand Down
9 changes: 9 additions & 0 deletions lua/legendary/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ local config = {
max_timestamps = 10,
},
},
lazy_nvim = {
-- Automatically register keymaps that are defined on lazy.nvim plugin specs
-- using the `keys = {}` property.
auto_register = false,
},
which_key = {
-- Automatically add which-key tables to legendary
-- see ./doc/WHICH_KEY.md for more details
Expand Down Expand Up @@ -133,6 +138,9 @@ local config = {
log_level = 'info',
}

---@class LegendaryLazyNvimConfig
---@field auto_register boolean

---@class LegendaryWhichkeyConfig
---@field auto_register boolean
---@field mappings table[]
Expand Down Expand Up @@ -176,6 +184,7 @@ local config = {
---@field include_builtin boolean
---@field include_legendary_cmds boolean
---@field sort LegendarySortOpts
---@field lazy_nvim LegendaryLazyNvimConfig
---@field which_key LegendaryWhichkeyConfig
---@field extensions table<string, any>
---@field scratchpad LegendaryScratchpadConfig
Expand Down
5 changes: 5 additions & 0 deletions lua/legendary/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ local ItemGroup = Lazy.require_on_exported_call('legendary.data.itemgroup')
local Log = Lazy.require_on_exported_call('legendary.log')

local Extensions = Lazy.require_on_exported_call('legendary.extensions')
local LegendaryLazyNvim = Lazy.require_on_index('legendary.integrations.lazy-nvim')
local LegendaryWhichKey = Lazy.require_on_index('legendary.integrations.which-key')

---@param parser LegendaryItem
Expand Down Expand Up @@ -84,6 +85,10 @@ end
function M.setup(cfg)
Config.setup(cfg)

if Config.lazy_nvim.auto_register then
LegendaryLazyNvim.load_lazy_nvim_keys()
end

if Config.which_key.auto_register then
LegendaryWhichKey.whichkey_listen()
end
Expand Down
33 changes: 33 additions & 0 deletions lua/legendary/integrations/lazy-nvim.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
local Log = require('legendary.log')

local M = {}

function M.load_lazy_nvim_keys()
local has_lazy, _ = pcall(require, 'lazy')
if not has_lazy then
Log.warn("lazy.nvim integration is enabled, but cannot `require('lazy')`, aborting.")
return
end

local LazyNvimConfig = require('lazy.core.config')
local Handler = require('lazy.core.handler')
for _, plugin in pairs(LazyNvimConfig.plugins) do
local keys = Handler.handlers.keys:values(plugin)
for _, keymap in pairs(keys) do
if keymap.desc and #keymap.desc > 0 then
-- we don't need the implementation, since
-- lazy.nvim will have already bound it. We
-- just need the description-only item to appear
-- in the legendary.nvim finder.
local legendary_keymap = {
keymap[1], -- lhs
description = keymap.desc,
mode = keymap.mode, ---@type string|string[]|nil
}
require('legendary').keymap(legendary_keymap)
end
end
end
end

return M
Loading