4ea76365cd
Move the hash-anchored edit core (hash computation, validation, edit operations, text normalization, chunk formatter, diff utilities, and a runtime-aware xxHash32 binding) into a new @oh-my-opencode/hashline-core workspace package. The src/tools/hashline-edit/ surface becomes a set of thin re-export shims, so existing import paths in the plugin keep working while the pure logic lives behind a stable package boundary that has no opencode runtime dependencies. Tests: bun test packages/hashline-core src/tools/hashline-edit
44 lines
1.3 KiB
TypeScript
44 lines
1.3 KiB
TypeScript
import type { HashlineEdit } from "./types"
|
|
import { toNewLines } from "./edit-text-normalization"
|
|
import { normalizeLineRef } from "./validation"
|
|
|
|
function normalizeEditPayload(payload: string | string[]): string {
|
|
return toNewLines(payload).join("\n")
|
|
}
|
|
|
|
function canonicalAnchor(anchor: string | undefined): string {
|
|
if (!anchor) return ""
|
|
return normalizeLineRef(anchor)
|
|
}
|
|
|
|
function buildDedupeKey(edit: HashlineEdit): string {
|
|
switch (edit.op) {
|
|
case "replace":
|
|
return `replace|${canonicalAnchor(edit.pos)}|${edit.end ? canonicalAnchor(edit.end) : ""}|${normalizeEditPayload(edit.lines)}`
|
|
case "append":
|
|
return `append|${canonicalAnchor(edit.pos)}|${normalizeEditPayload(edit.lines)}`
|
|
case "prepend":
|
|
return `prepend|${canonicalAnchor(edit.pos)}|${normalizeEditPayload(edit.lines)}`
|
|
default:
|
|
return JSON.stringify(edit)
|
|
}
|
|
}
|
|
|
|
export function dedupeEdits(edits: HashlineEdit[]): { edits: HashlineEdit[]; deduplicatedEdits: number } {
|
|
const seen = new Set<string>()
|
|
const deduped: HashlineEdit[] = []
|
|
let deduplicatedEdits = 0
|
|
|
|
for (const edit of edits) {
|
|
const key = buildDedupeKey(edit)
|
|
if (seen.has(key)) {
|
|
deduplicatedEdits += 1
|
|
continue
|
|
}
|
|
seen.add(key)
|
|
deduped.push(edit)
|
|
}
|
|
|
|
return { edits: deduped, deduplicatedEdits }
|
|
}
|