File size: 4,944 Bytes
4c76b0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Interactive terminal diff renderer.
 *
 * Produces colored line and intra-line highlights for the Pi TUI review surfaces.
 */
import { diffWords } from "diff";
import { interactiveAgentTheme as theme } from "../theme/theme.js";

/**
 * Parse diff line to extract prefix, line number, and content.
 * Format: "+123 content" or "-123 content" or " 123 content" or "     ..."
 */
function parseDiffLine(line: string): { prefix: string; lineNum: string; content: string } | null {
  const match = line.match(/^([+-\s])(\s*\d*)\s(.*)$/);
  if (!match) {
    return null;
  }
  const [, prefix, lineNum, content] = match;
  return prefix !== undefined && lineNum !== undefined && content !== undefined
    ? { prefix, lineNum, content }
    : null;
}

/**
 * Replace tabs with spaces for consistent rendering.
 */
function replaceTabs(text: string): string {
  return text.replace(/\t/g, "   ");
}

/**
 * Compute word-level diff and render with inverse on changed parts.
 * Uses diffWords which groups whitespace with adjacent words for cleaner highlighting.
 * Strips leading whitespace from inverse to avoid highlighting indentation.
 */
function renderIntraLineDiff(
  oldContent: string,
  newContent: string,
): { removedLine: string; addedLine: string } {
  let removedLine = "";
  let addedLine = "";
  const seen = { added: false, removed: false };

  for (const part of diffWords(oldContent, newContent)) {
    const kind = part.added ? "added" : part.removed ? "removed" : undefined;
    let value = part.value;
    if (kind) {
      const changed = seen[kind] ? value : value.trimStart();
      const leadingWhitespace = value.slice(0, value.length - changed.length);
      value = leadingWhitespace + (changed ? theme.inverse(changed) : "");
      seen[kind] = true;
    }
    if (!part.added) {
      removedLine += value;
    }
    if (!part.removed) {
      addedLine += value;
    }
  }

  return { removedLine, addedLine };
}

/**
 * Render a diff string with colored lines and intra-line change highlighting.
 * - Context lines: dim/gray
 * - Removed lines: red, with inverse on changed tokens
 * - Added lines: green, with inverse on changed tokens
 */
export function renderDiff(diffText: string): string {
  const lines = diffText.split("\n");
  const result: string[] = [];

  let i = 0;
  while (i < lines.length) {
    const line = lines.at(i);
    if (line === undefined) {
      break;
    }
    const parsed = parseDiffLine(line);

    if (!parsed) {
      result.push(theme.fg("toolDiffContext", line));
      i++;
      continue;
    }

    if (parsed.prefix === "-") {
      // Collect consecutive removed lines
      const removedLines: { lineNum: string; content: string }[] = [];
      while (i < lines.length) {
        const currentLine = lines.at(i);
        const p = currentLine === undefined ? null : parseDiffLine(currentLine);
        if (!p || p.prefix !== "-") {
          break;
        }
        removedLines.push({ lineNum: p.lineNum, content: p.content });
        i++;
      }

      // Collect consecutive added lines
      const addedLines: { lineNum: string; content: string }[] = [];
      while (i < lines.length) {
        const currentLine = lines.at(i);
        const p = currentLine === undefined ? null : parseDiffLine(currentLine);
        if (!p || p.prefix !== "+") {
          break;
        }
        addedLines.push({ lineNum: p.lineNum, content: p.content });
        i++;
      }

      // Only do intra-line diffing when there's exactly one removed and one added line
      // (indicating a single line modification). Otherwise, show lines as-is.
      if (removedLines.length === 1 && addedLines.length === 1) {
        const removed = removedLines[0];
        const added = addedLines[0];
        if (!removed || !added) {
          continue;
        }

        const { removedLine, addedLine } = renderIntraLineDiff(
          replaceTabs(removed.content),
          replaceTabs(added.content),
        );

        result.push(theme.fg("toolDiffRemoved", `-${removed.lineNum} ${removedLine}`));
        result.push(theme.fg("toolDiffAdded", `+${added.lineNum} ${addedLine}`));
      } else {
        // Show all removed lines first, then all added lines
        for (const removed of removedLines) {
          result.push(
            theme.fg("toolDiffRemoved", `-${removed.lineNum} ${replaceTabs(removed.content)}`),
          );
        }
        for (const added of addedLines) {
          result.push(theme.fg("toolDiffAdded", `+${added.lineNum} ${replaceTabs(added.content)}`));
        }
      }
    } else if (parsed.prefix === "+") {
      // Standalone added line
      result.push(theme.fg("toolDiffAdded", `+${parsed.lineNum} ${replaceTabs(parsed.content)}`));
      i++;
    } else {
      // Context line
      result.push(theme.fg("toolDiffContext", ` ${parsed.lineNum} ${replaceTabs(parsed.content)}`));
      i++;
    }
  }

  return result.join("\n");
}