diff --git a/README.md b/README.md index e2f34e3..0a19998 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ return { sources = { default = { "buffer", - "ripgrep", -- 👈🏻 add "ripgrep" here + "ripgrep", -- 👈🏻 add "ripgrep" here }, providers = { -- 👇🏻👇🏻 add the ripgrep provider config below @@ -108,6 +108,14 @@ 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 diff --git a/integration-tests/cypress/support/tui-sandbox.ts b/integration-tests/cypress/support/tui-sandbox.ts index 8df6905..a6b98df 100644 --- a/integration-tests/cypress/support/tui-sandbox.ts +++ b/integration-tests/cypress/support/tui-sandbox.ts @@ -127,5 +127,19 @@ declare global { afterEach(async () => { if (!testWindow) return - await testWindow.runExCommand({ command: "messages", log: true }) + const timeout = new Promise((resolve, reject) => + setTimeout(() => { + Cypress.log({ + name: "timeout when waiting for :messages to finish. Neovim might be stuck.", + }) + reject( + "timeout when waiting for :messages to finish. Neovim might be stuck.", + ) + }, 5_000), + ) + + await Promise.race([ + timeout, + testWindow.runExCommand({ command: "messages" }), + ]) }) diff --git a/integration-tests/package.json b/integration-tests/package.json index 40ac151..70c8036 100644 --- a/integration-tests/package.json +++ b/integration-tests/package.json @@ -17,7 +17,7 @@ "zod": "3.24.1" }, "devDependencies": { - "@tui-sandbox/library": "7.5.5", + "@tui-sandbox/library": "7.5.6", "@types/node": "22.10.3", "@types/tinycolor2": "1.4.6", "@typescript-eslint/eslint-plugin": "8.19.0", diff --git a/integration-tests/test-environment/.config/nvim/init.lua b/integration-tests/test-environment/.config/nvim/init.lua index f14c86e..3dac1b0 100644 --- a/integration-tests/test-environment/.config/nvim/init.lua +++ b/integration-tests/test-environment/.config/nvim/init.lua @@ -71,6 +71,9 @@ local plugins = { ---@type blink-ripgrep.Options opts = { debug = true, + future_features = { + kill_previous_searches = true, + }, }, }, }, diff --git a/lua/blink-ripgrep/init.lua b/lua/blink-ripgrep/init.lua index c5f3b9a..93ea63b 100644 --- a/lua/blink-ripgrep/init.lua +++ b/lua/blink-ripgrep/init.lua @@ -11,6 +11,10 @@ ---@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[] @@ -55,6 +59,9 @@ 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 @@ -157,6 +164,9 @@ local function render_item_documentation(opts, file, match) ) end +---@type vim.Ringbuf +local ripgrep_invocations = vim.ringbuf(3) + function RgSource:get_completions(context, resolve) local prefix = self.get_prefix(context) @@ -184,7 +194,24 @@ function RgSource:get_completions(context, resolve) end end - vim.system(cmd, nil, function(result) + 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 resolve() @@ -244,6 +271,10 @@ function RgSource:get_completions(context, resolve) end) end) end) + + if kill_previous_searches then + ripgrep_invocations:push(rg) + end end return RgSource diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a002bf8..648e169 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -37,8 +37,8 @@ importers: version: 3.24.1 devDependencies: '@tui-sandbox/library': - specifier: 7.5.5 - version: 7.5.5(cypress@13.17.0)(prettier@3.4.2)(type-fest@4.31.0)(typescript@5.7.2) + specifier: 7.5.6 + version: 7.5.6(cypress@13.17.0)(prettier@3.4.2)(type-fest@4.31.0)(typescript@5.7.2) '@types/node': specifier: 22.10.3 version: 22.10.3 @@ -370,8 +370,8 @@ packages: peerDependencies: typescript: '>=5.7.2' - '@tui-sandbox/library@7.5.5': - resolution: {integrity: sha512-9dXFnS7iHpSHMT0thl0XyIVpSlQzVAq7xb2elxnCM2rSsDyGoLnFHHhOFEsqq2r29JcRX7EZz79R4Naqjf2ddg==} + '@tui-sandbox/library@7.5.6': + resolution: {integrity: sha512-lcqCKbFpdwkHkidUbIst+4KUPCRbxYjreZQ6VqCm6JBQO9+V1ERfoA0wjbfeUOoBwKcWpcbsRVTh8Y1m+7lZ2w==} hasBin: true peerDependencies: cypress: ^13 @@ -2691,7 +2691,7 @@ snapshots: dependencies: typescript: 5.7.2 - '@tui-sandbox/library@7.5.5(cypress@13.17.0)(prettier@3.4.2)(type-fest@4.31.0)(typescript@5.7.2)': + '@tui-sandbox/library@7.5.6(cypress@13.17.0)(prettier@3.4.2)(type-fest@4.31.0)(typescript@5.7.2)': dependencies: '@catppuccin/palette': 1.7.1 '@trpc/client': 11.0.0-rc.682(@trpc/server@11.0.0-rc.682(typescript@5.7.2))(typescript@5.7.2) diff --git a/vim.toml b/vim.toml index 77b3b7f..1abe658 100644 --- a/vim.toml +++ b/vim.toml @@ -20,6 +20,9 @@ type = "function" [[after_each.args]] type = "function" +[assert] +any = true + [assert.is_not] any = true