2026-02-22 14:13:59 +09:00
|
|
|
import { dedupeEdits } from "./edit-deduplication"
|
|
|
|
|
import { collectLineRefs, getEditLineNumber } from "./edit-ordering"
|
2026-02-16 16:32:33 +09:00
|
|
|
import type { HashlineEdit } from "./types"
|
2026-02-22 03:38:04 +09:00
|
|
|
import {
|
2026-02-22 14:13:59 +09:00
|
|
|
applyAppend,
|
|
|
|
|
applyInsertAfter,
|
|
|
|
|
applyInsertBefore,
|
|
|
|
|
applyPrepend,
|
|
|
|
|
applyReplaceLines,
|
|
|
|
|
applySetLine,
|
|
|
|
|
} from "./edit-operation-primitives"
|
|
|
|
|
import { validateLineRefs } from "./validation"
|
2026-02-16 16:32:33 +09:00
|
|
|
|
2026-02-22 03:38:04 +09:00
|
|
|
export interface HashlineApplyReport {
|
|
|
|
|
content: string
|
|
|
|
|
noopEdits: number
|
|
|
|
|
deduplicatedEdits: number
|
2026-02-20 11:02:07 +09:00
|
|
|
}
|
|
|
|
|
|
2026-02-22 03:38:04 +09:00
|
|
|
export function applyHashlineEditsWithReport(content: string, edits: HashlineEdit[]): HashlineApplyReport {
|
2026-02-16 16:32:33 +09:00
|
|
|
if (edits.length === 0) {
|
2026-02-22 03:38:04 +09:00
|
|
|
return {
|
|
|
|
|
content,
|
|
|
|
|
noopEdits: 0,
|
|
|
|
|
deduplicatedEdits: 0,
|
|
|
|
|
}
|
2026-02-16 16:32:33 +09:00
|
|
|
}
|
|
|
|
|
|
2026-02-22 03:38:04 +09:00
|
|
|
const dedupeResult = dedupeEdits(edits)
|
|
|
|
|
const sortedEdits = [...dedupeResult.edits].sort((a, b) => getEditLineNumber(b) - getEditLineNumber(a))
|
|
|
|
|
|
|
|
|
|
let noopEdits = 0
|
2026-02-16 16:32:33 +09:00
|
|
|
|
2026-02-24 05:06:41 +09:00
|
|
|
let lines = content.length === 0 ? [] : content.split("\n")
|
2026-02-16 16:32:33 +09:00
|
|
|
|
2026-02-22 14:13:59 +09:00
|
|
|
const refs = collectLineRefs(sortedEdits)
|
2026-02-20 11:02:07 +09:00
|
|
|
validateLineRefs(lines, refs)
|
|
|
|
|
|
2026-02-16 16:32:33 +09:00
|
|
|
for (const edit of sortedEdits) {
|
2026-02-24 05:06:41 +09:00
|
|
|
switch (edit.op) {
|
|
|
|
|
case "replace": {
|
|
|
|
|
const next = edit.end
|
|
|
|
|
? applyReplaceLines(lines, edit.pos, edit.end, edit.lines, { skipValidation: true })
|
|
|
|
|
: applySetLine(lines, edit.pos, edit.lines, { skipValidation: true })
|
2026-02-22 14:13:59 +09:00
|
|
|
if (next.join("\n") === lines.join("\n")) {
|
|
|
|
|
noopEdits += 1
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
lines = next
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
case "append": {
|
2026-02-24 05:06:41 +09:00
|
|
|
const next = edit.pos
|
|
|
|
|
? applyInsertAfter(lines, edit.pos, edit.lines, { skipValidation: true })
|
|
|
|
|
: applyAppend(lines, edit.lines)
|
2026-02-22 14:13:59 +09:00
|
|
|
if (next.join("\n") === lines.join("\n")) {
|
|
|
|
|
noopEdits += 1
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
lines = next
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
case "prepend": {
|
2026-02-24 05:06:41 +09:00
|
|
|
const next = edit.pos
|
|
|
|
|
? applyInsertBefore(lines, edit.pos, edit.lines, { skipValidation: true })
|
|
|
|
|
: applyPrepend(lines, edit.lines)
|
2026-02-22 03:38:04 +09:00
|
|
|
if (next.join("\n") === lines.join("\n")) {
|
|
|
|
|
noopEdits += 1
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
lines = next
|
2026-02-16 16:32:33 +09:00
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-22 03:38:04 +09:00
|
|
|
return {
|
|
|
|
|
content: lines.join("\n"),
|
|
|
|
|
noopEdits,
|
|
|
|
|
deduplicatedEdits: dedupeResult.deduplicatedEdits,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function applyHashlineEdits(content: string, edits: HashlineEdit[]): string {
|
|
|
|
|
return applyHashlineEditsWithReport(content, edits).content
|
2026-02-16 16:32:33 +09:00
|
|
|
}
|
2026-02-22 14:13:59 +09:00
|
|
|
|
|
|
|
|
export {
|
|
|
|
|
applySetLine,
|
|
|
|
|
applyReplaceLines,
|
|
|
|
|
applyInsertAfter,
|
|
|
|
|
applyInsertBefore,
|
|
|
|
|
} from "./edit-operation-primitives"
|