summaryrefslogtreecommitdiffhomepage
path: root/packages/web/src/components/DiffView.tsx
blob: 237f25bdf7c0268ba29c02a5f45a90a29abcd7ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { type Component, createMemo } from "solid-js"
import { parsePatch } from "diff"
import CodeBlock from "./CodeBlock"
import styles from "./diffview.module.css"

type DiffRow = {
  left: string
  right: string
  type: "added" | "removed" | "unchanged" | "modified"
}

interface DiffViewProps {
  diff: string
  lang?: string
  class?: string
}

const DiffView: Component<DiffViewProps> = (props) => {

  const rows = createMemo(() => {
    const diffRows: DiffRow[] = []

    try {
      const patches = parsePatch(props.diff)

      for (const patch of patches) {
        for (const hunk of patch.hunks) {
          const lines = hunk.lines
          let i = 0

          while (i < lines.length) {
            const line = lines[i]
            const content = line.slice(1)
            const prefix = line[0]

            if (prefix === '-') {
              // Look ahead for consecutive additions to pair with removals
              const removals: string[] = [content]
              let j = i + 1

              // Collect all consecutive removals
              while (j < lines.length && lines[j][0] === '-') {
                removals.push(lines[j].slice(1))
                j++
              }

              // Collect all consecutive additions that follow
              const additions: string[] = []
              while (j < lines.length && lines[j][0] === '+') {
                additions.push(lines[j].slice(1))
                j++
              }

              // Pair removals with additions
              const maxLength = Math.max(removals.length, additions.length)
              for (let k = 0; k < maxLength; k++) {
                const hasLeft = !!removals[k]
                const hasRight = !!additions[k]

                if (hasLeft && hasRight) {
                  // Replacement - left is removed, right is added
                  diffRows.push({
                    left: removals[k],
                    right: additions[k],
                    type: "modified"
                  })
                } else if (hasLeft) {
                  // Pure removal
                  diffRows.push({
                    left: removals[k],
                    right: "",
                    type: "removed"
                  })
                } else {
                  // Pure addition
                  diffRows.push({
                    left: "",
                    right: additions[k],
                    type: "added"
                  })
                }
              }

              i = j
            } else if (prefix === '+') {
              // Standalone addition (not paired with removal)
              diffRows.push({
                left: "",
                right: content,
                type: "added"
              })
              i++
            } else if (prefix === ' ') {
              diffRows.push({
                left: content,
                right: content,
                type: "unchanged"
              })
              i++
            } else {
              i++
            }
          }
        }
      }
    } catch (error) {
      console.error("Failed to parse patch:", error)
      return []
    }

    return diffRows
  })

  return (
    <div class={`${styles.diff} ${props.class ?? ""}`}>
      {rows().map((r) => (
        <div class={styles.row}>
          <div class={styles.beforeColumn}>
            <CodeBlock
              code={r.left}
              lang={props.lang}
              data-section="cell"
              data-diff-type={r.type === "removed" || r.type === "modified" ? "removed" : ""}
              data-display-mobile={r.type === "added" && !r.left ? "false" : undefined}
            />
            {(r.type === "added" || r.type === "modified") && r.right !== undefined && (
              <CodeBlock
                code={r.right}
                lang={props.lang}
                data-section="cell"
                data-diff-type="added"
                data-display-mobile="true"
              />
            )}
          </div>

          <div class={styles.afterColumn}>
            <CodeBlock
              code={r.right}
              lang={props.lang}
              data-section="cell"
              data-diff-type={r.type === "added" || r.type === "modified" ? "added" : ""}
            />
          </div>
        </div>
      ))}
    </div>
  )
}

export default DiffView

// String to test diff viewer with
const testDiff = `--- combined_before.txt	2025-06-24 16:38:08
+++ combined_after.txt	2025-06-24 16:38:12
@@ -1,21 +1,25 @@
 unchanged line
-deleted line
-old content
+added line
+new content
 
-removed empty line below
+added empty line above
 
-	tab indented
-trailing spaces   
-very long line that will definitely wrap in most editors and cause potential alignment issues when displayed in a two column diff view
-unicode content: 🚀 ✨ 中文
-mixed	content with	tabs and spaces
+    space indented
+no trailing spaces
+short line
+very long replacement line that will also wrap and test how the diff viewer handles long line additions after short line removals
+different unicode: 🎉 💻 日本語
+normalized content with consistent spacing
+newline to content
 
-content to remove
-whitespace only:    	  
-multiple
-consecutive
-deletions
-single deletion
+    	  
+single addition
+first addition
+second addition
+third addition
 line before addition
+first added line
+
+third added line
 line after addition
 final unchanged line`