Skip to content

Commit

Permalink
refactor: move mode checks to the main file
Browse files Browse the repository at this point in the history
  • Loading branch information
mikavilpas committed Feb 22, 2025
1 parent 2b6834b commit db73596
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
18 changes: 2 additions & 16 deletions lua/blink-ripgrep/backends/ripgrep/ripgrep.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
---@class blink-ripgrep.Backend
---@field config blink-ripgrep.Options
local RipgrepBackend = {}

---@param config table
Expand All @@ -10,21 +9,6 @@ function RipgrepBackend.new(config)
end

function RipgrepBackend:get_matches(prefix, context, resolve)
if self.config.mode ~= "on" then
if self.config.debug then
local debug = require("blink-ripgrep.debug")
debug.add_debug_message("mode is off, skipping the search")
debug.add_debug_invocation({ "ignored-because-mode-is-off" })
end
resolve()
return
end

if string.len(prefix) < self.config.prefix_min_len then
resolve()
return
end

-- builtin default command
local command_module =
require("blink-ripgrep.backends.ripgrep.ripgrep_command")
Expand Down Expand Up @@ -122,6 +106,8 @@ function RipgrepBackend:get_matches(prefix, context, resolve)
end
end

-- Had some issues with E550, might be fixed upstream nowadays. See
-- https://github.com/mikavilpas/blink-ripgrep.nvim/issues/53
vim.schedule(function()
resolve({
is_incomplete_forward = false,
Expand Down
17 changes: 17 additions & 0 deletions lua/blink-ripgrep/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
---| "off" # Don't show completions at all

---@class blink-ripgrep.Backend # a backend defines how to get matches from the project's files for a search
---@field config blink-ripgrep.Options
---@field get_matches fun(self: blink-ripgrep.Backend, prefix: string, context: blink.cmp.Context, resolve: fun(response: blink.cmp.CompletionResponse | nil)): nil | fun(): nil # start a search process. Return an optional cancellation function that kills the search in case the user has canceled the completion.

---@class blink-ripgrep.RgSource : blink.cmp.Source
Expand Down Expand Up @@ -86,9 +87,25 @@ function RgSource.new(input_opts)
end

function RgSource:get_completions(context, resolve)
if self.config.mode ~= "on" then
if self.config.debug then
local debug = require("blink-ripgrep.debug")
debug.add_debug_message("mode is off, skipping the search")
debug.add_debug_invocation({ "ignored-because-mode-is-off" })
end
resolve()
return
end

local ripgrep =
require("blink-ripgrep.backends.ripgrep.ripgrep").new(RgSource.config)
local prefix = self.get_prefix(context)

if string.len(prefix) < self.config.prefix_min_len then
resolve()
return
end

local cancellation_function = ripgrep:get_matches(prefix, context, resolve)

return cancellation_function
Expand Down

0 comments on commit db73596

Please sign in to comment.