(text: string)
| 101 | } |
| 102 | |
| 103 | function getDiffLines(text: string): FileEditDiffLine[] { |
| 104 | const rawLines = text.split(/\r?\n/); |
| 105 | const hasUnifiedDiff = rawLines.some((line) => |
| 106 | /^(diff --git|--- |\+\+\+ |@@)/.test(line), |
| 107 | ); |
| 108 | const lines: FileEditDiffLine[] = []; |
| 109 | let inUnifiedDiff = false; |
| 110 | |
| 111 | for (const rawLine of rawLines) { |
| 112 | const shikiKind = getShikiDiffKind(rawLine); |
| 113 | if (shikiKind) { |
| 114 | lines.push({ |
| 115 | kind: shikiKind, |
| 116 | text: rawLine.replace( |
| 117 | shikiKind === "add" ? SHIKI_ADD_RE : SHIKI_REMOVE_RE, |
| 118 | "", |
| 119 | ), |
| 120 | }); |
| 121 | continue; |
| 122 | } |
| 123 | |
| 124 | if (hasUnifiedDiff && !inUnifiedDiff) { |
| 125 | if (!/^(diff --git|--- |\+\+\+ |@@)/.test(rawLine)) { |
| 126 | continue; |
| 127 | } |
| 128 | inUnifiedDiff = true; |
| 129 | } |
| 130 | |
| 131 | if (hasUnifiedDiff) { |
| 132 | lines.push(classifyUnifiedDiffLine(rawLine)); |
| 133 | continue; |
| 134 | } |
| 135 | |
| 136 | lines.push({ kind: "context", text: rawLine }); |
| 137 | } |
| 138 | |
| 139 | return trimTrailingEmptyContextLines( |
| 140 | lines.filter((line) => line.text.length > 0 || line.kind !== "meta"), |
| 141 | ); |
| 142 | } |
| 143 | |
| 144 | function trimTrailingEmptyContextLines( |
| 145 | lines: FileEditDiffLine[], |
no test coverage detected