refactor(hashline-edit): replace custom diff with diff library
🤖 Generated with assistance of [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { createTwoFilesPatch } from "diff"
|
||||
import { computeLineHash } from "./hash-computation"
|
||||
|
||||
export function toHashlineContent(content: string): string {
|
||||
@@ -15,59 +16,7 @@ export function toHashlineContent(content: string): string {
|
||||
}
|
||||
|
||||
export function generateUnifiedDiff(oldContent: string, newContent: string, filePath: string): string {
|
||||
const oldLines = oldContent.split("\n")
|
||||
const newLines = newContent.split("\n")
|
||||
const maxLines = Math.max(oldLines.length, newLines.length)
|
||||
|
||||
let diff = `--- ${filePath}\n+++ ${filePath}\n`
|
||||
let inHunk = false
|
||||
let oldStart = 1
|
||||
let newStart = 1
|
||||
let oldCount = 0
|
||||
let newCount = 0
|
||||
let hunkLines: string[] = []
|
||||
|
||||
for (let i = 0; i < maxLines; i++) {
|
||||
const oldLine = oldLines[i] ?? ""
|
||||
const newLine = newLines[i] ?? ""
|
||||
|
||||
if (oldLine !== newLine) {
|
||||
if (!inHunk) {
|
||||
oldStart = i + 1
|
||||
newStart = i + 1
|
||||
oldCount = 0
|
||||
newCount = 0
|
||||
hunkLines = []
|
||||
inHunk = true
|
||||
}
|
||||
|
||||
if (oldLines[i] !== undefined) {
|
||||
hunkLines.push(`-${oldLine}`)
|
||||
oldCount++
|
||||
}
|
||||
if (newLines[i] !== undefined) {
|
||||
hunkLines.push(`+${newLine}`)
|
||||
newCount++
|
||||
}
|
||||
} else if (inHunk) {
|
||||
hunkLines.push(` ${oldLine}`)
|
||||
oldCount++
|
||||
newCount++
|
||||
|
||||
if (hunkLines.length > 6) {
|
||||
diff += `@@ -${oldStart},${oldCount} +${newStart},${newCount} @@\n`
|
||||
diff += hunkLines.join("\n") + "\n"
|
||||
inHunk = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (inHunk && hunkLines.length > 0) {
|
||||
diff += `@@ -${oldStart},${oldCount} +${newStart},${newCount} @@\n`
|
||||
diff += hunkLines.join("\n") + "\n"
|
||||
}
|
||||
|
||||
return diff || `--- ${filePath}\n+++ ${filePath}\n`
|
||||
return createTwoFilesPatch(filePath, filePath, oldContent, newContent)
|
||||
}
|
||||
|
||||
export function countLineDiffs(oldContent: string, newContent: string): { additions: number; deletions: number } {
|
||||
|
||||
Reference in New Issue
Block a user