Skip to content

Commit

Permalink
fix: Lazy loading - no autocmds/warn on unmodifiable buffers - closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewferrier committed Apr 23, 2024
1 parent e6bc11e commit 61dbc50
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 120 deletions.
12 changes: 12 additions & 0 deletions lua/debugprint/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ M.debugprint = function(opts)
local func_opts =
require("debugprint.options").get_and_validate_function_opts(opts)

if not utils.is_modifiable() then
return
end

if func_opts.motion == true then
cache_request = func_opts
vim.go.operatorfunc =
Expand Down Expand Up @@ -234,6 +238,10 @@ M.deleteprints = function(opts)
local lines_to_consider, initial_line = get_lines_to_handle(opts)
local delete_adjust = 0

if not utils.is_modifiable() then
return
end

for count, line in ipairs(lines_to_consider) do
if string.find(line, global_opts.print_tag, 1, true) ~= nil then
local line_to_delete = count
Expand All @@ -255,6 +263,10 @@ end
M.toggle_comment_debugprints = function(opts)
local lines_to_consider, initial_line = get_lines_to_handle(opts)

if not utils.is_modifiable() then
return
end

for count, line in ipairs(lines_to_consider) do
if string.find(line, global_opts.print_tag, 1, true) ~= nil then
local line_to_toggle = count + initial_line - 1
Expand Down
237 changes: 117 additions & 120 deletions lua/debugprint/setup.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ local M = {}

local debugprint = require("debugprint")

local map_key = function(mode, lhs, buffer, opts)
if
lhs ~= nil
and vim.api.nvim_get_option_value("modifiable", { buf = buffer })
then
vim.api.nvim_buf_set_keymap(buffer, mode, lhs, "", opts)
local map_key = function(mode, lhs, opts)
if lhs ~= nil then
vim.api.nvim_set_keymap(mode, lhs, "", opts)
end
end

Expand All @@ -18,130 +15,130 @@ local feedkeys = function(keys)
end

