-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathset_defaults.lua
87 lines (83 loc) · 2.25 KB
/
set_defaults.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
local util = require("overseer.parser.util")
local SetDefaults = {
desc = "A decorator that adds values to any items extracted by the child",
doc_args = {
{
name = "opts",
type = "object",
desc = "Configuration options",
position_optional = true,
fields = {
{
name = "values",
type = "object",
desc = "Hardcoded key-value pairs to set as default values",
},
{
name = "hoist_item",
type = "boolean",
desc = "Take the current pending item, and use its fields as the default key-value pairs",
default = true,
},
},
},
{
name = "child",
type = "parser",
desc = "The child parser node",
},
},
examples = {
{
desc = [[Extract the filename from a header line, then for each line of output beneath it parse the test name + status, and also add the filename to each item]],
code = [[
{"sequence",
{"extract", {append = false}, "^Test result (.+)$", "filename"}
{"set_defaults",
{"loop",
{"extract", "^Test (.+): (.+)$", "test_name", "status"}
}
}
}
]],
},
},
}
function SetDefaults.new(opts, child)
if child == nil then
child = opts
opts = {}
end
opts = vim.tbl_deep_extend("keep", opts, {
values = {},
hoist_item = true,
})
vim.validate({
values = { opts.values, "t" },
hoist_item = { opts.hoist_item, "b" },
})
return setmetatable({
default_values = opts.values,
hoist_item = opts.hoist_item,
current_defaults = nil,
child = util.hydrate(child),
}, { __index = SetDefaults })
end
function SetDefaults:reset()
self.current_defaults = nil
self.child:reset()
end
function SetDefaults:ingest(line, ctx)
if not self.current_defaults then
self.current_defaults = vim.deepcopy(self.default_values)
if self.hoist_item then
self.current_defaults = vim.tbl_extend("force", self.current_defaults, ctx.item)
end
end
local prev_default_values = ctx.default_values
ctx.default_values = vim.tbl_deep_extend("force", ctx.default_values or {}, self.current_defaults)
local status = self.child:ingest(line, ctx)
ctx.default_values = prev_default_values
return status
end
return SetDefaults