Skip to content

Commit f6987a6

Browse files
committed
refactor: Use inbuilt commenting on NeoVim 0.10+
1 parent ff44034 commit f6987a6

File tree

3 files changed

+39
-29
lines changed

3 files changed

+39
-29
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ return {
7272
"andrewferrier/debugprint.nvim",
7373
opts = { … },
7474
dependencies = {
75-
"echasnovski/mini.nvim" -- Needed to enable :ToggleCommentDebugPrints
75+
"echasnovski/mini.nvim" -- Needed to enable :ToggleCommentDebugPrints for NeoVim <= 0.9
7676
"nvim-treesitter/nvim-treesitter" -- Only needed for NeoVim 0.8
7777
},
7878
-- Remove the following line to use development versions,
@@ -93,7 +93,7 @@ packer.startup(function(use)
9393
require("debugprint").setup(opts)
9494
end,
9595
requires = {
96-
"echasnovski/mini.nvim" -- Needed to enable :ToggleCommentDebugPrints
96+
"echasnovski/mini.nvim" -- Needed to enable :ToggleCommentDebugPrints for NeoVim <= 0.9
9797
}
9898
})
9999

lua/debugprint/init.lua

+17-27
Original file line numberDiff line numberDiff line change
@@ -257,34 +257,24 @@ M.deleteprints = function(opts)
257257
end
258258

259259
M.toggle_comment_debugprints = function(opts)
260-
local status, comment = pcall(require, "mini.comment")
261-
262-
if status == true then
263-
local lines_to_consider
264-
local initial_line
265-
266-
lines_to_consider, initial_line = get_lines_to_handle(opts)
267-
268-
for count, line in ipairs(lines_to_consider) do
269-
if string.find(line, global_opts.print_tag, 1, true) ~= nil then
270-
local line_to_toggle = count + initial_line - 1
271-
vim.api.nvim_buf_set_lines(
272-
0,
273-
line_to_toggle,
274-
line_to_toggle,
275-
false,
276-
{}
277-
)
278-
279-
comment.toggle_lines(line_to_toggle, line_to_toggle, {})
280-
end
260+
local lines_to_consider
261+
local initial_line
262+
263+
lines_to_consider, initial_line = get_lines_to_handle(opts)
264+
265+
for count, line in ipairs(lines_to_consider) do
266+
if string.find(line, global_opts.print_tag, 1, true) ~= nil then
267+
local line_to_toggle = count + initial_line - 1
268+
vim.api.nvim_buf_set_lines(
269+
0,
270+
line_to_toggle,
271+
line_to_toggle,
272+
false,
273+
{}
274+
)
275+
276+
utils.toggle_comment_line(line_to_toggle)
281277
end
282-
else
283-
vim.notify(
284-
"mini.nvim is required to toggle comment debugprint lines",
285-
vim.log.levels.ERROR,
286-
{}
287-
)
288278
end
289279
end
290280

lua/debugprint/utils.lua

+20
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,26 @@ M.indent_line = function(line_nr, move_to_indented_line)
8787
end
8888
end
8989

90+
M.toggle_comment_line = function(line)
91+
if vim.fn.has("nvim-0.10.0") == 1 then
92+
local pos = vim.api.nvim_win_get_cursor(0)
93+
vim.cmd(line .. "norm gcc")
94+
vim.api.nvim_win_set_cursor(0, pos)
95+
else
96+
local status, comment = pcall(require, "mini.comment")
97+
98+
if status == true then
99+
comment.toggle_lines(line, line, {})
100+
else
101+
vim.notify_once(
102+
"mini.nvim is required to toggle comment debugprint lines prior to NeoVim 0.10",
103+
vim.log.levels.ERROR,
104+
{}
105+
)
106+
end
107+
end
108+
end
109+
90110
M.NOOP = function() end
91111

92112
M.set_callback = function(func_name)

0 commit comments

Comments
 (0)