Regenerate nvim config
This commit is contained in:
@ -0,0 +1,51 @@
|
||||
local Search = require("flash.search")
|
||||
local State = require("flash.state")
|
||||
|
||||
describe("search", function()
|
||||
before_each(function()
|
||||
vim.opt.ignorecase = true
|
||||
vim.opt.smartcase = true
|
||||
vim.api.nvim_buf_set_lines(1, 0, -1, false, {})
|
||||
end)
|
||||
|
||||
---@param opts? Flash.State.Config | string
|
||||
local function get_search(opts)
|
||||
if type(opts) == "string" then
|
||||
opts = { pattern = opts }
|
||||
end
|
||||
local state = State.new(opts)
|
||||
local win = vim.api.nvim_get_current_win()
|
||||
return Search.new(win, state)
|
||||
end
|
||||
|
||||
local function set(text, pos)
|
||||
local lines = vim.split(vim.trim(text), "\n")
|
||||
lines = vim.tbl_map(function(line)
|
||||
return vim.trim(line)
|
||||
end, lines)
|
||||
vim.api.nvim_buf_set_lines(1, 0, -1, false, lines)
|
||||
vim.api.nvim_win_set_cursor(0, pos or { 1, 0 })
|
||||
end
|
||||
|
||||
it("finds matches", function()
|
||||
set([[
|
||||
foobar
|
||||
line1
|
||||
line2
|
||||
]])
|
||||
|
||||
local search = get_search("line")
|
||||
local matches = search:get()
|
||||
assert.same("\\Vline", search.state.pattern.search)
|
||||
assert.same({
|
||||
{ win = 1000, pos = { 2, 0 }, end_pos = { 2, 3 } },
|
||||
{ win = 1000, pos = { 3, 0 }, end_pos = { 3, 3 } },
|
||||
}, matches)
|
||||
assert.same({ win = 1000, pos = { 2, 0 }, end_pos = { 2, 3 } }, search:find())
|
||||
assert.same({ win = 1000, pos = { 3, 0 }, end_pos = { 3, 3 } }, search:find({ count = 2 }))
|
||||
assert.same(
|
||||
{ win = 1000, pos = { 3, 0 }, end_pos = { 3, 3 } },
|
||||
search:find({ forward = false })
|
||||
)
|
||||
end)
|
||||
end)
|
||||
@ -0,0 +1,85 @@
|
||||
local Jump = require("flash.jump")
|
||||
local Pos = require("flash.search.pos")
|
||||
local State = require("flash.state")
|
||||
local assert = require("luassert")
|
||||
|
||||
describe("jump", function()
|
||||
local function set(text, pos)
|
||||
local lines = vim.split(vim.trim(text), "\n")
|
||||
lines = vim.tbl_map(function(line)
|
||||
return vim.trim(line)
|
||||
end, lines)
|
||||
if vim.fn.mode() == "v" then
|
||||
vim.cmd("normal! v")
|
||||
end
|
||||
vim.api.nvim_buf_set_lines(1, 0, -1, false, lines)
|
||||
vim.api.nvim_win_set_cursor(0, pos or { 1, 0 })
|
||||
end
|
||||
|
||||
---@param match Flash.Match
|
||||
---@param opts? Flash.State.Config
|
||||
local function jump(match, opts)
|
||||
match.win = vim.api.nvim_get_current_win()
|
||||
local state = State.new(opts)
|
||||
Jump.jump(match, state)
|
||||
end
|
||||
|
||||
before_each(function()
|
||||
vim.opt.ignorecase = true
|
||||
vim.opt.smartcase = true
|
||||
vim.api.nvim_buf_set_lines(0, 0, -1, false, {})
|
||||
set([[
|
||||
line1 foo
|
||||
line2 foo
|
||||
line3 foo
|
||||
]])
|
||||
end)
|
||||
|
||||
it("jumps to start", function()
|
||||
local match = {
|
||||
pos = Pos({ 1, 6 }),
|
||||
end_pos = Pos({ 1, 8 }),
|
||||
}
|
||||
jump(match)
|
||||
assert.same({ 1, 6 }, vim.api.nvim_win_get_cursor(0))
|
||||
end)
|
||||
|
||||
it("selects to start", function()
|
||||
assert.same({ 1, 0 }, vim.api.nvim_win_get_cursor(0))
|
||||
local match = {
|
||||
pos = Pos({ 1, 6 }),
|
||||
end_pos = Pos({ 1, 8 }),
|
||||
}
|
||||
|
||||
assert.same("n", vim.fn.mode())
|
||||
vim.cmd("normal! v")
|
||||
jump(match, { jump = { pos = "start", inclusive = false } })
|
||||
assert.same("v", vim.fn.mode())
|
||||
vim.cmd("normal! v")
|
||||
|
||||
assert.same({ 1, 6 }, vim.api.nvim_win_get_cursor(0))
|
||||
assert.same({ 1, 0 }, vim.api.nvim_buf_get_mark(0, "<"))
|
||||
assert.same({ 1, 6 }, vim.api.nvim_buf_get_mark(0, ">"))
|
||||
end)
|
||||
|
||||
-- it("yanks to start", function()
|
||||
-- assert.same({ 1, 0 }, vim.api.nvim_win_get_cursor(0))
|
||||
-- local match = {
|
||||
-- pos = Pos({ 1, 6 }),
|
||||
-- end_pos = Pos({ 1, 8 }),
|
||||
-- }
|
||||
--
|
||||
-- assert.same("n", vim.fn.mode())
|
||||
-- -- vim.cmd("normal! y")
|
||||
-- vim.api.nvim_feedkeys("y", "n", false)
|
||||
-- assert.same("no", vim.fn.mode(true))
|
||||
-- jump(match, { jump = { pos = "start", inclusive = false } })
|
||||
-- assert.same("n", vim.fn.mode())
|
||||
--
|
||||
-- vim.print(vim.fn.getmarklist(vim.api.nvim_get_current_buf()))
|
||||
--
|
||||
-- assert.same({ 1, 6 }, vim.api.nvim_win_get_cursor(0))
|
||||
-- assert.same({ 1, 0 }, vim.api.nvim_buf_get_mark(0, "["))
|
||||
-- assert.same({ 1, 6 }, vim.api.nvim_buf_get_mark(0, "]"))
|
||||
-- end)
|
||||
end)
|
||||
@ -0,0 +1,103 @@
|
||||
local Labeler = require("flash.labeler")
|
||||
local Search = require("flash.search")
|
||||
local State = require("flash.state")
|
||||
local assert = require("luassert")
|
||||
|
||||
describe("labeler", function()
|
||||
local function set(text, pos)
|
||||
local lines = vim.split(vim.trim(text), "\n")
|
||||
lines = vim.tbl_map(function(line)
|
||||
return vim.trim(line)
|
||||
end, lines)
|
||||
vim.api.nvim_buf_set_lines(1, 0, -1, false, lines)
|
||||
vim.api.nvim_win_set_cursor(0, pos or { 1, 0 })
|
||||
end
|
||||
|
||||
local function search(pattern)
|
||||
local state = State.new({ pattern = pattern, search = {
|
||||
mode = "search",
|
||||
} })
|
||||
return Labeler.new(state)
|
||||
end
|
||||
|
||||
before_each(function()
|
||||
vim.opt.ignorecase = true
|
||||
vim.opt.smartcase = true
|
||||
end)
|
||||
|
||||
it("skips labels", function()
|
||||
set([[
|
||||
foo foo
|
||||
bar
|
||||
barfoo
|
||||
]])
|
||||
local labels = search("bar"):skip(1000, { "a", "b", "c", "f" })
|
||||
assert.same({ "a", "b", "c" }, labels)
|
||||
end)
|
||||
it("skips all labels for an empty pattern", function()
|
||||
set([[
|
||||
test pattern
|
||||
]])
|
||||
local labels = search(""):skip(1000, { "a", "b", "c", "t" })
|
||||
assert.same({}, labels)
|
||||
end)
|
||||
|
||||
it("skips all labels for an invalid pattern", function()
|
||||
set([[
|
||||
invalid pattern
|
||||
]])
|
||||
local labels = search("[i"):skip(1000, { "a", "b", "i", "v" })
|
||||
assert.same({}, labels)
|
||||
end)
|
||||
|
||||
it("skips all labels when pattern ends with unescaped backslash", function()
|
||||
set([[
|
||||
pattern with backslash\
|
||||
]])
|
||||
local labels = search("backslash\\"):skip(1000, { "a", "b", "s", "\\" })
|
||||
assert.same({}, labels)
|
||||
end)
|
||||
|
||||
it("skips label that matches pattern", function()
|
||||
set([[
|
||||
pattern withc
|
||||
]])
|
||||
local labels = search("with"):skip(1000, { "a", "b", "c", "p", "w" })
|
||||
assert.same({ "a", "b", "p", "w" }, labels)
|
||||
end)
|
||||
|
||||
it("considers ignorecase when skipping labels", function()
|
||||
set([[
|
||||
pattern withC
|
||||
]])
|
||||
vim.opt.ignorecase = true
|
||||
local labels = search("with"):skip(1000, { "a", "b", "C", "p", "w" })
|
||||
assert.same({ "a", "b", "p", "w" }, labels)
|
||||
end)
|
||||
|
||||
it("considers ignorecase3 when skipping labels", function()
|
||||
set([[
|
||||
pattern withC
|
||||
]])
|
||||
vim.opt.ignorecase = true
|
||||
local labels = search("with"):skip(1000, { "a", "b", "c", "p", "w" })
|
||||
assert.same({ "a", "b", "p", "w" }, labels)
|
||||
end)
|
||||
|
||||
it("considers ignorecase2 when skipping labels", function()
|
||||
set([[
|
||||
pattern withc
|
||||
]])
|
||||
vim.opt.ignorecase = true
|
||||
local labels = search("with"):skip(1000, { "a", "b", "C", "p", "w" })
|
||||
assert.same({ "a", "b", "p", "w" }, labels)
|
||||
end)
|
||||
|
||||
it("skips all labels when pattern is an incomplete regex", function()
|
||||
set([[
|
||||
pattern with incomplete regex (
|
||||
]])
|
||||
local labels = search("regex \\("):skip(1000, { "a", "b", "i", "r", "(" })
|
||||
assert.same({}, labels)
|
||||
end)
|
||||
end)
|
||||
@ -0,0 +1,184 @@
|
||||
local Matcher = require("flash.search.matcher")
|
||||
local assert = require("luassert")
|
||||
|
||||
describe("search", function()
|
||||
local matcher = Matcher.new(1000)
|
||||
local matches = {
|
||||
{ win = 1000, pos = { 1, 0 }, end_pos = { 1, 2 } },
|
||||
{ win = 1000, pos = { 1, 7 }, end_pos = { 1, 9 } },
|
||||
{ win = 1000, pos = { 3, 4 }, end_pos = { 3, 6 } },
|
||||
}
|
||||
matcher:set(matches)
|
||||
|
||||
it("sets matches", function()
|
||||
assert.same(matches, matcher:get())
|
||||
end)
|
||||
|
||||
it("finds backward from after end", function()
|
||||
assert.same(
|
||||
matches[3],
|
||||
matcher:find({
|
||||
forward = false,
|
||||
pos = { 4, 6 },
|
||||
wrap = false,
|
||||
})
|
||||
)
|
||||
end)
|
||||
|
||||
it("handles count = 0", function()
|
||||
assert.same(
|
||||
matches[2],
|
||||
matcher:find({
|
||||
pos = { 1, 7 },
|
||||
count = 0,
|
||||
})
|
||||
)
|
||||
assert.is_nil(matcher:find({
|
||||
pos = { 2, 7 },
|
||||
count = 0,
|
||||
}))
|
||||
end)
|
||||
|
||||
it("returns forward matches", function()
|
||||
assert.same(
|
||||
{ matches[3] },
|
||||
matcher:get({
|
||||
from = { 2, 6 },
|
||||
})
|
||||
)
|
||||
end)
|
||||
|
||||
it("returns forward matches", function()
|
||||
assert.same(
|
||||
{ matches[3] },
|
||||
matcher:get({
|
||||
from = { 3, 4 },
|
||||
})
|
||||
)
|
||||
end)
|
||||
|
||||
it("returns backward matches", function()
|
||||
assert.same(
|
||||
{ matches[1] },
|
||||
matcher:get({
|
||||
to = { 1, 6 },
|
||||
})
|
||||
)
|
||||
end)
|
||||
|
||||
it("returns backward matches", function()
|
||||
assert.same(
|
||||
{ matches[1] },
|
||||
matcher:get({
|
||||
to = { 1, 0 },
|
||||
})
|
||||
)
|
||||
end)
|
||||
|
||||
it("finds matcher", function()
|
||||
assert.same({ win = 1000, pos = { 1, 7 }, end_pos = { 1, 9 } }, matcher:find())
|
||||
assert.same({ win = 1000, pos = { 3, 4 }, end_pos = { 3, 6 } }, matcher:find({ count = 2 }))
|
||||
assert.same(
|
||||
{ win = 1000, pos = { 3, 4 }, end_pos = { 3, 6 } },
|
||||
matcher:find({ forward = false })
|
||||
)
|
||||
assert.same(
|
||||
{ win = 1000, pos = { 1, 7 }, end_pos = { 1, 9 } },
|
||||
matcher:find({
|
||||
forward = false,
|
||||
pos = { 2, 7 },
|
||||
})
|
||||
)
|
||||
assert.same(
|
||||
{ win = 1000, pos = { 1, 7 }, end_pos = { 1, 9 } },
|
||||
matcher:find({
|
||||
forward = false,
|
||||
pos = { 3, 4 },
|
||||
})
|
||||
)
|
||||
end)
|
||||
|
||||
it("sorts matches", function()
|
||||
local m = Matcher.new(1000)
|
||||
local mm = {
|
||||
{ pos = { 3, 4 }, end_pos = { 3, 6 } },
|
||||
{ pos = { 1, 0 }, end_pos = { 1, 2 } },
|
||||
{ pos = { 1, 7 }, end_pos = { 1, 9 } },
|
||||
}
|
||||
m:set(mm)
|
||||
assert.same({
|
||||
{ win = 1000, pos = { 1, 0 }, end_pos = { 1, 2 } },
|
||||
{ win = 1000, pos = { 1, 7 }, end_pos = { 1, 9 } },
|
||||
{ win = 1000, pos = { 3, 4 }, end_pos = { 3, 6 } },
|
||||
}, m:get())
|
||||
end)
|
||||
it("finds forward skipping match at current position", function()
|
||||
assert.same(
|
||||
matches[2],
|
||||
matcher:find({
|
||||
forward = true,
|
||||
pos = { 1, 0 },
|
||||
wrap = false,
|
||||
})
|
||||
)
|
||||
end)
|
||||
|
||||
it("finds backward skipping match at current position", function()
|
||||
assert.same(
|
||||
matches[2],
|
||||
matcher:find({
|
||||
forward = false,
|
||||
pos = { 3, 4 },
|
||||
wrap = true,
|
||||
})
|
||||
)
|
||||
end)
|
||||
|
||||
it("finds forward from a non-match position", function()
|
||||
assert.same(
|
||||
matches[2],
|
||||
matcher:find({
|
||||
forward = true,
|
||||
pos = { 1, 3 },
|
||||
wrap = false,
|
||||
})
|
||||
)
|
||||
end)
|
||||
|
||||
it("finds backward from a non-match position", function()
|
||||
assert.same(
|
||||
matches[2],
|
||||
matcher:find({
|
||||
forward = false,
|
||||
pos = { 3, 2 },
|
||||
wrap = true,
|
||||
})
|
||||
)
|
||||
end)
|
||||
|
||||
it("returns nil when wrapping is disabled and no match is found forward", function()
|
||||
assert.is_nil(matcher:find({
|
||||
forward = true,
|
||||
pos = { 4, 0 },
|
||||
wrap = false,
|
||||
}))
|
||||
end)
|
||||
|
||||
it("returns nil when wrapping is disabled and no match is found backward", function()
|
||||
assert.is_nil(matcher:find({
|
||||
forward = false,
|
||||
pos = { 0, 0 },
|
||||
wrap = false,
|
||||
}))
|
||||
end)
|
||||
|
||||
it("can handle multiple matches on the same pos", function()
|
||||
local mm = Matcher.new(1000)
|
||||
mm:set({
|
||||
{ win = 1000, pos = { 1, 0 }, end_pos = { 1, 1 } },
|
||||
{ win = 1000, pos = { 1, 0 }, end_pos = { 1, 2 } },
|
||||
{ win = 1000, pos = { 1, 0 }, end_pos = { 1, 3 } },
|
||||
})
|
||||
-- assert.same()
|
||||
end)
|
||||
end)
|
||||
@ -0,0 +1,43 @@
|
||||
--# selene: allow(incorrect_standard_library_use)
|
||||
local Pos = require("flash.search.pos")
|
||||
|
||||
describe("pos", function()
|
||||
it("handles new", function()
|
||||
assert.same(Pos({ row = 1, col = 2 }), { 1, 2 })
|
||||
assert.same(Pos({ 1, 2 }), { 1, 2 })
|
||||
end)
|
||||
|
||||
it("handles cmp", function()
|
||||
assert(Pos({ 1, 2 }) < Pos({ 2, 2 }))
|
||||
assert(Pos({ 1, 2 }) < Pos({ 1, 3 }))
|
||||
assert(Pos({ 1, 2 }) <= Pos({ 1, 2 }))
|
||||
assert(Pos({ 1, 2 }) <= Pos({ 2, 2 }))
|
||||
assert(Pos({ 1, 2 }) <= Pos({ 1, 3 }))
|
||||
assert(Pos({ 1, 2 }) == Pos({ 1, 2 }))
|
||||
assert(Pos({ 1, 2 }) == Pos({ 1, 2 }))
|
||||
assert(Pos({ 1, 2 }) ~= Pos({ 2, 2 }))
|
||||
assert(Pos({ 1, 2 }) <= Pos({ 2, 2 }))
|
||||
assert(Pos({ 1, 2 }) >= Pos({ 1, 2 }))
|
||||
assert(Pos({ 1, 2 }) >= Pos({ 1, 1 }))
|
||||
end)
|
||||
|
||||
it("handles add", function()
|
||||
assert.same(Pos({ 1, 2 }) + Pos({ 1, 2 }), { 2, 4 })
|
||||
assert.same(Pos({ 1, 2 }) + { 1, 2 }, { 2, 4 })
|
||||
assert.same(Pos({ 1, 2 }) + { row = 1, col = 2 }, { 2, 4 })
|
||||
end)
|
||||
|
||||
it("handles sub", function()
|
||||
assert.same(Pos({ 1, 2 }) - Pos({ 1, 2 }), { 0, 0 })
|
||||
assert.same(Pos({ 1, 2 }) - { 1, 2 }, { 0, 0 })
|
||||
assert.same(Pos({ 1, 2 }) - { row = 1, col = 2 }, { 0, 0 })
|
||||
end)
|
||||
|
||||
it("handles tostring", function()
|
||||
assert.same(tostring(Pos({ 1, 2 })), "[1, 2]")
|
||||
end)
|
||||
|
||||
it("handles id", function()
|
||||
assert.same(Pos({ 1, 2 }):id(123), "123:1:2")
|
||||
end)
|
||||
end)
|
||||
@ -0,0 +1,182 @@
|
||||
local Search = require("flash.search")
|
||||
local State = require("flash.state")
|
||||
local assert = require("luassert")
|
||||
|
||||
describe("search", function()
|
||||
local function set(text, pos)
|
||||
local lines = vim.split(vim.trim(text), "\n")
|
||||
lines = vim.tbl_map(function(line)
|
||||
return vim.trim(line)
|
||||
end, lines)
|
||||
vim.api.nvim_buf_set_lines(1, 0, -1, false, lines)
|
||||
vim.api.nvim_win_set_cursor(0, pos or { 1, 0 })
|
||||
end
|
||||
|
||||
before_each(function()
|
||||
vim.opt.ignorecase = true
|
||||
vim.opt.smartcase = true
|
||||
vim.api.nvim_buf_set_lines(1, 0, -1, false, {})
|
||||
set([[
|
||||
foo foo
|
||||
bar
|
||||
barfoo
|
||||
]])
|
||||
end)
|
||||
|
||||
local state = State.new({ pattern = "foo" })
|
||||
local search = Search.new(1000, state)
|
||||
|
||||
local matches = {
|
||||
{ win = 1000, pos = { 1, 0 }, end_pos = { 1, 2 } },
|
||||
{ win = 1000, pos = { 1, 4 }, end_pos = { 1, 6 } },
|
||||
{ win = 1000, pos = { 3, 3 }, end_pos = { 3, 5 } },
|
||||
}
|
||||
|
||||
it("sets matches", function()
|
||||
assert.same(matches, search:get())
|
||||
end)
|
||||
|
||||
it("finds backward from after end", function()
|
||||
assert.same(
|
||||
matches[3],
|
||||
search:find({
|
||||
forward = false,
|
||||
pos = { 4, 6 },
|
||||
wrap = false,
|
||||
})
|
||||
)
|
||||
end)
|
||||
|
||||
it("handles count = 0", function()
|
||||
assert.same(
|
||||
matches[2],
|
||||
search:find({
|
||||
pos = { 1, 4 },
|
||||
count = 0,
|
||||
})
|
||||
)
|
||||
assert.is_nil(search:find({
|
||||
pos = { 2, 7 },
|
||||
count = 0,
|
||||
}))
|
||||
end)
|
||||
|
||||
it("returns forward matches", function()
|
||||
assert.same(
|
||||
{ matches[3] },
|
||||
search:get({
|
||||
from = { 2, 6 },
|
||||
})
|
||||
)
|
||||
end)
|
||||
|
||||
it("returns forward matches", function()
|
||||
assert.same(
|
||||
{ matches[3] },
|
||||
search:get({
|
||||
from = { 3, 3 },
|
||||
})
|
||||
)
|
||||
end)
|
||||
|
||||
it("returns backward matches", function()
|
||||
assert.same(
|
||||
{ matches[1] },
|
||||
search:get({
|
||||
to = { 1, 3 },
|
||||
})
|
||||
)
|
||||
end)
|
||||
|
||||
it("returns backward matches at pos", function()
|
||||
assert.same(
|
||||
{ matches[1] },
|
||||
search:get({
|
||||
to = { 1, 0 },
|
||||
})
|
||||
)
|
||||
end)
|
||||
|
||||
it("finds matcher", function()
|
||||
assert.same({ win = 1000, pos = { 1, 4 }, end_pos = { 1, 6 } }, search:find())
|
||||
assert.same({ win = 1000, pos = { 3, 3 }, end_pos = { 3, 5 } }, search:find({ count = 2 }))
|
||||
assert.same(
|
||||
{ win = 1000, pos = { 3, 3 }, end_pos = { 3, 5 } },
|
||||
search:find({ forward = false })
|
||||
)
|
||||
assert.same(
|
||||
{ win = 1000, pos = { 1, 4 }, end_pos = { 1, 6 } },
|
||||
search:find({
|
||||
forward = false,
|
||||
pos = { 2, 7 },
|
||||
})
|
||||
)
|
||||
assert.same(
|
||||
{ win = 1000, pos = { 1, 4 }, end_pos = { 1, 6 } },
|
||||
search:find({
|
||||
forward = false,
|
||||
pos = { 3, 2 },
|
||||
})
|
||||
)
|
||||
end)
|
||||
|
||||
it("finds forward skipping match at current position", function()
|
||||
assert.same(
|
||||
matches[2],
|
||||
search:find({
|
||||
forward = true,
|
||||
pos = { 1, 0 },
|
||||
wrap = false,
|
||||
})
|
||||
)
|
||||
end)
|
||||
|
||||
it("finds backward skipping match at current position", function()
|
||||
assert.same(
|
||||
matches[2],
|
||||
search:find({
|
||||
forward = false,
|
||||
pos = { 3, 3 },
|
||||
wrap = true,
|
||||
})
|
||||
)
|
||||
end)
|
||||
|
||||
it("finds forward from a non-match position", function()
|
||||
assert.same(
|
||||
matches[2],
|
||||
search:find({
|
||||
forward = true,
|
||||
pos = { 1, 3 },
|
||||
wrap = false,
|
||||
})
|
||||
)
|
||||
end)
|
||||
|
||||
it("finds backward from a non-match position", function()
|
||||
assert.same(
|
||||
matches[2],
|
||||
search:find({
|
||||
forward = false,
|
||||
pos = { 3, 2 },
|
||||
wrap = true,
|
||||
})
|
||||
)
|
||||
end)
|
||||
|
||||
it("returns nil when wrapping is disabled and no match is found forward", function()
|
||||
assert.is_nil(search:find({
|
||||
forward = true,
|
||||
pos = { 4, 0 },
|
||||
wrap = false,
|
||||
}))
|
||||
end)
|
||||
|
||||
it("returns nil when wrapping is disabled and no match is found backward", function()
|
||||
assert.is_nil(search:find({
|
||||
forward = false,
|
||||
pos = { 1, 0 },
|
||||
wrap = false,
|
||||
}))
|
||||
end)
|
||||
end)
|
||||
@ -0,0 +1,190 @@
|
||||
local Search = require("flash.search")
|
||||
local State = require("flash.state")
|
||||
|
||||
describe("search.view", function()
|
||||
before_each(function()
|
||||
vim.opt.ignorecase = true
|
||||
vim.opt.smartcase = true
|
||||
vim.api.nvim_buf_set_lines(1, 0, -1, false, {})
|
||||
end)
|
||||
|
||||
local function get_matches(pattern)
|
||||
local state = State.new({ pattern = pattern, search = {
|
||||
mode = "search",
|
||||
} })
|
||||
local win = vim.api.nvim_get_current_win()
|
||||
local search = Search.new(win, state)
|
||||
return search:get()
|
||||
end
|
||||
|
||||
local function set(text, pos)
|
||||
local lines = vim.split(vim.trim(text), "\n")
|
||||
lines = vim.tbl_map(function(line)
|
||||
return vim.trim(line)
|
||||
end, lines)
|
||||
vim.api.nvim_buf_set_lines(1, 0, -1, false, lines)
|
||||
vim.api.nvim_win_set_cursor(0, pos or { 1, 0 })
|
||||
end
|
||||
|
||||
it("finds matches", function()
|
||||
set([[
|
||||
foobar
|
||||
line1
|
||||
line2
|
||||
]])
|
||||
|
||||
local matches = get_matches("line")
|
||||
assert.same({
|
||||
{ win = 1000, pos = { 2, 0 }, end_pos = { 2, 3 } },
|
||||
{ win = 1000, pos = { 3, 0 }, end_pos = { 3, 3 } },
|
||||
}, matches)
|
||||
end)
|
||||
|
||||
it("finds multi matches on same line", function()
|
||||
set([[
|
||||
foobar foobar
|
||||
line1
|
||||
lineFoo
|
||||
]])
|
||||
|
||||
local matches = get_matches("foo")
|
||||
assert.same({
|
||||
{ win = 1000, pos = { 1, 0 }, end_pos = { 1, 2 } },
|
||||
{ win = 1000, pos = { 1, 7 }, end_pos = { 1, 9 } },
|
||||
{ win = 1000, pos = { 3, 4 }, end_pos = { 3, 6 } },
|
||||
}, matches)
|
||||
end)
|
||||
|
||||
it("deals with case", function()
|
||||
set([[
|
||||
foobar
|
||||
Line1
|
||||
line2
|
||||
]])
|
||||
|
||||
local matches = get_matches("line")
|
||||
assert.same({
|
||||
{ win = 1000, pos = { 2, 0 }, end_pos = { 2, 3 } },
|
||||
{ win = 1000, pos = { 3, 0 }, end_pos = { 3, 3 } },
|
||||
}, matches)
|
||||
end)
|
||||
|
||||
it("deals with smartcase", function()
|
||||
set([[
|
||||
foobar
|
||||
Line1
|
||||
line2
|
||||
]])
|
||||
|
||||
local matches = get_matches("Line")
|
||||
assert.same({
|
||||
{ win = 1000, pos = { 2, 0 }, end_pos = { 2, 3 } },
|
||||
}, matches)
|
||||
end)
|
||||
|
||||
it("finds matches on each line", function()
|
||||
set([[
|
||||
line1
|
||||
line2
|
||||
line3
|
||||
]])
|
||||
|
||||
local matches = get_matches("line")
|
||||
assert.same({
|
||||
{ win = 1000, pos = { 1, 0 }, end_pos = { 1, 3 } },
|
||||
{ win = 1000, pos = { 2, 0 }, end_pos = { 2, 3 } },
|
||||
{ win = 1000, pos = { 3, 0 }, end_pos = { 3, 3 } },
|
||||
}, matches)
|
||||
end)
|
||||
|
||||
it("handles '\\Vi\\zs\\.'", function()
|
||||
set([[
|
||||
line1
|
||||
line2
|
||||
line3
|
||||
]])
|
||||
|
||||
local matches = get_matches([[\Vi\zs\m.]])
|
||||
assert.same({
|
||||
{ win = 1000, pos = { 1, 2 }, end_pos = { 1, 2 } },
|
||||
{ win = 1000, pos = { 2, 2 }, end_pos = { 2, 2 } },
|
||||
{ win = 1000, pos = { 3, 2 }, end_pos = { 3, 2 } },
|
||||
}, matches)
|
||||
end)
|
||||
|
||||
it("handles ^", function()
|
||||
set([[
|
||||
foobar
|
||||
line1
|
||||
line2
|
||||
]])
|
||||
|
||||
local matches = get_matches("^")
|
||||
assert.same({
|
||||
{ win = 1000, pos = { 1, 0 }, end_pos = { 1, 0 } },
|
||||
{ win = 1000, pos = { 2, 0 }, end_pos = { 2, 0 } },
|
||||
{ win = 1000, pos = { 3, 0 }, end_pos = { 3, 0 } },
|
||||
}, matches)
|
||||
end)
|
||||
|
||||
it("handles ^", function()
|
||||
set([[
|
||||
foobar
|
||||
line1
|
||||
line2
|
||||
]])
|
||||
|
||||
local matches = get_matches("^")
|
||||
assert.same({
|
||||
{ win = 1000, pos = { 1, 0 }, end_pos = { 1, 0 } },
|
||||
{ win = 1000, pos = { 2, 0 }, end_pos = { 2, 0 } },
|
||||
{ win = 1000, pos = { 3, 0 }, end_pos = { 3, 0 } },
|
||||
}, matches)
|
||||
end)
|
||||
|
||||
it("handles ^.\\?", function()
|
||||
set([[
|
||||
foobar
|
||||
line1
|
||||
line2
|
||||
]])
|
||||
|
||||
local matches = get_matches("^.\\?")
|
||||
assert.same({
|
||||
{ win = 1000, pos = { 1, 0 }, end_pos = { 1, 0 } },
|
||||
{ win = 1000, pos = { 2, 0 }, end_pos = { 2, 0 } },
|
||||
{ win = 1000, pos = { 3, 0 }, end_pos = { 3, 0 } },
|
||||
}, matches)
|
||||
end)
|
||||
|
||||
it("handles ^l", function()
|
||||
set([[
|
||||
foobar
|
||||
line1
|
||||
line2
|
||||
]])
|
||||
|
||||
local matches = get_matches("^l")
|
||||
assert.same({
|
||||
{ win = 1000, pos = { 2, 0 }, end_pos = { 2, 0 } },
|
||||
{ win = 1000, pos = { 3, 0 }, end_pos = { 3, 0 } },
|
||||
}, matches)
|
||||
end)
|
||||
|
||||
it("handles wrapping", function()
|
||||
set(
|
||||
[[
|
||||
foo
|
||||
line1
|
||||
foo
|
||||
]],
|
||||
{ 3, 0 }
|
||||
)
|
||||
|
||||
local matches = get_matches("foo")
|
||||
assert.same({
|
||||
{ win = 1000, pos = { 1, 0 }, end_pos = { 1, 2 } },
|
||||
{ win = 1000, pos = { 3, 0 }, end_pos = { 3, 2 } },
|
||||
}, matches)
|
||||
end)
|
||||
end)
|
||||
Reference in New Issue
Block a user