Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: extract debug functions to a separate module #114

Merged
merged 1 commit into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions lua/blink-ripgrep/debug.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
local M = {}

-- selene: allow(global_usage)
_G.blink_ripgrep_invocations = _G.blink_ripgrep_invocations or {}

--- Add the invocation to the global list of invocations so that they can be
--- asserted against in tests. Otherwise the tests cannot reliably verify that
--- something did or not happen.
---@param invocation unknown
function M.add_debug_invocation(invocation)
-- selene: allow(global_usage)
table.insert(_G.blink_ripgrep_invocations, invocation)
end

--- Variant of vim.notify that does not display the messages to the user until
--- they execute `:messages`. Displaying the messages might disturb the user or
--- interfere with tests.
---@param message string
function M.add_debug_message(message)
vim.api.nvim_exec2(string.format("echomsg '%s'", message), {})
end

return M
37 changes: 10 additions & 27 deletions lua/blink-ripgrep/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -182,17 +182,9 @@ function RgSource:get_completions(context, resolve)

if cmd == nil then
if RgSource.config.debug then
vim.api.nvim_exec2(
"echomsg 'no command returned, skipping the search'",
{}
)
-- selene: allow(global_usage)
_G.blink_ripgrep_invocations = _G.blink_ripgrep_invocations or {}
-- selene: allow(global_usage)
table.insert(
_G.blink_ripgrep_invocations,
{ "ignored-because-no-command" }
)
local debug = require("blink-ripgrep.debug")
debug.add_debug_message("no command returned, skipping the search")
debug.add_debug_invocation({ "ignored-because-no-command" })
end

resolve()
Expand All @@ -201,20 +193,12 @@ function RgSource:get_completions(context, resolve)

if vim.tbl_contains(RgSource.config.ignore_paths, cmd.root) then
if RgSource.config.debug then
vim.api.nvim_exec2(
string.format("echomsg 'skipping search in ignored path %s'", cmd.root),
{}
)
local debug = require("blink-ripgrep.debug")
debug.add_debug_message("skipping search in ignored path" .. cmd.root)
debug.add_debug_invocation({ "ignored", cmd.root })
end
resolve()

if RgSource.config.debug then
-- selene: allow(global_usage)
_G.blink_ripgrep_invocations = _G.blink_ripgrep_invocations or {}
-- selene: allow(global_usage)
table.insert(_G.blink_ripgrep_invocations, { "ignored", cmd.root })
end

return
end

Expand All @@ -224,10 +208,7 @@ function RgSource:get_completions(context, resolve)
end

require("blink-ripgrep.visualization").flash_search_prefix(prefix)
-- selene: allow(global_usage)
_G.blink_ripgrep_invocations = _G.blink_ripgrep_invocations or {}
-- selene: allow(global_usage)
table.insert(_G.blink_ripgrep_invocations, cmd)
require("blink-ripgrep.debug").add_debug_invocation(cmd)
end

local rg = vim.system(cmd.command, nil, function(result)
Expand Down Expand Up @@ -294,7 +275,9 @@ function RgSource:get_completions(context, resolve)
return function()
rg:kill(9)
if RgSource.config.debug then
vim.api.nvim_exec2("echomsg 'killed previous invocation'", {})
require("blink-ripgrep.debug").add_debug_message(
"killed previous invocation"
)
end
end
end
Expand Down
Loading