A client of `lembas serve --stdio`, in vscode/: plain JavaScript, no dependencies, no build. scripts/package-vscode.sh writes the .vsix with Node alone; the version comes from VERSION. - Chat in a side bar view: transcript, mode, the session's model, Stop, new session. Approval cards are answered there; one that carries ACP diff content opens the editor's diff view before the answer. - Ask about a selection, with the editor context as _meta.lembas.editor. - Rewrite a selection through _lembas/edit: proposed changes with Accept / Reject per change and for all, and a diff beside the file. Nothing is written before it is accepted. - Autocomplete as an inline completion provider on _lembas/complete: debounced, the request before cancelled by its own id, a cancelled or stale answer shown as nothing. No model chosen: said once, with a button that opens the picker. - The two model settings through _lembas/editor/settings, and a quick switch for the window that is saved nowhere. - Files the agent shows (_lembas/show, _lembas/show/read): pictures in the chat, anything else a line with Open, under a size limit. - fs/read_text_file answered from the open document. Model output is untrusted: the extension's own Markdown reader makes a tree of plain objects and the page builds elements and text from it, under a Content-Security-Policy with one nonce'd script. Tests (scripts/test-vscode.sh, node --test): the modules in src/ against a stand-in agent, the package's structure, and extension.js against an in-memory stand-in for the editor's API. Nothing was run inside VS Code. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
180 lines
9.0 KiB
JavaScript
180 lines
9.0 KiB
JavaScript
'use strict';
|
|
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const h = require('../src/hunks');
|
|
|
|
/** Apply an edit of `editFor` to a list of lines, as an editor would to its text. */
|
|
function applyEdit(lines, e) {
|
|
const text = lines.join('\n');
|
|
const starts = [0];
|
|
for (let i = 0; i < text.length; i++) if (text[i] === '\n') starts.push(i + 1);
|
|
const offset = (p) => (p.end ? (p.line + 1 < starts.length ? starts[p.line + 1] - 1 : text.length) : starts[p.line]);
|
|
return (text.slice(0, offset(e.start)) + e.text + text.slice(offset(e.stop))).split('\n');
|
|
}
|
|
|
|
const apply = (a, hunks) => {
|
|
const out = a.slice();
|
|
for (const k of hunks.slice().reverse()) out.splice(k.aStart, k.aCount, ...k.b);
|
|
return out;
|
|
};
|
|
|
|
test('the differences between two lists of lines', () => {
|
|
const a = ['one', 'two', 'three', 'four', 'five'];
|
|
assert.deepEqual(h.diffLines(a, a), []);
|
|
assert.deepEqual(h.diffLines(a, ['one', 'TWO', 'three', 'four', 'five']), [{ aStart: 1, aCount: 1, bStart: 1, bCount: 1 }]);
|
|
assert.deepEqual(h.diffLines(a, ['one', 'three', 'four', 'five']), [{ aStart: 1, aCount: 1, bStart: 1, bCount: 0 }]);
|
|
assert.deepEqual(h.diffLines(a, ['zero', 'one', 'two', 'three', 'four', 'five']), [{ aStart: 0, aCount: 0, bStart: 0, bCount: 1 }]);
|
|
assert.deepEqual(h.diffLines(a, [...a, 'six', 'seven']), [{ aStart: 5, aCount: 0, bStart: 5, bCount: 2 }]);
|
|
assert.deepEqual(h.diffLines(a, ['ONE', 'two', 'three', 'four', 'FIVE']), [
|
|
{ aStart: 0, aCount: 1, bStart: 0, bCount: 1 },
|
|
{ aStart: 4, aCount: 1, bStart: 4, bCount: 1 },
|
|
]);
|
|
assert.deepEqual(h.diffLines([], ['a']), [{ aStart: 0, aCount: 0, bStart: 0, bCount: 1 }]);
|
|
assert.deepEqual(h.diffLines(['a'], []), [{ aStart: 0, aCount: 1, bStart: 0, bCount: 0 }]);
|
|
});
|
|
|
|
test('whatever the two texts, the hunks turn the one into the other', () => {
|
|
let seed = 7;
|
|
const rnd = (n) => {
|
|
seed = (seed * 1103515245 + 12345) & 0x7fffffff;
|
|
return seed % n;
|
|
};
|
|
for (let round = 0; round < 300; round++) {
|
|
const a = Array.from({ length: rnd(14) }, () => 'line ' + rnd(5));
|
|
const b = a.slice();
|
|
for (let edits = rnd(5); edits > 0; edits--) {
|
|
const at = rnd(b.length + 1);
|
|
if (rnd(2)) b.splice(at, rnd(3), 'new ' + rnd(4));
|
|
else b.splice(at, rnd(3));
|
|
}
|
|
const hunks = h.diffLines(a, b);
|
|
const withB = hunks.map((k) => ({ ...k, b: b.slice(k.bStart, k.bStart + k.bCount) }));
|
|
assert.deepEqual(apply(a, withB), b, `round ${round}`);
|
|
for (let i = 1; i < hunks.length; i++) {
|
|
assert.ok(hunks[i].aStart > hunks[i - 1].aStart + hunks[i - 1].aCount, 'an unchanged line lies between two hunks');
|
|
}
|
|
}
|
|
});
|
|
|
|
test('texts too different to compare come back as one hunk, still correct', () => {
|
|
const a = Array.from({ length: 5000 }, (_, i) => 'a' + i);
|
|
const b = Array.from({ length: 5000 }, (_, i) => 'b' + i);
|
|
const hunks = h.diffLines(['same', ...a, 'end'], ['same', ...b, 'end']);
|
|
assert.deepEqual(hunks, [{ aStart: 1, aCount: 5000, bStart: 1, bCount: 5000 }]);
|
|
});
|
|
|
|
const DOC = ['# bread', 'def a():', ' return 1', '', 'def b():', ' return 2', 'print(a())'];
|
|
|
|
test('a rewrite of lines 2-6 becomes hunks at the document\'s own lines', () => {
|
|
const old = DOC.slice(1, 6);
|
|
const fresh = ['def a():', ' return 10', '', 'def b():', ' """Two."""', ' return 2'];
|
|
const hunks = h.propose(1, old, fresh);
|
|
assert.deepEqual(hunks, [
|
|
{ id: 1, line: 2, old: [' return 1'], new: [' return 10'] },
|
|
{ id: 2, line: 5, old: [], new: [' """Two."""'] },
|
|
]);
|
|
assert.deepEqual(h.withAll(DOC, hunks), ['# bread', ...fresh, 'print(a())']);
|
|
});
|
|
|
|
const OLD = ['a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9'];
|
|
const FRESH = ['A1', 'a2', 'a3', 'a5', 'a6', 'new', 'a7', 'a8', 'A9', 'tail'];
|
|
const AROUND = ['top', ...OLD, 'bottom'];
|
|
|
|
test('accepting hunks one by one, in any order, gives the rewrite; rejecting leaves the text', () => {
|
|
const orders = [[0, 1, 2, 3], [3, 2, 1, 0], [1, 3, 0, 2], [2, 0, 3, 1]];
|
|
for (const order of orders) {
|
|
let lines = AROUND.slice();
|
|
let hunks = h.propose(1, OLD, FRESH);
|
|
assert.equal(hunks.length, 4); // a1 replaced, a4 taken away, "new" added, a9 replaced with two lines
|
|
const ids = order.map((i) => hunks[i].id);
|
|
for (const id of ids) {
|
|
const hunk = hunks.find((k) => k.id === id);
|
|
assert.ok(h.stillFits(hunk, (i) => lines[i], lines.length), `hunk ${id} fits in order ${order}`);
|
|
lines = applyEdit(lines, h.editFor(hunk, lines.length));
|
|
hunks = h.afterAccept(hunks, hunk);
|
|
}
|
|
assert.deepEqual(lines, ['top', ...FRESH, 'bottom'], `order ${order}`);
|
|
assert.equal(hunks.length, 0);
|
|
}
|
|
// Accept only the second and the fourth: where the others were proposed, the old text stays.
|
|
let lines = AROUND.slice();
|
|
let hunks = h.propose(1, OLD, FRESH);
|
|
for (const id of [2, 4]) {
|
|
const hunk = hunks.find((k) => k.id === id);
|
|
lines = applyEdit(lines, h.editFor(hunk, lines.length));
|
|
hunks = h.afterAccept(hunks, hunk);
|
|
}
|
|
assert.deepEqual(lines, ['top', 'a1', 'a2', 'a3', 'a5', 'a6', 'a7', 'a8', 'A9', 'tail', 'bottom']);
|
|
assert.deepEqual(hunks.map((k) => [k.id, k.line]), [[1, 1], [3, 6]]);
|
|
});
|
|
|
|
test('the edit for each kind of hunk, at the edges of a document', () => {
|
|
// Lines added after the last line.
|
|
let lines = ['a', 'b'];
|
|
lines = applyEdit(lines, h.editFor({ line: 2, old: [], new: ['c', 'd'] }, 2));
|
|
assert.deepEqual(lines, ['a', 'b', 'c', 'd']);
|
|
// Lines added in front of the first.
|
|
assert.deepEqual(applyEdit(['a', 'b'], h.editFor({ line: 0, old: [], new: ['z'] }, 2)), ['z', 'a', 'b']);
|
|
// The last line taken away: the line break before it goes too.
|
|
assert.deepEqual(applyEdit(['a', 'b', 'c'], h.editFor({ line: 2, old: ['c'], new: [] }, 3)), ['a', 'b']);
|
|
// A middle line taken away.
|
|
assert.deepEqual(applyEdit(['a', 'b', 'c'], h.editFor({ line: 1, old: ['b'], new: [] }, 3)), ['a', 'c']);
|
|
// Every line taken away leaves one empty line, which is what an empty document is.
|
|
assert.deepEqual(applyEdit(['a', 'b'], h.editFor({ line: 0, old: ['a', 'b'], new: [] }, 2)), ['']);
|
|
// Lines replaced by more lines.
|
|
assert.deepEqual(applyEdit(['a', 'b', 'c'], h.editFor({ line: 1, old: ['b'], new: ['x', 'y'] }, 3)), ['a', 'x', 'y', 'c']);
|
|
});
|
|
|
|
test('all hunks as one set of edits against the same text (accept all)', () => {
|
|
const hunks = h.propose(1, OLD, FRESH);
|
|
// An editor applies edits given against one state; from the bottom up is the same thing.
|
|
let lines = AROUND.slice();
|
|
for (const hunk of hunks.slice().sort((p, q) => q.line - p.line)) lines = applyEdit(lines, h.editFor(hunk, AROUND.length));
|
|
assert.deepEqual(lines, ['top', ...FRESH, 'bottom']);
|
|
});
|
|
|
|
test('a hunk whose text was changed by hand no longer fits', () => {
|
|
const [hunk] = h.propose(1, [' return 1'], [' return 10']);
|
|
const lines = ['x', ' return 1'];
|
|
assert.ok(h.stillFits(hunk, (i) => lines[i], lines.length));
|
|
lines[1] = ' return 1 # edited';
|
|
assert.ok(!h.stillFits(hunk, (i) => lines[i], lines.length));
|
|
assert.ok(!h.stillFits({ line: 5, old: ['a'], new: [] }, (i) => lines[i], lines.length), 'past the end');
|
|
assert.ok(h.stillFits({ line: 2, old: [], new: ['z'] }, (i) => lines[i], lines.length), 'adding after the last line');
|
|
assert.ok(!h.stillFits({ line: 3, old: [], new: ['z'] }, (i) => lines[i], lines.length));
|
|
});
|
|
|
|
test('typing elsewhere moves the hunks below it; typing inside one drops it', () => {
|
|
const hunks = [
|
|
{ id: 1, line: 2, old: ['b', 'c'], new: ['B'] },
|
|
{ id: 2, line: 8, old: [], new: ['added'] },
|
|
{ id: 3, line: 10, old: ['k'], new: [] },
|
|
];
|
|
// A new line typed at the end of line 0.
|
|
let out = h.afterChange(hunks, { startLine: 0, endLine: 0, newLineCount: 2 });
|
|
assert.deepEqual(out.map((k) => k.line), [3, 9, 11]);
|
|
// Two lines deleted between the first and the second hunk.
|
|
out = h.afterChange(hunks, { startLine: 5, endLine: 7, newLineCount: 1 });
|
|
assert.deepEqual(out.map((k) => [k.id, k.line]), [[1, 2], [2, 6], [3, 8]]);
|
|
// A character typed inside the first hunk's lines.
|
|
out = h.afterChange(hunks, { startLine: 3, endLine: 3, newLineCount: 1 });
|
|
assert.deepEqual(out.map((k) => k.id), [2, 3]);
|
|
// A change below everything.
|
|
out = h.afterChange(hunks, { startLine: 20, endLine: 20, newLineCount: 3 });
|
|
assert.deepEqual(out, hunks);
|
|
// A deletion that swallows the place an addition was meant for.
|
|
out = h.afterChange(hunks, { startLine: 7, endLine: 9, newLineCount: 1 });
|
|
assert.deepEqual(out.map((k) => [k.id, k.line]), [[1, 2], [3, 8]]);
|
|
});
|
|
|
|
test('the lines a selection covers', () => {
|
|
assert.deepEqual(h.wholeLines({ startLine: 3, endLine: 5, endCharacter: 4 }), { first: 3, last: 5 });
|
|
assert.deepEqual(h.wholeLines({ startLine: 3, endLine: 6, endCharacter: 0 }), { first: 3, last: 5 }, 'not the line it only ends in front of');
|
|
assert.deepEqual(h.wholeLines({ startLine: 3, endLine: 3, endCharacter: 0 }), { first: 3, last: 3 });
|
|
});
|
|
|
|
test('a text is split into lines whatever its line breaks', () => {
|
|
assert.deepEqual(h.splitLines('a\r\nb\nc'), ['a', 'b', 'c']);
|
|
assert.deepEqual(h.splitLines('a\n'), ['a', '']);
|
|
});
|