content-diff.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import { parsePatch } from "diff"
  2. import { createMemo, For } from "solid-js"
  3. import { ContentCode } from "./content-code"
  4. import styles from "./content-diff.module.css"
  5. type DiffRow = {
  6. left: string
  7. right: string
  8. type: "added" | "removed" | "unchanged" | "modified"
  9. }
  10. interface Props {
  11. diff: string
  12. lang?: string
  13. }
  14. export function ContentDiff(props: Props) {
  15. const rows = createMemo(() => {
  16. const diffRows: DiffRow[] = []
  17. try {
  18. const patches = parsePatch(props.diff)
  19. for (const patch of patches) {
  20. for (const hunk of patch.hunks) {
  21. const lines = hunk.lines
  22. let i = 0
  23. while (i < lines.length) {
  24. const line = lines[i]
  25. const content = line.slice(1)
  26. const prefix = line[0]
  27. if (prefix === "-") {
  28. // Look ahead for consecutive additions to pair with removals
  29. const removals: string[] = [content]
  30. let j = i + 1
  31. // Collect all consecutive removals
  32. while (j < lines.length && lines[j][0] === "-") {
  33. removals.push(lines[j].slice(1))
  34. j++
  35. }
  36. // Collect all consecutive additions that follow
  37. const additions: string[] = []
  38. while (j < lines.length && lines[j][0] === "+") {
  39. additions.push(lines[j].slice(1))
  40. j++
  41. }
  42. // Pair removals with additions
  43. const maxLength = Math.max(removals.length, additions.length)
  44. for (let k = 0; k < maxLength; k++) {
  45. const hasLeft = k < removals.length
  46. const hasRight = k < additions.length
  47. if (hasLeft && hasRight) {
  48. // Replacement - left is removed, right is added
  49. diffRows.push({
  50. left: removals[k],
  51. right: additions[k],
  52. type: "modified",
  53. })
  54. } else if (hasLeft) {
  55. // Pure removal
  56. diffRows.push({
  57. left: removals[k],
  58. right: "",
  59. type: "removed",
  60. })
  61. } else if (hasRight) {
  62. // Pure addition - only create if we actually have content
  63. diffRows.push({
  64. left: "",
  65. right: additions[k],
  66. type: "added",
  67. })
  68. }
  69. }
  70. i = j
  71. } else if (prefix === "+") {
  72. // Standalone addition (not paired with removal)
  73. diffRows.push({
  74. left: "",
  75. right: content,
  76. type: "added",
  77. })
  78. i++
  79. } else if (prefix === " ") {
  80. diffRows.push({
  81. left: content === "" ? " " : content,
  82. right: content === "" ? " " : content,
  83. type: "unchanged",
  84. })
  85. i++
  86. } else {
  87. i++
  88. }
  89. }
  90. }
  91. }
  92. } catch (error) {
  93. console.error("Failed to parse patch:", error)
  94. return []
  95. }
  96. return diffRows
  97. })
  98. const mobileRows = createMemo(() => {
  99. const mobileBlocks: {
  100. type: "removed" | "added" | "unchanged"
  101. lines: string[]
  102. }[] = []
  103. const currentRows = rows()
  104. let i = 0
  105. while (i < currentRows.length) {
  106. const removedLines: string[] = []
  107. const addedLines: string[] = []
  108. // Collect consecutive modified/removed/added rows
  109. while (
  110. i < currentRows.length &&
  111. (currentRows[i].type === "modified" || currentRows[i].type === "removed" || currentRows[i].type === "added")
  112. ) {
  113. const row = currentRows[i]
  114. if (row.left && (row.type === "removed" || row.type === "modified")) {
  115. removedLines.push(row.left)
  116. }
  117. if (row.right && (row.type === "added" || row.type === "modified")) {
  118. addedLines.push(row.right)
  119. }
  120. i++
  121. }
  122. // Add grouped blocks
  123. if (removedLines.length > 0) {
  124. mobileBlocks.push({ type: "removed", lines: removedLines })
  125. }
  126. if (addedLines.length > 0) {
  127. mobileBlocks.push({ type: "added", lines: addedLines })
  128. }
  129. // Add unchanged rows as-is
  130. if (i < currentRows.length && currentRows[i].type === "unchanged") {
  131. mobileBlocks.push({
  132. type: "unchanged",
  133. lines: [currentRows[i].left],
  134. })
  135. i++
  136. }
  137. }
  138. return mobileBlocks
  139. })
  140. return (
  141. <div class={styles.root}>
  142. <div data-component="desktop">
  143. <For each={rows()}>
  144. {(row) => (
  145. <div data-component="diff-row" data-type={row.type}>
  146. <div
  147. data-slot="before"
  148. data-diff-type={row.type === "removed" || row.type === "modified" ? "removed" : ""}
  149. >
  150. <ContentCode code={row.left} flush lang={props.lang} />
  151. </div>
  152. <div data-slot="after" data-diff-type={row.type === "added" || row.type === "modified" ? "added" : ""}>
  153. <ContentCode code={row.right} lang={props.lang} flush />
  154. </div>
  155. </div>
  156. )}
  157. </For>
  158. </div>
  159. <div data-component="mobile">
  160. <For each={mobileRows()}>
  161. {(block) => (
  162. <div data-component="diff-block" data-type={block.type}>
  163. <For each={block.lines}>
  164. {(line) => (
  165. <div data-diff-type={block.type === "removed" ? "removed" : block.type === "added" ? "added" : ""}>
  166. <ContentCode code={line} lang={props.lang} flush />
  167. </div>
  168. )}
  169. </For>
  170. </div>
  171. )}
  172. </For>
  173. </div>
  174. </div>
  175. )
  176. }
  177. // const testDiff = `--- combined_before.txt 2025-06-24 16:38:08
  178. // +++ combined_after.txt 2025-06-24 16:38:12
  179. // @@ -1,21 +1,25 @@
  180. // unchanged line
  181. // -deleted line
  182. // -old content
  183. // +added line
  184. // +new content
  185. //
  186. // -removed empty line below
  187. // +added empty line above
  188. //
  189. // - tab indented
  190. // -trailing spaces
  191. // -very long line that will definitely wrap in most editors and cause potential alignment issues when displayed in a two column diff view
  192. // -unicode content: 🚀 ✨ 中文
  193. // -mixed content with tabs and spaces
  194. // + space indented
  195. // +no trailing spaces
  196. // +short line
  197. // +very long replacement line that will also wrap and test how the diff viewer handles long line additions after short line removals
  198. // +different unicode: 🎉 💻 日本語
  199. // +normalized content with consistent spacing
  200. // +newline to content
  201. //
  202. // -content to remove
  203. // -whitespace only:
  204. // -multiple
  205. // -consecutive
  206. // -deletions
  207. // -single deletion
  208. // +
  209. // +single addition
  210. // +first addition
  211. // +second addition
  212. // +third addition
  213. // line before addition
  214. // +first added line
  215. // +
  216. // +third added line
  217. // line after addition
  218. // final unchanged line`