Skip to content

Commit 8116969

Browse files
author
Andrew Ferrier
committed
feat: Support visual keymapping - closes #22
1 parent 6f55f22 commit 8116969

File tree

4 files changed

+217
-10
lines changed

4 files changed

+217
-10
lines changed

README.md

+13-8
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ It provides various improvements:
2828

2929
* It can pick up a variable name from under the cursor.
3030

31+
* It provides keymappings for visual mode, so you can select a variable
32+
visually and print it out.
33+
3134
* It indents the lines it inserts more accurately.
3235

3336
* The output when printing a 'plain' debug line, or a variable, is more
@@ -97,14 +100,16 @@ standard way to use it. There are also some function invocations which are not
97100
mapped to any keymappings by default, but could be. This is all shown in the
98101
following table.
99102

100-
| Keymap | Purpose | Equivalent Lua Function |
101-
| ------ | ---------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------- |
102-
| `g?p` | Insert a 'plain' debug line appropriate to the filetype just below the current line | `require('debugprint').debugprint()` |
103-
| `g?P` | The same, but above the current line | `require('debugprint').debugprint({above = true})` |
104-
| `g?v` | Insert a variable debugging line below the current line. If the cursor is on a variable name, use that, otherwise prompt for one. | `require('debugprint').debugprint({variable = true})` |
105-
| `g?V` | The same, but above the current line | `require('debugprint').debugprint({above = true, variable = true})` |
106-
| None by default | Always prompt for a variable name, and insert a debugging line just below the current line which outputs it | `require('debugprint').debugprint({ignore_treesitter = true, variable = true})` |
107-
| None by default | Always prompt for a variable name, and insert a debugging line just above the current line which outputs it | `require('debugprint').debugprint({ignore_treesitter = true, above = true, variable = true})` |
103+
| Mode | Keymap | Purpose | Equivalent Lua Function |
104+
| ------ | --------------- | --------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------- |
105+
| Normal | `g?p` | Insert a 'plain' debug line appropriate to the filetype just below the current line | `require('debugprint').debugprint()` |
106+
| Normal | `g?P` | The same, but above the current line | `require('debugprint').debugprint({above = true})` |
107+
| Normal | `g?v` | Insert a variable debugging line below the current line. If the cursor is on a variable name, use that, otherwise prompt for one. | `require('debugprint').debugprint({variable = true})` |
108+
| Normal | `g?V` | The same, but above the current line | `require('debugprint').debugprint({above = true, variable = true})` |
109+
| Normal | None by default | Always prompt for a variable name, and insert a debugging line just below the current line which outputs it | `require('debugprint').debugprint({ignore_treesitter = true, variable = true})` |
110+
| Normal | None by default | Always prompt for a variable name, and insert a debugging line just above the current line which outputs it | `require('debugprint').debugprint({ignore_treesitter = true, above = true, variable = true})` |
111+
| Visual | `g?v` | Find the visually select variable name, and insert a debugging line just below the current line which outputs it | `require('debugprint').debugprint({variable = true})` |
112+
| Visual | `g?V` | Find the visually select variable name, and insert a debugging line just above the current line which outputs it | `require('debugprint').debugprint({above = true, variable = true})` |
108113

109114
These keybindings are chosen specifically because by default in NeoVim they are
110115
used to convert sections to ROT-13, which most folks don't use. You can disable

lua/debugprint/init.lua

+55-1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,43 @@ local find_treesitter_variable = function()
9090
end
9191
end
9292

93+
local get_visual_selection = function()
94+
local mode = vim.fn.mode():lower()
95+
if not (mode:find("^v") or mode:find("^ctrl-v")) then
96+
return nil
97+
end
98+
99+
local first_pos, last_pos = vim.fn.getpos("v"), vim.fn.getpos(".")
100+
101+
local line1 = first_pos[2] - 1
102+
local line2 = last_pos[2] - 1
103+
local col1 = first_pos[3] - 1
104+
local col2 = last_pos[3]
105+
106+
if line2 < line1 or (line1 == line2 and col2 < col1) then
107+
local linet = line2
108+
line2 = line1
109+
line1 = linet
110+
111+
local colt = col2
112+
col2 = col1
113+
col1 = colt
114+
115+
col1 = col1 - 1
116+
col2 = col2 + 1
117+
end
118+
119+
if line1 ~= line2 then
120+
vim.notify(
121+
"debugprint not supported when multiple lines selected.",
122+
vim.log.levels.ERROR
123+
)
124+
return false
125+
end
126+
127+
return vim.api.nvim_buf_get_text(0, line1, col1, line2, col2, {})[1]
128+
end
129+
93130
local debugprint_logic = function(funcopts)
94131
local current_line = vim.api.nvim_win_get_cursor(0)[1]
95132
local filetype =
@@ -147,8 +184,15 @@ local debugprint_cache = function(o)
147184
end
148185

149186
if o.variable == true then
187+
o.variable_name = get_visual_selection()
188+
189+
if o.variable_name == false then
190+
return
191+
end
192+
150193
if
151-
o.ignore_treesitter ~= true
194+
o.variable_name == nil
195+
and o.ignore_treesitter ~= true
152196
and opts.ignore_treesitter ~= true
153197
then
154198
o.variable_name = find_treesitter_variable()
@@ -226,6 +270,16 @@ M.setup = function(o)
226270
end, {
227271
expr = true,
228272
})
273+
vim.keymap.set("x", "g?v", function()
274+
return M.debugprint({ variable = true })
275+
end, {
276+
expr = true,
277+
})
278+
vim.keymap.set("x", "g?V", function()
279+
return M.debugprint({ above = true, variable = true })
280+
end, {
281+
expr = true,
282+
})
229283

