Skip to content

Commit c4ebe66

Browse files
committed
Set desc for all mappings we set.
And ensure we hopefully always will via a test. In particular this means that which-key users should now see a description of what each of our mappings do.
1 parent 0f757cb commit c4ebe66

File tree

4 files changed

+105
-29
lines changed

4 files changed

+105
-29
lines changed

lua/lean/init.lua

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,71 @@ local subprocess_check_output = require('lean._util').subprocess_check_output
1616

1717
local lean = {
1818
mappings = {
19-
n = {
20-
['<LocalLeader>i'] = '<Cmd>LeanInfoviewToggle<CR>',
21-
['<LocalLeader>p'] = '<Cmd>LeanInfoviewPinTogglePause<CR>',
22-
['<LocalLeader>x'] = '<Cmd>LeanInfoviewAddPin<CR>',
23-
['<LocalLeader>c'] = '<Cmd>LeanInfoviewClearPins<CR>',
24-
['<LocalLeader>dx'] = '<Cmd>LeanInfoviewSetDiffPin<CR>',
25-
['<LocalLeader>dc'] = '<Cmd>LeanInfoviewClearDiffPin<CR>',
26-
['<LocalLeader>dd'] = '<Cmd>LeanInfoviewToggleAutoDiffPin<CR>',
27-
['<LocalLeader>dt'] = '<Cmd>LeanInfoviewToggleNoClearAutoDiffPin<CR>',
28-
['<LocalLeader>w'] = '<Cmd>LeanInfoviewEnableWidgets<CR>',
29-
['<LocalLeader>W'] = '<Cmd>LeanInfoviewDisableWidgets<CR>',
30-
['<LocalLeader>v'] = '<Cmd>LeanInfoviewViewOptions<CR>',
31-
['<LocalLeader><Tab>'] = '<Cmd>LeanGotoInfoview<CR>',
32-
['<LocalLeader>\\'] = '<Cmd>LeanAbbreviationsReverseLookup<CR>',
19+
{
20+
'<LocalLeader>i',
21+
'<Cmd>LeanInfoviewToggle<CR>',
22+
{ desc = 'Toggle showing the infoview.' },
23+
},
24+
{
25+
'<LocalLeader>p',
26+
'<Cmd>LeanInfoviewPinTogglePause<CR>',
27+
{ desc = 'Toggle pausing infoview pins.' },
28+
},
29+
{
30+
'<LocalLeader>x',
31+
'<Cmd>LeanInfoviewAddPin<CR>',
32+
{ desc = 'Add an infoview pin.' },
33+
},
34+
{
35+
'<LocalLeader>c',
36+
'<Cmd>LeanInfoviewClearPins<CR>',
37+
{ desc = 'Clear all infoview pins.' },
38+
},
39+
{
40+
'<LocalLeader>dx',
41+
'<Cmd>LeanInfoviewSetDiffPin<CR>',
42+
{ desc = 'Set an infoview diff pin.' },
43+
},
44+
{
45+
'<LocalLeader>dc',
46+
'<Cmd>LeanInfoviewClearDiffPin<CR>',
47+
{ desc = 'Clear all infoview diff pins.' },
48+
},
49+
{
50+
'<LocalLeader>dd',
51+
'<Cmd>LeanInfoviewToggleAutoDiffPin<CR>',
52+
{ desc = 'Toggle "auto-diff" mode in the infoview.' },
53+
},
54+
{
55+
'<LocalLeader>dt',
56+
'<Cmd>LeanInfoviewToggleNoClearAutoDiffPin<CR>',
57+
{ desc = 'Toggle "auto-diff" mode and clear any existing pins.' },
58+
},
59+
{
60+
'<LocalLeader>w',
61+
'<Cmd>LeanInfoviewEnableWidgets<CR>',
62+
{ desc = 'Enable infoview widgets.' },
63+
},
64+
{
65+
'<LocalLeader>W',
66+
'<Cmd>LeanInfoviewDisableWidgets<CR>',
67+
{ desc = 'Disable infoview widgets.' },
68+
},
69+
{
70+
'<LocalLeader>v',
71+
'<Cmd>LeanInfoviewViewOptions<CR>',
72+
{ desc = 'Change the infoview view options.' },
73+
},
74+
{
75+
'<LocalLeader><Tab>',
76+
'<Cmd>LeanGotoInfoview<CR>',
77+
{ desc = 'Jump to the current infoview.' },
78+
},
79+
{
80+
'<LocalLeader>\\',
81+
'<Cmd>LeanAbbreviationsReverseLookup<CR>',
82+
{ desc = 'Show how to type the unicode character under the cursor.' },
3383
},
34-
i = {},
3584
},
3685
}
3786

@@ -109,11 +158,10 @@ end
109158
---Enable mappings for a given buffer
110159
---@param bufnr? number the bufnr to enable mappings in, defaulting to 0
111160
function lean.use_suggested_mappings(bufnr)
112-
local opts = { noremap = true, buffer = bufnr or 0 }
113-
for mode, mode_mappings in pairs(lean.mappings) do
114-
for lhs, rhs in pairs(mode_mappings) do
115-
vim.keymap.set(mode, lhs, rhs, opts)
116-
end
161+
local opts = { buffer = bufnr or 0 }
162+
for _, each in ipairs(lean.mappings) do
163+
local lhs, rhs, more_opts = unpack(each)
164+
vim.keymap.set(each.mode or 'n', lhs, rhs, vim.tbl_extend('error', opts, more_opts))
117165
end
118166
end
119167

lua/lean/tui.lua

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -426,31 +426,30 @@ function BufRenderer:new(obj)
426426
end,
427427
})
428428

