Skip to content

Commit bf6c457

Browse files
committed
fix: Support empty or false to remove default keybindings
1 parent fa03961 commit bf6c457

File tree

5 files changed

+153
-46
lines changed

5 files changed

+153
-46
lines changed

README.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,12 @@ return {
130130
plain_above = "g?P",
131131
variable_below = "g?v",
132132
variable_above = "g?V",
133-
variable_below_alwaysprompt = nil,
134-
variable_above_alwaysprompt = nil,
133+
variable_below_alwaysprompt = "",
134+
variable_above_alwaysprompt = "",
135135
textobj_below = "g?o",
136136
textobj_above = "g?O",
137-
toggle_comment_debug_prints = nil,
138-
delete_debug_prints = nil,
137+
toggle_comment_debug_prints = "",
138+
delete_debug_prints = "",
139139
},
140140
insert = {
141141
plain = "<C-G>p",
@@ -156,7 +156,8 @@ return {
156156
```
157157

158158
You only need to include the keys / commands which you wish to override, others
159-
will default as shown above. Setting any key or command to `nil` will skip it.
159+
will default as shown above. Setting any key or command to `""` or `false` will
160+
skip it.
160161

161162
The default keymappings are chosen specifically because ordinarily in NeoVim
162163
they are used to convert sections to ROT-13, which most folks don't use.

lua/debugprint/options.lua

+24-21
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ local GLOBAL_OPTION_DEFAULTS = {
88
plain_above = "g?P",
99
variable_below = "g?v",
1010
variable_above = "g?V",
11-
variable_below_alwaysprompt = nil,
12-
variable_above_alwaysprompt = nil,
11+
variable_below_alwaysprompt = "",
12+
variable_above_alwaysprompt = "",
1313
textobj_below = "g?o",
1414
textobj_above = "g?O",
15-
toggle_comment_debug_prints = nil,
16-
delete_debug_prints = nil,
15+
toggle_comment_debug_prints = "",
16+
delete_debug_prints = "",
1717
},
1818
insert = {
1919
plain = "<C-G>p",
@@ -39,7 +39,7 @@ local GLOBAL_OPTION_DEFAULTS = {
3939
---@param o DebugprintGlobalOptions
4040
---@return nil
4141
local validate_global_opts = function(o)
42-
local STRING_NIL = { "string", "nil" }
42+
local STRING_FALSE_NIL = { "string", "boolean", "nil" }
4343

4444
vim.validate({
4545
keymaps = { o.keymaps, "table" },
@@ -58,12 +58,12 @@ local validate_global_opts = function(o)
5858

5959
commands_delete_debug_prints = {
6060
o.commands.delete_debug_prints,
61-
STRING_NIL,
61+
STRING_FALSE_NIL,
6262
},
6363

6464
commands_toggle_comment_debug_prints = {
6565
o.commands.toggle_comment_debug_prints,
66-
STRING_NIL,
66+
STRING_FALSE_NIL,
6767
},
6868
})
6969

@@ -73,39 +73,42 @@ local validate_global_opts = function(o)
7373

7474
if normal ~= nil then
7575
vim.validate({
76-
plain_below = { normal.plain_below, STRING_NIL },
77-
plain_above = { normal.plain_above, STRING_NIL },
78-
variable_below = { normal.variable_below, STRING_NIL },
79-
variable_above = { normal.variable_above, STRING_NIL },
76+
plain_below = { normal.plain_below, STRING_FALSE_NIL },
77+
plain_above = { normal.plain_above, STRING_FALSE_NIL },
78+
variable_below = { normal.variable_below, STRING_FALSE_NIL },
79+
variable_above = { normal.variable_above, STRING_FALSE_NIL },
8080
variable_below_alwaysprompt = {
8181
normal.variable_below_alwaysprompt,
82-
STRING_NIL,
82+
STRING_FALSE_NIL,
8383
},
8484
variable_above_alwaysprompt = {
8585
normal.variable_above_alwaysprompt,
86-
STRING_NIL,
86+
STRING_FALSE_NIL,
87+
},
88+
textobj_below = { normal.textobj_below, STRING_FALSE_NIL },
89+
textobj_above = { normal.textobj_above, STRING_FALSE_NIL },
90+
delete_debug_prints = {
91+
normal.delete_debug_prints,
92+
STRING_FALSE_NIL,
8793
},
88-
textobj_below = { normal.textobj_below, STRING_NIL },
89-
textobj_above = { normal.textobj_above, STRING_NIL },
90-
delete_debug_prints = { normal.delete_debug_prints, STRING_NIL },
9194
toggle_comment_debug_prints = {
9295
normal.toggle_comment_debug_prints,
93-
STRING_NIL,
96+
STRING_FALSE_NIL,
9497
},
9598
})
9699
end
97100

98101
if insert ~= nil then
99102
vim.validate({
100-
variable_below = { insert.plain, STRING_NIL },
101-
variable_above = { insert.variable, STRING_NIL },
103+
variable_below = { insert.plain, STRING_FALSE_NIL },
104+
variable_above = { insert.variable, STRING_FALSE_NIL },
102105
})
103106
end
104107

105108
if visual ~= nil then
106109
vim.validate({
107-
variable_below = { visual.variable_below, STRING_NIL },
108-
variable_above = { visual.variable_above, STRING_NIL },
110+
variable_below = { visual.variable_below, STRING_FALSE_NIL },
111+
variable_above = { visual.variable_above, STRING_FALSE_NIL },
109112
})
110113
end
111114
end

lua/debugprint/setup.lua

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ local M = {}
33
local debugprint = require("debugprint")
44

55
---@param mode string
6-
---@param lhs string
6+
---@param lhs string|false
77
---@param opts table
88
---@return nil
99
local map_key = function(mode, lhs, opts)
10-
if lhs ~= nil and lhs ~= '' then
10+
if lhs ~= nil and lhs ~= "" and lhs ~= false then
1111
vim.api.nvim_set_keymap(mode, lhs, "", opts)
1212
end
1313
end
1414

15-
---@param name string
15+
---@param name string|false
1616
---@param command function
1717
---@param opts table
1818
---@return nil
1919
local create_command = function(name, command, opts)
20-
if name then
20+
if name ~= nil and name ~= "" and name ~= false then
2121
vim.api.nvim_create_user_command(name, command, opts)
2222
end
2323
end

lua/debugprint/types.lua

+16-16
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,28 @@
3232
---@field visual? DebugprintKeymapVisualOptions
3333

3434
---@class DebugprintKeymapNormalOptions
35-
---@field plain_below? string
36-
---@field plain_above? string
37-
---@field variable_below? string
38-
---@field variable_above? string
39-
---@field variable_below_alwaysprompt? string
40-
---@field variable_above_alwaysprompt? string
41-
---@field textobj_below? string
42-
---@field textobj_above? string
43-
---@field delete_debug_prints? string
44-
---@field toggle_comment_debug_prints? string
35+
---@field plain_below? string|false
36+
---@field plain_above? string|false
37+
---@field variable_below? string|false
38+
---@field variable_above? string|false
39+
---@field variable_below_alwaysprompt? string|false
40+
---@field variable_above_alwaysprompt? string|false
41+
---@field textobj_below? string|false
42+
---@field textobj_above? string|false
43+
---@field delete_debug_prints? string|false
44+
---@field toggle_comment_debug_prints? string|false
4545

4646
---@class DebugprintKeymapInsertOptions
47-
---@field plain? string
48-
---@field variable? string
47+
---@field plain? string|false
48+
---@field variable? string|false
4949

5050
---@class DebugprintKeymapVisualOptions
51-
---@field variable_below? string
52-
---@field variable_above? string
51+
---@field variable_below? string|false
52+
---@field variable_above? string|false
5353

5454
---@class DebugprintCommandOptions
55-
---@field delete_debug_prints? string
56-
---@field toggle_comment_debug_prints? string
55+
---@field delete_debug_prints? string|false
56+
---@field toggle_comment_debug_prints? string|false
5757

5858
---@class DebugprintFunctionOptions
5959
---@field above? boolean

tests/debugprint.lua

+103
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,17 @@ local teardown = function()
8585
pcall(vim.keymap.del, { "n", "x" }, "g?V")
8686
pcall(vim.keymap.del, "n", "g?o")
8787
pcall(vim.keymap.del, "n", "g?O")
88+
pcall(vim.api.nvim_del_user_command, "DeleteDebugPrints")
89+
pcall(vim.api.nvim_del_user_command, "ToggleCommentDebugPrints")
8890
vim.cmd("set modifiable")
8991
end
9092

93+
---@param name string
94+
local command_exists = function(name)
95+
local commands = vim.api.nvim_get_commands({})
96+
return commands[name] ~= nil
97+
end
98+
9199
describe("can do setup()", function()
92100
after_each(teardown)
93101

@@ -2492,3 +2500,98 @@ describe("can support insert mode", function()
24922500
})
24932501
end)
24942502
end)
2503+
2504+
describe("can disable built-in keymaps/commands", function()
2505+
after_each(teardown)
2506+
2507+
it("with nil - does NOT disable", function()
2508+
debugprint.setup({
2509+
keymaps = { normal = { plain_below = nil } },
2510+
})
2511+
2512+
local filename = init_file({
2513+
"foo",
2514+
"bar",
2515+
}, "lua", 1, 0)
2516+
2517+
feedkeys("g?p")
2518+
2519+
check_lines({
2520+
"foo",
2521+
"print('DEBUGPRINT[1]: " .. filename .. ":1 (after foo)')",
2522+
"bar",
2523+
})
2524+
end)
2525+
2526+
it("with false - does disable", function()
2527+
debugprint.setup({
2528+
keymaps = { normal = { plain_below = false } },
2529+
})
2530+
2531+
local filename = init_file({
2532+
"foo",
2533+
"bar",
2534+
}, "lua", 1, 0)
2535+
2536+
feedkeys("g?p")
2537+
2538+
check_lines({
2539+
"foo",
2540+
"bar",
2541+
})
2542+
end)
2543+
2544+
it("with empty string - does disable", function()
2545+
debugprint.setup({
2546+
keymaps = { normal = { plain_below = "" } },
2547+
})
2548+
2549+
local filename = init_file({
2550+
"foo",
2551+
"bar",
2552+
}, "lua", 1, 0)
2553+
2554+
feedkeys("g?p")
2555+
2556+
check_lines({
2557+
"foo",
2558+
"bar",
2559+
})
2560+
end)
2561+
2562+
it("custom command nil - does NOT disable", function()
2563+
assert.equals(notify_message, nil)
2564+
2565+
debugprint.setup({ commands = { delete_debug_prints = nil } })
2566+
assert.equals(command_exists("DeleteDebugPrints"), true)
2567+
2568+
init_file({
2569+
"function x()",
2570+
" local xyz = 3",
2571+
"end",
2572+
}, "lua", 2, 1)
2573+
2574+
feedkeys("g?p")
2575+
vim.cmd("DeleteDebugPrint")
2576+
2577+
assert.equals(notify_message, "1 debug line deleted.")
2578+
2579+
check_lines({
2580+
"function x()",
2581+
" local xyz = 3",
2582+
"end",
2583+
})
2584+
end)
2585+
2586+
-- These cannot be tested directly because there doesn't seem to be a way to
2587+
-- intercept a Vim-level error
2588+
it("custom command false - does disable", function()
2589+
debugprint.setup({ commands = { delete_debug_prints = false } })
2590+
assert.equals(command_exists("DeleteDebugPrints"), false)
2591+
end)
2592+
2593+
it("custom command zero-length string - does disable", function()
2594+
debugprint.setup({ commands = { delete_debug_prints = "" } })
2595+
assert.equals(command_exists("DeleteDebugPrints"), false)
2596+
end)
2597+
end)

0 commit comments

Comments
 (0)