@plannotator/sem-jiff 0.0.0-stage → 0.2.2
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 +77 -0
- package/LICENSE-APACHE +191 -0
- package/LICENSE-MIT +21 -0
- package/README.md +122 -2
- package/THIRD-PARTY-NOTICES +367 -0
- package/dist/candidate.d.ts +14 -0
- package/dist/candidate.js +1 -0
- package/dist/classifier.d.ts +25 -0
- package/dist/classifier.js +32 -0
- package/dist/copy-wasm-bin.d.ts +2 -0
- package/dist/copy-wasm-bin.js +11 -0
- package/dist/copy-wasm.d.ts +3 -0
- package/dist/copy-wasm.js +43 -0
- package/dist/coverage.d.ts +7 -0
- package/dist/coverage.js +43 -0
- package/dist/edits.d.ts +10 -0
- package/dist/edits.js +230 -0
- package/dist/extract.d.ts +5 -0
- package/dist/extract.js +24 -0
- package/dist/grammars.d.ts +22 -0
- package/dist/grammars.js +37 -0
- package/dist/index.d.ts +79 -0
- package/dist/index.js +581 -0
- package/dist/inline.d.ts +21 -0
- package/dist/inline.js +959 -0
- package/dist/languages.d.ts +2 -0
- package/dist/languages.js +28 -0
- package/dist/linediff.d.ts +28 -0
- package/dist/linediff.js +205 -0
- package/dist/opinions.d.ts +84 -0
- package/dist/opinions.js +201 -0
- package/dist/pairing.d.ts +10 -0
- package/dist/pairing.js +269 -0
- package/dist/parse.d.ts +119 -0
- package/dist/parse.js +1293 -0
- package/dist/pierre-types.d.ts +101 -0
- package/dist/pierre-types.js +5 -0
- package/dist/runtime.d.ts +48 -0
- package/dist/runtime.js +60 -0
- package/dist/structure.d.ts +23 -0
- package/dist/structure.js +1111 -0
- package/dist/summary.d.ts +19 -0
- package/dist/summary.js +15 -0
- package/dist/symbol-types.d.ts +18 -0
- package/dist/symbol-types.js +1 -0
- package/dist/symbols.d.ts +6 -0
- package/dist/symbols.js +458 -0
- package/dist/syntax.d.ts +69 -0
- package/dist/syntax.js +450 -0
- package/dist/textdiff.d.ts +15 -0
- package/dist/textdiff.js +64 -0
- package/dist/textmoves.d.ts +8 -0
- package/dist/textmoves.js +143 -0
- package/package.json +108 -4
- package/src/candidate.ts +10 -0
- package/src/grammars.ts +44 -0
- package/src/opinions.ts +271 -0
- package/wasm/SHA256SUMS +12 -0
- package/wasm/tree-sitter-c_sharp.wasm +0 -0
- package/wasm/tree-sitter-go.wasm +0 -0
- package/wasm/tree-sitter-java.wasm +0 -0
- package/wasm/tree-sitter-kotlin.wasm +0 -0
- package/wasm/tree-sitter-php.wasm +0 -0
- package/wasm/tree-sitter-python.wasm +0 -0
- package/wasm/tree-sitter-ruby.wasm +0 -0
- package/wasm/tree-sitter-rust.wasm +0 -0
- package/wasm/tree-sitter-swift.wasm +0 -0
- package/wasm/tree-sitter-tsx.wasm +0 -0
- package/wasm/tree-sitter-typescript.wasm +0 -0
- package/wasm/web-tree-sitter.wasm +0 -0
package/dist/edits.js
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { range } from "./syntax.js";
|
|
2
|
+
import { computeTextDiff, lastWork, positions, WORK_PER_SECOND } from "./textdiff.js";
|
|
3
|
+
/** Lines a character range touches; an empty range or one ending at column 0 does not take the next line. */
|
|
4
|
+
function linesOf(start, end, empty) {
|
|
5
|
+
if (empty)
|
|
6
|
+
return null;
|
|
7
|
+
const last = end.character === 0 && end.line > start.line ? end.line - 1 : end.line;
|
|
8
|
+
return { start: start.line, end: last };
|
|
9
|
+
}
|
|
10
|
+
const lineStart = (line) => ({ line, character: 0 });
|
|
11
|
+
const wordCache = new Map();
|
|
12
|
+
const words = (line) => {
|
|
13
|
+
let w = wordCache.get(line);
|
|
14
|
+
if (!w) {
|
|
15
|
+
if (wordCache.size > 50_000)
|
|
16
|
+
wordCache.clear();
|
|
17
|
+
w = line.match(/\w+|\S/g) ?? [];
|
|
18
|
+
wordCache.set(line, w);
|
|
19
|
+
}
|
|
20
|
+
return w;
|
|
21
|
+
};
|
|
22
|
+
function similar(a, b) {
|
|
23
|
+
const x = words(a);
|
|
24
|
+
const y = words(b);
|
|
25
|
+
if (!x.length || !y.length)
|
|
26
|
+
return x.length === y.length;
|
|
27
|
+
const counts = new Map();
|
|
28
|
+
for (const t of x)
|
|
29
|
+
counts.set(t, (counts.get(t) ?? 0) + 1);
|
|
30
|
+
let shared = 0;
|
|
31
|
+
for (const t of y) {
|
|
32
|
+
const c = counts.get(t);
|
|
33
|
+
if (c) {
|
|
34
|
+
shared++;
|
|
35
|
+
counts.set(t, c - 1);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return (2 * shared) / (x.length + y.length) >= 0.5;
|
|
39
|
+
}
|
|
40
|
+
/** One line edited in place: the span between their common prefix and suffix. */
|
|
41
|
+
function inLine(oldLine, newLine, oldLines, newLines) {
|
|
42
|
+
const a = oldLines[oldLine - 1] ?? '';
|
|
43
|
+
const b = newLines[newLine - 1] ?? '';
|
|
44
|
+
if (a === b)
|
|
45
|
+
return null;
|
|
46
|
+
let p = 0;
|
|
47
|
+
while (p < a.length && p < b.length && a[p] === b[p])
|
|
48
|
+
p++;
|
|
49
|
+
let q = 0;
|
|
50
|
+
while (q < a.length - p && q < b.length - p && a[a.length - 1 - q] === b[b.length - 1 - q])
|
|
51
|
+
q++;
|
|
52
|
+
return {
|
|
53
|
+
old: { start: { line: oldLine, character: p }, end: { line: oldLine, character: a.length - q }, empty: a.length - q === p },
|
|
54
|
+
new: { start: { line: newLine, character: p }, end: { line: newLine, character: b.length - q }, empty: b.length - q === p },
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* VS Code's diff can express "edit this line, delete the next five" as one
|
|
59
|
+
* replacement. Re-align the lines it touches: similar lines pair up as in-line
|
|
60
|
+
* edits, the rest become whole-line deletions and insertions.
|
|
61
|
+
*/
|
|
62
|
+
function splitByLines(p, oldLines, newLines) {
|
|
63
|
+
const o = { start: p.old.start.line, end: p.old.end.character === 0 && p.old.end.line > p.old.start.line ? p.old.end.line - 1 : p.old.end.line };
|
|
64
|
+
const n = { start: p.new.start.line, end: p.new.end.character === 0 && p.new.end.line > p.new.start.line ? p.new.end.line - 1 : p.new.end.line };
|
|
65
|
+
const oc = o.end - o.start + 1;
|
|
66
|
+
const nc = n.end - n.start + 1;
|
|
67
|
+
const wholeLines = p.old.start.character === 0 && p.new.start.character === 0 && p.old.end.character === 0 && p.new.end.character === 0;
|
|
68
|
+
if ((oc <= 1 && nc <= 1) || (wholeLines && (p.old.empty || p.new.empty)) || oc * nc > 40_000)
|
|
69
|
+
return [p];
|
|
70
|
+
// Longest common subsequence of similar lines.
|
|
71
|
+
const dp = Array.from({ length: oc + 1 }, () => new Array(nc + 1).fill(0));
|
|
72
|
+
const sim = Array.from({ length: oc }, (_, i) => Array.from({ length: nc }, (_, j) => similar(oldLines[o.start - 1 + i] ?? '', newLines[n.start - 1 + j] ?? '')));
|
|
73
|
+
for (let i = oc - 1; i >= 0; i--)
|
|
74
|
+
for (let j = nc - 1; j >= 0; j--)
|
|
75
|
+
dp[i][j] = sim[i][j] ? dp[i + 1][j + 1] + 1 : Math.max(dp[i + 1][j], dp[i][j + 1]);
|
|
76
|
+
const out = [];
|
|
77
|
+
let i = 0;
|
|
78
|
+
let j = 0;
|
|
79
|
+
const del = (line, at) => ({
|
|
80
|
+
old: { start: lineStart(line), end: lineStart(line + 1), empty: false },
|
|
81
|
+
new: { start: lineStart(at), end: lineStart(at), empty: true },
|
|
82
|
+
});
|
|
83
|
+
const ins = (line, at) => ({
|
|
84
|
+
old: { start: lineStart(at), end: lineStart(at), empty: true },
|
|
85
|
+
new: { start: lineStart(line), end: lineStart(line + 1), empty: false },
|
|
86
|
+
});
|
|
87
|
+
while (i < oc || j < nc) {
|
|
88
|
+
if (i < oc && j < nc && sim[i][j] && dp[i][j] === dp[i + 1][j + 1] + 1) {
|
|
89
|
+
const e = inLine(o.start + i, n.start + j, oldLines, newLines);
|
|
90
|
+
if (e)
|
|
91
|
+
out.push(e);
|
|
92
|
+
i++;
|
|
93
|
+
j++;
|
|
94
|
+
}
|
|
95
|
+
else if (i < oc && (j >= nc || dp[i + 1][j] >= dp[i][j + 1])) {
|
|
96
|
+
out.push(del(o.start + i, n.start + j));
|
|
97
|
+
i++;
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
out.push(ins(n.start + j, o.start + i));
|
|
101
|
+
j++;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return out;
|
|
105
|
+
}
|
|
106
|
+
export function detectEdits(replacements, oldText, newText) {
|
|
107
|
+
const at = { old: positions(oldText), new: positions(newText) };
|
|
108
|
+
const newLineCount = newText.split('\n').length - (newText.endsWith('\n') ? 1 : 0);
|
|
109
|
+
const oldLineCount = oldText.split('\n').length - (oldText.endsWith('\n') ? 1 : 0);
|
|
110
|
+
// Map each replacement to both files, splitting multi-line ones into lines.
|
|
111
|
+
const oldLines = oldText.split('\n');
|
|
112
|
+
const newLines = newText.split('\n');
|
|
113
|
+
const pieces = [];
|
|
114
|
+
let delta = 0;
|
|
115
|
+
for (const r of replacements) {
|
|
116
|
+
const ns = r.start + delta;
|
|
117
|
+
const ne = ns + r.newText.length;
|
|
118
|
+
delta += r.newText.length - (r.end - r.start);
|
|
119
|
+
const piece = {
|
|
120
|
+
old: { start: at.old(r.start), end: at.old(r.end), empty: r.end === r.start },
|
|
121
|
+
new: { start: at.new(ns), end: at.new(ne), empty: r.newText.length === 0 },
|
|
122
|
+
};
|
|
123
|
+
pieces.push(...splitByLines(piece, oldLines, newLines));
|
|
124
|
+
}
|
|
125
|
+
// Group pieces whose lines touch on either side into blocks.
|
|
126
|
+
const blocks = [];
|
|
127
|
+
const join = (a, b) => !a ? b : !b ? a : { start: Math.min(a.start, b.start), end: Math.max(a.end, b.end) };
|
|
128
|
+
const touches = (a, b) => !!a && !!b && b.start <= a.end;
|
|
129
|
+
for (const p of pieces) {
|
|
130
|
+
// An edit inside a line (not a whole-line insertion or deletion) touches that line on both sides.
|
|
131
|
+
const o = linesOf(p.old.start, p.old.end, p.old.empty) ?? (p.old.start.character > 0 || !p.new.empty && p.new.end.character > 0 ? { start: p.old.start.line, end: p.old.start.line } : null);
|
|
132
|
+
const n = linesOf(p.new.start, p.new.end, p.new.empty) ?? (p.new.start.character > 0 || !p.old.empty && p.old.end.character > 0 ? { start: p.new.start.line, end: p.new.start.line } : null);
|
|
133
|
+
// Whole lines inserted or deleted stay their own block, so they read as added or
|
|
134
|
+
// deleted rather than being absorbed into a neighbouring in-line edit.
|
|
135
|
+
const wholeLines = (p.old.empty || p.new.empty) && p.old.start.character === 0 && p.new.start.character === 0 && p.old.end.character === 0 && p.new.end.character === 0;
|
|
136
|
+
const last = blocks.at(-1);
|
|
137
|
+
const lastWhole = last?.whole ?? false;
|
|
138
|
+
if (last && lastWhole === wholeLines && (touches(last.old, o) || touches(last.new, n))) {
|
|
139
|
+
last.pieces.push(p);
|
|
140
|
+
last.old = join(last.old, o);
|
|
141
|
+
last.new = join(last.new, n);
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
blocks.push({ pieces: [p], old: o, new: n, whole: wholeLines });
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
const ops = [];
|
|
148
|
+
for (const b of blocks) {
|
|
149
|
+
const first = b.pieces[0];
|
|
150
|
+
if (b.old && b.new) {
|
|
151
|
+
const spans = [
|
|
152
|
+
{
|
|
153
|
+
role: 'root',
|
|
154
|
+
relation: 'mapped',
|
|
155
|
+
old: [range(lineStart(b.old.start), lineStart(b.old.end + 1))],
|
|
156
|
+
new: [range(lineStart(b.new.start), lineStart(b.new.end + 1))],
|
|
157
|
+
linePairs: [{ old: b.old, new: b.new }],
|
|
158
|
+
},
|
|
159
|
+
];
|
|
160
|
+
for (const p of b.pieces) {
|
|
161
|
+
if (p.old.empty)
|
|
162
|
+
spans.push({ role: 'focus', relation: 'introduced', new: [range(p.new.start, p.new.end)] });
|
|
163
|
+
else if (p.new.empty)
|
|
164
|
+
spans.push({ role: 'focus', relation: 'removed', old: [range(p.old.start, p.old.end)] });
|
|
165
|
+
else
|
|
166
|
+
spans.push({ role: 'focus', relation: 'replaced', old: [range(p.old.start, p.old.end)], new: [range(p.new.start, p.new.end)], linePairs: [] });
|
|
167
|
+
}
|
|
168
|
+
ops.push({ type: 'edited', spans, lines: { old: [{ ...b.old, spanIndex: 0 }], new: [{ ...b.new, spanIndex: 0 }] } });
|
|
169
|
+
}
|
|
170
|
+
else if (b.old) {
|
|
171
|
+
// Deleted lines connect to where they would sit in the new file.
|
|
172
|
+
const at = first.new.start.line;
|
|
173
|
+
const attachment = newLineCount === 0 ? { empty: true } : at <= newLineCount ? { line: at, edge: 'before' } : { line: newLineCount, edge: 'after' };
|
|
174
|
+
ops.push({
|
|
175
|
+
type: 'deleted',
|
|
176
|
+
spans: [{ role: 'root', relation: 'removed', old: [range(lineStart(b.old.start), lineStart(b.old.end + 1))] }],
|
|
177
|
+
lines: { old: [{ ...b.old, spanIndex: 0, attachment }], new: [] },
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
else if (b.new) {
|
|
181
|
+
const at = first.old.start.line;
|
|
182
|
+
const attachment = oldLineCount === 0 ? { empty: true } : at <= oldLineCount ? { line: at, edge: 'before' } : { line: oldLineCount, edge: 'after' };
|
|
183
|
+
ops.push({
|
|
184
|
+
type: 'added',
|
|
185
|
+
spans: [{ role: 'root', relation: 'introduced', new: [range(lineStart(b.new.start), lineStart(b.new.end + 1))] }],
|
|
186
|
+
lines: { old: [], new: [{ ...b.new, spanIndex: 0, attachment }] },
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return ops;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Character-level replacements for the changed blocks of a line diff: blocks
|
|
194
|
+
* changed on both sides are refined with VS Code's diff; one-sided blocks
|
|
195
|
+
* become whole-line insertions or deletions.
|
|
196
|
+
*/
|
|
197
|
+
/** Total work for character-level diffs of changed blocks in one file, and the most for one block. */
|
|
198
|
+
const EDIT_BUDGET = 0.6 * WORK_PER_SECOND;
|
|
199
|
+
const BLOCK_BUDGET = 0.25 * WORK_PER_SECOND;
|
|
200
|
+
export function replacementsFromBlocks(blocks, oldText, newText) {
|
|
201
|
+
const lineOffsets = (text) => {
|
|
202
|
+
const starts = [0];
|
|
203
|
+
for (let i = 0; i < text.length; i++)
|
|
204
|
+
if (text.charCodeAt(i) === 10)
|
|
205
|
+
starts.push(i + 1);
|
|
206
|
+
starts.push(text.length);
|
|
207
|
+
return (line) => starts[Math.min(line - 1, starts.length - 1)];
|
|
208
|
+
};
|
|
209
|
+
const o = lineOffsets(oldText);
|
|
210
|
+
const n = lineOffsets(newText);
|
|
211
|
+
const out = [];
|
|
212
|
+
// One work budget for the whole file (work, not time, so the result is repeatable): past it, changed blocks are replaced whole
|
|
213
|
+
// (still correct, just less precise) instead of each getting its own character diff.
|
|
214
|
+
let left = EDIT_BUDGET;
|
|
215
|
+
for (const b of blocks) {
|
|
216
|
+
const os = b.old ? o(b.old.start) : o(b.oldAt);
|
|
217
|
+
const oe = b.old ? o(b.old.end + 1) : os;
|
|
218
|
+
const ns = b.new ? n(b.new.start) : n(b.newAt);
|
|
219
|
+
const ne = b.new ? n(b.new.end + 1) : ns;
|
|
220
|
+
if (!b.old || !b.new || left < BLOCK_BUDGET / 12) {
|
|
221
|
+
out.push({ start: os, end: oe, newText: newText.slice(ns, ne) });
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
const inner = computeTextDiff(oldText.slice(os, oe), newText.slice(ns, ne), Math.min(BLOCK_BUDGET, left));
|
|
225
|
+
left -= lastWork;
|
|
226
|
+
for (const r of inner.edits.replacements)
|
|
227
|
+
out.push({ start: os + r.range.start, end: os + r.range.endExclusive, newText: r.newText });
|
|
228
|
+
}
|
|
229
|
+
return out;
|
|
230
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Node } from 'web-tree-sitter';
|
|
2
|
+
import { type InlineOptions } from './inline.js';
|
|
3
|
+
import type { LanguageConfig } from './parse.js';
|
|
4
|
+
import type { SemanticDiffOperation } from './pierre-types.js';
|
|
5
|
+
export declare function detectExtracted(oldRoot: Node, newRoot: Node, newText: string, cfg: LanguageConfig, options?: InlineOptions): SemanticDiffOperation[];
|
package/dist/extract.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { detectInlined, DEFAULT_INLINE_OPTIONS } from "./inline.js";
|
|
2
|
+
function mirrorSpan(s) {
|
|
3
|
+
// Role and kind are copied from a valid span, so their pairing (kind only on
|
|
4
|
+
// focus spans) still holds; TypeScript cannot follow that through the union.
|
|
5
|
+
const keep = { role: s.role, ...(s.kind ? { kind: s.kind } : {}) };
|
|
6
|
+
if (s.relation === 'removed')
|
|
7
|
+
return { ...keep, relation: 'introduced', new: s.old, ...(s.anchors ? { anchors: s.anchors } : {}) };
|
|
8
|
+
if (s.relation === 'introduced')
|
|
9
|
+
return { ...keep, relation: 'removed', old: s.new, ...(s.anchors ? { anchors: s.anchors } : {}) };
|
|
10
|
+
return {
|
|
11
|
+
...keep,
|
|
12
|
+
relation: s.relation,
|
|
13
|
+
old: s.new,
|
|
14
|
+
new: s.old,
|
|
15
|
+
linePairs: s.linePairs.map((p) => ({ old: p.new, new: p.old })),
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
export function detectExtracted(oldRoot, newRoot, newText, cfg, options = DEFAULT_INLINE_OPTIONS) {
|
|
19
|
+
return detectInlined(newRoot, oldRoot, newText, cfg, { ...options, mirrored: true }).map((op) => ({
|
|
20
|
+
type: 'extractedFunction',
|
|
21
|
+
spans: op.spans.map(mirrorSpan),
|
|
22
|
+
lines: { old: op.lines.new, new: op.lines.old },
|
|
23
|
+
}));
|
|
24
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** web-tree-sitter's own runtime. */
|
|
2
|
+
export declare const SEM_DIFF_RUNTIME_FILE = "web-tree-sitter.wasm";
|
|
3
|
+
/** The grammar file each language loads (keys are the engine's language ids). */
|
|
4
|
+
export declare const SEM_DIFF_GRAMMARS: {
|
|
5
|
+
readonly typescript: "tree-sitter-typescript.wasm";
|
|
6
|
+
readonly tsx: "tree-sitter-tsx.wasm";
|
|
7
|
+
readonly python: "tree-sitter-python.wasm";
|
|
8
|
+
readonly go: "tree-sitter-go.wasm";
|
|
9
|
+
readonly rust: "tree-sitter-rust.wasm";
|
|
10
|
+
readonly java: "tree-sitter-java.wasm";
|
|
11
|
+
readonly csharp: "tree-sitter-c_sharp.wasm";
|
|
12
|
+
readonly php: "tree-sitter-php.wasm";
|
|
13
|
+
readonly ruby: "tree-sitter-ruby.wasm";
|
|
14
|
+
readonly kotlin: "tree-sitter-kotlin.wasm";
|
|
15
|
+
readonly swift: "tree-sitter-swift.wasm";
|
|
16
|
+
};
|
|
17
|
+
/** Every grammar file, once each, in the order of SEM_DIFF_GRAMMARS. */
|
|
18
|
+
export declare const SEM_DIFF_GRAMMAR_FILES: readonly ["tree-sitter-typescript.wasm", "tree-sitter-tsx.wasm", "tree-sitter-python.wasm", "tree-sitter-go.wasm", "tree-sitter-rust.wasm", "tree-sitter-java.wasm", "tree-sitter-c_sharp.wasm", "tree-sitter-php.wasm", "tree-sitter-ruby.wasm", "tree-sitter-kotlin.wasm", "tree-sitter-swift.wasm"];
|
|
19
|
+
/** Everything in the package's `wasm/` folder: the runtime first, then the grammars. */
|
|
20
|
+
export declare const SEM_DIFF_WASM_FILES: readonly ["web-tree-sitter.wasm", "tree-sitter-typescript.wasm", "tree-sitter-tsx.wasm", "tree-sitter-python.wasm", "tree-sitter-go.wasm", "tree-sitter-rust.wasm", "tree-sitter-java.wasm", "tree-sitter-c_sharp.wasm", "tree-sitter-php.wasm", "tree-sitter-ruby.wasm", "tree-sitter-kotlin.wasm", "tree-sitter-swift.wasm"];
|
|
21
|
+
/** One of the wasm files the package ships. */
|
|
22
|
+
export type SemDiffWasmFile = (typeof SEM_DIFF_WASM_FILES)[number];
|
package/dist/grammars.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// The wasm files the engine loads, by the names a host serves them under. The package ships them in
|
|
2
|
+
// its `wasm/` folder (`@plannotator/sem-jiff/wasm/<file>`); a host that runs the engine in a page or a
|
|
3
|
+
// compiled binary copies them from there (`copyWasm` in `@plannotator/sem-jiff/copy-wasm`, or the
|
|
4
|
+
// `sem-jiff-copy-wasm` bin) and points `configure({ locateGrammar, locateRuntime })` at them.
|
|
5
|
+
// This file imports nothing, so a host can vendor it as source (it ships as `src/grammars.ts`).
|
|
6
|
+
/** web-tree-sitter's own runtime. */
|
|
7
|
+
export const SEM_DIFF_RUNTIME_FILE = 'web-tree-sitter.wasm';
|
|
8
|
+
/** The grammar file each language loads (keys are the engine's language ids). */
|
|
9
|
+
export const SEM_DIFF_GRAMMARS = {
|
|
10
|
+
typescript: 'tree-sitter-typescript.wasm',
|
|
11
|
+
tsx: 'tree-sitter-tsx.wasm',
|
|
12
|
+
python: 'tree-sitter-python.wasm',
|
|
13
|
+
go: 'tree-sitter-go.wasm',
|
|
14
|
+
rust: 'tree-sitter-rust.wasm',
|
|
15
|
+
java: 'tree-sitter-java.wasm',
|
|
16
|
+
csharp: 'tree-sitter-c_sharp.wasm',
|
|
17
|
+
php: 'tree-sitter-php.wasm',
|
|
18
|
+
ruby: 'tree-sitter-ruby.wasm',
|
|
19
|
+
kotlin: 'tree-sitter-kotlin.wasm',
|
|
20
|
+
swift: 'tree-sitter-swift.wasm',
|
|
21
|
+
};
|
|
22
|
+
/** Every grammar file, once each, in the order of SEM_DIFF_GRAMMARS. */
|
|
23
|
+
export const SEM_DIFF_GRAMMAR_FILES = [
|
|
24
|
+
'tree-sitter-typescript.wasm',
|
|
25
|
+
'tree-sitter-tsx.wasm',
|
|
26
|
+
'tree-sitter-python.wasm',
|
|
27
|
+
'tree-sitter-go.wasm',
|
|
28
|
+
'tree-sitter-rust.wasm',
|
|
29
|
+
'tree-sitter-java.wasm',
|
|
30
|
+
'tree-sitter-c_sharp.wasm',
|
|
31
|
+
'tree-sitter-php.wasm',
|
|
32
|
+
'tree-sitter-ruby.wasm',
|
|
33
|
+
'tree-sitter-kotlin.wasm',
|
|
34
|
+
'tree-sitter-swift.wasm',
|
|
35
|
+
];
|
|
36
|
+
/** Everything in the package's `wasm/` folder: the runtime first, then the grammars. */
|
|
37
|
+
export const SEM_DIFF_WASM_FILES = [SEM_DIFF_RUNTIME_FILE, ...SEM_DIFF_GRAMMAR_FILES];
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { LanguageId } from './languages.js';
|
|
2
|
+
import type { SemanticDiffFold } from './pierre-types.js';
|
|
3
|
+
import type { SemDiffOperation } from './symbol-types.js';
|
|
4
|
+
export type { SemanticDiffFold, SemanticDiffOperation } from './pierre-types.js';
|
|
5
|
+
export { languageForPath, type LanguageId } from './languages.js';
|
|
6
|
+
export type { OperationSymbol, SemDiffOperation, SymbolKind } from './symbol-types.js';
|
|
7
|
+
export { configure, type Candidate, type SemDiffSetup, type WasmSource } from './runtime.js';
|
|
8
|
+
export { reviewSummary, READ_KINDS, SKIM_KINDS, type ReviewSummary } from './summary.js';
|
|
9
|
+
import type { Candidate } from './runtime.js';
|
|
10
|
+
import { type OpinionAnswer, type OpinionBand } from './opinions.js';
|
|
11
|
+
export interface SemanticFileDiff {
|
|
12
|
+
/** Pierre's operations, each with the optional `symbol` it is about (docs/symbols.md). */
|
|
13
|
+
semanticOperations: SemDiffOperation[];
|
|
14
|
+
semanticFolds: SemanticDiffFold[];
|
|
15
|
+
/**
|
|
16
|
+
* Unified diff built from the declaration-aware line diff. Give it to your
|
|
17
|
+
* diff viewer so its layout matches the operations; a viewer's own line diff
|
|
18
|
+
* can pair lines across functions.
|
|
19
|
+
*/
|
|
20
|
+
patch: string;
|
|
21
|
+
/** What each semantic pass did: how long it took, and whether it ran, failed or was skipped. */
|
|
22
|
+
diagnostics: PassReport[];
|
|
23
|
+
/** Present when a second opinion was asked for: what it was asked, what it added, and how it went. */
|
|
24
|
+
secondOpinion?: SecondOpinionReport;
|
|
25
|
+
}
|
|
26
|
+
export type SecondOpinionState = 'ok' | 'failed' | 'refusedKey' | 'noCredit';
|
|
27
|
+
/** One link the model made: an extract or inline the rules alone rejected. */
|
|
28
|
+
export interface SecondOpinionLink {
|
|
29
|
+
operationIndex: number;
|
|
30
|
+
candidateId: string;
|
|
31
|
+
kind: 'extract' | 'inline';
|
|
32
|
+
helper: string;
|
|
33
|
+
caller: string;
|
|
34
|
+
band?: OpinionBand;
|
|
35
|
+
score: number;
|
|
36
|
+
model: string;
|
|
37
|
+
question: string;
|
|
38
|
+
answer: string;
|
|
39
|
+
}
|
|
40
|
+
export interface SecondOpinionReport {
|
|
41
|
+
state: SecondOpinionState;
|
|
42
|
+
asked: number;
|
|
43
|
+
answered: number;
|
|
44
|
+
accepted: number;
|
|
45
|
+
links: SecondOpinionLink[];
|
|
46
|
+
reason?: string;
|
|
47
|
+
}
|
|
48
|
+
export interface PassReport {
|
|
49
|
+
pass: 'inline-extract' | 'second-opinion' | 'structure' | 'text-moves';
|
|
50
|
+
ms: number;
|
|
51
|
+
status: 'ok' | 'failed' | 'skipped';
|
|
52
|
+
reason?: string;
|
|
53
|
+
}
|
|
54
|
+
export interface DiffOptions {
|
|
55
|
+
/** Above this many characters (old + new), semantic passes are skipped: a plain diff only. Default 2,000,000. */
|
|
56
|
+
maxChars?: number;
|
|
57
|
+
/**
|
|
58
|
+
* Once this many milliseconds have gone by, remaining semantic passes are skipped. Off by
|
|
59
|
+
* default: timing depends on the machine, so a budget makes the output vary between runs.
|
|
60
|
+
*/
|
|
61
|
+
timeBudgetMs?: number;
|
|
62
|
+
/**
|
|
63
|
+
* A second opinion on the extract/inline candidates the rules reject (bring your own model,
|
|
64
|
+
* e.g. `jevOpinion({ apiKey })` from `@plannotator/sem-jiff/opinions`). The engine collects
|
|
65
|
+
* them, calls this once per file with all of them, and accepts those answered `true`. Never
|
|
66
|
+
* asked about code still present in the caller. If it throws, the diff goes ahead without it.
|
|
67
|
+
*/
|
|
68
|
+
secondOpinion?: (candidates: Candidate[]) => Promise<Array<boolean | OpinionAnswer>>;
|
|
69
|
+
/** Candidate ids the reader turned down ("Not moved"): never asked, never accepted. */
|
|
70
|
+
rejectedCandidates?: Iterable<string>;
|
|
71
|
+
}
|
|
72
|
+
export type Sides = {
|
|
73
|
+
old: Set<number>;
|
|
74
|
+
new: Set<number>;
|
|
75
|
+
};
|
|
76
|
+
/** A stable id for a candidate: the same code always gets the same id. */
|
|
77
|
+
export declare function candidateId(c: Candidate): string;
|
|
78
|
+
export declare function semanticDiffFile(language: LanguageId, oldText: string, newText: string, name?: string, options?: DiffOptions): Promise<SemanticFileDiff>;
|
|
79
|
+
export declare function semanticDiff(language: LanguageId, oldText: string, newText: string): Promise<SemDiffOperation[]>;
|