|
@@ -29,24 +29,44 @@ function patch(diff: ReviewDiff) {
|
|
|
if (typeof diff.patch === "string") {
|
|
if (typeof diff.patch === "string") {
|
|
|
try {
|
|
try {
|
|
|
const [patch] = parsePatch(diff.patch)
|
|
const [patch] = parsePatch(diff.patch)
|
|
|
- const beforeLines = []
|
|
|
|
|
- const afterLines = []
|
|
|
|
|
|
|
+ const beforeLines: Array<{ text: string; newline: boolean }> = []
|
|
|
|
|
+ const afterLines: Array<{ text: string; newline: boolean }> = []
|
|
|
|
|
+ let previous: "-" | "+" | " " | undefined
|
|
|
|
|
|
|
|
for (const hunk of patch.hunks) {
|
|
for (const hunk of patch.hunks) {
|
|
|
for (const line of hunk.lines) {
|
|
for (const line of hunk.lines) {
|
|
|
|
|
+ if (line.startsWith("\\")) {
|
|
|
|
|
+ if (previous === "-" || previous === " ") {
|
|
|
|
|
+ const before = beforeLines.at(-1)
|
|
|
|
|
+ if (before) before.newline = false
|
|
|
|
|
+ }
|
|
|
|
|
+ if (previous === "+" || previous === " ") {
|
|
|
|
|
+ const after = afterLines.at(-1)
|
|
|
|
|
+ if (after) after.newline = false
|
|
|
|
|
+ }
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
if (line.startsWith("-")) {
|
|
if (line.startsWith("-")) {
|
|
|
- beforeLines.push(line.slice(1))
|
|
|
|
|
|
|
+ beforeLines.push({ text: line.slice(1), newline: true })
|
|
|
|
|
+ previous = "-"
|
|
|
} else if (line.startsWith("+")) {
|
|
} else if (line.startsWith("+")) {
|
|
|
- afterLines.push(line.slice(1))
|
|
|
|
|
|
|
+ afterLines.push({ text: line.slice(1), newline: true })
|
|
|
|
|
+ previous = "+"
|
|
|
} else {
|
|
} else {
|
|
|
// context line (starts with ' ')
|
|
// context line (starts with ' ')
|
|
|
- beforeLines.push(line.slice(1))
|
|
|
|
|
- afterLines.push(line.slice(1))
|
|
|
|
|
|
|
+ beforeLines.push({ text: line.slice(1), newline: true })
|
|
|
|
|
+ afterLines.push({ text: line.slice(1), newline: true })
|
|
|
|
|
+ previous = " "
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return { before: beforeLines.join("\n"), after: afterLines.join("\n"), patch: diff.patch }
|
|
|
|
|
|
|
+ return {
|
|
|
|
|
+ before: beforeLines.map((line) => line.text + (line.newline ? "\n" : "")).join(""),
|
|
|
|
|
+ after: afterLines.map((line) => line.text + (line.newline ? "\n" : "")).join(""),
|
|
|
|
|
+ patch: diff.patch,
|
|
|
|
|
+ }
|
|
|
} catch {
|
|
} catch {
|
|
|
return { before: "", after: "", patch: diff.patch }
|
|
return { before: "", after: "", patch: diff.patch }
|
|
|
}
|
|
}
|