-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathskip_until.lua
80 lines (76 loc) · 1.89 KB
/
skip_until.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
local parser = require("overseer.parser")
local SkipUntil = {
desc = "Skip over lines until one matches",
doc_args = {
{
name = "opts",
type = "object",
desc = "Configuration options",
position_optional = true,
fields = {
{
name = "skip_matching_line",
type = "boolean",
desc = "Consumes the line that matches. Later nodes will only see the next line.",
default = true,
},
{
name = "regex",
type = "boolean",
desc = "Use vim regex instead of lua pattern (see :help pattern)",
default = true,
},
},
},
{
name = "pattern",
vararg = true,
type = "string|string[]|fun(line: string): string",
desc = "The lua pattern to use for matching. The node succeeds if any of these patterns match.",
},
},
examples = {
{
desc = [[Skip input until we see "Error" or "Warning"]],
code = [[{"skip_until", "^Error:", "^Warning:"}]],
},
},
}
function SkipUntil.new(opts, ...)
local patterns
if type(opts) ~= "table" then
patterns = { opts, ... }
opts = {}
else
patterns = { ... }
end
vim.validate({
skip_matching_line = { opts.skip_matching_line, "b", true },
})
if opts.skip_matching_line == nil then
opts.skip_matching_line = true
end
return setmetatable({
skip_matching_line = opts.skip_matching_line,
test = parser.util.patterns_to_test(patterns, opts.regex),
done = false,
}, { __index = SkipUntil })
end
function SkipUntil:reset()
self.done = false
end
function SkipUntil:ingest(line)
if self.done then
return parser.STATUS.SUCCESS
end
if self.test(line) then
self.done = true
if self.skip_matching_line then
return parser.STATUS.RUNNING
else
return parser.STATUS.SUCCESS
end
end
return parser.STATUS.RUNNING
end
return SkipUntil