{"file":"src/engine/build.ts","version":2,"draft":"login-timeout","total":520,"from":1,"to":520,"lines":[{"n":1,"text":"// Building an atom from an edit against a view. Agents say \"replace this exact","atom":"a0","author":"init","version":0},{"n":2,"text":"// text with that text\" (or \"replace these visible lines with these lines\");","atom":"a0","author":"init","version":0},{"n":3,"text":"// the engine turns it into deletes, inserts, claim keys and derived deps.","atom":"a0","author":"init","version":0},{"n":4,"text":"","atom":"a0","author":"init","version":0},{"n":5,"text":"import { Graph } from \"./graph.ts\";","atom":"a0","author":"init","version":0},{"n":6,"text":"import { attrKey, gapKey, isStartNode, lineKey, parseKey, posKey, startNode, writerOf } from \"./keys.ts\";","atom":"a0","author":"init","version":0},{"n":7,"text":"import { diffLines, joinLines, normalizeText, splitLines } from \"./text.ts\";","atom":"a0","author":"init","version":0},{"n":8,"text":"import {","atom":"a0","author":"init","version":0},{"n":9,"text":"\tATTR_NAMES,","atom":"a0","author":"init","version":0},{"n":10,"text":"\ttype Atom,","atom":"a0","author":"init","version":0},{"n":11,"text":"\ttype AtomId,","atom":"a0","author":"init","version":0},{"n":12,"text":"\ttype AttrSet,","atom":"a0","author":"init","version":0},{"n":13,"text":"\tEngineError,","atom":"a0","author":"init","version":0},{"n":14,"text":"\ttype InsertRun,","atom":"a0","author":"init","version":0},{"n":15,"text":"\ttype Key,","atom":"a0","author":"init","version":0},{"n":16,"text":"\ttype LineId,","atom":"a0","author":"init","version":0},{"n":17,"text":"\tMODES,","atom":"a0","author":"init","version":0},{"n":18,"text":"\ttype Mode,","atom":"a0","author":"init","version":0},{"n":19,"text":"\ttype NodeId,","atom":"a0","author":"init","version":0},{"n":20,"text":"\tREASON_KINDS,","atom":"a0","author":"init","version":0},{"n":21,"text":"\ttype Reason,","atom":"a0","author":"init","version":0},{"n":22,"text":"\ttype Relocation,","atom":"a0","author":"init","version":0},{"n":23,"text":"} from \"./types.ts\";","atom":"a0","author":"init","version":0},{"n":24,"text":"import { View } from \"./view.ts\";","atom":"a0","author":"init","version":0},{"n":25,"text":"","atom":"a0","author":"init","version":0},{"n":26,"text":"/** Replace the one exact occurrence of `old` in `file` with `new`. */","atom":"a0","author":"init","version":0},{"n":27,"text":"export interface TextEdit {","atom":"a0","author":"init","version":0},{"n":28,"text":"\treadonly file: string;","atom":"a0","author":"init","version":0},{"n":29,"text":"\treadonly old: string;","atom":"a0","author":"init","version":0},{"n":30,"text":"\treadonly new: string;","atom":"a0","author":"init","version":0},{"n":31,"text":"\treadonly mode?: Mode;","atom":"a0","author":"init","version":0},{"n":32,"text":"}","atom":"a0","author":"init","version":0},{"n":33,"text":"","atom":"a0","author":"init","version":0},{"n":34,"text":"/**","atom":"a0","author":"init","version":0},{"n":35,"text":" * Replace visible lines [start, end) of `file` (0-based) with `lines`. With","atom":"a0","author":"init","version":0},{"n":36,"text":" * start === end it is a pure insertion before line `start`. This is the form","atom":"a0","author":"init","version":0},{"n":37,"text":" * a git hunk maps onto.","atom":"a0","author":"init","version":0},{"n":38,"text":" */","atom":"a0","author":"init","version":0},{"n":39,"text":"export interface SpliceEdit {","atom":"a0","author":"init","version":0},{"n":40,"text":"\treadonly file: string;","atom":"a0","author":"init","version":0},{"n":41,"text":"\treadonly start: number;","atom":"a0","author":"init","version":0},{"n":42,"text":"\treadonly end: number;","atom":"a0","author":"init","version":0},{"n":43,"text":"\treadonly lines: readonly string[];","atom":"a0","author":"init","version":0},{"n":44,"text":"\treadonly mode?: Mode;","atom":"a0","author":"init","version":0},{"n":45,"text":"}","atom":"a0","author":"init","version":0},{"n":46,"text":"","atom":"a0","author":"init","version":0},{"n":47,"text":"export type EditInput = TextEdit | SpliceEdit;","atom":"a0","author":"init","version":0},{"n":48,"text":"","atom":"a0","author":"init","version":0},{"n":49,"text":"/** Visible lines [start, end) of a file, 0-based, end exclusive, non-empty. */","atom":"a0","author":"init","version":0},{"n":50,"text":"export interface LineRange {","atom":"a0","author":"init","version":0},{"n":51,"text":"\treadonly file: string;","atom":"a0","author":"init","version":0},{"n":52,"text":"\treadonly start: number;","atom":"a0","author":"init","version":0},{"n":53,"text":"\treadonly end: number;","atom":"a0","author":"init","version":0},{"n":54,"text":"}","atom":"a0","author":"init","version":0},{"n":55,"text":"","atom":"a0","author":"init","version":0},{"n":56,"text":"/**","atom":"a0","author":"init","version":0},{"n":57,"text":" * Move visible lines [start, end) of `file` (0-based, non-empty) so they sit","atom":"a0","author":"init","version":0},{"n":58,"text":" * just before visible line `at` of `to` (at === its line count: the end).","atom":"a0","author":"init","version":0},{"n":59,"text":" * `to` may be the same file, with `at` outside [start, end]. The lines keep","atom":"a0","author":"init","version":0},{"n":60,"text":" * their ids, so edits to them, made before or after, follow them (#28).","atom":"a0","author":"init","version":0},{"n":61,"text":" */","atom":"a0","author":"init","version":0},{"n":62,"text":"export interface MoveInput {","atom":"a0","author":"init","version":0},{"n":63,"text":"\treadonly file: string;","atom":"a0","author":"init","version":0},{"n":64,"text":"\treadonly start: number;","atom":"a0","author":"init","version":0},{"n":65,"text":"\treadonly end: number;","atom":"a0","author":"init","version":0},{"n":66,"text":"\treadonly to: string;","atom":"a0","author":"init","version":0},{"n":67,"text":"\treadonly at: number;","atom":"a0","author":"init","version":0},{"n":68,"text":"}","atom":"a0","author":"init","version":0},{"n":69,"text":"","atom":"a0","author":"init","version":0},{"n":70,"text":"export interface AtomInput {","atom":"a0","author":"init","version":0},{"n":71,"text":"\treadonly author: string;","atom":"a0","author":"init","version":0},{"n":72,"text":"\t/** The why. */","atom":"a0","author":"init","version":0},{"n":73,"text":"\treadonly message: string;","atom":"a0","author":"init","version":0},{"n":74,"text":"\t/** Milliseconds since the epoch. Defaults to 0; the engine never reads a clock. */","atom":"a0","author":"init","version":0},{"n":75,"text":"\treadonly time?: number;","atom":"a0","author":"init","version":0},{"n":76,"text":"\t/** Edits, all matched against the same view; their line spans may not overlap. */","atom":"a0","author":"init","version":0},{"n":77,"text":"\treadonly edits?: readonly EditInput[];","atom":"a0","author":"init","version":0},{"n":78,"text":"\t/**","atom":"a0","author":"init","version":0},{"n":79,"text":"\t * \"My change relies on these lines staying as they are\": holds each line and","atom":"a0","author":"init","version":0},{"n":80,"text":"\t * the gaps between them, so an edit landing anywhere in the range trips it.","atom":"a0","author":"init","version":0},{"n":81,"text":"\t */","atom":"a0","author":"init","version":0},{"n":82,"text":"\treadonly reliesOn?: readonly LineRange[];","atom":"a0","author":"init","version":0},{"n":83,"text":"\t/** Claim spans: hold these lines and the gaps between them (what a claim-only atom is made of). */","atom":"a0","author":"init","version":0},{"n":84,"text":"\treadonly spans?: readonly LineRange[];","atom":"a0","author":"init","version":0},{"n":85,"text":"\t/** Explicit dependencies. */","atom":"a0","author":"init","version":0},{"n":86,"text":"\treadonly needs?: readonly AtomId[];","atom":"a0","author":"init","version":0},{"n":87,"text":"\t/** Atoms whose conflict this atom settles. Implies needs. */","atom":"a0","author":"init","version":0},{"n":88,"text":"\treadonly resolves?: readonly AtomId[];","atom":"a0","author":"init","version":0},{"n":89,"text":"\treadonly reasons?: readonly Reason[];","atom":"a0","author":"init","version":0},{"n":90,"text":"\t/** File attributes to set: an empty file's existence, the executable bit, a link target. */","atom":"a0","author":"init","version":0},{"n":91,"text":"\treadonly attrs?: readonly AttrSet[];","atom":"a0","author":"init","version":0},{"n":92,"text":"\t/** One block of lines to move. Not together with edits. */","atom":"a0","author":"init","version":0},{"n":93,"text":"\treadonly move?: MoveInput;","atom":"a0","author":"init","version":0},{"n":94,"text":"}","atom":"a0","author":"init","version":0},{"n":95,"text":"","atom":"a0","author":"init","version":0},{"n":96,"text":"/** An edit after matching: visible lines [start, end) of `file` become `lines`. */","atom":"a0","author":"init","version":0},{"n":97,"text":"interface Region {","atom":"a0","author":"init","version":0},{"n":98,"text":"\treadonly file: string;","atom":"a0","author":"init","version":0},{"n":99,"text":"\treadonly start: number;","atom":"a0","author":"init","version":0},{"n":100,"text":"\treadonly end: number;","atom":"a0","author":"init","version":0},{"n":101,"text":"\treadonly lines: readonly string[];","atom":"a0","author":"init","version":0},{"n":102,"text":"\treadonly mode: Mode;","atom":"a0","author":"init","version":0},{"n":103,"text":"\treadonly order: number;","atom":"a0","author":"init","version":0},{"n":104,"text":"}","atom":"a0","author":"init","version":0},{"n":105,"text":"","atom":"a0","author":"init","version":0},{"n":106,"text":"/**","atom":"a0","author":"init","version":0},{"n":107,"text":" * Build (but do not accept) an atom from edits against a view. The atom gets","atom":"a0","author":"init","version":0},{"n":108,"text":" * the graph's next id; accept it with `graph.add(atom)` and put it in a set.","atom":"a0","author":"init","version":0},{"n":109,"text":" * Its dependencies are derived from what it touched in the view (see deriveDeps).","atom":"a0","author":"init","version":0},{"n":110,"text":" */","atom":"a0","author":"init","version":0},{"n":111,"text":"export function buildAtom(view: View, input: AtomInput): Atom {","atom":"a0","author":"init","version":0},{"n":112,"text":"\treturn build(view, input, false);","atom":"a0","author":"init","version":0},{"n":113,"text":"}","atom":"a0","author":"init","version":0},{"n":114,"text":"","atom":"a0","author":"init","version":0},{"n":115,"text":"/** The starting snapshot: a0, inserting every line of every file. Empty files are skipped. */","atom":"a0","author":"init","version":0},{"n":116,"text":"export function buildBase(","atom":"a0","author":"init","version":0},{"n":117,"text":"\tfiles: Readonly<Record<string, string>>,","atom":"a0","author":"init","version":0},{"n":118,"text":"\tmeta: { author: string; message: string; time?: number },","atom":"a0","author":"init","version":0},{"n":119,"text":"\tattrs: readonly AttrSet[] = [],","atom":"a0","author":"init","version":0},{"n":120,"text":"): Atom {","atom":"a0","author":"init","version":0},{"n":121,"text":"\tconst view = new View(new Graph(), []);","atom":"a0","author":"init","version":0},{"n":122,"text":"\tconst edits: TextEdit[] = Object.keys(files)","atom":"a0","author":"init","version":0},{"n":123,"text":"\t\t.sort()","atom":"a0","author":"init","version":0},{"n":124,"text":"\t\t.filter((file) => files[file] !== \"\")","atom":"a0","author":"init","version":0},{"n":125,"text":"\t\t.map((file) => ({ file, old: \"\", new: files[file]! }));","atom":"a0","author":"init","version":0},{"n":126,"text":"\treturn build(view, { ...meta, edits, attrs }, true);","atom":"a0","author":"init","version":0},{"n":127,"text":"}","atom":"a0","author":"init","version":0},{"n":128,"text":"","atom":"a0","author":"init","version":0},{"n":129,"text":"function build(view: View, input: AtomInput, allowEmpty: boolean): Atom {","atom":"a0","author":"init","version":0},{"n":130,"text":"\tconst graph = view.graph;","atom":"a0","author":"init","version":0},{"n":131,"text":"\tconst id = graph.nextId();","atom":"a0","author":"init","version":0},{"n":132,"text":"\tif (typeof input.author !== \"string\" || typeof input.message !== \"string\") {","atom":"a0","author":"init","version":0},{"n":133,"text":"\t\tthrow new EngineError(\"bad-edit\", \"an atom needs an author and a message\");","atom":"a0","author":"init","version":0},{"n":134,"text":"\t}","atom":"a0","author":"init","version":0},{"n":135,"text":"\tconst time = input.time ?? 0;","atom":"a0","author":"init","version":0},{"n":136,"text":"\tif (!Number.isFinite(time)) throw new EngineError(\"bad-edit\", \"time must be a finite number\");","atom":"a0","author":"init","version":0},{"n":137,"text":"","atom":"a0","author":"init","version":0},{"n":138,"text":"\tconst regions = resolveRegions(view, input.edits ?? []);","atom":"a0","author":"init","version":0},{"n":139,"text":"\tif (input.move !== undefined && regions.length > 0)","atom":"a0","author":"init","version":0},{"n":140,"text":"\t\tthrow new EngineError(\"bad-edit\", \"a move goes in an atom of its own, without edits\");","atom":"a0","author":"init","version":0},{"n":141,"text":"","atom":"a0","author":"init","version":0},{"n":142,"text":"\tconst inserts: InsertRun[] = [];","atom":"a0","author":"init","version":0},{"n":143,"text":"\tconst deletes: LineId[] = [];","atom":"a0","author":"init","version":0},{"n":144,"text":"\tconst writes = new Set<Key>();","atom":"a0","author":"init","version":0},{"n":145,"text":"\tconst reads = new Set<Key>();","atom":"a0","author":"init","version":0},{"n":146,"text":"\tconst holds = new Set<Key>();","atom":"a0","author":"init","version":0},{"n":147,"text":"\tconst emptiedBy = new Set<AtomId>();","atom":"a0","author":"init","version":0},{"n":148,"text":"","atom":"a0","author":"init","version":0},{"n":149,"text":"\tfor (const region of regions) {","atom":"a0","author":"init","version":0},{"n":150,"text":"\t\tconst visible = view.lines(region.file);","atom":"a0","author":"init","version":0},{"n":151,"text":"\t\tconst oldLines = visible.slice(region.start, region.end).map((l) => l.text);","atom":"a0","author":"init","version":0},{"n":152,"text":"\t\tfor (const hunk of diffLines(oldLines, region.lines)) {","atom":"a0","author":"init","version":0},{"n":153,"text":"\t\t\tconst start = region.start + hunk.start;","atom":"a0","author":"init","version":0},{"n":154,"text":"\t\t\tconst end = region.start + hunk.end;","atom":"a0","author":"init","version":0},{"n":155,"text":"\t\t\tlet parent: NodeId;","atom":"a0","author":"init","version":0},{"n":156,"text":"\t\t\tif (end > start) {","atom":"a0","author":"init","version":0},{"n":157,"text":"\t\t\t\t// Replacement: delete the old lines; new lines hang off the last deleted one.","atom":"a0","author":"init","version":0},{"n":158,"text":"\t\t\t\tfor (let i = start; i < end; i++) {","atom":"a0","author":"init","version":0},{"n":159,"text":"\t\t\t\t\tconst line = visible[i]!.id;","atom":"a0","author":"init","version":0},{"n":160,"text":"\t\t\t\t\tdeletes.push(line);","atom":"a0","author":"init","version":0},{"n":161,"text":"\t\t\t\t\twrites.add(lineKey(line));","atom":"a0","author":"init","version":0},{"n":162,"text":"\t\t\t\t}","atom":"a0","author":"init","version":0},{"n":163,"text":"\t\t\t\tparent = visible[end - 1]!.id;","atom":"a0","author":"init","version":0},{"n":164,"text":"\t\t\t} else {","atom":"a0","author":"init","version":0},{"n":165,"text":"\t\t\t\t// Pure insertion between X (above, or the start node) and Y (below, or end of file).","atom":"a0","author":"init","version":0},{"n":166,"text":"\t\t\t\tconst above = start > 0 ? visible[start - 1]!.id : undefined;","atom":"a0","author":"init","version":0},{"n":167,"text":"\t\t\t\tconst below = start < visible.length ? visible[start]!.id : undefined;","atom":"a0","author":"init","version":0},{"n":168,"text":"\t\t\t\tparent = above ?? startNode(region.file);","atom":"a0","author":"init","version":0},{"n":169,"text":"\t\t\t\twrites.add(gapKey(parent));","atom":"a0","author":"init","version":0},{"n":170,"text":"\t\t\t\t// after reads only X and before only Y; at the edge of a file each","atom":"a0","author":"init","version":0},{"n":171,"text":"\t\t\t\t// reads the neighbour there is, so an insertion into a non-empty file","atom":"a0","author":"init","version":0},{"n":172,"text":"\t\t\t\t// always claims a line of it.","atom":"a0","author":"init","version":0},{"n":173,"text":"\t\t\t\tif (above !== undefined && (region.mode !== \"before\" || below === undefined)) reads.add(lineKey(above));","atom":"a0","author":"init","version":0},{"n":174,"text":"\t\t\t\tif (below !== undefined && (region.mode !== \"after\" || above === undefined)) reads.add(lineKey(below));","atom":"a0","author":"init","version":0},{"n":175,"text":"\t\t\t\t// Creating a file over a deleted one relies on it being empty.","atom":"a0","author":"init","version":0},{"n":176,"text":"\t\t\t\tif (visible.length === 0) {","atom":"a0","author":"init","version":0},{"n":177,"text":"\t\t\t\t\tfor (const line of view.sequence(region.file)) for (const d of line.deletedBy) emptiedBy.add(d);","atom":"a0","author":"init","version":0},{"n":178,"text":"\t\t\t\t}","atom":"a0","author":"init","version":0},{"n":179,"text":"\t\t\t}","atom":"a0","author":"init","version":0},{"n":180,"text":"\t\t\tif (hunk.lines.length > 0) inserts.push({ file: region.file, parent, lines: [...hunk.lines] });","atom":"a0","author":"init","version":0},{"n":181,"text":"\t\t}","atom":"a0","author":"init","version":0},{"n":182,"text":"\t\tif (region.mode === \"wide\")","atom":"a0","author":"init","version":0},{"n":183,"text":"\t\t\tholdSpan(","atom":"a0","author":"init","version":0},{"n":184,"text":"\t\t\t\tvisible.slice(region.start, region.end).map((l) => l.id),","atom":"a0","author":"init","version":0},{"n":185,"text":"\t\t\t\tholds,","atom":"a0","author":"init","version":0},{"n":186,"text":"\t\t\t);","atom":"a0","author":"init","version":0},{"n":187,"text":"\t}","atom":"a0","author":"init","version":0},{"n":188,"text":"","atom":"a0","author":"init","version":0},{"n":189,"text":"\tconst moves = input.move !== undefined ? planMove(view, input.move, writes, reads) : [];","atom":"a0","author":"init","version":0},{"n":190,"text":"","atom":"a0","author":"init","version":0},{"n":191,"text":"\tconst attrs: AttrSet[] = [];","atom":"a0","author":"init","version":0},{"n":192,"text":"\tfor (const a of input.attrs ?? []) {","atom":"a0","author":"init","version":0},{"n":193,"text":"\t\tif (typeof a?.file !== \"string\" || a.file === \"\" || /[\\n\\r]/.test(a.file) || !ATTR_NAMES.includes(a.name)) {","atom":"a0","author":"init","version":0},{"n":194,"text":"\t\t\tthrow new EngineError(\"bad-edit\", `an attribute needs a file and one of ${ATTR_NAMES.join(\", \")}`);","atom":"a0","author":"init","version":0},{"n":195,"text":"\t\t}","atom":"a0","author":"init","version":0},{"n":196,"text":"\t\tif (a.value !== null && typeof a.value !== \"string\")","atom":"a0","author":"init","version":0},{"n":197,"text":"\t\t\tthrow new EngineError(\"bad-edit\", \"an attribute value is a string or null\");","atom":"a0","author":"init","version":0},{"n":198,"text":"\t\tif (attrs.some((x) => x.file === a.file && x.name === a.name))","atom":"a0","author":"init","version":0},{"n":199,"text":"\t\t\tthrow new EngineError(\"bad-edit\", `${a.name} of ${a.file} set twice`);","atom":"a0","author":"init","version":0},{"n":200,"text":"\t\tattrs.push({ file: a.file, name: a.name, value: a.value });","atom":"a0","author":"init","version":0},{"n":201,"text":"\t\twrites.add(attrKey(a.file, a.name));","atom":"a0","author":"init","version":0},{"n":202,"text":"\t}","atom":"a0","author":"init","version":0},{"n":203,"text":"","atom":"a0","author":"init","version":0},{"n":204,"text":"\tfor (const range of input.reliesOn ?? []) holdSpan(rangeLines(view, range), holds);","atom":"a0","author":"init","version":0},{"n":205,"text":"\tfor (const range of input.spans ?? []) holdSpan(rangeLines(view, range), holds);","atom":"a0","author":"init","version":0},{"n":206,"text":"","atom":"a0","author":"init","version":0},{"n":207,"text":"\tconst needs = uniqueAtoms(view, input.needs ?? [], \"needs\");","atom":"a0","author":"init","version":0},{"n":208,"text":"\tconst resolves = uniqueAtoms(view, input.resolves ?? [], \"resolves\");","atom":"a0","author":"init","version":0},{"n":209,"text":"\tconst reasons = (input.reasons ?? []).map((r) => {","atom":"a0","author":"init","version":0},{"n":210,"text":"\t\tif (!REASON_KINDS.includes(r.kind)) {","atom":"a0","author":"init","version":0},{"n":211,"text":"\t\t\tthrow new EngineError(\"bad-edit\", `reason kind must be one of ${REASON_KINDS.join(\", \")}, not ${String(r.kind)}`);","atom":"a0","author":"init","version":0},{"n":212,"text":"\t\t}","atom":"a0","author":"init","version":0},{"n":213,"text":"\t\tif (!graph.has(r.to)) throw new EngineError(\"unknown-atom\", `reason points at unknown atom ${r.to}`);","atom":"a0","author":"init","version":0},{"n":214,"text":"\t\tif (typeof r.text !== \"string\") throw new EngineError(\"bad-edit\", \"a reason needs text\");","atom":"a0","author":"init","version":0},{"n":215,"text":"\t\treturn { to: r.to, kind: r.kind, text: r.text };","atom":"a0","author":"init","version":0},{"n":216,"text":"\t});","atom":"a0","author":"init","version":0},{"n":217,"text":"","atom":"a0","author":"init","version":0},{"n":218,"text":"\tconst nothing =","atom":"a0","author":"init","version":0},{"n":219,"text":"\t\tinserts.length === 0 &&","atom":"a0","author":"init","version":0},{"n":220,"text":"\t\tdeletes.length === 0 &&","atom":"a0","author":"init","version":0},{"n":221,"text":"\t\tholds.size === 0 &&","atom":"a0","author":"init","version":0},{"n":222,"text":"\t\tneeds.length === 0 &&","atom":"a0","author":"init","version":0},{"n":223,"text":"\t\tresolves.length === 0 &&","atom":"a0","author":"init","version":0},{"n":224,"text":"\t\treasons.length === 0 &&","atom":"a0","author":"init","version":0},{"n":225,"text":"\t\tattrs.length === 0 &&","atom":"a0","author":"init","version":0},{"n":226,"text":"\t\tmoves.length === 0;","atom":"a0","author":"init","version":0},{"n":227,"text":"\tif (nothing && !allowEmpty) {","atom":"a0","author":"init","version":0},{"n":228,"text":"\t\tthrow new EngineError(\"empty-atom\", \"the edit changes nothing and claims nothing\");","atom":"a0","author":"init","version":0},{"n":229,"text":"\t}","atom":"a0","author":"init","version":0},{"n":230,"text":"","atom":"a0","author":"init","version":0},{"n":231,"text":"\tconst sortedWrites = [...writes].sort();","atom":"a0","author":"init","version":0},{"n":232,"text":"\tconst sortedReads = [...reads].sort();","atom":"a0","author":"init","version":0},{"n":233,"text":"\tconst sortedHolds = [...holds].sort();","atom":"a0","author":"init","version":0},{"n":234,"text":"\tconst deps = deriveDeps(view, {","atom":"a0","author":"init","version":0},{"n":235,"text":"\t\twrites: sortedWrites,","atom":"a0","author":"init","version":0},{"n":236,"text":"\t\treads: sortedReads,","atom":"a0","author":"init","version":0},{"n":237,"text":"\t\tholds: sortedHolds,","atom":"a0","author":"init","version":0},{"n":238,"text":"\t\temptiedBy,","atom":"a0","author":"init","version":0},{"n":239,"text":"\t\tneeds,","atom":"a0","author":"init","version":0},{"n":240,"text":"\t\tresolves,","atom":"a0","author":"init","version":0},{"n":241,"text":"\t});","atom":"a0","author":"init","version":0},{"n":242,"text":"","atom":"a0","author":"init","version":0},{"n":243,"text":"\treturn {","atom":"a0","author":"init","version":0},{"n":244,"text":"\t\tid,","atom":"a0","author":"init","version":0},{"n":245,"text":"\t\tseq: graph.size,","atom":"a0","author":"init","version":0},{"n":246,"text":"\t\tauthor: input.author,","atom":"a0","author":"init","version":0},{"n":247,"text":"\t\tmessage: input.message,","atom":"a0","author":"init","version":0},{"n":248,"text":"\t\ttime,","atom":"a0","author":"init","version":0},{"n":249,"text":"\t\tinserts,","atom":"a0","author":"init","version":0},{"n":250,"text":"\t\tdeletes,","atom":"a0","author":"init","version":0},{"n":251,"text":"\t\twrites: sortedWrites,","atom":"a0","author":"init","version":0},{"n":252,"text":"\t\treads: sortedReads,","atom":"a0","author":"init","version":0},{"n":253,"text":"\t\tholds: sortedHolds,","atom":"a0","author":"init","version":0},{"n":254,"text":"\t\tneeds,","atom":"a0","author":"init","version":0},{"n":255,"text":"\t\tresolves,","atom":"a0","author":"init","version":0},{"n":256,"text":"\t\treasons,","atom":"a0","author":"init","version":0},{"n":257,"text":"\t\tdeps,","atom":"a0","author":"init","version":0},{"n":258,"text":"\t\t...(attrs.length > 0 ? { attrs } : {}),","atom":"a0","author":"init","version":0},{"n":259,"text":"\t\t...(moves.length > 0 ? { moves } : {}),","atom":"a0","author":"init","version":0},{"n":260,"text":"\t};","atom":"a0","author":"init","version":0},{"n":261,"text":"}","atom":"a0","author":"init","version":0},{"n":262,"text":"","atom":"a0","author":"init","version":0},{"n":263,"text":"/**","atom":"a0","author":"init","version":0},{"n":264,"text":" * The relocations that move a block. In the file's full sequence (tombstones","atom":"a0","author":"init","version":0},{"n":265,"text":" * included), the block runs from its first line to its last. Afterwards the","atom":"a0","author":"init","version":0},{"n":266,"text":" * block must sit right after D (the visible line before `at`, or the start","atom":"a0","author":"init","version":0},{"n":267,"text":" * node), and everything else must keep its order. So:","atom":"a0","author":"init","version":0},{"n":268,"text":" * - the block's first line moves to D;","atom":"a0","author":"init","version":0},{"n":269,"text":" * - a later block line whose parent is outside the block moves to the block","atom":"a0","author":"init","version":0},{"n":270,"text":" *   line before it (which has no children, the walk being depth-first);","atom":"a0","author":"init","version":0},{"n":271,"text":" * - a line after the block whose parent is inside it moves to the line before","atom":"a0","author":"init","version":0},{"n":272,"text":" *   it once the block is gone (the line before the block, for the first).","atom":"a0","author":"init","version":0},{"n":273,"text":" * It writes P: of every block line and of every line it relocates, and G: of","atom":"a0","author":"init","version":0},{"n":274,"text":" * every new parent. It reads P:D, so two moves made apart that would loop","atom":"a0","author":"init","version":0},{"n":275,"text":" * lines into each other collide in all but the rarest cases (the view breaks","atom":"a0","author":"init","version":0},{"n":276,"text":" * any loop that is left).","atom":"a0","author":"init","version":0},{"n":277,"text":" */","atom":"a0","author":"init","version":0},{"n":278,"text":"function planMove(view: View, move: MoveInput, writes: Set<Key>, reads: Set<Key>): Relocation[] {","atom":"a0","author":"init","version":0},{"n":279,"text":"\tfor (const path of [move.file, move.to]) {","atom":"a0","author":"init","version":0},{"n":280,"text":"\t\tif (typeof path !== \"string\" || path === \"\" || /[\\n\\r]/.test(path))","atom":"a0","author":"init","version":0},{"n":281,"text":"\t\t\tthrow new EngineError(\"bad-edit\", `bad file path ${JSON.stringify(path)}`);","atom":"a0","author":"init","version":0},{"n":282,"text":"\t}","atom":"a0","author":"init","version":0},{"n":283,"text":"\tconst visible = view.lines(move.file);","atom":"a0","author":"init","version":0},{"n":284,"text":"\tconst { start, end, at } = move;","atom":"a0","author":"init","version":0},{"n":285,"text":"\tif (!Number.isInteger(start) || !Number.isInteger(end) || start < 0 || end > visible.length || start >= end) {","atom":"a0","author":"init","version":0},{"n":286,"text":"\t\tthrow new EngineError(","atom":"a0","author":"init","version":0},{"n":287,"text":"\t\t\t\"bad-range\",","atom":"a0","author":"init","version":0},{"n":288,"text":"\t\t\t`${move.file} lines [${start}, ${end}) are not a non-empty range of its ${visible.length} visible lines`,","atom":"a0","author":"init","version":0},{"n":289,"text":"\t\t);","atom":"a0","author":"init","version":0},{"n":290,"text":"\t}","atom":"a0","author":"init","version":0},{"n":291,"text":"\tconst dest = view.lines(move.to);","atom":"a0","author":"init","version":0},{"n":292,"text":"\tif (!Number.isInteger(at) || at < 0 || at > dest.length) {","atom":"a0","author":"init","version":0},{"n":293,"text":"\t\tthrow new EngineError(\"bad-range\", `${move.to} has no place ${at} (it has ${dest.length} visible lines)`);","atom":"a0","author":"init","version":0},{"n":294,"text":"\t}","atom":"a0","author":"init","version":0},{"n":295,"text":"\tif (move.to === move.file && at >= start && at <= end)","atom":"a0","author":"init","version":0},{"n":296,"text":"\t\tthrow new EngineError(\"bad-range\", \"the lines are already there\");","atom":"a0","author":"init","version":0},{"n":297,"text":"\tconst sequence = view.sequence(move.file);","atom":"a0","author":"init","version":0},{"n":298,"text":"\tconst i = view.sequenceIndex(move.file, visible[start]!.id);","atom":"a0","author":"init","version":0},{"n":299,"text":"\tconst j = view.sequenceIndex(move.file, visible[end - 1]!.id);","atom":"a0","author":"init","version":0},{"n":300,"text":"\tconst block = sequence.slice(i, j + 1).map((l) => l.id);","atom":"a0","author":"init","version":0},{"n":301,"text":"\tconst inBlock = new Set(block);","atom":"a0","author":"init","version":0},{"n":302,"text":"\tconst target: NodeId = at > 0 ? dest[at - 1]!.id : startNode(move.to);","atom":"a0","author":"init","version":0},{"n":303,"text":"","atom":"a0","author":"init","version":0},{"n":304,"text":"\tconst out: Relocation[] = [{ line: block[0]!, parent: target }];","atom":"a0","author":"init","version":0},{"n":305,"text":"\tfor (let k = 1; k < block.length; k++) {","atom":"a0","author":"init","version":0},{"n":306,"text":"\t\tif (!inBlock.has(view.parentOf(block[k]!))) out.push({ line: block[k]!, parent: block[k - 1]! });","atom":"a0","author":"init","version":0},{"n":307,"text":"\t}","atom":"a0","author":"init","version":0},{"n":308,"text":"\tfor (let m = j + 1; m < sequence.length; m++) {","atom":"a0","author":"init","version":0},{"n":309,"text":"\t\tconst line = sequence[m]!.id;","atom":"a0","author":"init","version":0},{"n":310,"text":"\t\tif (!inBlock.has(view.parentOf(line))) continue;","atom":"a0","author":"init","version":0},{"n":311,"text":"\t\tconst parent = m === j + 1 ? (i > 0 ? sequence[i - 1]!.id : startNode(move.file)) : sequence[m - 1]!.id;","atom":"a0","author":"init","version":0},{"n":312,"text":"\t\tout.push({ line, parent });","atom":"a0","author":"init","version":0},{"n":313,"text":"\t}","atom":"a0","author":"init","version":0},{"n":314,"text":"\tfor (const line of block) writes.add(posKey(line));","atom":"a0","author":"init","version":0},{"n":315,"text":"\tfor (const r of out) {","atom":"a0","author":"init","version":0},{"n":316,"text":"\t\twrites.add(posKey(r.line));","atom":"a0","author":"init","version":0},{"n":317,"text":"\t\twrites.add(gapKey(r.parent));","atom":"a0","author":"init","version":0},{"n":318,"text":"\t}","atom":"a0","author":"init","version":0},{"n":319,"text":"\tif (!isStartNode(target)) reads.add(posKey(target));","atom":"a0","author":"init","version":0},{"n":320,"text":"\treturn out;","atom":"a0","author":"init","version":0},{"n":321,"text":"}","atom":"a0","author":"init","version":0},{"n":322,"text":"","atom":"a0","author":"init","version":0},{"n":323,"text":"/**","atom":"a0","author":"init","version":0},{"n":324,"text":" * Derived dependencies of a new atom against its view (docs/prototype.md,","atom":"a0","author":"init","version":0},{"n":325,"text":" * \"Dependencies\"):","atom":"a0","author":"init","version":0},{"n":326,"text":" * - blame: the writers of every line its keys name (a G: key names the line","atom":"a0","author":"init","version":0},{"n":327,"text":" *   the gap follows);","atom":"a0","author":"init","version":0},{"n":328,"text":" * - gap rule: atoms in the view that wrote a gap it inserts into or holds.","atom":"a0","author":"init","version":0},{"n":329,"text":" *   Their lines hang off the same line, so the new run outranks all of them","atom":"a0","author":"init","version":0},{"n":330,"text":" *   and lands directly after its parent;","atom":"a0","author":"init","version":0},{"n":331,"text":" * - implicit-read rule: atoms in the view whose implicit reads include a key","atom":"a0","author":"init","version":0},{"n":332,"text":" *   it writes (it saw that insert and changed its neighbour);","atom":"a0","author":"init","version":0},{"n":333,"text":" * - creating a file over a deleted one: the atoms that deleted its lines;","atom":"a0","author":"init","version":0},{"n":334,"text":" * - needs and resolves.","atom":"a0","author":"init","version":0},{"n":335,"text":" * Nothing else from the view: not the line below an insertion, not the","atom":"a0","author":"init","version":0},{"n":336,"text":" * tombstones around it.","atom":"a0","author":"init","version":0},{"n":337,"text":" */","atom":"a0","author":"init","version":0},{"n":338,"text":"function deriveDeps(","atom":"a0","author":"init","version":0},{"n":339,"text":"\tview: View,","atom":"a0","author":"init","version":0},{"n":340,"text":"\tparts: {","atom":"a0","author":"init","version":0},{"n":341,"text":"\t\twrites: readonly Key[];","atom":"a0","author":"init","version":0},{"n":342,"text":"\t\treads: readonly Key[];","atom":"a0","author":"init","version":0},{"n":343,"text":"\t\tholds: readonly Key[];","atom":"a0","author":"init","version":0},{"n":344,"text":"\t\temptiedBy: ReadonlySet<AtomId>;","atom":"a0","author":"init","version":0},{"n":345,"text":"\t\tneeds: readonly AtomId[];","atom":"a0","author":"init","version":0},{"n":346,"text":"\t\tresolves: readonly AtomId[];","atom":"a0","author":"init","version":0},{"n":347,"text":"\t},","atom":"a0","author":"init","version":0},{"n":348,"text":"): AtomId[] {","atom":"a0","author":"init","version":0},{"n":349,"text":"\tconst graph = view.graph;","atom":"a0","author":"init","version":0},{"n":350,"text":"\tconst deps = new Set<AtomId>(parts.emptiedBy);","atom":"a0","author":"init","version":0},{"n":351,"text":"\tfor (const key of [...parts.writes, ...parts.reads, ...parts.holds]) {","atom":"a0","author":"init","version":0},{"n":352,"text":"\t\tconst { kind, node } = parseKey(key);","atom":"a0","author":"init","version":0},{"n":353,"text":"\t\t// An attribute builds on whoever set it last in the view, the way an insertion builds on a gap's writers;","atom":"a0","author":"init","version":0},{"n":354,"text":"\t\t// a move builds on the earlier moves of its lines.","atom":"a0","author":"init","version":0},{"n":355,"text":"\t\tif (kind !== \"attr\" && !isStartNode(node)) deps.add(writerOf(node));","atom":"a0","author":"init","version":0},{"n":356,"text":"\t\tif (kind !== \"line\") for (const writer of graph.writersOf(key)) if (view.set.has(writer)) deps.add(writer);","atom":"a0","author":"init","version":0},{"n":357,"text":"\t}","atom":"a0","author":"init","version":0},{"n":358,"text":"\tfor (const key of parts.writes) {","atom":"a0","author":"init","version":0},{"n":359,"text":"\t\tfor (const reader of graph.readersOf(key)) if (view.set.has(reader)) deps.add(reader);","atom":"a0","author":"init","version":0},{"n":360,"text":"\t}","atom":"a0","author":"init","version":0},{"n":361,"text":"\tfor (const id of parts.needs) deps.add(id);","atom":"a0","author":"init","version":0},{"n":362,"text":"\tfor (const id of parts.resolves) deps.add(id);","atom":"a0","author":"init","version":0},{"n":363,"text":"\treturn [...deps].sort((a, b) => graph.atom(a).seq - graph.atom(b).seq);","atom":"a0","author":"init","version":0},{"n":364,"text":"}","atom":"a0","author":"init","version":0},{"n":365,"text":"","atom":"a0","author":"init","version":0},{"n":366,"text":"/** Hold every line of a span and every gap between consecutive lines of it. */","atom":"a0","author":"init","version":0},{"n":367,"text":"function holdSpan(lines: readonly LineId[], holds: Set<Key>): void {","atom":"a0","author":"init","version":0},{"n":368,"text":"\tlines.forEach((line, i) => {","atom":"a0","author":"init","version":0},{"n":369,"text":"\t\tholds.add(lineKey(line));","atom":"a0","author":"init","version":0},{"n":370,"text":"\t\tif (i + 1 < lines.length) holds.add(gapKey(line));","atom":"a0","author":"init","version":0},{"n":371,"text":"\t});","atom":"a0","author":"init","version":0},{"n":372,"text":"}","atom":"a0","author":"init","version":0},{"n":373,"text":"","atom":"a0","author":"init","version":0},{"n":374,"text":"function rangeLines(view: View, range: LineRange): LineId[] {","atom":"a0","author":"init","version":0},{"n":375,"text":"\tconst visible = view.lines(range.file);","atom":"a0","author":"init","version":0},{"n":376,"text":"\tif (","atom":"a0","author":"init","version":0},{"n":377,"text":"\t\t!Number.isInteger(range.start) ||","atom":"a0","author":"init","version":0},{"n":378,"text":"\t\t!Number.isInteger(range.end) ||","atom":"a0","author":"init","version":0},{"n":379,"text":"\t\trange.start < 0 ||","atom":"a0","author":"init","version":0},{"n":380,"text":"\t\trange.end > visible.length ||","atom":"a0","author":"init","version":0},{"n":381,"text":"\t\trange.start >= range.end","atom":"a0","author":"init","version":0},{"n":382,"text":"\t) {","atom":"a0","author":"init","version":0},{"n":383,"text":"\t\tthrow new EngineError(","atom":"a0","author":"init","version":0},{"n":384,"text":"\t\t\t\"bad-range\",","atom":"a0","author":"init","version":0},{"n":385,"text":"\t\t\t`${range.file} lines [${range.start}, ${range.end}) are not a non-empty range of its ${visible.length} visible lines`,","atom":"a0","author":"init","version":0},{"n":386,"text":"\t\t);","atom":"a0","author":"init","version":0},{"n":387,"text":"\t}","atom":"a0","author":"init","version":0},{"n":388,"text":"\treturn visible.slice(range.start, range.end).map((l) => l.id);","atom":"a0","author":"init","version":0},{"n":389,"text":"}","atom":"a0","author":"init","version":0},{"n":390,"text":"","atom":"a0","author":"init","version":0},{"n":391,"text":"function uniqueAtoms(view: View, ids: readonly AtomId[], what: string): AtomId[] {","atom":"a0","author":"init","version":0},{"n":392,"text":"\tconst out: AtomId[] = [];","atom":"a0","author":"init","version":0},{"n":393,"text":"\tfor (const id of ids) {","atom":"a0","author":"init","version":0},{"n":394,"text":"\t\tif (!view.graph.has(id)) throw new EngineError(\"unknown-atom\", `${what} names unknown atom ${id}`);","atom":"a0","author":"init","version":0},{"n":395,"text":"\t\tif (!view.set.has(id)) throw new EngineError(\"not-in-view\", `${what} names ${id}, which is not in the view`);","atom":"a0","author":"init","version":0},{"n":396,"text":"\t\tif (!out.includes(id)) out.push(id);","atom":"a0","author":"init","version":0},{"n":397,"text":"\t}","atom":"a0","author":"init","version":0},{"n":398,"text":"\treturn out.sort((a, b) => view.graph.atom(a).seq - view.graph.atom(b).seq);","atom":"a0","author":"init","version":0},{"n":399,"text":"}","atom":"a0","author":"init","version":0},{"n":400,"text":"","atom":"a0","author":"init","version":0},{"n":401,"text":"// Matching edits ---------------------------------------------------------------","atom":"a0","author":"init","version":0},{"n":402,"text":"","atom":"a0","author":"init","version":0},{"n":403,"text":"function resolveRegions(view: View, edits: readonly EditInput[]): Region[] {","atom":"a0","author":"init","version":0},{"n":404,"text":"\tconst regions = edits.map((edit, order) => {","atom":"a0","author":"init","version":0},{"n":405,"text":"\t\tconst mode = edit.mode ?? \"both\";","atom":"a0","author":"init","version":0},{"n":406,"text":"\t\tif (!MODES.includes(mode)) throw new EngineError(\"bad-edit\", `mode must be one of ${MODES.join(\", \")}`);","atom":"a0","author":"init","version":0},{"n":407,"text":"\t\t// A path may start with '^' (a start node is '^' + path, so '^^x' stays unambiguous).","atom":"a0","author":"init","version":0},{"n":408,"text":"\t\tif (typeof edit.file !== \"string\" || edit.file === \"\" || /[\\n\\r]/.test(edit.file)) {","atom":"a0","author":"init","version":0},{"n":409,"text":"\t\t\tthrow new EngineError(\"bad-edit\", `bad file path ${JSON.stringify(edit.file)}`);","atom":"a0","author":"init","version":0},{"n":410,"text":"\t\t}","atom":"a0","author":"init","version":0},{"n":411,"text":"\t\treturn \"old\" in edit ? matchText(view, edit, mode, order) : matchSplice(view, edit, mode, order);","atom":"a0","author":"init","version":0},{"n":412,"text":"\t});","atom":"a0","author":"init","version":0},{"n":413,"text":"","atom":"a0","author":"init","version":0},{"n":414,"text":"\t// Files in order of first appearance; within a file, regions in position order.","atom":"a0","author":"init","version":0},{"n":415,"text":"\tconst fileOrder = new Map<string, number>();","atom":"a0","author":"init","version":0},{"n":416,"text":"\tfor (const r of regions) if (!fileOrder.has(r.file)) fileOrder.set(r.file, fileOrder.size);","atom":"a0","author":"init","version":0},{"n":417,"text":"\tregions.sort(","atom":"a0","author":"init","version":0},{"n":418,"text":"\t\t(a, b) =>","atom":"a0","author":"init","version":0},{"n":419,"text":"\t\t\tfileOrder.get(a.file)! - fileOrder.get(b.file)! || a.start - b.start || a.end - b.end || a.order - b.order,","atom":"a0","author":"init","version":0},{"n":420,"text":"\t);","atom":"a0","author":"init","version":0},{"n":421,"text":"\tfor (let i = 1; i < regions.length; i++) {","atom":"a0","author":"init","version":0},{"n":422,"text":"\t\tconst prev = regions[i - 1]!;","atom":"a0","author":"init","version":0},{"n":423,"text":"\t\tconst cur = regions[i]!;","atom":"a0","author":"init","version":0},{"n":424,"text":"\t\tif (prev.file === cur.file && prev.end > cur.start) {","atom":"a0","author":"init","version":0},{"n":425,"text":"\t\t\tthrow new EngineError(","atom":"a0","author":"init","version":0},{"n":426,"text":"\t\t\t\t\"overlapping-edits\",","atom":"a0","author":"init","version":0},{"n":427,"text":"\t\t\t\t`two edits to ${cur.file} overlap (lines ${prev.start + 1}-${prev.end} and ${cur.start + 1}-${cur.end})`,","atom":"a0","author":"init","version":0},{"n":428,"text":"\t\t\t);","atom":"a0","author":"init","version":0},{"n":429,"text":"\t\t}","atom":"a0","author":"init","version":0},{"n":430,"text":"\t}","atom":"a0","author":"init","version":0},{"n":431,"text":"\treturn regions;","atom":"a0","author":"init","version":0},{"n":432,"text":"}","atom":"a0","author":"init","version":0},{"n":433,"text":"","atom":"a0","author":"init","version":0},{"n":434,"text":"/**","atom":"a0","author":"init","version":0},{"n":435,"text":" * Find `old` (exactly once) in the file's visible text and widen the match to","atom":"a0","author":"init","version":0},{"n":436,"text":" * whole lines. If the replacement would leave a line without its newline, the","atom":"a0","author":"init","version":0},{"n":437,"text":" * next line joins the region (that is what the plain-text edit does).","atom":"a0","author":"init","version":0},{"n":438,"text":" */","atom":"a0","author":"init","version":0},{"n":439,"text":"function matchText(view: View, edit: TextEdit, mode: Mode, order: number): Region {","atom":"a0","author":"init","version":0},{"n":440,"text":"\tif (typeof edit.old !== \"string\" || typeof edit.new !== \"string\") {","atom":"a0","author":"init","version":0},{"n":441,"text":"\t\tthrow new EngineError(\"bad-edit\", \"a text edit needs old and new strings\");","atom":"a0","author":"init","version":0},{"n":442,"text":"\t}","atom":"a0","author":"init","version":0},{"n":443,"text":"\tconst file = edit.file;","atom":"a0","author":"init","version":0},{"n":444,"text":"\tconst visible = view.lines(file);","atom":"a0","author":"init","version":0},{"n":445,"text":"\tconst oldText = normalizeText(edit.old);","atom":"a0","author":"init","version":0},{"n":446,"text":"\tconst newText = normalizeText(edit.new);","atom":"a0","author":"init","version":0},{"n":447,"text":"\tif (oldText === \"\") {","atom":"a0","author":"init","version":0},{"n":448,"text":"\t\tif (visible.length > 0) {","atom":"a0","author":"init","version":0},{"n":449,"text":"\t\t\tthrow new EngineError(","atom":"a0","author":"init","version":0},{"n":450,"text":"\t\t\t\t\"ambiguous-match\",","atom":"a0","author":"init","version":0},{"n":451,"text":"\t\t\t\t`empty old text matches everywhere in ${file}; an empty old text only creates a file`,","atom":"a0","author":"init","version":0},{"n":452,"text":"\t\t\t);","atom":"a0","author":"init","version":0},{"n":453,"text":"\t\t}","atom":"a0","author":"init","version":0},{"n":454,"text":"\t\treturn { file, start: 0, end: 0, lines: splitLines(newText), mode, order };","atom":"a0","author":"init","version":0},{"n":455,"text":"\t}","atom":"a0","author":"init","version":0},{"n":456,"text":"\tconst text = joinLines(visible.map((l) => l.text));","atom":"a0","author":"init","version":0},{"n":457,"text":"\tconst at = text.indexOf(oldText);","atom":"a0","author":"init","version":0},{"n":458,"text":"\tif (at < 0) throw new EngineError(\"no-match\", `old text not found in ${file}`);","atom":"a0","author":"init","version":0},{"n":459,"text":"\tif (text.indexOf(oldText, at + 1) >= 0) {","atom":"a0","author":"init","version":0},{"n":460,"text":"\t\tthrow new EngineError(","atom":"a0","author":"init","version":0},{"n":461,"text":"\t\t\t\"ambiguous-match\",","atom":"a0","author":"init","version":0},{"n":462,"text":"\t\t\t`old text matches more than once in ${file}; add context to make it unique`,","atom":"a0","author":"init","version":0},{"n":463,"text":"\t\t);","atom":"a0","author":"init","version":0},{"n":464,"text":"\t}","atom":"a0","author":"init","version":0},{"n":465,"text":"\tconst starts: number[] = [];","atom":"a0","author":"init","version":0},{"n":466,"text":"\tlet pos = 0;","atom":"a0","author":"init","version":0},{"n":467,"text":"\tfor (const line of visible) {","atom":"a0","author":"init","version":0},{"n":468,"text":"\t\tstarts.push(pos);","atom":"a0","author":"init","version":0},{"n":469,"text":"\t\tpos += line.text.length + 1;","atom":"a0","author":"init","version":0},{"n":470,"text":"\t}","atom":"a0","author":"init","version":0},{"n":471,"text":"\tconst lineAt = (offset: number): number => {","atom":"a0","author":"init","version":0},{"n":472,"text":"\t\tlet lo = 0;","atom":"a0","author":"init","version":0},{"n":473,"text":"\t\tlet hi = starts.length - 1;","atom":"a0","author":"init","version":0},{"n":474,"text":"\t\twhile (lo < hi) {","atom":"a0","author":"init","version":0},{"n":475,"text":"\t\t\tconst mid = (lo + hi + 1) >> 1;","atom":"a0","author":"init","version":0},{"n":476,"text":"\t\t\tif (starts[mid]! <= offset) lo = mid;","atom":"a0","author":"init","version":0},{"n":477,"text":"\t\t\telse hi = mid - 1;","atom":"a0","author":"init","version":0},{"n":478,"text":"\t\t}","atom":"a0","author":"init","version":0},{"n":479,"text":"\t\treturn lo;","atom":"a0","author":"init","version":0},{"n":480,"text":"\t};","atom":"a0","author":"init","version":0},{"n":481,"text":"\tconst end = at + oldText.length;","atom":"a0","author":"init","version":0},{"n":482,"text":"\tconst startLine = lineAt(at);","atom":"a0","author":"init","version":0},{"n":483,"text":"\tlet endLine = lineAt(end - 1);","atom":"a0","author":"init","version":0},{"n":484,"text":"\tconst lineStart = starts[startLine]!;","atom":"a0","author":"init","version":0},{"n":485,"text":"\tconst lineEnd = starts[endLine]! + visible[endLine]!.text.length + 1;","atom":"a0","author":"init","version":0},{"n":486,"text":"\tlet replacement = text.slice(lineStart, at) + newText + text.slice(end, lineEnd);","atom":"a0","author":"init","version":0},{"n":487,"text":"\tif (replacement !== \"\" && !replacement.endsWith(\"\\n\")) {","atom":"a0","author":"init","version":0},{"n":488,"text":"\t\tif (endLine + 1 < visible.length) {","atom":"a0","author":"init","version":0},{"n":489,"text":"\t\t\tendLine++;","atom":"a0","author":"init","version":0},{"n":490,"text":"\t\t\treplacement += visible[endLine]!.text + \"\\n\";","atom":"a0","author":"init","version":0},{"n":491,"text":"\t\t} else {","atom":"a0","author":"init","version":0},{"n":492,"text":"\t\t\treplacement += \"\\n\";","atom":"a0","author":"init","version":0},{"n":493,"text":"\t\t}","atom":"a0","author":"init","version":0},{"n":494,"text":"\t}","atom":"a0","author":"init","version":0},{"n":495,"text":"\treturn { file, start: startLine, end: endLine + 1, lines: splitLines(replacement), mode, order };","atom":"a0","author":"init","version":0},{"n":496,"text":"}","atom":"a0","author":"init","version":0},{"n":497,"text":"","atom":"a0","author":"init","version":0},{"n":498,"text":"function matchSplice(view: View, edit: SpliceEdit, mode: Mode, order: number): Region {","atom":"a0","author":"init","version":0},{"n":499,"text":"\tconst count = view.lines(edit.file).length;","atom":"a0","author":"init","version":0},{"n":500,"text":"\tif (","atom":"a0","author":"init","version":0},{"n":501,"text":"\t\t!Number.isInteger(edit.start) ||","atom":"a0","author":"init","version":0},{"n":502,"text":"\t\t!Number.isInteger(edit.end) ||","atom":"a0","author":"init","version":0},{"n":503,"text":"\t\tedit.start < 0 ||","atom":"a0","author":"init","version":0},{"n":504,"text":"\t\tedit.start > edit.end ||","atom":"a0","author":"init","version":0},{"n":505,"text":"\t\tedit.end > count","atom":"a0","author":"init","version":0},{"n":506,"text":"\t) {","atom":"a0","author":"init","version":0},{"n":507,"text":"\t\tthrow new EngineError(","atom":"a0","author":"init","version":0},{"n":508,"text":"\t\t\t\"bad-range\",","atom":"a0","author":"init","version":0},{"n":509,"text":"\t\t\t`${edit.file} lines [${edit.start}, ${edit.end}) are outside its ${count} visible lines`,","atom":"a0","author":"init","version":0},{"n":510,"text":"\t\t);","atom":"a0","author":"init","version":0},{"n":511,"text":"\t}","atom":"a0","author":"init","version":0},{"n":512,"text":"\tif (!Array.isArray(edit.lines)) throw new EngineError(\"bad-edit\", \"a splice edit needs lines\");","atom":"a0","author":"init","version":0},{"n":513,"text":"\tconst lines = edit.lines.map((line) => {","atom":"a0","author":"init","version":0},{"n":514,"text":"\t\tif (typeof line !== \"string\" || /[\\n\\r]/.test(line)) {","atom":"a0","author":"init","version":0},{"n":515,"text":"\t\t\tthrow new EngineError(\"bad-edit\", \"splice lines must be strings without line breaks\");","atom":"a0","author":"init","version":0},{"n":516,"text":"\t\t}","atom":"a0","author":"init","version":0},{"n":517,"text":"\t\treturn line;","atom":"a0","author":"init","version":0},{"n":518,"text":"\t});","atom":"a0","author":"init","version":0},{"n":519,"text":"\treturn { file: edit.file, start: edit.start, end: edit.end, lines, mode, order };","atom":"a0","author":"init","version":0},{"n":520,"text":"}","atom":"a0","author":"init","version":0}],"atoms":[{"atom":"a0","author":"init","message":"Start the project","time":1791506895507,"timeIso":"2026-10-09T00:48:15.507Z","publish":{"draft":null,"version":0,"agent":"init","message":"Start the project","time":1791506895507,"timeIso":"2026-10-09T00:48:15.507Z"}}]}