Skip to content

Commit

Permalink
perf: kill previous ripgrep searches when a new search is started (#106)
Browse files Browse the repository at this point in the history
This supersedes the previous `future_features` version of
kill_previous_searches. That should be removed, as this is now the
default.

Closes #102
  • Loading branch information
mikavilpas authored Jan 12, 2025
1 parent 705069a commit 8df7edd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 40 deletions.
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,6 @@ return {
-- Show debug information in `:messages` that can help in
-- diagnosing issues with the plugin.
debug = false,

-- Features that are not yet stable and might change in the future.
future_features = {
-- Kill previous searches when a new search is started. This is
-- useful to save resources and might become the default in the
-- future.
kill_previous_searches = false,
},
},
-- (optional) customize how the results are displayed. Many options
-- are available - make sure your lua LSP is set up so you get
Expand Down
35 changes: 35 additions & 0 deletions integration-tests/cypress/e2e/blink-ripgrep/basic_spec.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,41 @@ describe("debug mode", () => {
})
})
})

it("can clean up (kill) a previous rg search", () => {
// to save resources, the plugin should clean up a previous search when a
// new search is started. Blink should handle this internally, see
// https://github.com/mikavilpas/blink-ripgrep.nvim/issues/102

cy.visit("/")
cy.startNeovim({}).then(() => {
// wait until text on the start screen is visible
cy.contains("If you see this text, Neovim is ready!")
createFakeGitDirectoriesToLimitRipgrepScope()

// clear the current line and enter insert mode
cy.typeIntoTerminal("cc")

// debug mode should be on by default for all tests. Otherwise it doesn't
// make sense to test this, as nothing will be displayed.
cy.runLuaCode({
luaCode: `assert(require("blink-ripgrep").config.debug)`,
})

// search for something that does not exist. This should start a couple
// of searches
cy.typeIntoTerminal("yyyyyy", { delay: 80 })
cy.runExCommand({ command: "messages" }).then((result) => {
expect(result.value).to.contain("killed previous invocation")
})
cy.runLuaCode({
luaCode: `return _G.blink_ripgrep_invocations`,
}).should((result) => {
expect(result.value).to.be.an("array")
expect(result.value).to.have.length.above(3)
})
})
})
})

describe("using .gitignore files to exclude files from searching", () => {
Expand Down
3 changes: 0 additions & 3 deletions integration-tests/test-environment/.config/nvim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ local plugins = {
---@type blink-ripgrep.Options
opts = {
debug = true,
future_features = {
kill_previous_searches = true,
},
},
},
},
Expand Down
34 changes: 5 additions & 29 deletions lua/blink-ripgrep/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
---@field fallback_to_regex_highlighting? boolean # (default: true) When a result is found for a file whose filetype does not have a treesitter parser installed, fall back to regex based highlighting that is bundled in Neovim.
---@field project_root_marker? unknown # Specifies how to find the root of the project where the ripgrep search will start from. Accepts the same options as the marker given to `:h vim.fs.root()` which offers many possibilities for configuration. Defaults to ".git".
---@field debug? boolean # Show debug information in `:messages` that can help in diagnosing issues with the plugin.
---@field future_features? blink-ripgrep.FutureFeatures # Features that are not yet stable and might change in the future.

---@class blink-ripgrep.FutureFeatures
---@field kill_previous_searches? boolean # Kill previous searches when a new search is started. This is useful to save resources and might become the default in the future.

---@class blink-ripgrep.RgSource : blink.cmp.Source
---@field get_command fun(context: blink.cmp.Context, prefix: string): string[]
Expand Down Expand Up @@ -59,9 +55,6 @@ RgSource.config = {
search_casing = "--ignore-case",
fallback_to_regex_highlighting = true,
project_root_marker = ".git",
future_features = {
kill_previous_searches = false,
},
}

-- set up default options so that they are used by the next search
Expand Down Expand Up @@ -164,9 +157,6 @@ local function render_item_documentation(opts, file, match)
)
end

---@type vim.Ringbuf<vim.SystemObj>
local ripgrep_invocations = vim.ringbuf(3)

function RgSource:get_completions(context, resolve)
local prefix = self.get_prefix(context)

Expand Down Expand Up @@ -194,23 +184,6 @@ function RgSource:get_completions(context, resolve)
end
end

local kill_previous_searches = (
RgSource.config.future_features
and RgSource.config.future_features.kill_previous_searches
) or false

if kill_previous_searches then
-- drain all previous invocations and kill them to save resources
for val in ripgrep_invocations do
---@cast val vim.SystemObj
val:kill(9)
if RgSource.config.debug then
vim.api.nvim_exec2("echomsg 'killed previous invocation'", {})
end
end
assert(#ripgrep_invocations == 0)
end

local rg = vim.system(cmd, nil, function(result)
vim.schedule(function()
if result.code ~= 0 then
Expand Down Expand Up @@ -272,8 +245,11 @@ function RgSource:get_completions(context, resolve)
end)
end)

if kill_previous_searches then
ripgrep_invocations:push(rg)
return function()
rg:kill(9)
if RgSource.config.debug then
vim.api.nvim_exec2("echomsg 'killed previous invocation'", {})
end
end
end

Expand Down

0 comments on commit 8df7edd

Please sign in to comment.