Skip to content

Commit ee9d6ff

Browse files
committed
feat!: New keymap/command configuration - closes #44
The mechanism for setting up keymappings or commands for debugprint.nvim has changed to resolve some issues some users saw with custom mapping of keys - see GitHub issue #44 for the history. If you didn't customize the default keymappings or commands before, you shouldn't have to change anything. However, if you did, you should switch to the new mechanism instead. Please see [README link](https://github.com/andrewferrier/debugprint.nvim/blob/main/README.md#keymappings-and-commands) for an explanation of the new mechanism. Old keymapping configuration should continue to work for now, although direct keymapping via `vim.keymap.set()` is no longer officially supported and may be removed in future, and you may see deprecation warnings.
1 parent 5b450f9 commit ee9d6ff

File tree

4 files changed

+376
-143
lines changed

4 files changed

+376
-143
lines changed

README.md

+58-74
Original file line numberDiff line numberDiff line change
@@ -110,93 +110,77 @@ the box'. There are also some function invocations which are not mapped to any
110110
keymappings or commands by default, but could be. This is all shown in the
111111
following table.
112112

113-
| Mode | Default Keymap / Cmd | Purpose | Lua Function |
114-
| ---------- | -------------------- | --------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------- |
115-
| Normal | `g?p` | Insert plain debug line below current line | `require('debugprint').debugprint()` |
116-
| Normal | `g?P` | Insert plain debug line above current line | `require('debugprint').debugprint({above = true})` |
117-
| Normal | `g?v` | Insert variable debug line below current line. If cursor is on a variable, use it, otherwise prompt | `require('debugprint').debugprint({variable = true})` |
118-
| Normal | `g?V` | Insert variable debug line above current line. If cursor is on a variable, use it, otherwise prompt | `require('debugprint').debugprint({above = true, variable = true})` |
119-
| Normal | None | Prompt for variable name, insert variable debugging line below the current line | `require('debugprint').debugprint({ignore_treesitter = true, variable = true})` |
120-
| Normal | None | Prompt for variable name, insert variable debugging line above the current line | `require('debugprint').debugprint({ignore_treesitter = true, above = true, variable = true})` |
121-
| Visual | `g?v` | Insert visually-selected variable debugging line below the current line | `require('debugprint').debugprint({variable = true})` |
122-
| Visual | `g?v` | Insert visually-selected variable debugging line below the current line | `require('debugprint').debugprint({variable = true})` |
123-
| Op-pending | `g?o` | Locate variable using motion, and insert a variable debugging line below the current line | `require('debugprint').debugprint({motion = true})` |
124-
| Op-pending | `g?O` | Locate variable using motion, and insert a variable debugging line above the current line | `require('debugprint').debugprint({motion = true, above = true})` |
125-
| Command | `:DeleteDebugPrints` | Delete all debug lines added to this buffer | `require('debugprint').deleteprints()` |
126-
127-
### Custom Keymappings and Commands
128-
129-
The keymappings are chosen specifically because by default they are
130-
used to convert sections to ROT-13, which most folks don't use. You can disable
131-
the defaults above from being created by setting `create_keymaps` and/or
132-
`create_commands`, and map them yourself to something else if you prefer:
113+
| Mode | Default Key / Cmd | Purpose | Above/Below Line |
114+
| ---------- | -------------------- | ------------------------------------------- | ---------------- |
115+
| Normal | `g?p` | Plain debug | Below |
116+
| Normal | `g?P` | Plain debug | Above |
117+
| Normal | `g?v` | Variable debug | Below |
118+
| Normal | `g?V` | Variable debug | Above |
119+
| Normal | None | Variable debug (always prompt for variable) | Below |
120+
| Normal | None | Variable debug (always prompt for variable) | Above |
121+
| Visual | `g?v` | Variable debug | Below |
122+
| Visual | `g?v` | Variable debug | Above |
123+
| Op-pending | `g?o` | Variable debug | Below |
124+
| Op-pending | `g?O` | Variable debug | Above |
125+
| Command | `:DeleteDebugPrints` | Delete all debug lines added to this buffer | |
126+
127+
The keys and commands outlined above can be specifically overridden using the
128+
`keymaps` and `commands` objects inside the `opts` object used above during
129+
configuration of debugprint. For example, if configuring via `lazy.nvim`, it
130+
might look like this:
133131

