From 079e30b5fb349f1d6d801169d93dc92c81ee3b62 Mon Sep 17 00:00:00 2001 From: Cannon07 Date: Tue, 28 Apr 2026 01:35:01 +0530 Subject: [PATCH] feat: make keymaps configurable via setup() The plugin previously hardcoded `]c`/`[c` (buffer-local in inline diff buffers) and `dq` (global), which conflicted with users' own mappings. Add a `keys` table to setup() with the existing values as defaults so behavior is unchanged. Each entry can be set to a different lhs or to `false` to skip the binding; `keys = false` disables all defaults. Also expose `(CodePreviewCloseAll)` so users can bind the close action even when defaults are disabled. Closes #43 Co-Authored-By: Claude Opus 4.7 --- README.md | 26 ++++++++++++++++++++++--- lua/code-preview/diff.lua | 41 +++++++++++++++++++++++---------------- lua/code-preview/init.lua | 19 +++++++++++++++++- 3 files changed, 65 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 5885c25..019e190 100644 --- a/README.md +++ b/README.md @@ -217,9 +217,29 @@ require("code-preview").setup({ ## Keymaps -| Key | Description | -|-----|-------------| -| `dq` | Close the diff (same as `:CodePreviewCloseDiff`) | +| Key | Scope | Description | +|-----|-------|-------------| +| `dq` | global | Close the diff (same as `:CodePreviewCloseDiff`) | +| `]c` | inline diff buffer | Jump to next change | +| `[c` | inline diff buffer | Jump to previous change | + +All defaults are configurable via `setup()`: + +```lua +require("code-preview").setup({ + keys = { + next_change = "]c", -- inline diff: next change + prev_change = "[c", -- inline diff: previous change + close_all = "dq", -- close diff and clear indicators + }, +}) +``` + +Set any entry to `false` to skip that binding, or `keys = false` to skip them all. A `(CodePreviewCloseAll)` mapping is always defined, so you can bind it yourself even with `keys = false`: + +```lua +vim.keymap.set("n", "x", "(CodePreviewCloseAll)") +``` --- diff --git a/lua/code-preview/diff.lua b/lua/code-preview/diff.lua index ea823a6..83285e9 100644 --- a/lua/code-preview/diff.lua +++ b/lua/code-preview/diff.lua @@ -345,25 +345,32 @@ local function show_inline_diff(original_path, proposed_path, real_file_path, cf end end - vim.keymap.set("n", "]c", function() - local cur = vim.api.nvim_win_get_cursor(0)[1] - for lnum = cur + 1, vim.api.nvim_buf_line_count(buf) do - if line_types[lnum] then - vim.api.nvim_win_set_cursor(0, { lnum, 0 }) - return - end + local keys_cfg = (cfg and cfg.keys) or {} + if keys_cfg ~= false then + if keys_cfg.next_change then + vim.keymap.set("n", keys_cfg.next_change, function() + local cur = vim.api.nvim_win_get_cursor(0)[1] + for lnum = cur + 1, vim.api.nvim_buf_line_count(buf) do + if line_types[lnum] then + vim.api.nvim_win_set_cursor(0, { lnum, 0 }) + return + end + end + end, { buffer = buf, desc = "Next change" }) end - end, { buffer = buf, desc = "Next change" }) - - vim.keymap.set("n", "[c", function() - local cur = vim.api.nvim_win_get_cursor(0)[1] - for lnum = cur - 1, 1, -1 do - if line_types[lnum] then - vim.api.nvim_win_set_cursor(0, { lnum, 0 }) - return - end + + if keys_cfg.prev_change then + vim.keymap.set("n", keys_cfg.prev_change, function() + local cur = vim.api.nvim_win_get_cursor(0)[1] + for lnum = cur - 1, 1, -1 do + if line_types[lnum] then + vim.api.nvim_win_set_cursor(0, { lnum, 0 }) + return + end + end + end, { buffer = buf, desc = "Previous change" }) end - end, { buffer = buf, desc = "Previous change" }) + end if first_change_line then vim.api.nvim_win_set_cursor(win, { first_change_line, 0 }) diff --git a/lua/code-preview/init.lua b/lua/code-preview/init.lua index 23f4d59..ab29de5 100644 --- a/lua/code-preview/init.lua +++ b/lua/code-preview/init.lua @@ -33,6 +33,13 @@ local default_config = { deleted = { fg = "#e06c75", bold = true, strikethrough = true }, }, }, + keys = { + -- Set any entry to false to skip that binding. Set `keys = false` to skip all. + -- (CodePreviewCloseAll) is always defined so users can map it themselves. + next_change = "]c", -- buffer-local in inline diff buffers + prev_change = "[c", -- buffer-local in inline diff buffers + close_all = "dq", -- global; close diff and clear indicators + }, highlights = { current = { DiffAdd = { bg = "#4c2e2e" }, @@ -140,9 +147,19 @@ function M.setup(user_config) require("code-preview.neo_tree").setup(M.config) end - vim.keymap.set("n", "dq", function() + -- mapping is always defined so users can bind it themselves + -- regardless of the `keys` config (e.g. `keys = false` to disable defaults). + vim.keymap.set("n", "(CodePreviewCloseAll)", function() require("code-preview.diff").close_diff_and_clear() end, { desc = "Close code-preview diff" }) + + if M.config.keys ~= false then + local close_all = M.config.keys and M.config.keys.close_all + if close_all then + vim.keymap.set("n", close_all, "(CodePreviewCloseAll)", + { desc = "Close code-preview diff" }) + end + end end --- Query hook context for the PreToolUse shell script.