230284
vim.keymap.set("n", "dqp", function()
231285
notify_deprecated()

tests/debugprint.lua

+148
Original file line numberDiff line numberDiff line change
@@ -692,3 +692,151 @@ describe("can handle treesitter identifiers", function()
692692
assert.are.same(vim.api.nvim_win_get_cursor(0), { 2, 6 })
693693
end)
694694
end)
695+
696+
describe("visual selection", function()
697+
it("standard", function()
698+
debugprint.setup({ ignore_treesitter = true })
699+
700+
set_lines({
701+
"function x() {",
702+
"local xyz = 3",
703+
"end",
704+
})
705+
706+
local filename = write_file("lua")
707+
vim.api.nvim_win_set_cursor(0, { 2, 6 })
708+
feedkeys("vllg?v")
709+
710+
check_lines({
711+
"function x() {",
712+
"local xyz = 3",
713+
"print('DEBUG[1]: " .. filename .. ":2: xyz=' .. vim.inspect(xyz))",
714+
"end",
715+
})
716+
end)
717+
718+
it("repeat", function()
719+
debugprint.setup({ ignore_treesitter = true })
720+
721+
set_lines({
722+
"function x() {",
723+
"local xyz = 3",
724+
"end",
725+
})
726+
727+
local filename = write_file("lua")
728+
vim.api.nvim_win_set_cursor(0, { 2, 6 })
729+
feedkeys("vllg?v.")
730+
731+
check_lines({
732+
"function x() {",
733+
"local xyz = 3",
734+
"print('DEBUG[2]: " .. filename .. ":2: xyz=' .. vim.inspect(xyz))",
735+
"print('DEBUG[1]: " .. filename .. ":2: xyz=' .. vim.inspect(xyz))",
736+
"end",
737+
})
738+
end)
739+
740+
it("standard line extremes", function()
741+
debugprint.setup({ ignore_treesitter = true })
742+
743+
set_lines({
744+
"function x() {",
745+
"xyz",
746+
"end",
747+
})
748+
749+
local filename = write_file("lua")
750+
vim.api.nvim_win_set_cursor(0, { 2, 0 })
751+
feedkeys("vllg?v")
752+
753+
check_lines({
754+
"function x() {",
755+
"xyz",
756+
"print('DEBUG[1]: " .. filename .. ":2: xyz=' .. vim.inspect(xyz))",
757+
"end",
758+
})
759+
end)
760+
761+
it("reverse", function()
762+
debugprint.setup({ ignore_treesitter = true })
763+
764+
set_lines({
765+
"function x() {",
766+
"local xyz = 3",
767+
"end",
768+
})
769+
770+
local filename = write_file("lua")
771+
vim.api.nvim_win_set_cursor(0, { 2, 8 })
772+
feedkeys("vhhg?v")
773+
774+
check_lines({
775+
"function x() {",
776+
"local xyz = 3",
777+
"print('DEBUG[1]: " .. filename .. ":2: xyz=' .. vim.inspect(xyz))",
778+
"end",
779+
})
780+
end)
781+
782+
it("reverse extremes", function()
783+
debugprint.setup({ ignore_treesitter = true })
784+
785+
set_lines({
786+
"function x() {",
787+
"local xyz = 3",
788+
"end",
789+
})
790+
791+
local filename = write_file("lua")
792+
vim.api.nvim_win_set_cursor(0, { 2, 6 })
793+
feedkeys("vllg?v")
794+
795+
check_lines({
796+
"function x() {",
797+
"local xyz = 3",
798+
"print('DEBUG[1]: " .. filename .. ":2: xyz=' .. vim.inspect(xyz))",
799+
"end",
800+
})
801+
end)
802+
803+
it("above", function()
804+
debugprint.setup({ ignore_treesitter = true })
805+
806+
set_lines({
807+
"function x() {",
808+
"local xyz = 3",
809+
"end",
810+
})
811+
812+
local filename = write_file("lua")
813+
vim.api.nvim_win_set_cursor(0, { 2, 6 })
814+
feedkeys("vllg?V")
815+
816+
check_lines({
817+
"function x() {",
818+
" print('DEBUG[1]: " .. filename .. ":2: xyz=' .. vim.inspect(xyz))",
819+
"local xyz = 3",
820+
"end",
821+
})
822+
end)
823+
824+
it("ignore multiline", function()
825+
debugprint.setup({ ignore_treesitter = true })
826+
827+
set_lines({
828+
"function x() {",
829+
"local xyz = 3",
830+
"end",
831+
})
832+
833+
write_file("lua")
834+
vim.api.nvim_win_set_cursor(0, { 1, 1 })
835+
feedkeys("vjg?v")
836+
837+
assert.are.same(
838+
"debugprint not supported when multiple lines selected.",
839+
notify_message
840+
)
841+
end)
842+
end)

tests/minimal.vim

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ set rtp+=../nvim-treesitter
88
runtime! plugin/plenary.vim
99
runtime! plugin/nvim-treesitter.lua
1010

11-
TSInstallSync lua
11+
TSInstallSync! lua

0 commit comments

Comments
 (0)