Skip to content

Commit e48f04d

Browse files
committed
feat(help): Add highlights. Allow multiple keymap groups.
1 parent ce1810a commit e48f04d

File tree

7 files changed

+192
-104
lines changed

7 files changed

+192
-104
lines changed

README.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -246,20 +246,27 @@ require("diffview").setup({
246246
{ "n", "<leader>cb", actions.conflict_choose("base"), { desc = "Choose the BASE version of a conflict" } },
247247
{ "n", "<leader>ca", actions.conflict_choose("all"), { desc = "Choose all the versions of a conflict" } },
248248
{ "n", "dx", actions.conflict_choose("none"), { desc = "Delete the conflict region" } },
249-
{ "n", "g?", actions.help("view"), { desc = "Open the help panel" } },
250249
},
251-
diff1 = { --[[ Mappings in single window diff layouts ]] },
252-
diff2 = { --[[ Mappings in 2-way diff layouts ]] },
250+
diff1 = {
251+
-- Mappings in single window diff layouts
252+
{ "n", "g?", actions.help({ "view", "diff1" }), { desc = "Open the help panel" } },
253+
},
254+
diff2 = {
255+
-- Mappings in 2-way diff layouts
256+
{ "n", "g?", actions.help({ "view", "diff2" }), { desc = "Open the help panel" } },
257+
},
253258
diff3 = {
254259
-- Mappings in 3-way diff layouts
255-
{ { "n", "x" }, "2do", actions.diffget("ours"), { desc = "Obtain the diff hunk from the OURS version of the file" } },
256-
{ { "n", "x" }, "3do", actions.diffget("theirs"), { desc = "Obtain the diff hunk from the THEIRS version of the file" } },
260+
{ { "n", "x" }, "2do", actions.diffget("ours"), { desc = "Obtain the diff hunk from the OURS version of the file" } },
261+
{ { "n", "x" }, "3do", actions.diffget("theirs"), { desc = "Obtain the diff hunk from the THEIRS version of the file" } },
262+
{ "n", "g?", actions.help({ "view", "diff3" }), { desc = "Open the help panel" } },
257263
},
258264
diff4 = {
259265
-- Mappings in 4-way diff layouts
260-
{ { "n", "x" }, "1do", actions.diffget("base"), { desc = "Obtain the diff hunk from the BASE version of the file" } },
261-
{ { "n", "x" }, "2do", actions.diffget("ours"), { desc = "Obtain the diff hunk from the OURS version of the file" } },
262-
{ { "n", "x" }, "3do", actions.diffget("theirs"), { desc = "Obtain the diff hunk from the THEIRS version of the file" } },
266+
{ { "n", "x" }, "1do", actions.diffget("base"), { desc = "Obtain the diff hunk from the BASE version of the file" } },
267+
{ { "n", "x" }, "2do", actions.diffget("ours"), { desc = "Obtain the diff hunk from the OURS version of the file" } },
268+
{ { "n", "x" }, "3do", actions.diffget("theirs"), { desc = "Obtain the diff hunk from the THEIRS version of the file" } },
269+
{ "n", "g?", actions.help({ "view", "diff4" }), { desc = "Open the help panel" } },
263270
},
264271
file_panel = {
265272
{ "n", "j", actions.next_entry, { desc = "Bring the cursor to the next file entry" } },

doc/diffview_defaults.txt

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,27 @@ require("diffview").setup({
100100
{ "n", "<leader>cb", actions.conflict_choose("base"), { desc = "Choose the BASE version of a conflict" } },
101101
{ "n", "<leader>ca", actions.conflict_choose("all"), { desc = "Choose all the versions of a conflict" } },
102102
{ "n", "dx", actions.conflict_choose("none"), { desc = "Delete the conflict region" } },
103-
{ "n", "g?", actions.help("view"), { desc = "Open the help panel" } },
104103
},
105-
diff1 = { --[[ Mappings in single window diff layouts ]] },
106-
diff2 = { --[[ Mappings in 2-way diff layouts ]] },
104+
diff1 = {
105+
-- Mappings in single window diff layouts
106+
{ "n", "g?", actions.help({ "view", "diff1" }), { desc = "Open the help panel" } },
107+
},
108+
diff2 = {
109+
-- Mappings in 2-way diff layouts
110+
{ "n", "g?", actions.help({ "view", "diff2" }), { desc = "Open the help panel" } },
111+
},
107112
diff3 = {
108113
-- Mappings in 3-way diff layouts
109-
{ { "n", "x" }, "2do", actions.diffget("ours"), { desc = "Obtain the diff hunk from the OURS version of the file" } },
110-
{ { "n", "x" }, "3do", actions.diffget("theirs"), { desc = "Obtain the diff hunk from the THEIRS version of the file" } },
114+
{ { "n", "x" }, "2do", actions.diffget("ours"), { desc = "Obtain the diff hunk from the OURS version of the file" } },
115+
{ { "n", "x" }, "3do", actions.diffget("theirs"), { desc = "Obtain the diff hunk from the THEIRS version of the file" } },
116+
{ "n", "g?", actions.help({ "view", "diff3" }), { desc = "Open the help panel" } },
111117
},
112118
diff4 = {
113119
-- Mappings in 4-way diff layouts
114-
{ { "n", "x" }, "1do", actions.diffget("base"), { desc = "Obtain the diff hunk from the BASE version of the file" } },
115-
{ { "n", "x" }, "2do", actions.diffget("ours"), { desc = "Obtain the diff hunk from the OURS version of the file" } },
116-
{ { "n", "x" }, "3do", actions.diffget("theirs"), { desc = "Obtain the diff hunk from the THEIRS version of the file" } },
120+
{ { "n", "x" }, "1do", actions.diffget("base"), { desc = "Obtain the diff hunk from the BASE version of the file" } },
121+
{ { "n", "x" }, "2do", actions.diffget("ours"), { desc = "Obtain the diff hunk from the OURS version of the file" } },
122+
{ { "n", "x" }, "3do", actions.diffget("theirs"), { desc = "Obtain the diff hunk from the THEIRS version of the file" } },
123+
{ "n", "g?", actions.help({ "view", "diff4" }), { desc = "Open the help panel" } },
117124
},
118125
file_panel = {
119126
{ "n", "j", actions.next_entry, { desc = "Bring the cursor to the next file entry" } },

lua/diffview/actions.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,12 +466,15 @@ function M.cycle_layout()
466466
end
467467
end
468468

469-
function M.help(conf_name)
469+
---@param keymap_groups string|string[]
470+
function M.help(keymap_groups)
471+
keymap_groups = type(keymap_groups) == "table" and keymap_groups or { keymap_groups }
472+
470473
return function()
471474
local view = lib.get_current_view()
472475

473476
if view then
474-
local help_panel = HelpPanel(view, conf_name) --[[@as HelpPanel ]]
477+
local help_panel = HelpPanel(view, keymap_groups) --[[@as HelpPanel ]]
475478
help_panel:focus()
476479
end
477480
end

lua/diffview/config.lua

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,12 @@ M.defaults = {
9494
win_opts = {}
9595
},
9696
},
97-
help_panel = {
98-
win_config = {
99-
win_opts = {}
100-
},
101-
},
10297
default_args = {
10398
DiffviewOpen = {},
10499
DiffviewFileHistory = {},
105100
},
106101
hooks = {},
107-
-- Tabularize formatting pattern: `\vactions\.\w+(\(.{-}\))?,?|\{\ desc\ \=`
102+
-- Tabularize formatting pattern: `\v(\"[^"]{-}\",\ze(\s*)actions)|actions\.\w+(\(.{-}\))?,?|\{\ desc\ \=`
108103
keymaps = {
109104
disable_defaults = false, -- Disable the default keymaps
110105
view = {
@@ -125,20 +120,27 @@ M.defaults = {
125120
{ "n", "<leader>cb", actions.conflict_choose("base"), { desc = "Choose the BASE version of a conflict" } },
126121
{ "n", "<leader>ca", actions.conflict_choose("all"), { desc = "Choose all the versions of a conflict" } },
127122
{ "n", "dx", actions.conflict_choose("none"), { desc = "Delete the conflict region" } },
128-
{ "n", "g?", actions.help("view"), { desc = "Open the help panel" } },
129123
},
130-
diff1 = { --[[ Mappings in single window diff layouts ]] },
131-
diff2 = { --[[ Mappings in 2-way diff layouts ]] },
124+
diff1 = {
125+
-- Mappings in single window diff layouts
126+
{ "n", "g?", actions.help({ "view", "diff1" }), { desc = "Open the help panel" } },
127+
},
128+
diff2 = {
129+
-- Mappings in 2-way diff layouts
130+
{ "n", "g?", actions.help({ "view", "diff2" }), { desc = "Open the help panel" } },
131+
},
132132
diff3 = {
133133
-- Mappings in 3-way diff layouts
134-
{ { "n", "x" }, "2do", actions.diffget("ours"), { desc = "Obtain the diff hunk from the OURS version of the file" } },
135-
{ { "n", "x" }, "3do", actions.diffget("theirs"), { desc = "Obtain the diff hunk from the THEIRS version of the file" } },
134+
{ { "n", "x" }, "2do", actions.diffget("ours"), { desc = "Obtain the diff hunk from the OURS version of the file" } },
135+
{ { "n", "x" }, "3do", actions.diffget("theirs"), { desc = "Obtain the diff hunk from the THEIRS version of the file" } },
136+
{ "n", "g?", actions.help({ "view", "diff3" }), { desc = "Open the help panel" } },
136137
},
137138
diff4 = {
138139
-- Mappings in 4-way diff layouts
139-
{ { "n", "x" }, "1do", actions.diffget("base"), { desc = "Obtain the diff hunk from the BASE version of the file" } },
140-
{ { "n", "x" }, "2do", actions.diffget("ours"), { desc = "Obtain the diff hunk from the OURS version of the file" } },
141-
{ { "n", "x" }, "3do", actions.diffget("theirs"), { desc = "Obtain the diff hunk from the THEIRS version of the file" } },
140+
{ { "n", "x" }, "1do", actions.diffget("base"), { desc = "Obtain the diff hunk from the BASE version of the file" } },
141+
{ { "n", "x" }, "2do", actions.diffget("ours"), { desc = "Obtain the diff hunk from the OURS version of the file" } },
142+
{ { "n", "x" }, "3do", actions.diffget("theirs"), { desc = "Obtain the diff hunk from the THEIRS version of the file" } },
143+
{ "n", "g?", actions.help({ "view", "diff4" }), { desc = "Open the help panel" } },
142144
},
143145
file_panel = {
144146
{ "n", "j", actions.next_entry, { desc = "Bring the cursor to the next file entry" } },

lua/diffview/renderer.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ function RenderComponent:destroy()
173173
self.components = nil
174174
end
175175

176+
---@return RenderComponent?
176177
function RenderComponent:get_comp_on_line(line)
177178
line = line - 1
178179

lua/diffview/ui/panel.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,8 @@ function Panel:close()
308308
elseif self:is_focused() then
309309
vim.cmd("wincmd p")
310310
end
311-
api.nvim_win_hide(self.winid)
311+
312+
pcall(api.nvim_win_close, self.winid, true)
312313
end
313314
end
314315

0 commit comments

Comments
 (0)