-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsession.lua
More file actions
282 lines (256 loc) · 6.57 KB
/
Copy pathsession.lua
File metadata and controls
282 lines (256 loc) · 6.57 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
-- In-memory log of Jumpy activity for the current Neovim run. Records each
-- prompt and its per-file hunk outcomes so the sidebar can present a reviewable,
-- iterable history. Entries are plain, JSON-serializable tables (see persist.lua)
-- and carry a generic `kind` so future agentic steps (tool_read/tool_edit) can
-- be appended without reshaping this module.
local M = {}
local HUNK_STATUS = {
pending = true,
accepted = true,
rejected = true,
superseded = true,
}
local current = nil
local next_entry_id = 0
local observers = {}
local function has_vim()
return type(vim) == "table"
end
local function now()
return os.time()
end
local function project_root()
local ok, root = pcall(function()
return require("jumpy.path").project_root()
end)
if ok and type(root) == "string" and root ~= "" then
return root
end
return "."
end
local function generate_id()
local stamp = os.date("!%Y%m%dT%H%M%S")
local pid = 0
if has_vim() and vim.fn and vim.fn.getpid then
pid = vim.fn.getpid()
end
return string.format("%s-%d", stamp, pid)
end
local function notify()
for _, fn in ipairs(observers) do
pcall(fn, current)
end
end
-- Persist is a no-op outside Neovim (tests) and best-effort otherwise so a
-- write failure never breaks the editing flow.
local function save()
if not has_vim() or not current then
return
end
pcall(function()
require("jumpy.persist").save(current)
end)
end
local function ensure_session()
if not current then
current = {
id = generate_id(),
root = project_root(),
started = now(),
entries = {},
}
end
return current
end
--- Find the single live result tracking `bufnr`, searching newest-first.
local function live_result_for_buf(bufnr)
if not current then
return nil
end
for i = #current.entries, 1, -1 do
local results = current.entries[i].results
for _, result in ipairs(results) do
if result.live and result.bufnr == bufnr then
return result
end
end
end
return nil
end
--- A new proposal for a buffer replaces whatever was pending there, so the
--- previous live result is frozen: still-pending hunks become "superseded" and
--- it stops matching future mark_hunk/mark_all calls (which key on bufnr).
local function supersede_buf(bufnr)
if not current then
return
end
for _, entry in ipairs(current.entries) do
for _, result in ipairs(entry.results) do
if result.live and result.bufnr == bufnr then
for _, hunk in pairs(result.hunks) do
if hunk.status == "pending" then
hunk.status = "superseded"
end
end
result.live = false
end
end
end
end
--- @param opts table { text = string, mode = string }
--- @return table entry
function M.record_prompt(opts)
opts = opts or {}
local session = ensure_session()
next_entry_id = next_entry_id + 1
local entry = {
id = next_entry_id,
kind = "prompt",
text = opts.text or "",
mode = opts.mode or "buffer",
time = now(),
results = {},
}
table.insert(session.entries, entry)
notify()
save()
return entry
end
--- Attach a proposed-hunks result to `entry`, superseding any prior live result
--- for the same buffer first.
--- @param entry table entry returned by record_prompt
--- @param opts table { path = string, bufnr = number|nil, hunks = { { idx, line, preview } } }
function M.record_result(entry, opts)
if not entry then
return
end
opts = opts or {}
if opts.bufnr ~= nil then
supersede_buf(opts.bufnr)
end
local hunks = {}
for _, h in ipairs(opts.hunks or {}) do
hunks[h.idx] = {
status = "pending",
line = h.line,
preview = h.preview or "",
-- Diff geometry + content, kept so a hunk's before/after can be
-- re-rendered later (jumpy.inspect). Optional: older sessions and
-- callers that omit it simply won't support inspection.
old_start = h.old_start,
old_count = h.old_count,
new_start = h.new_start,
new_count = h.new_count,
removed_lines = h.removed_lines,
added_lines = h.added_lines,
}
end
table.insert(entry.results, {
path = opts.path,
bufnr = opts.bufnr,
live = true,
hunks = hunks,
})
notify()
save()
end
--- @return boolean whether a live hunk was updated
function M.mark_hunk(bufnr, idx, status)
if not HUNK_STATUS[status] then
return false
end
local result = live_result_for_buf(bufnr)
if not result then
return false
end
local hunk = result.hunks[idx]
if not hunk then
return false
end
hunk.status = status
notify()
save()
return true
end
--- Mark every still-pending hunk of a buffer's live result.
function M.mark_all(bufnr, status)
if not HUNK_STATUS[status] then
return false
end
local result = live_result_for_buf(bufnr)
if not result then
return false
end
local changed = false
for _, hunk in pairs(result.hunks) do
if hunk.status == "pending" then
hunk.status = status
changed = true
end
end
if changed then
notify()
save()
end
return changed
end
--- Keep a live hunk's preview (and, if given, its replacement content) in sync
--- when it is reprompted in place.
function M.update_hunk_preview(bufnr, idx, preview, added_lines)
local result = live_result_for_buf(bufnr)
if not result or not result.hunks[idx] then
return false
end
local hunk = result.hunks[idx]
hunk.preview = preview or hunk.preview
if added_lines then
hunk.added_lines = added_lines
hunk.new_count = #added_lines
end
notify()
save()
return true
end
--- Derived counts for a result, for display.
function M.status_counts(result)
local counts = { pending = 0, accepted = 0, rejected = 0, superseded = 0, total = 0 }
for _, hunk in pairs(result.hunks or {}) do
counts[hunk.status] = (counts[hunk.status] or 0) + 1
counts.total = counts.total + 1
end
return counts
end
function M.get_session()
return current
end
function M.get_entries()
return current and current.entries or {}
end
--- Persist and end the current run's session; the next activity starts fresh.
function M.clear()
if current then
save()
end
current = nil
notify()
end
--- Register an observer called with the current session on every change.
--- Returns an unsubscribe function.
function M.subscribe(fn)
table.insert(observers, fn)
return function()
for i, o in ipairs(observers) do
if o == fn then
table.remove(observers, i)
return
end
end
end
end
--- Test helper: drop all state.
function M._reset()
current = nil
next_entry_id = 0
observers = {}
end
return M