Skip to content

Commit

Permalink
feat(debug,opt-in): temporarily highlight the search prefix (#95)
Browse files Browse the repository at this point in the history
Issue
=====

It's hard to understand when exactly ripgrep is called to find matching
words in the project. This is an important concern because of
performance - ripgrep can take a long time to search through a large
project.

Solution
========

Temporarily highlight the search prefix when a new ripgrep search is
started. This will help users and maintainers understand the behavior of
the plugin.

To enable this feature, set `debug` to `true` in the configuration. It's
recommended to only enable this feature when debugging the plugin.
  • Loading branch information
mikavilpas authored Jan 7, 2025
1 parent 182da8e commit 7854eee
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
46 changes: 46 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 @@ -308,6 +308,52 @@ describe("debug mode", () => {
})
})
})

it("highlights the search word when a new ripgrep search is started", () => {
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)`,
})

// this will match text from ../../../test-environment/other-file.lua
//
// If the plugin works, this text should show up as a suggestion.
cy.typeIntoTerminal("hip")
// the search should have been started for the prefix "hip"
cy.contains("hip").should(
"have.css",
"backgroundColor",
rgbify(flavors.macchiato.colors.flamingo.rgb),
)
//
// blink is now in the Fuzzy(3) stage, and additional keypresses must not
// start a new ripgrep search. They must be used for filtering the
// results instead.
// https://cmp.saghen.dev/development/architecture.html#architecture
cy.contains("Hippopotamus" + "234 (rg)") // wait for blink to show up
cy.typeIntoTerminal("234")

// wait for the highlight to disappear to test that too
cy.contains("hip").should(
"have.css",
"backgroundColor",
rgbify(flavors.macchiato.colors.base.rgb),
)

// TODO should programmatically check that only one search was started,
// this will not catch all cases
})
})
})

describe("using .gitignore files to exclude files from searching", () => {
Expand Down
6 changes: 6 additions & 0 deletions integration-tests/test-environment/.config/nvim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ local plugins = {
"BlinkRipgrepMatch",
{ fg = colors.base, bg = colors.mauve }
)

vim.api.nvim_set_hl(
0,
"BlinkRipgrepSearchPrefix",
{ fg = colors.base, bg = colors.flamingo }
)
end,
},

Expand Down
1 change: 1 addition & 0 deletions lua/blink-ripgrep/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ function RgSource:get_completions(context, resolve)

if RgSource.config.debug then
command.debugify_for_shell(cmd)
require("blink-ripgrep.visualization").flash_search_prefix(prefix)
end
end

Expand Down
32 changes: 32 additions & 0 deletions lua/blink-ripgrep/visualization.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
local visualization = {}

local ns_id = vim.api.nvim_create_namespace("blink_ripgrep_debug")

vim.api.nvim_set_hl(
0,
"BlinkRipgrepSearchPrefix",
{ link = "Search", default = true }
)

---@param prefix string
function visualization.flash_search_prefix(prefix)
vim.api.nvim_buf_clear_namespace(0, ns_id, 0, -1)
local cursor = vim.api.nvim_win_get_cursor(0)

local hlstart = cursor[2] - #prefix
local hlend = cursor[2]
vim.api.nvim_buf_add_highlight(
0,
ns_id,
"BlinkRipgrepSearchPrefix",
cursor[1] - 1,
hlstart,
hlend
)

vim.defer_fn(function()
vim.api.nvim_buf_clear_namespace(0, ns_id, 0, -1)
end, 500)
end

return visualization

0 comments on commit 7854eee

Please sign in to comment.