-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathextract_multiline.lua
101 lines (95 loc) · 2.37 KB
/
extract_multiline.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
local parser = require("overseer.parser")
local ExtractMultiline = {
desc = "Extract a multiline string as a single field on an item",
doc_args = {
{
name = "opts",
type = "object",
desc = "Configuration options",
position_optional = true,
fields = {
{
name = "append",
type = "boolean",
desc = "After parsing, append the item to the results list. When false, the pending item will stick around.",
default = true,
},
},
},
{
name = "pattern",
type = "string|function",
desc = "The lua pattern to use for matching. As long as the pattern matches, lines will continue to be appended to the field.",
},
{
name = "field",
type = "string",
desc = "The name of the field to add to the item",
},
},
examples = {
{
desc = [[Extract all indented lines as a message]],
code = [[{"extract_multiline", "^( .+)", "message"}]],
},
},
}
function ExtractMultiline.new(opts, pattern, field)
if field == nil then
field = pattern
pattern = opts
opts = {}
end
opts = vim.tbl_deep_extend("keep", opts, {
append = true,
})
return setmetatable({
append = opts.append,
done = nil,
any_match = false,
pattern = pattern,
field = field,
}, { __index = ExtractMultiline })
end
function ExtractMultiline:reset()
self.done = nil
self.any_match = false
end
local function append_line(item, key, value)
if not item[key] then
item[key] = value
else
item[key] = item[key] .. "\n" .. value
end
end
function ExtractMultiline:ingest(line, ctx)
if self.done then
return self.done
end
local item = ctx.item
local result
if type(self.pattern) == "string" then
result = line:match(self.pattern)
else
result = self.pattern(line)
end
if result then
self.any_match = true
if type(self.field) == "table" then
local key, postprocess = unpack(self.field)
append_line(item, key, postprocess(result, self))
else
append_line(item, self.field, result)
end
return parser.STATUS.RUNNING
else
if self.any_match or not vim.tbl_isempty(item) then
self.done = parser.STATUS.SUCCESS
parser.util.append_item(self.append, line, ctx)
else
self.done = parser.STATUS.FAILURE
end
return self.done
end
end
return ExtractMultiline