429-
local opts = { buffer = obj.buf }
430429
-- Note that only closures work here, we can't go storing this on vim.b due
431430
-- to neovim/neovim#12544 wherein the metatable would be lost on retrieval.
432431
-- In other words, if we do vim.b[..].foo = new_renderer, when we come back
433432
-- to look at it, it's not a BufRenderer anymore. Surprise!
434433
vim.keymap.set('n', '<Tab>', function()
435434
new_renderer:enter_tooltip()
436-
end, opts)
435+
end, { buffer = obj.buf, desc = 'Enter a tooltip.' })
437436
vim.keymap.set('n', 'J', function()
438437
new_renderer:enter_tooltip()
439-
end, opts)
438+
end, { buffer = obj.buf, desc = 'Enter a tooltip.' })
440439
vim.keymap.set('n', '<S-Tab>', function()
441440
new_renderer:goto_parent_tooltip()
442-
end, opts)
441+
end, { buffer = obj.buf, desc = 'Go to the "parent" tooltip.' })
443442
vim.keymap.set(
444443
'n',
445444
'<LocalLeader>\\',
446445
require'lean.abbreviations'.show_reverse_lookup,
447-
opts
446+
{ buffer = obj.buf, desc = 'Show how to type the unicode character under the cursor.' }
448447
)
449448

450449
for key, event in pairs(obj.keymaps or {}) do
451450
vim.keymap.set('n', key, function()
452451
new_renderer:event(event)
453-
end, opts)
452+
end, { buffer = obj.buf, desc = ('Fire a %s event.'):format(event) })
454453
end
455454

456455
return new_renderer

spec/mappings_spec.lua

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
local infoview = require 'lean.infoview'
2-
local lean = require 'lean'
32
local clean_buffer = require('spec.helpers').clean_buffer
43

54
vim.g.lean_config = { mappings = true }
65

76
describe('mappings', function()
87
describe('for lean buffers', function()
98
local any_lean_lhs = '<LocalLeader>i'
10-
local rhs = lean.mappings.n[any_lean_lhs]
9+
local rhs = '<Cmd>LeanInfoviewToggle<CR>'
1110

1211
it(
1312
'are bound in lean buffers',
@@ -22,6 +21,19 @@ describe('mappings', function()
2221
assert.is.empty(vim.fn.maparg(any_lean_lhs, 'n'))
2322
vim.cmd.bwipeout()
2423
end)
24+
25+
it(
26+
'all have desc set',
27+
clean_buffer(function()
28+
local mappings = vim.api.nvim_buf_get_keymap(0, 'nivx')
29+
assert.message('unexpectedly empty').is_not_empty(mappings)
30+
31+
local msg = 'no desc for `%s`:\n%s'
32+
for _, each in ipairs(mappings) do
33+
assert.message(msg:format(each.lhs, vim.inspect(each))).is_not.empty(each.desc)
34+
end
35+
end)
36+
)
2537
end)
2638

2739
describe('for infoviews', function()
@@ -36,6 +48,23 @@ describe('mappings', function()
3648
end)
3749
)
3850

51+
it(
52+
'all have desc set',
53+
clean_buffer(function()
54+
infoview.go_to()
55+
56+
-- TODO: Also check tooltips.
57+
58+
local mappings = vim.api.nvim_buf_get_keymap(0, 'nivx')
59+
assert.message('unexpectedly empty').is_not_empty(mappings)
60+
61+
local msg = 'no desc for `%s`:\n%s'
62+
for _, each in ipairs(mappings) do
63+
assert.message(msg:format(each.lhs, vim.inspect(each))).is_not.empty(each.desc)
64+
end
65+
end)
66+
)
67+
3968
it('are not bound in other buffers', function()
4069
vim.cmd.new()
4170
assert.is.empty(vim.fn.maparg(any_infoview_lhs, 'n', false, true))

spec/use_suggested_mappings_spec.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ describe('mappings', function()
66
'are bound in the current buffer and not others',
77
clean_buffer(function()
88
lean.use_suggested_mappings()
9-
assert.is.same(lean.mappings.n['<LocalLeader>i'], vim.fn.maparg('<LocalLeader>i', 'n'))
9+
assert.is.same('<Cmd>LeanInfoviewToggle<CR>', vim.fn.maparg('<LocalLeader>i', 'n'))
1010

1111
vim.cmd.new()
1212
assert.is.empty(vim.fn.maparg('<LocalLeader>i', 'n'))

0 commit comments

Comments
 (0)