-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrequests.lua
More file actions
98 lines (87 loc) · 2.15 KB
/
Copy pathrequests.lua
File metadata and controls
98 lines (87 loc) · 2.15 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
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
-- Registry of in-flight LLM requests. Each request owns its own id, job and
-- target files, so multiple prompts can run in parallel without sharing the
-- single global "loading" state that made jumpy single-flight.
local M = {}
local next_id = 0
local inflight = {}
--- Register a new request.
--- @param opts table|nil { label = string, targets = { [key] = true } }
--- @return number request id
function M.begin(opts)
opts = opts or {}
next_id = next_id + 1
inflight[next_id] = {
label = opts.label,
targets = opts.targets or {},
job_id = nil,
cancelled = false,
}
return next_id
end
function M.set_job(id, job_id)
local req = inflight[id]
if req then
req.job_id = job_id
end
end
function M.finish(id)
inflight[id] = nil
end
--- Unknown/finished ids count as cancelled so late callbacks are discarded.
function M.is_cancelled(id)
local req = inflight[id]
return req == nil or req.cancelled
end
--- A target key is a normalized absolute file path, or "buf:N" for
--- unnamed buffers.
function M.target_key(bufnr, abs_path)
if abs_path and abs_path ~= "" then
return abs_path
end
if bufnr then
local name = vim.api.nvim_buf_get_name(bufnr)
if name ~= "" then
return require("jumpy.path").normalize_abs(name)
end
return "buf:" .. bufnr
end
return nil
end
function M.is_target_busy(target)
for _, req in pairs(inflight) do
if not req.cancelled and req.targets[target] then
return true
end
end
return false
end
function M.count()
local n = 0
for _, req in pairs(inflight) do
if not req.cancelled then
n = n + 1
end
end
return n
end
--- Cancel every in-flight request. Jobs are stopped; each request is cleaned
--- up by its own on_exit handler. Returns how many requests were cancelled.
function M.cancel_all()
local n = 0
for _, req in pairs(inflight) do
if not req.cancelled then
req.cancelled = true
n = n + 1
if req.job_id and vim and vim.fn and vim.fn.jobstop then
pcall(vim.fn.jobstop, req.job_id)
end
end
end
return n
end
--- Test helper: drop all state.
function M._reset()
next_id = 0
inflight = {}
end
return M