-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathunique.lua
More file actions
58 lines (58 loc) · 2.02 KB
/
unique.lua
File metadata and controls
58 lines (58 loc) · 2.02 KB
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
local task_list = require("overseer.task_list")
local util = require("overseer.util")
---@type overseer.ComponentFileDefinition
return {
desc = "Ensure that this task does not have any duplicates",
-- Doesn't make sense for user to add this using a form.
editable = false,
params = {
replace = {
desc = "If a prior task exists, replace it. When false, will restart the existing task and dispose the current task",
long_desc = "Note that when this is false a new task that is created will restart the existing one and _dispose itself_. This can lead to unexpected behavior if you are creating a task and then trying to use that reference (to run actions on it, use it as a dependency, etc)",
type = "boolean",
default = true,
},
restart_interrupts = {
desc = "When replace = false, should restarting the existing task interrupt it",
type = "boolean",
default = true,
},
soft = {
desc = "Only dispose duplicate tasks if they are completed. Implies replace = true.",
type = "boolean",
default = false,
},
compare = {
desc = "Comparison function that returns true when a task is equal",
type = "opaque",
optional = true,
},
},
constructor = function(params)
local compare = params.compare or function(a, b)
return a.name == b.name
end
return {
on_pre_start = function(_, task)
local tasks = task_list.list_tasks()
for _, t in ipairs(tasks) do
if t ~= task and compare(t, task) then
if params.replace then
if t:is_complete() or not params.soft then
task:subscribe("on_start", function()
util.replace_buffer_in_wins(t:get_bufnr(), task:get_bufnr())
return false
end)
t:dispose(true)
end
else
task:dispose(true)
t:restart(params.restart_interrupts)
return false
end
end
end
end,
}
end,
}