Skip to content

Commit

Permalink
feat(state): state will now automatically updated on changedtick or w…
Browse files Browse the repository at this point in the history
…hen buf changes
  • Loading branch information
folke committed Jun 12, 2023
1 parent 99c99a7 commit 60193cb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lua/flash/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ local M = {}

---@param opts? Flash.Config
function M.jump(opts)
M.state = State.new(opts)
local state = State.new(opts)

while true do
local c = Util.get_char()
Expand Down
36 changes: 28 additions & 8 deletions lua/flash/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@ end
function M.new(opts)
local self = setmetatable({}, { __index = M })
self.opts = Config.get(opts)
self.win = vim.api.nvim_get_current_win()
self.buf = vim.api.nvim_win_get_buf(self.win)
self.pos = vim.api.nvim_win_get_cursor(self.win)
self.results = {}
self.wins = { self.win }
self.wins = {}
self.pattern = ""
self.current = 1
self.labeler = require("flash.labeler").new(self)
Expand All @@ -40,7 +37,8 @@ function M.new(opts)
end

---@param match Flash.Match
function M:on_jump(match)
---@protected
function M:_jump(match)
Jump.jump(match, self)
Jump.on_jump(self)
end
Expand All @@ -50,7 +48,7 @@ end
function M:jump(label)
local match = self:get(label)
if match then
self:on_jump(match)
self:_jump(match)
return match
end
end
Expand Down Expand Up @@ -107,11 +105,29 @@ function M:check_jump(pattern)
end
end

---@param opts? {search:string, labels:boolean, results?:Flash.Match[]}
function M:is_dirty()
if self.buf ~= vim.api.nvim_get_current_buf() then
return true
end
if self.changedtick ~= vim.b[self.buf].changedtick then
return true
end
end

---@param opts? {search:string, labels:boolean, results?:Flash.Match[], highlight:boolean}
---@return boolean? abort `true` if the search was aborted
function M:update(opts)
opts = opts or {}

local dirty = self:is_dirty()

if dirty then
self.win = vim.api.nvim_get_current_win()
self.buf = vim.api.nvim_win_get_buf(self.win)
self.pos = vim.api.nvim_win_get_cursor(self.win)
self.changedtick = vim.b[self.buf].changedtick
end

-- prioritize current window
---@type window[]
local wins = self.opts.search.multi_window and vim.api.nvim_tabpage_list_wins(0) or {}
Expand All @@ -130,6 +146,8 @@ function M:update(opts)
return true
end
self:search(opts.search)
elseif dirty then
self:search(self.pattern)
end

if self.opts.jump.auto_jump and #self.results == 1 then
Expand All @@ -139,7 +157,9 @@ function M:update(opts)
if opts.labels ~= false then
self.labeler:update()
end
self:highlight()
if opts.highlight ~= false then
self:highlight()
end
end

function M:search(pattern)
Expand Down

0 comments on commit 60193cb

Please sign in to comment.