134132
```lua
135-
opts = {
136-
create_keymaps = false,
137-
create_commands = false
138-
...
133+
return {
134+
"andrewferrier/debugprint.nvim",
135+
opts = {
136+
keymaps = {
137+
normal = {
138+
plain_below = "g?p",
139+
plain_above = "g?P",
140+
variable_below = "g?v",
141+
variable_above = "g?V",
142+
variable_below_alwaysprompt = nil,
143+
variable_above_alwaysprompt = nil,
144+
textobj_below = "g?o",
145+
textobj_above = "g?O",
146+
},
147+
visual = {
148+
variable_below = "g?v",
149+
variable_above = "g?V",
150+
},
151+
},
152+
commands = {
153+
delete_debug_prints = "DeleteDebugPrints",
154+
},
155+
}
156+
version = "*"
139157
}
140-
141-
require("debugprint").setup(opts)
142-
143-
vim.keymap.set("n", "<Leader>d", function()
144-
-- Note: setting `expr=true` and returning the value are essential
145-
return require('debugprint').debugprint()
146-
end, {
147-
expr = true,
148-
})
149-
vim.keymap.set("n", "<Leader>D", function()
150-
-- Note: setting `expr=true` and returning the value are essential
151-
return require('debugprint').debugprint({ above = true })
152-
end, {
153-
expr = true,
154-
})
155-
vim.keymap.set("n", "<Leader>dq", function()
156-
-- Note: setting `expr=true` and returning the value are essential
157-
return require('debugprint').debugprint({ variable = true })
158-
end, {
159-
expr = true,
160-
})
161-
vim.keymap.set("n", "<Leader>Dq", function()
162-
-- Note: setting `expr=true` and returning the value are essential
163-
return require('debugprint').debugprint({ above = true, variable = true })
164-
end, {
165-
expr = true,
166-
})
167-
vim.keymap.set("n", "<Leader>do", function()
168-
-- Note: setting `expr=true` and returning the value are essential
169-
-- It's also important to use motion = true for operator-pending motions
170-
return require('debugprint').debugprint({ motion = true })
171-
end, {
172-
expr = true,
173-
})
174-
175-
vim.api.nvim_create_user_command("DeleteDebugs", function(opts)
176-
-- Note: you must set `range=true` and pass through opts for ranges to work
177-
require('debugprint').deleteprints(opts)
178-
end, {
179-
range = true})
180-
end)
181-
...
182158
```
183159

184-
or, to have a keymapping instead for deleting debug lines (this will affect
185-
the entire buffer, visual and operator-pending modes will not work):
160+
You only need to include the keys / commands which you wish to override, others
161+
will default as shown above.
186162

