refactor(packages): extract hashline-core package
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
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import { createTwoFilesPatch } from "diff"
|
||||
import { computeLineHash } from "./hash-computation"
|
||||
|
||||
export function toHashlineContent(content: string): string {
|
||||
if (!content) return content
|
||||
const lines = content.split("\n")
|
||||
const lastLine = lines[lines.length - 1]
|
||||
const hasTrailingNewline = lastLine === ""
|
||||
const contentLines = hasTrailingNewline ? lines.slice(0, -1) : lines
|
||||
const hashlined = contentLines.map((line, i) => {
|
||||
const lineNum = i + 1
|
||||
const hash = computeLineHash(lineNum, line)
|
||||
return `${lineNum}#${hash}|${line}`
|
||||
})
|
||||
return hasTrailingNewline ? hashlined.join("\n") + "\n" : hashlined.join("\n")
|
||||
}
|
||||
|
||||
export function generateUnifiedDiff(oldContent: string, newContent: string, filePath: string): string {
|
||||
return createTwoFilesPatch(filePath, filePath, oldContent, newContent, undefined, undefined, { context: 3 })
|
||||
}
|
||||
|
||||
export function countLineDiffs(oldContent: string, newContent: string): { additions: number; deletions: number } {
|
||||
const oldLines = oldContent.split("\n")
|
||||
const newLines = newContent.split("\n")
|
||||
|
||||
const oldSet = new Map<string, number>()
|
||||
for (const line of oldLines) {
|
||||
oldSet.set(line, (oldSet.get(line) ?? 0) + 1)
|
||||
}
|
||||
|
||||
const newSet = new Map<string, number>()
|
||||
for (const line of newLines) {
|
||||
newSet.set(line, (newSet.get(line) ?? 0) + 1)
|
||||
}
|
||||
|
||||
let deletions = 0
|
||||
for (const [line, count] of oldSet) {
|
||||
const newCount = newSet.get(line) ?? 0
|
||||
if (count > newCount) {
|
||||
deletions += count - newCount
|
||||
}
|
||||
}
|
||||
|
||||
let additions = 0
|
||||
for (const [line, count] of newSet) {
|
||||
const oldCount = oldSet.get(line) ?? 0
|
||||
if (count > oldCount) {
|
||||
additions += count - oldCount
|
||||
}
|
||||
}
|
||||
|
||||
return { additions, deletions }
|
||||
}
|
||||
Reference in New Issue
Block a user