feat: add support for wrapping history on reaching history begin or end (#2349)

This commit is contained in:
Reid Swan
2023-01-30 23:06:16 +02:00
committed by GitHub
parent 5dfd807771
commit 19ce7f8f24
3 changed files with 58 additions and 34 deletions

View File

@@ -53,6 +53,7 @@ local histories = {}
---@field limit string: Will have the limit of the history. Can be nil, if limit is disabled.
---@field content table: History table. Needs to be filled by your own History implementation
---@field index number: Used to keep track of the next or previous index. Default is #content + 1
---@field cycle_wrap boolean: Controls if history will wrap on reaching beginning or end
histories.History = {}
histories.History.__index = histories.History
@@ -75,6 +76,7 @@ function histories.History:new(opts)
obj.path = vim.fn.expand(conf.history.path)
obj.content = {}
obj.index = 1
obj.cycle_wrap = conf.history.cycle_wrap
opts.init(obj)
obj._reset = opts.reset
@@ -125,6 +127,10 @@ function histories.History:get_next(line, picker)
end
local next_idx = self.index + 1
if next_idx > #self.content and self.cycle_wrap then
next_idx = 1
end
if next_idx <= #self.content then
self.index = next_idx
return self.content[next_idx]
@@ -150,6 +156,10 @@ function histories.History:get_prev(line, picker)
end
local next_idx = self.index - 1
if next_idx < 1 and self.cycle_wrap then
next_idx = #self.content
end
if self.index == #self.content + 1 then
if line ~= "" then
self:append(line, picker, true)