refactor(hashline-edit): enforce three-op edit model

Unify internal hashline edit handling around replace/append/prepend to remove legacy operation shapes. This keeps normalization, ordering, deduplication, execution, and tests aligned with the new op/pos/end/lines contract.
This commit is contained in:
minpeter
2026-02-24 05:06:41 +09:00
parent 6ec0ff732b
commit 08b663df86
10 changed files with 86 additions and 261 deletions
+20 -39
View File
@@ -1,4 +1,4 @@
import type { HashlineEdit } from "./types"
import type { AppendEdit, HashlineEdit, PrependEdit, ReplaceEdit } from "./types"
type HashlineToolOp = "replace" | "append" | "prepend"
@@ -36,62 +36,43 @@ function normalizeReplaceEdit(edit: RawHashlineEdit, index: number): HashlineEdi
const pos = normalizeAnchor(edit.pos)
const end = normalizeAnchor(edit.end)
const anchor = requireLine(pos ?? end, index, "replace")
const text = requireLines(edit, index)
const lines = requireLines(edit, index)
if (pos && end) {
return {
type: "replace_lines",
start_line: pos,
end_line: end,
text,
}
}
return {
type: "set_line",
line: anchor,
text,
const normalized: ReplaceEdit = {
op: "replace",
pos: anchor,
lines,
}
if (end) normalized.end = end
return normalized
}
function normalizeAppendEdit(edit: RawHashlineEdit, index: number): HashlineEdit {
const pos = normalizeAnchor(edit.pos)
const end = normalizeAnchor(edit.end)
const anchor = pos ?? end
const text = requireLines(edit, index)
const lines = requireLines(edit, index)
if (!anchor) {
return {
type: "append",
text,
}
}
return {
type: "insert_after",
line: anchor,
text,
const normalized: AppendEdit = {
op: "append",
lines,
}
if (anchor) normalized.pos = anchor
return normalized
}
function normalizePrependEdit(edit: RawHashlineEdit, index: number): HashlineEdit {
const pos = normalizeAnchor(edit.pos)
const end = normalizeAnchor(edit.end)
const anchor = pos ?? end
const text = requireLines(edit, index)
const lines = requireLines(edit, index)
if (!anchor) {
return {
type: "prepend",
text,
}
}
return {
type: "insert_before",
line: anchor,
text,
const normalized: PrependEdit = {
op: "prepend",
lines,
}
if (anchor) normalized.pos = anchor
return normalized
}
export function normalizeHashlineEdits(rawEdits: RawHashlineEdit[]): HashlineEdit[] {