feat(lua): add ringbuffer (#22894)

https://en.wikipedia.org/wiki/Circular_buffer
This commit is contained in:
Mathias Fußenegger
2023-06-08 12:11:24 +02:00
committed by GitHub
parent 38b0bb3c93
commit 7c661207cc
4 changed files with 190 additions and 0 deletions

View File

@@ -3040,6 +3040,46 @@ describe('lua stdlib', function()
eq(4, exec_lua [[ return vim.re.match("abcde", '[a-c]+') ]])
end)
it("vim.ringbuf", function()
local results = exec_lua([[
local ringbuf = vim.ringbuf(3)
ringbuf:push("a") -- idx: 0
local peeka1 = ringbuf:peek()
local peeka2 = ringbuf:peek()
local popa = ringbuf:pop()
local popnil = ringbuf:pop()
ringbuf:push("a") -- idx: 1
ringbuf:push("b") -- idx: 2
-- doesn't read last added item, but uses separate read index
local pop_after_add_b = ringbuf:pop()
ringbuf:push("c") -- idx: 3 wraps around, overrides idx: 0 "a"
ringbuf:push("d") -- idx: 4 wraps around, overrides idx: 1 "a"
return {
peeka1 = peeka1,
peeka2 = peeka2,
pop1 = popa,
pop2 = popnil,
pop3 = ringbuf:pop(),
pop4 = ringbuf:pop(),
pop5 = ringbuf:pop(),
pop_after_add_b = pop_after_add_b,
}
]])
local expected = {
peeka1 = "a",
peeka2 = "a",
pop1 = "a",
pop2 = nil,
pop3 = "b",
pop4 = "c",
pop5 = "d",
pop_after_add_b = "a",
}
eq(expected, results)
end)
end)
describe('lua: builtin modules', function()