reslop 0.1.8 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +25 -1
- package/README.md +27 -38
- package/bin/reslop.js +2 -2
- package/lib/ansi.js +6 -2
- package/lib/api.js +2 -2
- package/lib/capabilities.js +10 -14
- package/lib/cli.js +28 -47
- package/lib/clipboard.js +1 -1
- package/lib/deps-diff.js +712 -0
- package/lib/deps.js +556 -32
- package/lib/detect.js +1 -7
- package/lib/diff/align.js +7 -10
- package/lib/diff/diff.js +53 -0
- package/lib/diff/display.js +5 -4
- package/lib/diff/inline.js +26 -21
- package/lib/diff/items.js +6 -4
- package/lib/diff/patch.js +1 -3
- package/lib/diff/unit.js +392 -0
- package/lib/files.js +34 -8
- package/lib/{git/branches.js → git-branches.js} +103 -10
- package/lib/{git/dependencies.js → git-deps.js} +111 -38
- package/lib/git.js +592 -65
- package/lib/github.js +28 -197
- package/lib/gitlab.js +24 -195
- package/lib/highlight/bash.js +1 -3
- package/lib/highlight/css.js +1 -3
- package/lib/highlight/js.js +11 -14
- package/lib/highlight.js +6 -9
- package/lib/keys.js +22 -9
- package/lib/manifest.js +330 -0
- package/lib/remote.js +374 -0
- package/lib/render/branches.js +34 -66
- package/lib/render/chrome.js +57 -22
- package/lib/render/commits.js +85 -0
- package/lib/render/diff.js +16 -16
- package/lib/render/files.js +29 -37
- package/lib/render/frame.js +29 -13
- package/lib/render/notes.js +4 -3
- package/lib/render/primitives.js +46 -1
- package/lib/{render.js → render/render.js} +6 -7
- package/lib/render/unit.js +41 -0
- package/lib/review.js +750 -14
- package/lib/select.js +5 -10
- package/lib/session/accessors.js +9 -1
- package/lib/session/actions.js +93 -36
- package/lib/session/branches.js +130 -144
- package/lib/session/change-actions.js +206 -124
- package/lib/session/commit.js +217 -62
- package/lib/session/compose.js +158 -13
- package/lib/session/dispatch.js +109 -55
- package/lib/session/files-pane.js +145 -142
- package/lib/session/items.js +131 -34
- package/lib/session/lifecycle.js +82 -42
- package/lib/session/load.js +19 -33
- package/lib/session/navigation.js +15 -3
- package/lib/session/ops.js +28 -41
- package/lib/session/pointer.js +58 -54
- package/lib/session/review.js +23 -37
- package/lib/session/terminal.js +6 -12
- package/lib/session/todos.js +8 -12
- package/lib/session/unit.js +208 -0
- package/lib/session/update.js +33 -46
- package/lib/session/view.js +82 -11
- package/lib/session/watch.js +11 -19
- package/lib/session.js +29 -226
- package/lib/source.js +3 -3
- package/lib/update.js +40 -75
- package/lib/{git/proc.js → utilities.js} +75 -11
- package/lib/wrap.js +1 -3
- package/package.json +2 -2
- package/lib/deps/changes.js +0 -331
- package/lib/deps/items.js +0 -131
- package/lib/deps/manifest.js +0 -145
- package/lib/deps/merge.js +0 -120
- package/lib/deps/proposals.js +0 -164
- package/lib/deps/reports.js +0 -180
- package/lib/deps/rows.js +0 -177
- package/lib/deps/scan.js +0 -138
- package/lib/deps/usage.js +0 -68
- package/lib/deps/versions.js +0 -119
- package/lib/diff.js +0 -33
- package/lib/git/changes.js +0 -164
- package/lib/git/read.js +0 -253
- package/lib/git/run.js +0 -100
- package/lib/npm/commands.js +0 -100
- package/lib/remote/changes.js +0 -37
- package/lib/remote/headers.js +0 -36
- package/lib/remote/http.js +0 -122
- package/lib/remote/notes.js +0 -94
- package/lib/review/format.js +0 -434
- package/lib/review/storage.js +0 -150
- package/lib/review/store.js +0 -178
- package/lib/review/templates.js +0 -49
- package/lib/session/origins.js +0 -7
- package/lib/sys.js +0 -84
package/lib/review.js
CHANGED
|
@@ -1,19 +1,754 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
3
|
+
const fs = require('node:fs');
|
|
4
|
+
const path = require('node:path');
|
|
5
|
+
|
|
6
|
+
const { TODO_FILE, REVIEW_DIR } = require('./files.js');
|
|
7
|
+
|
|
8
|
+
const createStore = (reviewPath, templates = []) => ({
|
|
9
|
+
reviewPath,
|
|
10
|
+
dirty: false,
|
|
11
|
+
nextTodoId: 1,
|
|
12
|
+
feedback: new Map(),
|
|
13
|
+
code: new Map(),
|
|
14
|
+
todos: [],
|
|
15
|
+
templates: [...templates],
|
|
16
|
+
status: 'editing',
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const noteCounts = (store) => {
|
|
20
|
+
const counts = { feedback: 0, todo: 0, todoDone: 0, code: 0 };
|
|
21
|
+
if (!store) return counts;
|
|
22
|
+
for (const todo of store.todos) {
|
|
23
|
+
if (!todo.text.trim()) continue;
|
|
24
|
+
counts.todo += 1;
|
|
25
|
+
if (todo.done) counts.todoDone += 1;
|
|
26
|
+
}
|
|
27
|
+
for (const note of store.feedback.values()) {
|
|
28
|
+
if (note.text.trim()) counts.feedback += 1;
|
|
29
|
+
}
|
|
30
|
+
counts.code = store.code.size;
|
|
31
|
+
return counts;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const hasNotes = (store) => {
|
|
35
|
+
const counts = noteCounts(store);
|
|
36
|
+
return counts.feedback > 0 || counts.todo > 0 || counts.code > 0;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const setFeedback = (store, key, note) => {
|
|
40
|
+
const text = note.text ?? '';
|
|
41
|
+
const trimmed = text.trim();
|
|
42
|
+
const prev = store.feedback.get(key);
|
|
43
|
+
if (!trimmed) {
|
|
44
|
+
if (!prev) return;
|
|
45
|
+
store.feedback.delete(key);
|
|
46
|
+
store.dirty = true;
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (prev && prev.text === text) return;
|
|
50
|
+
store.feedback.set(key, {
|
|
51
|
+
file: note.file,
|
|
52
|
+
oldStart: note.oldStart ?? 0,
|
|
53
|
+
newStart: note.newStart ?? 0,
|
|
54
|
+
blockId: note.blockId ?? 0,
|
|
55
|
+
origin: note.origin ?? '',
|
|
56
|
+
header: note.header ?? '',
|
|
57
|
+
text,
|
|
58
|
+
done: note.done ?? prev?.done ?? false,
|
|
59
|
+
});
|
|
60
|
+
store.dirty = true;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const setCode = (store, key, note) => {
|
|
64
|
+
const prev = store.code.get(key);
|
|
65
|
+
if (note.clear) {
|
|
66
|
+
if (!prev) return;
|
|
67
|
+
store.code.delete(key);
|
|
68
|
+
store.dirty = true;
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const text = note.text ?? '';
|
|
72
|
+
if (prev && prev.text === text) return;
|
|
73
|
+
store.code.set(key, {
|
|
74
|
+
file: note.file,
|
|
75
|
+
oldStart: note.oldStart ?? 0,
|
|
76
|
+
newStart: note.newStart ?? 0,
|
|
77
|
+
blockId: note.blockId ?? 0,
|
|
78
|
+
origin: note.origin ?? '',
|
|
79
|
+
header: note.header ?? '',
|
|
80
|
+
text,
|
|
81
|
+
done: note.done ?? prev?.done ?? false,
|
|
82
|
+
});
|
|
83
|
+
store.dirty = true;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const addTodo = (store, file, text = '') => {
|
|
87
|
+
const id = store.nextTodoId;
|
|
88
|
+
store.nextTodoId += 1;
|
|
89
|
+
const todo = { id, file, text, done: false };
|
|
90
|
+
store.todos.push(todo);
|
|
91
|
+
store.dirty = true;
|
|
92
|
+
return todo;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const removeTodo = (store, id) => {
|
|
96
|
+
const next = [];
|
|
97
|
+
let found = false;
|
|
98
|
+
for (const todo of store.todos) {
|
|
99
|
+
if (todo.id === id) {
|
|
100
|
+
found = true;
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
next.push(todo);
|
|
104
|
+
}
|
|
105
|
+
if (!found) return false;
|
|
106
|
+
store.todos = next;
|
|
107
|
+
store.dirty = true;
|
|
108
|
+
return true;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const setTodoText = (store, id, text) => {
|
|
112
|
+
const i = store.todos.findIndex((todo) => todo.id === id);
|
|
113
|
+
if (i < 0) return;
|
|
114
|
+
const prev = store.todos[i];
|
|
115
|
+
const trimmed = text.trim();
|
|
116
|
+
if (!trimmed) return void removeTodo(store, id);
|
|
117
|
+
if (prev.text === text) return;
|
|
118
|
+
store.todos[i] = { id: prev.id, file: prev.file, text, done: prev.done };
|
|
119
|
+
store.dirty = true;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const checkLabel = (text, done) => {
|
|
123
|
+
const mark = done ? 'x' : ' ';
|
|
124
|
+
return `[${mark}] ${text}`;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const feedbackKey = (note) => {
|
|
128
|
+
const file = note.file ?? '';
|
|
129
|
+
const oldStart = note.oldStart ?? 0;
|
|
130
|
+
const newStart = note.newStart ?? 0;
|
|
131
|
+
const block = note.blockId ?? 0;
|
|
132
|
+
return `${file}:${oldStart}:${newStart}:${block}`;
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const applyImportedNotes = (store, imported) => {
|
|
136
|
+
if (!store || !imported) return;
|
|
137
|
+
const feedbacks = imported.feedback ?? [];
|
|
138
|
+
for (const note of feedbacks) {
|
|
139
|
+
const key = feedbackKey(note);
|
|
140
|
+
const prev = store.feedback.get(key);
|
|
141
|
+
const text = prev ? `${prev.text}\n\n${note.text}` : note.text;
|
|
142
|
+
const done = prev ? prev.done && !!note.done : !!note.done;
|
|
143
|
+
setFeedback(store, key, {
|
|
144
|
+
file: note.file,
|
|
145
|
+
oldStart: note.oldStart ?? 0,
|
|
146
|
+
newStart: note.newStart ?? 0,
|
|
147
|
+
blockId: note.blockId ?? 0,
|
|
148
|
+
origin: note.origin ?? '',
|
|
149
|
+
header: note.header ?? '',
|
|
150
|
+
text,
|
|
151
|
+
done,
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
const todos = imported.todos ?? [];
|
|
155
|
+
for (const todo of todos) {
|
|
156
|
+
const added = addTodo(store, todo.file || '', todo.text ?? '');
|
|
157
|
+
if (todo.done) added.done = true;
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
const setStatus = (store, status) => {
|
|
162
|
+
if (!store) return;
|
|
163
|
+
store.status = status;
|
|
164
|
+
store.dirty = true;
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
const TEMPLATE_SHOW = 5;
|
|
168
|
+
|
|
169
|
+
const rankedTemplates = (templates) => {
|
|
170
|
+
const ranked = [...templates];
|
|
171
|
+
ranked.sort((a, b) => {
|
|
172
|
+
const byCount = b.count - a.count;
|
|
173
|
+
if (byCount) return byCount;
|
|
174
|
+
return a.text.localeCompare(b.text);
|
|
175
|
+
});
|
|
176
|
+
return ranked;
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
const prefixTemplates = (templates, prefix) => {
|
|
180
|
+
if (!prefix) return templates;
|
|
181
|
+
return templates.filter((entry) => entry.text.startsWith(prefix));
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
const upsertTemplate = (templates, text) => {
|
|
185
|
+
const trimmed = text.trim();
|
|
186
|
+
if (!trimmed) return templates;
|
|
187
|
+
const next = [...templates];
|
|
188
|
+
const i = next.findIndex((entry) => entry.text === trimmed);
|
|
189
|
+
if (i < 0) {
|
|
190
|
+
next.push({ text: trimmed, count: 1 });
|
|
191
|
+
return next;
|
|
192
|
+
}
|
|
193
|
+
const prev = next[i];
|
|
194
|
+
next[i] = { text: prev.text, count: prev.count + 1 };
|
|
195
|
+
return next;
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
const rememberTemplate = (store, prevText, nextText) => {
|
|
199
|
+
const trimmed = nextText.trim();
|
|
200
|
+
if (!trimmed) return;
|
|
201
|
+
const prev = (prevText ?? '').trim();
|
|
202
|
+
if (prev === trimmed) return;
|
|
203
|
+
store.templates = upsertTemplate(store.templates, trimmed);
|
|
204
|
+
store.dirty = true;
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
const REVIEW_STATUSES = ['editing', 'ready', 'partial', 'done'];
|
|
208
|
+
const FEED_HEAD = /^### Feedback `([^`]+)` \+(\d+) \((.*)\)\s*$/;
|
|
209
|
+
const FEED_BLOCK = /^(.*), block (\d+)$/;
|
|
210
|
+
const FEED_BLOCK_ONLY = /^block (\d+)$/;
|
|
211
|
+
const FEED_META = /^<!-- reslop:(.*) -->\s*$/;
|
|
212
|
+
const FEED_SUFFIX = /^(.*) - (.+):(\d+):(\d+):(\d+)$/;
|
|
213
|
+
const CHECK_ITEM = /^- \[([ xX])\] (.*)$/;
|
|
214
|
+
const CODE_HEAD = /^- \[([ xX])\] code `([^`]+)`\s*$/;
|
|
215
|
+
const FENCE_LINE = /^(`{3,})\s*$/;
|
|
216
|
+
const FILE_QUOTE = /^> (.+)$/;
|
|
217
|
+
const STATUS_LINE = /^status:\s*(\S+)\s*$/;
|
|
218
|
+
const GIT_ORIGINS = ['unstaged', 'staged', 'untracked', 'commit', 'pr'];
|
|
219
|
+
const SECTION_LINE = {
|
|
220
|
+
'### Todo': 'todo',
|
|
221
|
+
'### Feedback': 'feedback',
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
const AGENT_INSTRUCTIONS = [
|
|
225
|
+
'Execute reviews with `status: ready` or `status: partial`.',
|
|
226
|
+
'Do not start `editing` or `done` review files.',
|
|
227
|
+
'Work through unchecked items. After finishing one, mark it `[x]`.',
|
|
228
|
+
'For `code` items, replace added lines at that location with the fence.',
|
|
229
|
+
'Set `status` to `partial` if some remain, or `done` if all are `[x]`.',
|
|
230
|
+
'Do not redo items already marked `[x]`.',
|
|
231
|
+
].join('\n');
|
|
232
|
+
|
|
233
|
+
const toInt = (text) => parseInt(text, 10);
|
|
234
|
+
|
|
235
|
+
const canonicalStatus = (raw) => {
|
|
236
|
+
if (!raw) return '';
|
|
237
|
+
const name = raw === 'pending' ? 'ready' : raw;
|
|
238
|
+
if (REVIEW_STATUSES.includes(name)) return name;
|
|
239
|
+
return '';
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
const parseFrontmatterStatus = (text) => {
|
|
243
|
+
if (!text.startsWith('---')) return '';
|
|
244
|
+
const nl = text.indexOf('\n');
|
|
245
|
+
if (nl < 0) return '';
|
|
246
|
+
const rest = text.slice(nl + 1);
|
|
247
|
+
const end = rest.indexOf('\n---');
|
|
248
|
+
if (end < 0) return '';
|
|
249
|
+
const matter = rest.slice(0, end);
|
|
250
|
+
for (const line of matter.split('\n')) {
|
|
251
|
+
const match = STATUS_LINE.exec(line);
|
|
252
|
+
if (match) return canonicalStatus(match[1]);
|
|
253
|
+
}
|
|
254
|
+
return '';
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
const isDoneMark = (mark) => mark === 'x' || mark === 'X';
|
|
258
|
+
|
|
259
|
+
const pushTodo = (builder, file, text, done) => {
|
|
260
|
+
const id = builder.nextTodoId;
|
|
261
|
+
builder.nextTodoId += 1;
|
|
262
|
+
builder.todos.push({ id, file, text, done });
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
const checkboxLine = (text, done) => {
|
|
266
|
+
const flat = text.trim().replace(/\n/g, ' ');
|
|
267
|
+
const mark = done ? 'x' : ' ';
|
|
268
|
+
return `- [${mark}] ${flat}`;
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
const storeFeedback = (builder, note, key) => {
|
|
272
|
+
const next = {
|
|
273
|
+
file: note.file,
|
|
274
|
+
oldStart: note.oldStart ?? 0,
|
|
275
|
+
newStart: note.newStart ?? 0,
|
|
276
|
+
blockId: note.blockId ?? 0,
|
|
277
|
+
origin: note.origin ?? '',
|
|
278
|
+
header: note.header ?? '',
|
|
279
|
+
text: note.text,
|
|
280
|
+
done: note.done ?? false,
|
|
281
|
+
};
|
|
282
|
+
builder.feedback.set(key || feedbackKey(next), next);
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
const storeCode = (builder, note, key) => {
|
|
286
|
+
const next = {
|
|
287
|
+
file: note.file,
|
|
288
|
+
oldStart: note.oldStart ?? 0,
|
|
289
|
+
newStart: note.newStart ?? 0,
|
|
290
|
+
blockId: note.blockId ?? 0,
|
|
291
|
+
origin: note.origin ?? '',
|
|
292
|
+
header: note.header ?? '',
|
|
293
|
+
text: note.text,
|
|
294
|
+
done: note.done ?? false,
|
|
295
|
+
};
|
|
296
|
+
builder.code.set(key || feedbackKey(next), next);
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
const feedbackLine = (note) => {
|
|
300
|
+
const loc = feedbackKey(note);
|
|
301
|
+
return `${checkboxLine(note.text, note.done)} - ${loc}`;
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
const parseLoc = (raw) => {
|
|
305
|
+
const parts = raw.split(':');
|
|
306
|
+
const n = parts.length;
|
|
307
|
+
if (n < 4) return null;
|
|
308
|
+
const blockId = toInt(parts[n - 1]);
|
|
309
|
+
const newStart = toInt(parts[n - 2]);
|
|
310
|
+
const oldStart = toInt(parts[n - 3]);
|
|
311
|
+
if (!Number.isFinite(blockId + newStart + oldStart)) return null;
|
|
312
|
+
let fileParts = parts.slice(0, n - 3);
|
|
313
|
+
let origin = '';
|
|
314
|
+
if (fileParts.length > 1 && GIT_ORIGINS.includes(fileParts[0])) {
|
|
315
|
+
origin = fileParts[0];
|
|
316
|
+
fileParts = fileParts.slice(1);
|
|
317
|
+
}
|
|
318
|
+
if (!fileParts.length) return null;
|
|
319
|
+
const file = fileParts.join(':');
|
|
320
|
+
return { origin, file, oldStart, newStart, blockId };
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
const parseFeedSuffix = (textItem) => {
|
|
324
|
+
const match = FEED_SUFFIX.exec(textItem);
|
|
325
|
+
if (!match) return null;
|
|
326
|
+
const text = match[1];
|
|
327
|
+
const file = match[2];
|
|
328
|
+
const oldStart = toInt(match[3]);
|
|
329
|
+
const newStart = toInt(match[4]);
|
|
330
|
+
const blockId = toInt(match[5]);
|
|
331
|
+
return { text, file, oldStart, newStart, blockId };
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
const fileGroup = (groups, file) => {
|
|
335
|
+
let group = groups.get(file);
|
|
336
|
+
if (!group) {
|
|
337
|
+
group = { todos: [], feedback: [], code: [] };
|
|
338
|
+
groups.set(file, group);
|
|
339
|
+
}
|
|
340
|
+
return group;
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
const groupReviewNotes = (notes) => {
|
|
344
|
+
const groups = new Map();
|
|
345
|
+
for (const todo of notes.todos) {
|
|
346
|
+
if (!todo.text.trim()) continue;
|
|
347
|
+
fileGroup(groups, TODO_FILE).todos.push(todo);
|
|
348
|
+
}
|
|
349
|
+
for (const key of notes.feedback.keys()) {
|
|
350
|
+
const note = notes.feedback.get(key);
|
|
351
|
+
if (!note.text.trim()) continue;
|
|
352
|
+
fileGroup(groups, note.file).feedback.push({ key, note });
|
|
353
|
+
}
|
|
354
|
+
for (const key of notes.code.keys()) {
|
|
355
|
+
const note = notes.code.get(key);
|
|
356
|
+
fileGroup(groups, note.file).code.push({ key, note });
|
|
357
|
+
}
|
|
358
|
+
return groups;
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
const reviewHeader = (notes) => {
|
|
362
|
+
const name = path.basename(notes.reviewPath, '.md');
|
|
363
|
+
const status = canonicalStatus(notes.status) || 'editing';
|
|
364
|
+
return [
|
|
365
|
+
'---',
|
|
366
|
+
`status: ${status}`,
|
|
367
|
+
'---',
|
|
368
|
+
'',
|
|
369
|
+
`# reslop review ${name}`,
|
|
370
|
+
'',
|
|
371
|
+
'## Agent instructions',
|
|
372
|
+
'',
|
|
373
|
+
AGENT_INSTRUCTIONS,
|
|
374
|
+
'',
|
|
375
|
+
];
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
const fenceFor = (text) => {
|
|
379
|
+
let n = 3;
|
|
380
|
+
for (const line of text.split('\n')) {
|
|
381
|
+
const match = /^(`{3,})/.exec(line);
|
|
382
|
+
if (!match) continue;
|
|
383
|
+
if (match[1].length >= n) n = match[1].length + 1;
|
|
384
|
+
}
|
|
385
|
+
return '`'.repeat(n);
|
|
386
|
+
};
|
|
387
|
+
|
|
388
|
+
const emitCodeItem = (lines, note) => {
|
|
389
|
+
const loc = feedbackKey(note);
|
|
390
|
+
const mark = note.done ? 'x' : ' ';
|
|
391
|
+
const fence = fenceFor(note.text);
|
|
392
|
+
lines.push(`- [${mark}] code \`${loc}\``);
|
|
393
|
+
lines.push(fence);
|
|
394
|
+
if (note.text !== '') lines.push(note.text);
|
|
395
|
+
lines.push(fence);
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
const emitReviewGroups = (lines, groups) => {
|
|
399
|
+
for (const file of groups.keys()) {
|
|
400
|
+
const group = groups.get(file);
|
|
401
|
+
lines.push(`## ${file}`);
|
|
402
|
+
lines.push('');
|
|
403
|
+
for (const todo of group.todos) {
|
|
404
|
+
lines.push(checkboxLine(todo.text, todo.done));
|
|
405
|
+
}
|
|
406
|
+
for (const item of group.feedback) {
|
|
407
|
+
lines.push(feedbackLine(item.note));
|
|
408
|
+
}
|
|
409
|
+
for (const item of group.code) {
|
|
410
|
+
emitCodeItem(lines, item.note);
|
|
411
|
+
}
|
|
412
|
+
lines.push('');
|
|
413
|
+
}
|
|
414
|
+
};
|
|
415
|
+
|
|
416
|
+
const serializeReview = (notes) => {
|
|
417
|
+
const lines = reviewHeader(notes);
|
|
418
|
+
emitReviewGroups(lines, groupReviewNotes(notes));
|
|
419
|
+
return `${lines.join('\n')}\n`;
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
const parseFeedbackHeading = (line) => {
|
|
423
|
+
const match = FEED_HEAD.exec(line);
|
|
424
|
+
if (!match) return null;
|
|
425
|
+
const file = match[1];
|
|
426
|
+
const newStart = toInt(match[2]);
|
|
427
|
+
const rest = match[3];
|
|
428
|
+
const only = FEED_BLOCK_ONLY.exec(rest);
|
|
429
|
+
if (only) {
|
|
430
|
+
return { file, newStart, header: '', blockId: toInt(only[1]) };
|
|
431
|
+
}
|
|
432
|
+
const withHead = FEED_BLOCK.exec(rest);
|
|
433
|
+
if (!withHead) {
|
|
434
|
+
return { file, newStart, header: rest, blockId: 0 };
|
|
435
|
+
}
|
|
436
|
+
const header = withHead[1];
|
|
437
|
+
const blockId = toInt(withHead[2]);
|
|
438
|
+
return { file, newStart, header, blockId };
|
|
439
|
+
};
|
|
440
|
+
|
|
441
|
+
const flushPendingFeedback = (builder, pending) => {
|
|
442
|
+
if (!pending || !(pending.text ?? '').trim()) return null;
|
|
443
|
+
storeFeedback(builder, pending, pending.key);
|
|
444
|
+
return null;
|
|
445
|
+
};
|
|
446
|
+
|
|
447
|
+
const headingFile = (line) => {
|
|
448
|
+
const quoted = FILE_QUOTE.exec(line);
|
|
449
|
+
if (quoted) return quoted[1];
|
|
450
|
+
if (line.startsWith('## ') && line !== '## Agent instructions') {
|
|
451
|
+
return line.slice(3);
|
|
452
|
+
}
|
|
453
|
+
return null;
|
|
454
|
+
};
|
|
455
|
+
|
|
456
|
+
const startFeedbackPending = (heading, file) => ({
|
|
457
|
+
file: heading.file || file,
|
|
458
|
+
newStart: heading.newStart,
|
|
459
|
+
header: heading.header,
|
|
460
|
+
blockId: heading.blockId,
|
|
461
|
+
oldStart: heading.newStart,
|
|
462
|
+
origin: '',
|
|
463
|
+
key: '',
|
|
464
|
+
text: '',
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
const applyFeedMeta = (pending, raw) => {
|
|
468
|
+
const loc = parseLoc(raw);
|
|
469
|
+
if (!loc) return pending;
|
|
470
|
+
pending.origin = loc.origin;
|
|
471
|
+
pending.file = loc.file;
|
|
472
|
+
pending.oldStart = loc.oldStart;
|
|
473
|
+
pending.newStart = loc.newStart;
|
|
474
|
+
pending.blockId = loc.blockId;
|
|
475
|
+
pending.key = feedbackKey(loc);
|
|
476
|
+
return pending;
|
|
477
|
+
};
|
|
478
|
+
|
|
479
|
+
const applyCheckItem = (builder, file, section, pending, textItem, done) => {
|
|
480
|
+
if (section === 'todo' && file) {
|
|
481
|
+
pushTodo(builder, file, textItem, done);
|
|
482
|
+
return pending;
|
|
483
|
+
}
|
|
484
|
+
const parsed = parseFeedSuffix(textItem);
|
|
485
|
+
if (parsed) {
|
|
486
|
+
flushPendingFeedback(builder, pending);
|
|
487
|
+
storeFeedback(builder, {
|
|
488
|
+
file: parsed.file || file,
|
|
489
|
+
oldStart: parsed.oldStart,
|
|
490
|
+
newStart: parsed.newStart,
|
|
491
|
+
blockId: parsed.blockId,
|
|
492
|
+
origin: '',
|
|
493
|
+
header: '',
|
|
494
|
+
text: parsed.text,
|
|
495
|
+
done,
|
|
496
|
+
});
|
|
497
|
+
return null;
|
|
498
|
+
}
|
|
499
|
+
if (pending) {
|
|
500
|
+
pending.text = textItem;
|
|
501
|
+
pending.done = done;
|
|
502
|
+
return flushPendingFeedback(builder, pending);
|
|
503
|
+
}
|
|
504
|
+
if (file && section !== 'feedback') {
|
|
505
|
+
pushTodo(builder, file, textItem, done);
|
|
506
|
+
}
|
|
507
|
+
return pending;
|
|
508
|
+
};
|
|
509
|
+
|
|
510
|
+
const applyReviewLine = (builder, file, section, pending, line) => {
|
|
511
|
+
const nextFile = headingFile(line);
|
|
512
|
+
if (nextFile !== null) {
|
|
513
|
+
pending = flushPendingFeedback(builder, pending);
|
|
514
|
+
return { file: nextFile, section: '', pending };
|
|
515
|
+
}
|
|
516
|
+
const nextSection = SECTION_LINE[line];
|
|
517
|
+
if (nextSection) {
|
|
518
|
+
pending = flushPendingFeedback(builder, pending);
|
|
519
|
+
return { file, section: nextSection, pending };
|
|
520
|
+
}
|
|
521
|
+
const heading = parseFeedbackHeading(line);
|
|
522
|
+
if (heading) {
|
|
523
|
+
flushPendingFeedback(builder, pending);
|
|
524
|
+
pending = startFeedbackPending(heading, file);
|
|
525
|
+
return { file, section: 'feedback', pending };
|
|
526
|
+
}
|
|
527
|
+
const meta = FEED_META.exec(line);
|
|
528
|
+
if (meta && pending) {
|
|
529
|
+
pending = applyFeedMeta(pending, meta[1]);
|
|
530
|
+
return { file, section, pending };
|
|
531
|
+
}
|
|
532
|
+
const item = CHECK_ITEM.exec(line);
|
|
533
|
+
if (!item) return { file, section, pending };
|
|
534
|
+
const done = isDoneMark(item[1]);
|
|
535
|
+
pending = applyCheckItem(builder, file, section, pending, item[2], done);
|
|
536
|
+
return { file, section, pending };
|
|
537
|
+
};
|
|
538
|
+
|
|
539
|
+
const startCodePending = (line) => {
|
|
540
|
+
const match = CODE_HEAD.exec(line);
|
|
541
|
+
if (!match) return null;
|
|
542
|
+
const loc = parseLoc(match[2]);
|
|
543
|
+
if (!loc) return null;
|
|
544
|
+
return {
|
|
545
|
+
done: isDoneMark(match[1]),
|
|
546
|
+
origin: loc.origin,
|
|
547
|
+
file: loc.file,
|
|
548
|
+
oldStart: loc.oldStart,
|
|
549
|
+
newStart: loc.newStart,
|
|
550
|
+
blockId: loc.blockId,
|
|
551
|
+
open: '',
|
|
552
|
+
lines: [],
|
|
553
|
+
};
|
|
554
|
+
};
|
|
555
|
+
|
|
556
|
+
const storeCodeNote = (builder, pending, text) => {
|
|
557
|
+
storeCode(builder, {
|
|
558
|
+
file: pending.file,
|
|
559
|
+
oldStart: pending.oldStart,
|
|
560
|
+
newStart: pending.newStart,
|
|
561
|
+
blockId: pending.blockId,
|
|
562
|
+
origin: pending.origin,
|
|
563
|
+
header: '',
|
|
564
|
+
text,
|
|
565
|
+
done: pending.done,
|
|
566
|
+
});
|
|
567
|
+
};
|
|
568
|
+
|
|
569
|
+
const flushPendingCode = (builder, pending) => {
|
|
570
|
+
if (!pending) return;
|
|
571
|
+
const text = pending.open ? pending.lines.join('\n') : '';
|
|
572
|
+
storeCodeNote(builder, pending, text);
|
|
573
|
+
};
|
|
574
|
+
|
|
575
|
+
const takeCodeLine = (builder, pending, line) => {
|
|
576
|
+
const fence = FENCE_LINE.exec(line);
|
|
577
|
+
if (!pending.open) {
|
|
578
|
+
if (fence) {
|
|
579
|
+
pending.open = fence[1];
|
|
580
|
+
return pending;
|
|
581
|
+
}
|
|
582
|
+
if (!line.trim()) return pending;
|
|
583
|
+
storeCodeNote(builder, pending, '');
|
|
584
|
+
return 'fallthrough';
|
|
585
|
+
}
|
|
586
|
+
if (fence && fence[1].length >= pending.open.length) {
|
|
587
|
+
storeCodeNote(builder, pending, pending.lines.join('\n'));
|
|
588
|
+
return null;
|
|
589
|
+
}
|
|
590
|
+
pending.lines.push(line);
|
|
591
|
+
return pending;
|
|
592
|
+
};
|
|
593
|
+
|
|
594
|
+
const parseReview = (text, reviewPath, templates = []) => {
|
|
595
|
+
const builder = createStore(reviewPath, templates);
|
|
596
|
+
builder.status = parseFrontmatterStatus(text) || 'editing';
|
|
597
|
+
let file = '';
|
|
598
|
+
let section = '';
|
|
599
|
+
let pending = null;
|
|
600
|
+
let pendingCode = null;
|
|
601
|
+
for (const line of text.split('\n')) {
|
|
602
|
+
if (pendingCode) {
|
|
603
|
+
const nextCode = takeCodeLine(builder, pendingCode, line);
|
|
604
|
+
if (nextCode === 'fallthrough') {
|
|
605
|
+
pendingCode = null;
|
|
606
|
+
} else {
|
|
607
|
+
pendingCode = nextCode;
|
|
608
|
+
continue;
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
const started = startCodePending(line);
|
|
612
|
+
if (started) {
|
|
613
|
+
pending = flushPendingFeedback(builder, pending);
|
|
614
|
+
pendingCode = started;
|
|
615
|
+
continue;
|
|
616
|
+
}
|
|
617
|
+
const next = applyReviewLine(builder, file, section, pending, line);
|
|
618
|
+
file = next.file;
|
|
619
|
+
section = next.section;
|
|
620
|
+
pending = next.pending;
|
|
621
|
+
}
|
|
622
|
+
flushPendingCode(builder, pendingCode);
|
|
623
|
+
flushPendingFeedback(builder, pending);
|
|
624
|
+
builder.dirty = false;
|
|
625
|
+
return builder;
|
|
626
|
+
};
|
|
627
|
+
|
|
628
|
+
const TEMPLATES_FILE = 'templates.json';
|
|
629
|
+
const AUTOSAVE_MS = 3000;
|
|
630
|
+
const REVIEW_NAME = /^(\d{4}-\d{2}-\d{2})-(\d+)\.md$/;
|
|
631
|
+
|
|
632
|
+
const pad2 = (n) => `${n}`.padStart(2, '0');
|
|
633
|
+
|
|
634
|
+
const dateStamp = (date) => {
|
|
635
|
+
const y = date.getFullYear();
|
|
636
|
+
const month = pad2(date.getMonth() + 1);
|
|
637
|
+
const day = pad2(date.getDate());
|
|
638
|
+
return `${y}-${month}-${day}`;
|
|
639
|
+
};
|
|
640
|
+
|
|
641
|
+
const allocateReviewPath = (dir, date, existingNames) => {
|
|
642
|
+
const stamp = dateStamp(date);
|
|
643
|
+
let max = -1;
|
|
644
|
+
for (const name of existingNames) {
|
|
645
|
+
const match = REVIEW_NAME.exec(name);
|
|
646
|
+
if (!match) continue;
|
|
647
|
+
if (match[1] !== stamp) continue;
|
|
648
|
+
const n = toInt(match[2]);
|
|
649
|
+
if (n > max) max = n;
|
|
650
|
+
}
|
|
651
|
+
const next = max + 1;
|
|
652
|
+
const file = `${stamp}-${next < 100 ? pad2(next) : String(next)}.md`;
|
|
653
|
+
return path.join(dir, REVIEW_DIR, file);
|
|
654
|
+
};
|
|
655
|
+
|
|
656
|
+
const listReviewNames = (dir) => {
|
|
657
|
+
const folder = path.join(dir, REVIEW_DIR);
|
|
658
|
+
try {
|
|
659
|
+
return fs.readdirSync(folder);
|
|
660
|
+
} catch {
|
|
661
|
+
return [];
|
|
662
|
+
}
|
|
663
|
+
};
|
|
664
|
+
|
|
665
|
+
const compareReviewNames = (left, right) => {
|
|
666
|
+
const a = REVIEW_NAME.exec(left);
|
|
667
|
+
const b = REVIEW_NAME.exec(right);
|
|
668
|
+
if (!a && !b) return 0;
|
|
669
|
+
if (!a) return -1;
|
|
670
|
+
if (!b) return 1;
|
|
671
|
+
if (a[1] < b[1]) return -1;
|
|
672
|
+
if (a[1] > b[1]) return 1;
|
|
673
|
+
return toInt(a[2]) - toInt(b[2]);
|
|
674
|
+
};
|
|
675
|
+
|
|
676
|
+
const latestReviewName = (names) => {
|
|
677
|
+
let best = '';
|
|
678
|
+
for (const name of names) {
|
|
679
|
+
if (!REVIEW_NAME.test(name)) continue;
|
|
680
|
+
if (!best || compareReviewNames(name, best) > 0) best = name;
|
|
681
|
+
}
|
|
682
|
+
return best;
|
|
683
|
+
};
|
|
684
|
+
|
|
685
|
+
const readReviewStatus = (reviewPath) => {
|
|
686
|
+
try {
|
|
687
|
+
const text = fs.readFileSync(reviewPath, 'utf8');
|
|
688
|
+
return parseFrontmatterStatus(text);
|
|
689
|
+
} catch {
|
|
690
|
+
return '';
|
|
691
|
+
}
|
|
692
|
+
};
|
|
693
|
+
|
|
694
|
+
const resolveReviewPath = (dir, date, existingNames, extra = {}) => {
|
|
695
|
+
if (!extra.forceNew) {
|
|
696
|
+
const latest = latestReviewName(existingNames);
|
|
697
|
+
if (latest) {
|
|
698
|
+
const reviewPath = path.join(dir, REVIEW_DIR, latest);
|
|
699
|
+
if (readReviewStatus(reviewPath) === 'editing') {
|
|
700
|
+
return { reviewPath, resume: true };
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
return {
|
|
705
|
+
reviewPath: allocateReviewPath(dir, date, existingNames),
|
|
706
|
+
resume: false,
|
|
707
|
+
};
|
|
708
|
+
};
|
|
709
|
+
|
|
710
|
+
const loadReview = (reviewPath, templates) => {
|
|
711
|
+
const raw = fs.readFileSync(reviewPath, 'utf8');
|
|
712
|
+
return parseReview(raw, reviewPath, templates);
|
|
713
|
+
};
|
|
714
|
+
|
|
715
|
+
const loadTemplates = (dir) => {
|
|
716
|
+
const file = path.join(dir, REVIEW_DIR, TEMPLATES_FILE);
|
|
717
|
+
try {
|
|
718
|
+
const raw = fs.readFileSync(file, 'utf8');
|
|
719
|
+
const data = JSON.parse(raw);
|
|
720
|
+
if (!Array.isArray(data)) return [];
|
|
721
|
+
const templates = [];
|
|
722
|
+
for (const entry of data) {
|
|
723
|
+
if (!entry || typeof entry.text !== 'string') continue;
|
|
724
|
+
const count = toInt(entry.count);
|
|
725
|
+
templates.push({
|
|
726
|
+
text: entry.text,
|
|
727
|
+
count: Number.isFinite(count) ? count : 1,
|
|
728
|
+
});
|
|
729
|
+
}
|
|
730
|
+
return templates;
|
|
731
|
+
} catch {
|
|
732
|
+
return [];
|
|
733
|
+
}
|
|
734
|
+
};
|
|
735
|
+
|
|
736
|
+
const flushReview = (notes, force = false) => {
|
|
737
|
+
if (!notes || !notes.dirty) return false;
|
|
738
|
+
if (!hasNotes(notes) && !force) {
|
|
739
|
+
notes.dirty = false;
|
|
740
|
+
return false;
|
|
741
|
+
}
|
|
742
|
+
const folder = path.dirname(notes.reviewPath);
|
|
743
|
+
fs.mkdirSync(folder, { recursive: true });
|
|
744
|
+
fs.writeFileSync(notes.reviewPath, serializeReview(notes), 'utf8');
|
|
745
|
+
const top = path.dirname(folder);
|
|
746
|
+
const templatesFile = path.join(top, REVIEW_DIR, TEMPLATES_FILE);
|
|
747
|
+
const body = `${JSON.stringify(notes.templates, null, 2)}\n`;
|
|
748
|
+
fs.writeFileSync(templatesFile, body, 'utf8');
|
|
749
|
+
notes.dirty = false;
|
|
750
|
+
return true;
|
|
751
|
+
};
|
|
17
752
|
|
|
18
753
|
module.exports = {
|
|
19
754
|
TEMPLATE_SHOW,
|
|
@@ -38,6 +773,7 @@ module.exports = {
|
|
|
38
773
|
checkLabel,
|
|
39
774
|
feedbackKey,
|
|
40
775
|
applyImportedNotes,
|
|
776
|
+
setStatus,
|
|
41
777
|
serializeReview,
|
|
42
778
|
parseReview,
|
|
43
779
|
loadReview,
|