From 61f21e383429ad26a9388ead39268cbac2d81163 Mon Sep 17 00:00:00 2001 From: Mika Vilpas Date: Sun, 19 Jan 2025 19:58:55 +0200 Subject: [PATCH 1/2] docs: add some code comments --- lua/blink-ripgrep/highlighting.lua | 3 +++ lua/blink-ripgrep/visualization.lua | 2 ++ 2 files changed, 5 insertions(+) diff --git a/lua/blink-ripgrep/highlighting.lua b/lua/blink-ripgrep/highlighting.lua index f9603fc..4cd2adc 100644 --- a/lua/blink-ripgrep/highlighting.lua +++ b/lua/blink-ripgrep/highlighting.lua @@ -1,5 +1,8 @@ local M = {} +--- In the blink documentation window, when the context for the match is being +--- shown, highlight the match so that the user can easily see where the match +--- is. ---@param bufnr number ---@param match blink-ripgrep.RipgrepMatch ---@param highlight_ns_id number diff --git a/lua/blink-ripgrep/visualization.lua b/lua/blink-ripgrep/visualization.lua index cfaa416..a5b9888 100644 --- a/lua/blink-ripgrep/visualization.lua +++ b/lua/blink-ripgrep/visualization.lua @@ -8,6 +8,8 @@ vim.api.nvim_set_hl( { link = "Search", default = true } ) +-- Temporarily flash the search prefix so that the user can see what searches +-- are being performed. Should be called in debug mode only. ---@param prefix string function visualization.flash_search_prefix(prefix) vim.api.nvim_buf_clear_namespace(0, ns_id, 0, -1) From aaeb8b95cbf50cc4522b424d0f1589d77fb68857 Mon Sep 17 00:00:00 2001 From: Mika Vilpas Date: Sun, 19 Jan 2025 20:06:23 +0200 Subject: [PATCH 2/2] refactor: extract search prefix logic to a separate module --- lua/blink-ripgrep/init.lua | 42 +------ lua/blink-ripgrep/search_prefix.lua | 45 ++++++++ spec/blink-ripgrep/get_prefix_spec.lua | 146 ++++++++++++------------- 3 files changed, 120 insertions(+), 113 deletions(-) create mode 100644 lua/blink-ripgrep/search_prefix.lua diff --git a/lua/blink-ripgrep/init.lua b/lua/blink-ripgrep/init.lua index 1c13f77..7ccbb2e 100644 --- a/lua/blink-ripgrep/init.lua +++ b/lua/blink-ripgrep/init.lua @@ -27,27 +27,6 @@ pcall(function() end) vim.api.nvim_set_hl(0, "BlinkRipgrepMatch", { link = "Search", default = true }) -local word_pattern -do - -- match an ascii character as well as unicode continuation bytes. - -- Technically, unicode continuation bytes need to be applied in order to - -- construct valid utf-8 characters, but right now we trust that the user - -- only types valid utf-8 in their project. - local char = vim.lpeg.R("az", "AZ", "09", "\128\255") - - local non_starting_word_character = vim.lpeg.P(1) - char - local word_character = char + vim.lpeg.P("_") + vim.lpeg.P("-") - local non_middle_word_character = vim.lpeg.P(1) - word_character - - word_pattern = vim.lpeg.Ct( - ( - non_starting_word_character ^ 0 - * vim.lpeg.C(word_character ^ 1) - * non_middle_word_character ^ 0 - ) ^ 0 - ) -end - ---@type blink-ripgrep.Options RgSource.config = { prefix_min_len = 3, @@ -67,24 +46,6 @@ function RgSource.setup(options) RgSource.config = vim.tbl_deep_extend("force", RgSource.config, options or {}) end ----@param text_before_cursor string "The text of the entire line before the cursor" ----@return string -function RgSource.match_prefix(text_before_cursor) - local matches = vim.lpeg.match(word_pattern, text_before_cursor) - local last_match = matches and matches[#matches] - return last_match or "" -end - ----@param context blink.cmp.Context ----@return string -local function default_get_prefix(context) - local line = context.line - local col = context.cursor[2] - local text = line:sub(1, col) - local prefix = RgSource.match_prefix(text) - return prefix -end - ---@param input_opts blink-ripgrep.Options function RgSource.new(input_opts) local self = setmetatable({}, RgSource) @@ -92,7 +53,8 @@ function RgSource.new(input_opts) RgSource.config = vim.tbl_deep_extend("force", RgSource.config, input_opts or {}) - self.get_prefix = RgSource.config.get_prefix or default_get_prefix + self.get_prefix = RgSource.config.get_prefix + or require("blink-ripgrep.search_prefix").default_get_prefix self.get_command = RgSource.config.get_command diff --git a/lua/blink-ripgrep/search_prefix.lua b/lua/blink-ripgrep/search_prefix.lua new file mode 100644 index 0000000..f96075d --- /dev/null +++ b/lua/blink-ripgrep/search_prefix.lua @@ -0,0 +1,45 @@ +local M = {} + +--- The pattern that is used to match the prefix of a search query. The prefix +--- is the text before the cursor that is used to search for matching words in +--- the project. +local word_pattern +do + -- match an ascii character as well as unicode continuation bytes. + -- Technically, unicode continuation bytes need to be applied in order to + -- construct valid utf-8 characters, but right now we trust that the user + -- only types valid utf-8 in their project. + local char = vim.lpeg.R("az", "AZ", "09", "\128\255") + + local non_starting_word_character = vim.lpeg.P(1) - char + local word_character = char + vim.lpeg.P("_") + vim.lpeg.P("-") + local non_middle_word_character = vim.lpeg.P(1) - word_character + + word_pattern = vim.lpeg.Ct( + ( + non_starting_word_character ^ 0 + * vim.lpeg.C(word_character ^ 1) + * non_middle_word_character ^ 0 + ) ^ 0 + ) +end + +---@param text_before_cursor string "The text of the entire line before the cursor" +---@return string +function M.match_prefix(text_before_cursor) + local matches = vim.lpeg.match(word_pattern, text_before_cursor) + local last_match = matches and matches[#matches] + return last_match or "" +end + +---@param context blink.cmp.Context +---@return string +function M.default_get_prefix(context) + local line = context.line + local col = context.cursor[2] + local text = line:sub(1, col) + local prefix = M.match_prefix(text) + return prefix +end + +return M diff --git a/spec/blink-ripgrep/get_prefix_spec.lua b/spec/blink-ripgrep/get_prefix_spec.lua index 888168d..ae0094d 100644 --- a/spec/blink-ripgrep/get_prefix_spec.lua +++ b/spec/blink-ripgrep/get_prefix_spec.lua @@ -1,82 +1,82 @@ local assert = require("luassert") -local blink_ripgrep = require("blink-ripgrep") +local search_prefix = require("blink-ripgrep.search_prefix") describe("match_prefix", function() it("for simple strings", function() - assert.are_same(blink_ripgrep.match_prefix("hello"), "hello") - assert.are_same(blink_ripgrep.match_prefix("abc123"), "abc123") - assert.are_same(blink_ripgrep.match_prefix("abc-123"), "abc-123") - assert.are_same(blink_ripgrep.match_prefix("abc_123"), "abc_123") + assert.are_same(search_prefix.match_prefix("hello"), "hello") + assert.are_same(search_prefix.match_prefix("abc123"), "abc123") + assert.are_same(search_prefix.match_prefix("abc-123"), "abc-123") + assert.are_same(search_prefix.match_prefix("abc_123"), "abc_123") end) it( "matches when there is one nonmatching piece of input in the beginning", function() - assert.are_same(blink_ripgrep.match_prefix(".hello"), "hello") - assert.are_same(blink_ripgrep.match_prefix(",hello"), "hello") + assert.are_same(search_prefix.match_prefix(".hello"), "hello") + assert.are_same(search_prefix.match_prefix(",hello"), "hello") assert.are_same( - blink_ripgrep.match_prefix(".,,!@!@$@%<<@$