Skip to content

Commit

Permalink
refactor: extract debug functions to a separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
mikavilpas committed Jan 19, 2025
1 parent db1fa9c commit e5f03e8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
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

0 comments on commit e5f03e8

Please sign in to comment.