Skip to content

Commit

Permalink
feat(search): optional trigger character. Not recommended. Fixes #21
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jun 22, 2023
1 parent 680a6ae commit cb0977c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ Install the plugin with your preferred package manager:
-- behave like `incsearch`
incremental = false,
filetype_exclude = { "notify", "noice" },
-- Optional trigger character that needs to be typed before
-- a jump label can be used. It's NOT recommended to set this,
-- unless you know what you're doing
trigger = "",
},
jump = {
-- save location in the jumplist
Expand Down
2 changes: 1 addition & 1 deletion lua/flash/cache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ M.cache = setmetatable({}, { __mode = "k" })
function M.new(state)
local self = setmetatable({}, M)
self.state = state
self.pattern = Pattern.new("", state.opts.search.mode)
self.pattern = Pattern.new("", state.opts.search.mode, state.opts.search.trigger)
self.wins = {}
return self
end
Expand Down
4 changes: 4 additions & 0 deletions lua/flash/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ local defaults = {
-- behave like `incsearch`
incremental = false,
filetype_exclude = { "notify", "noice" },
-- Optional trigger character that needs to be typed before
-- a jump label can be used. It's NOT recommended to set this,
-- unless you know what you're doing
trigger = "",
},
jump = {
-- save location in the jumplist
Expand Down
10 changes: 8 additions & 2 deletions lua/flash/search/pattern.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local Util = require("flash.util")
---@field pattern string
---@field search string
---@field skip string
---@field trigger string
---@field mode Flash.Pattern.Mode
---@operator call:string Returns the input pattern
local M = {}
Expand All @@ -13,9 +14,11 @@ M.__index = M

---@param pattern string
---@param mode Flash.Pattern.Mode
function M.new(pattern, mode)
---@param trigger string
function M.new(pattern, mode, trigger)
local self = setmetatable({}, M)
self.mode = mode
self.trigger = trigger or ""
self:set(pattern or "")
return self
end
Expand All @@ -25,7 +28,7 @@ function M:__eq(other)
end

function M:clone()
return M.new(self.pattern, self.mode)
return M.new(self.pattern, self.mode, self.trigger)
end

function M:empty()
Expand All @@ -41,6 +44,9 @@ function M:set(pattern)
self.search = ""
self.skip = ""
else
if self.trigger ~= "" and pattern:sub(-1) == self.trigger then
pattern = pattern:sub(1, -2)
end
self.search, self.skip = M._get(pattern, self.mode)
end
return false
Expand Down
5 changes: 4 additions & 1 deletion lua/flash/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function M.new(opts)
self.matchers = {}
self.wins = {}
self.matcher = self.opts.matcher and Matcher.from(self.opts.matcher) or Search.new
self.pattern = Pattern.new(self.opts.pattern, self.opts.search.mode)
self.pattern = Pattern.new(self.opts.pattern, self.opts.search.mode, self.opts.search.trigger)
self.visible = true
self.cache = Cache.new(self)
self.labeler = self.opts.labeler or require("flash.labeler").new(self):labeler()
Expand Down Expand Up @@ -153,6 +153,9 @@ end
-- Checks if the given pattern is a jump label and jumps to it.
---@param pattern string
function M:check_jump(pattern)
if self.opts.search.trigger ~= "" and self.pattern():sub(-1) ~= self.opts.search.trigger then
return
end
if pattern:find(self.pattern(), 1, true) == 1 and #pattern == #self.pattern() + 1 then
local label = pattern:sub(-1)
if self:jump(label) then
Expand Down

0 comments on commit cb0977c

Please sign in to comment.