M.map_keys_and_commands = function(global_opts)
vim.api.nvim_create_autocmd({ "BufReadPost", "BufNewFile" }, {
group = vim.api.nvim_create_augroup(
"debugprint_augroup",
{ clear = true }
),
callback = function(opts)
map_key("n", global_opts.keymaps.normal.plain_below, opts.buf, {
callback = function()
feedkeys(debugprint.debugprint({}))
end,
desc = "Plain debug below current line",
})
map_key("n", global_opts.keymaps.normal.plain_below, {
callback = function()
feedkeys(debugprint.debugprint({}))
end,
desc = "Plain debug below current line",
})

map_key("n", global_opts.keymaps.normal.plain_above, opts.buf, {
callback = function()
feedkeys(debugprint.debugprint({ above = true }))
end,
desc = "Plain debug below current line",
})
map_key("n", global_opts.keymaps.normal.plain_above, {
callback = function()
feedkeys(debugprint.debugprint({ above = true }))
end,
desc = "Plain debug below current line",
})

map_key("n", global_opts.keymaps.normal.variable_below, opts.buf, {
callback = function()
feedkeys(debugprint.debugprint({ variable = true }))
end,
desc = "Variable debug below current line",
})
map_key("n", global_opts.keymaps.normal.variable_below, {
callback = function()
feedkeys(debugprint.debugprint({ variable = true }))
end,
desc = "Variable debug below current line",
})

map_key("n", global_opts.keymaps.normal.variable_above, opts.buf, {
callback = function()
feedkeys(debugprint.debugprint({
above = true,
variable = true,
}))
end,
desc = "Variable debug above current line",
})
map_key("n", global_opts.keymaps.normal.variable_above, {
callback = function()
feedkeys(debugprint.debugprint({
above = true,
variable = true,
}))
end,
desc = "Variable debug above current line",
})

map_key(
"n",
global_opts.keymaps.normal.variable_below_alwaysprompt,
opts.buf,
{
callback = function()
feedkeys(debugprint.debugprint({
variable = true,
ignore_treesitter = true,
}))
end,
desc = "Variable debug below current line (always prompt})",
}
)

map_key(
"n",
global_opts.keymaps.normal.variable_above_alwaysprompt,
opts.buf,
{
callback = function()
feedkeys(debugprint.debugprint({
above = true,
variable = true,
ignore_treesitter = true,
}))
end,
desc = "Variable debug above current line (always prompt})",
}
)

map_key("n", global_opts.keymaps.normal.textobj_below, opts.buf, {
callback = function()
return debugprint.debugprint({ motion = true })
end,
expr = true,
desc = "Text-obj-selected variable debug below current line",
})
map_key("n", global_opts.keymaps.normal.variable_below_alwaysprompt, {
callback = function()
feedkeys(debugprint.debugprint({
variable = true,
ignore_treesitter = true,
}))
end,
desc = "Variable debug below current line (always prompt})",
})

map_key("n", global_opts.keymaps.normal.textobj_above, opts.buf, {
callback = function()
return debugprint.debugprint({
motion = true,
above = true,
})
end,
expr = true,
desc = "Text-obj-selected variable debug above current line",
})
map_key("n", global_opts.keymaps.normal.variable_above_alwaysprompt, {
callback = function()
feedkeys(debugprint.debugprint({
above = true,
variable = true,
ignore_treesitter = true,
}))
end,
desc = "Variable debug above current line (always prompt})",
})

map_key(
"n",
global_opts.keymaps.normal.delete_debug_prints,
opts.buf,
{
callback = debugprint.deleteprints,
desc = "Delete all debugprint statements in the current buffer",
}
)

map_key(
"n",
global_opts.keymaps.normal.toggle_comment_debug_prints,
opts.buf,
{
callback = debugprint.toggle_comment_debugprints,
desc = "Comment/uncomment all debugprint statements in the current buffer",
}
)

map_key("x", global_opts.keymaps.visual.variable_below, opts.buf, {
callback = function()
feedkeys(debugprint.debugprint({ variable = true }))
end,
desc = "Variable debug below current line",
map_key("n", global_opts.keymaps.normal.textobj_below, {
callback = function()
return debugprint.debugprint({ motion = true })
end,
expr = true,
desc = "Text-obj-selected variable debug below current line",
})

map_key("n", global_opts.keymaps.normal.textobj_above, {
callback = function()
return debugprint.debugprint({
motion = true,
above = true,
})
end,
expr = true,
desc = "Text-obj-selected variable debug above current line",
})

map_key("x", global_opts.keymaps.visual.variable_below, {
callback = function()
feedkeys(debugprint.debugprint({ variable = true }))
end,
desc = "Variable debug below current line",
})

map_key("x", global_opts.keymaps.visual.variable_above, {
callback = function()
feedkeys(debugprint.debugprint({
above = true,
variable = true,
}))
end,
desc = "Variable debug above current line",
})

map_key("n", global_opts.keymaps.normal.delete_debug_prints, {
callback = debugprint.deleteprints,
desc = "Delete all debugprint statements in the current buffer",
})

map_key("n", global_opts.keymaps.normal.toggle_comment_debug_prints, {
callback = debugprint.toggle_comment_debugprints,
desc = "Comment/uncomment all debugprint statements in the current buffer",
})

map_key("x", global_opts.keymaps.visual.variable_below, {
callback = function()
feedkeys(debugprint.debugprint({ variable = true }))
end,
desc = "Variable debug below current line",
})

map_key("x", global_opts.keymaps.visual.variable_above, {
callback = function()
feedkeys(debugprint.debugprint({
above = true,
variable = true,
}))
end,
desc = "Variable debug above current line",
})

map_key("x", global_opts.keymaps.visual.variable_above, opts.buf, {
callback = function()
feedkeys(debugprint.debugprint({
above = true,
variable = true,
}))
end,
desc = "Variable debug above current line",
map_key("n", global_opts.keymaps.normal.variable_above_alwaysprompt, {
callback = function()
return debugprint.debugprint({
above = true,
variable = true,
ignore_treesitter = true,
})
end,
desc = "Variable debug above current line (always prompt})",
})

if global_opts.commands.delete_debug_prints then
Expand Down
9 changes: 9 additions & 0 deletions lua/debugprint/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ local get_node_at_cursor = function()
end
end

M.is_modifiable = function()
if not vim.api.nvim_get_option_value("modifiable", { buf = vim.api.nvim_get_current_buf() }) then
vim.notify('Buffer is not modifiable.', vim.log.levels.ERROR)
return false
else
return true
end
end

M.get_variable_name = function(
global_ignore_treesitter,
local_ignore_treesitter
Expand Down
28 changes: 28 additions & 0 deletions tests/debugprint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1837,3 +1837,31 @@ describe("handle deprecated options, create_keymaps=true", function()
})
end)
end)

describe("unmodifiable buffer", function()
before_each(function()
debugprint.setup({ create_keymaps = true })
end)

after_each(teardown)

it("basic", function()
assert.equals(notify_message, nil)

init_file({
"foo",
"bar",
}, "lua", 1, 0)

vim.cmd("set nomodifiable")

feedkeys("g?p")

check_lines({
"foo",
"bar",
})

assert.equals(notify_message, "Buffer is not modifiable.")
end)
end)

0 comments on commit 61dbc50

Please sign in to comment.