Skip to content

Commit

Permalink
Merge pull request #400 from mrjones2014/mrj/399/lazy-nvim-and-whichk…
Browse files Browse the repository at this point in the history
…ey-extensions

feat(extension)!: Move which-key.nvim and lazy.nvim integrations to extensions
  • Loading branch information
mrjones2014 authored Oct 10, 2023
2 parents a2ea9a4 + 433c06e commit 9744bf8
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 91 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ Define your keymaps, commands, and autocommands as simple Lua tables, building a
- A parser to convert Vimscript keymap commands (e.g. `vnoremap <silent> <leader>f :SomeCommand<CR>`) to `legendary.nvim` keymap tables (see [Converting Keymaps From Vimscript](./doc/API.md#converting-keymaps-from-vimscript))
- Anonymous mappings; show mappings/commands in the finder without having `legendary.nvim` handle creating them
- Extensions to automatically load keymaps and commands from other plugins
- The internal extension API is considered unstable, as it will likely need to evolve as we add extensions for additional plugins with different setups. This mostly affects plugin developers, not users.

## Prerequisites

Expand Down
42 changes: 42 additions & 0 deletions doc/EXTENSIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
- [Extensions](#extensions)
- [Extension API](#extension-api)
- [Built-in Extensions](#built-in-extensions)
- [`Lazy.nvim`](#lazynvim)
- [`Which-Key.nvim`](#which-keynvim)
- [`nvim-tree.lua`](#nvim-treelua)
- [`smart-splits.nvim`](#smart-splitsnvim)
- [`op.nvim`](#opnvim)
Expand Down Expand Up @@ -61,6 +63,46 @@ Loading keymaps/commands/etc. from an extension can use the same APIs a user wou

## Built-in Extensions

### `Lazy.nvim`

Automatically load key mappings defined in [lazy.nvim](https://github.com/folke/lazy.nvim) plugin specs
into `legendary.nvim`.

```lua
require('legendary').setup({
lazy_nvim = true,
})
```

### `Which-Key.nvim`

Automatically load key mappings defined by [which-key.nvim](https://github.com/folke/which-key.nvim) into `legendary.nvim`.

```lua
require('legendary').setup({
extensions = {
which_key = {
-- Automatically add which-key tables to legendary
-- see WHICH_KEY.md for more details
auto_register = false,
-- you can put which-key.nvim tables here,
-- or alternatively have them auto-register,
-- see WHICH_KEY.md
mappings = {},
opts = {},
-- controls whether legendary.nvim actually binds they keymaps,
-- or if you want to let which-key.nvim handle the bindings.
-- if not passed, true by default
do_binding = true,
-- controls whether to use legendary.nvim item groups
-- matching your which-key.nvim groups; if false, all keymaps
-- are added at toplevel instead of in a group.
use_groups = true,
},
},
})
```

### `nvim-tree.lua`

Automatically load keymaps and commands for the `NvimTree` buffer into `legendary.nvim`.
Expand Down
2 changes: 1 addition & 1 deletion doc/WHICH_KEY.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ require('legendary').setup({

-- or, if you'd prefer to manually register with legendary.nvim
require('which-key').register(your_which_key_tables, your_which_key_opts)
require('legendary.integrations.which-key').bind_whichkey(
require('legendary.util.which-key').bind_whichkey(
your_which_key_tables,
your_which_key_opts,
-- false if which-key.nvim handles binding them,
Expand Down
35 changes: 0 additions & 35 deletions lua/legendary/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,29 +87,6 @@ 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
auto_register = false,
-- you can put which-key.nvim tables here,
-- or alternatively have them auto-register,
-- see ./doc/WHICH_KEY.md
mappings = {},
opts = {},
-- controls whether legendary.nvim actually binds they keymaps,
-- or if you want to let which-key.nvim handle the bindings.
-- if not passed, true by default
do_binding = true,
-- controls whether to use legendary.nvim item groups
-- matching your which-key.nvim groups; if false, all keymaps
-- are added at toplevel instead of in a group.
use_groups = true,
},
-- Which extensions to load; no extensions are loaded by default.
-- Setting the plugin name to `false` disables loading the extension.
-- Setting it to any other value will attempt to load the extension,
Expand Down Expand Up @@ -138,16 +115,6 @@ local config = {
log_level = 'info',
}

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

---@class LegendaryWhichkeyConfig
---@field auto_register boolean
---@field mappings table[]
---@field opts table
---@field do_binding boolean
---@field use_groups boolean

---@class LegendaryScratchpadConfig
---@field view 'float'|'current'
---@field results_view 'float'|'print'
Expand Down Expand Up @@ -184,8 +151,6 @@ 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
---@field cache_path string
Expand Down
18 changes: 10 additions & 8 deletions lua/legendary/deprecate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,18 @@ end
function M.check_config(cfg)
---@diagnostic disable

if cfg.functions ~= nil then
M.write({ 'config.functions', 'WarningMsg' }, 'has been moved to', { 'config.funcs' })
cfg.funcs = cfg.functions
if cfg.which_key ~= nil then
M.write({ 'config.which_key', 'WarningMsg' }, 'has been moved to', { 'config.extensions.which_key', 'WarningMsg' })
cfg.extensions = cfg.extensions or {}
cfg.extensions.which_key = cfg.extensions.which_key or cfg.which_key or {}
end

if cfg.default_opts.functions ~= nil then
if cfg.default_opts.functions ~= nil then
M.write({ 'config.default_opts.functions', 'WarningMsg' }, 'has been moved to', { 'config.default_opts.funcs' })
cfg.default_opts.funcs = cfg.default_opts.functions
end
if cfg.lazy_nvim and cfg.lazy_nvim.auto_register then
M.write(
{ 'config.lazy_nvim', 'WarningMsg' },
'has been moved to',
{ 'config.extensions.lazy_nvim = true', 'WarningMsg' }
)
end

---@diagnostic enable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
local Log = require('legendary.log')

local M = {}

function M.load_lazy_nvim_keys()
return function()
local has_lazy, _ = pcall(require, 'lazy')
if not has_lazy then
Log.warn("lazy.nvim integration is enabled, but cannot `require('lazy')`, aborting.")
Expand Down Expand Up @@ -30,5 +28,3 @@ function M.load_lazy_nvim_keys()
end
end
end

return M
42 changes: 42 additions & 0 deletions lua/legendary/extensions/which_key.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
local Log = require('legendary.log')
local util = require('legendary.util.which_key')

---@class LegendaryWhichkeyConfig
---@field auto_register boolean
---@field mappings table[]
---@field opts table
---@field do_binding boolean
---@field use_groups boolean

---@param opts LegendaryWhichkeyConfig
return function(opts)
local loaded, which_key = pcall(require, 'which-key')

if loaded then
local wk = which_key

if opts then
if opts.do_binding == nil then
opts.do_binding = false
end

if #vim.tbl_keys(opts.mappings or {}) > 0 then
util.bind_whichkey(opts.mappings, opts.opts, opts.do_binding)
end
end

local original = wk.register
local listener = function(whichkey_tbls, whichkey_opts)
Log.trace('Preparing to register items from which-key.nvim automatically')
util.bind_whichkey(whichkey_tbls, whichkey_opts, false)
original(whichkey_tbls, whichkey_opts)
Log.trace('Successfully registered items from which-key.nvim')
end
wk.register = listener
else
Log.warn(
'which-key.nvim not available. If you are lazy-loading, be sure that which-key.nvim is added to runtime path '
.. 'before running legendary.nvim config.'
)
end
end
14 changes: 0 additions & 14 deletions lua/legendary/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ 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
---@return fun(items:table[])
Expand Down Expand Up @@ -85,18 +83,6 @@ 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

if #vim.tbl_keys(Config.which_key.mappings) > 0 then
LegendaryWhichKey.bind_whichkey(Config.which_key.mappings, Config.which_key.opts, Config.which_key.do_binding)
end

M.keymaps(Config.keymaps)
M.commands(Config.commands)
M.autocmds(Config.autocmds)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
---@mod legendary.integrations.which-key

local State = require('legendary.data.state')
local Keymap = require('legendary.data.keymap')
local Log = require('legendary.log')
local Config = require('legendary.config')

local Lazy = require('legendary.vendor.lazy')
Expand Down Expand Up @@ -124,28 +121,4 @@ function M.bind_whichkey(wk_tbls, wk_opts, do_binding)
end, legendary_tbls))
end

--- Enable auto-registering of which-key.nvim tables
--- with legendary.nvim
function M.whichkey_listen()
local loaded, which_key = pcall(require, 'which-key')

if loaded then
local wk = which_key
local original = wk.register
local listener = function(whichkey_tbls, whichkey_opts)
Log.trace('Preparing to register items from which-key.nvim automatically')
M.bind_whichkey(whichkey_tbls, whichkey_opts, false)
original(whichkey_tbls, whichkey_opts)
Log.trace('Successfully registered items from which-key.nvim')
end
wk.register = listener
return true
else
Log.warn(
'which-key.nvim not available. If you are lazy-loading, be sure that which-key.nvim is added to runtime path '
.. 'before running legendary.nvim config.'
)
end
end

return M

0 comments on commit 9744bf8

Please sign in to comment.