Skip to content

Commit

Permalink
feat(pattern): custom pattern functions
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jun 17, 2023
1 parent fb28676 commit b9e13f2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
7 changes: 6 additions & 1 deletion lua/flash/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,16 @@ local defaults = {
forward = true,
-- when `false`, find only matches in the given direction
wrap = true,
---@type "exact" | "search" | "fuzzy"
---@type Flash.Pattern.Mode
-- Each mode will take ignorecase and smartcase into account.
-- * exact: exact match
-- * search: regular search
-- * fuzzy: fuzzy search
-- * fun(str): custom function that returns a pattern
-- For example, to only match at the beginning of a word:
-- mode = function(str)
-- return "\\<" .. str
-- end,
-- NOTE: Mode is always set to `search` when triggering flash
-- in a regular search.
mode = "exact",
Expand Down
13 changes: 10 additions & 3 deletions lua/flash/search/pattern.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ local Util = require("flash.util")
local M = {}
M.__index = M

---@alias Flash.Pattern.Mode "exact" | "fuzzy" | "search"
---@alias Flash.Pattern.Mode "exact" | "fuzzy" | "search" | (fun(input:string):string,string?)

---@param pattern string
---@param mode Flash.Pattern.Mode
Expand Down Expand Up @@ -67,14 +67,21 @@ end
---@private
function M._get(pattern, mode)
local skip ---@type string?
if mode == "exact" then
pattern = "\\V" .. pattern:gsub("\\", "\\\\")
if type(mode) == "function" then
pattern, skip = mode(pattern)
elseif mode == "exact" then
pattern, skip = M._exact(pattern)
elseif mode == "fuzzy" then
pattern, skip = M._fuzzy(pattern)
end
return pattern, skip or pattern
end

---@param pattern string
function M._exact(pattern)
return "\\V" .. pattern:gsub("\\", "\\\\")
end

---@param opts? {ignorecase: boolean, whitespace:boolean}
function M._fuzzy(pattern, opts)
opts = vim.tbl_deep_extend("force", {
Expand Down

0 comments on commit b9e13f2

Please sign in to comment.