187-
```lua
188-
vim.keymap.set("n", "g?d", function()
189-
return require('debugprint').deleteprints()
190-
end)
191-
```
163+
The default keymappings are chosen specifically because ordinarily in NeoVim
164+
they are used to convert sections to ROT-13, which most folks don't use.
165+
166+
### Mapping Deprecation
167+
168+
*Note*: as of commit XXX, the old mechanism of configuring keymaps/commands
169+
which specifically allowed for mapping directly to
170+
`require('debugprint').debugprint(...)` is no longer officially supported or
171+
documented. This is primarily because of confusion which arose over how to do
172+
this mapping. Existing mappings performed this way are likely to continue to
173+
work for some time. You should, however, migrate over to the new method outlined
174+
above. If this doesn't give you the flexibility to map how you wish for some
175+
reason, please open an
176+
[issue](https://github.com/andrewferrier/debugprint.nvim/issues/new).
192177

193178
## Other Options
194179

195180
`debugprint` supports the following options in its global `opts` object:
196181

197182
| Option | Default | Purpose |
198183
| ------------------- | ------------ | -------------------------------------------------------------------------------------------------------------------------------------------- |
199-
| `create_keymaps` | `true` | Creates default keymappings - see above |
200184
| `move_to_debugline` | `false` | When adding a debug line, moves the cursor to that line |
201185
| `display_counter` | `true` | Whether to display/include the monotonically increasing counter in each debug message |
202186
| `display_snippet` | `true` | Whether to include a snippet of the line above/below in plain debug lines |

lua/debugprint/init.lua

+108-45
Original file line numberDiff line numberDiff line change
@@ -233,56 +233,119 @@ M.deleteprints = function(opts)
233233
end
234234
end
235235

236+
local map_key = function(mode, lhs, rhs, desc)
237+
if lhs ~= nil then
238+
if type(rhs) == "function" then
239+
vim.api.nvim_set_keymap(
240+
mode,
241+
lhs,
242+
rhs,
243+
{ expr = true, desc = desc }
244+
)
245+
else
246+
vim.api.nvim_set_keymap(mode, lhs, '', {
247+
expr = true,
248+
desc = desc,
249+
callback = function()
250+
return M.debugprint(rhs)
251+
end,
252+
})
253+
end
254+
end
255+
end
256+
236257
M.setup = function(opts)
237258
global_opts =
238259
require("debugprint.options").get_and_validate_global_opts(opts)
239260

240-
if global_opts.create_keymaps then
241-
vim.keymap.set("n", "g?p", function()
242-
return M.debugprint()
243-
end, {
244-
expr = true,
245-
desc = "Plain debug below current line",
246-
})
247-
vim.keymap.set("n", "g?P", function()
248-
return M.debugprint({ above = true })
249-
end, {
250-
expr = true,
251-
desc = "Plain debug above current line",
252-
})
253-
vim.keymap.set({ "n", "x" }, "g?v", function()
254-
return M.debugprint({ variable = true })
255-
end, {
256-
expr = true,
257-
desc = "Variable debug below current line",
258-
})
259-
vim.keymap.set({ "n", "x" }, "g?V", function()
260-
return M.debugprint({ above = true, variable = true })
261-
end, {
262-
expr = true,
263-
desc = "Variable debug above current line",
264-
})
265-
vim.keymap.set("n", "g?o", function()
266-
return M.debugprint({ motion = true })
267-
end, {
268-
expr = true,
269-
desc = "Text-obj-selected variable debug below current line",
270-
})
271-
vim.keymap.set("n", "g?O", function()
272-
return M.debugprint({ motion = true, above = true })
273-
end, {
274-
expr = true,
275-
desc = "Text-obj-selected variable debug above current line",
276-
})
277-
end
261+
map_key(
262+
"n",
263+
global_opts.keymaps.normal.plain_below,
264+
{},
265+
"Plain debug below current line"
266+
)
278267

279-
if global_opts.create_commands then
280-
vim.api.nvim_create_user_command("DeleteDebugPrints", function(cmd_opts)
281-
M.deleteprints(cmd_opts)
282-
end, {
283-
range = true,
284-
desc = "Delete all debugprint statements in the current buffer.",
285-
})
268+
map_key(
269+
"n",
270+
global_opts.keymaps.normal.plain_above,
271+
{ above = true },
272+
"Plain debug below current line"
273+
)
274+
275+
map_key(
276+
"n",
277+
global_opts.keymaps.normal.variable_below,
278+
{ variable = true },
279+
"Variable debug below current line"
280+
)
281+
282+
map_key(
283+
"n",
284+
global_opts.keymaps.normal.variable_above,
285+
{ above = true, variable = true },
286+
"Variable debug above current line"
287+
)
288+
289+
map_key(
290+
"n",
291+
global_opts.keymaps.normal.variable_below_alwaysprompt,
292+
{ variable = true, ignore_treesitter = true },
293+
"Variable debug below current line (always prompt)"
294+
)
295+
296+
map_key(
297+
"n",
298+
global_opts.keymaps.normal.variable_above_alwaysprompt,
299+
{ above = true, variable = true, ignore_treesitter = true },
300+
"Variable debug above current line (always prompt)"
301+
)
302+
303+
map_key(
304+
"n",
305+
global_opts.keymaps.normal.textobj_below,
306+
{ motion = true },
307+
"Text-obj-selected variable debug below current line"
308+
)
309+
310+
map_key(
311+
"n",
312+
global_opts.keymaps.normal.textobj_above,
313+
{ motion = true, above = true },
314+
"Text-obj-selected variable debug above current line"
315+
)
316+
317+
map_key(
318+
"n",
319+
global_opts.keymaps.normal.delete_debug_prints,
320+
M.deleteprints,
321+
"Delete all debugprint statements in the current buffer"
322+
)
323+
324+
map_key(
325+
"x",
326+
global_opts.keymaps.visual.variable_below,
327+
{ variable = true },
328+
"Variable debug below current line"
329+
)
330+
331+
map_key(
332+
"x",
333+
global_opts.keymaps.visual.variable_above,
334+
{ above = true, variable = true },
335+
"Variable debug above current line"
336+
)
337+
338+
if global_opts.commands.delete_debug_prints then
339+
vim.api.nvim_create_user_command(
340+
global_opts.commands.delete_debug_prints,
341+
function(cmd_opts)
342+
M.deleteprints(cmd_opts)
343+
end,
344+
{
345+
range = true,
346+
desc = "Delete all debugprint statements in the current buffer",
347+
}
348+
)
286349
end
287350

288351
-- Because we want to be idempotent, re-running setup() resets the counter

0 commit comments

Comments
 (0)