sailfork / t3code-github / apps/ mobile/ src/ features/ review/ nativeReviewDiffAdapter.ts

import type { NativeReviewDiffRow, NativeReviewDiffTheme } from "../diffs/nativeReviewDiffSurface";
import type { NativeReviewDiffFile } from "../diffs/nativeReviewDiffTypes";
import * as Arr from "effect/Array";
import { pipe } from "effect/Function";
import type { ResolvedMobileCodeSurface } from "../../lib/appearancePreferences";
import { resolveHighlightLanguage } from "../../lib/highlightLanguage";
import { type MobileThemeId, type MobileThemeVariables } from "../../lib/mobileTheme";
import { getMobileTerminalTheme, type TerminalAppearanceScheme } from "../terminal/terminalTheme";
import { computeWordAltDiffRanges } from "./reviewWordDiffs";
import {
  getReviewFilePreviewState,
  type ReviewParsedDiff,
  type ReviewRenderableFile,
  type ReviewRenderableLineRow,
} from "./reviewModel";
import type { ReviewInlineComment } from "./reviewCommentSelection";

const NATIVE_REVIEW_MAX_WORD_DIFF_RANGE_COUNT = 4;
const NATIVE_REVIEW_MAX_WORD_DIFF_COVERAGE = 0.45;
// Keep eager word matching within the old 2,000-row budget while retaining more rendered rows.
const NATIVE_REVIEW_MAX_WORD_DIFF_COMPARISONS = 1_000;
// Bound the rows serialized to the native heap before mapping and word matching.
const NATIVE_REVIEW_MAX_FILE_ROWS = 100_000;
const TRUNCATED_FILE_NOTICE = "This diff is too large to show in full.";
const NATIVE_HEX_COLOR = /^#([\da-f]{2})([\da-f]{2})([\da-f]{2})([\da-f]{2})?$/i;
const NATIVE_RGBA_COLOR =
  /^rgba?\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)(?:\s*,\s*([\d.]+))?\s*\)$/;

export const NATIVE_REVIEW_DIFF_CONTENT_WIDTH = 2_800;

export function buildNativeReviewSnippetRows(
  comment: Pick<ReviewInlineComment, "id" | "diff" | "fenceLanguage">,
): NativeReviewDiffRow[] {
  if ((comment.fenceLanguage ?? "diff") !== "diff" || !comment.diff.trim()) return [];
  const lines = comment.diff.replace(/\r\n/g, "\n").replace(/\n$/, "").split("\n");
  if (lines.some((line) => !/^[ +-]/.test(line) || /^(---|\+\+\+) /.test(line))) return [];
  return lines.map((line, index) => ({
    kind: "line",
    id: `${comment.id}:snippet:${index}`,
    content: line.slice(1),
    change: line[0] === "+" ? "add" : line[0] === "-" ? "delete" : "context",
    oldLineNumber: null,
    newLineNumber: null,
  }));
}

function opaqueNativeHexColor(color: string, background: string): string {
  const hex = NATIVE_HEX_COLOR.exec(color);
  if (hex && !hex[4]) return color;

  const rgba = NATIVE_RGBA_COLOR.exec(color);
  const backgroundHex = NATIVE_HEX_COLOR.exec(background);
  if ((!hex && !rgba) || !backgroundHex) return background;

  const alpha = hex
    ? Number.parseInt(hex[4] ?? "ff", 16) / 255
    : rgba?.[4] === undefined
      ? 1
      : Math.min(1, Math.max(0, Number(rgba[4])));
  const channels = [1, 2, 3].map((index) => {
    const foreground = hex ? Number.parseInt(hex[index] ?? "0", 16) : Number(rgba?.[index]);
    const behind = Number.parseInt(backgroundHex[index] ?? "0", 16);
    return Math.round(foreground * alpha + behind * (1 - alpha));
  });
  return `#${channels.map((channel) => channel.toString(16).padStart(2, "0")).join("")}`;
}

export function createNativeReviewDiffStyle(
  codeSurface: ResolvedMobileCodeSurface,
  wordWrap: boolean,
) {
  return {
    wordWrap,
    rowHeight: codeSurface.rowHeight,
    contentWidth: NATIVE_REVIEW_DIFF_CONTENT_WIDTH,
    changeBarWidth: 4,
    gutterWidth: codeSurface.gutterWidth,
    codePadding: codeSurface.codePadding,
    textVerticalInset: codeSurface.textVerticalInset,
    fileHeaderHeight: 56,
    fileHeaderHorizontalMargin: 8,
    fileHeaderVerticalMargin: 6,
    fileHeaderCornerRadius: 10,
    fileHeaderHorizontalPadding: 10,
    fileHeaderPathRightPadding: 118,
    fileHeaderCountColumnWidth: 38,
    fileHeaderCountGap: 5,
    codeFontSize: codeSurface.fontSize,
    codeFontWeight: "regular",
    lineNumberFontSize: codeSurface.lineNumberFontSize,
    lineNumberFontWeight: "regular",
    hunkFontSize: 11,
    hunkFontWeight: "medium",
    fileHeaderFontSize: 11,
    fileHeaderFontWeight: "semibold",
    fileHeaderMetaFontSize: 10,
    fileHeaderMetaFontWeight: "semibold",
    fileHeaderSubtextFontSize: 11,
    fileHeaderSubtextFontWeight: "medium",
    fileHeaderStatusFontSize: 9,
    fileHeaderStatusFontWeight: "bold",
    emptyStateFontSize: 12,
    emptyStateFontWeight: "medium",
  } as const;
}

export interface NativeReviewDiffData {
  readonly rows: ReadonlyArray<NativeReviewDiffRow>;
  readonly files: ReadonlyArray<NativeReviewDiffFile>;
  readonly commentTargetsByRowId: ReadonlyMap<string, NativeReviewDiffCommentTarget>;
  readonly rowIdByCommentLineId: ReadonlyMap<string, string>;
  readonly additions: number;
  readonly deletions: number;
}

export interface NativeReviewDiffCommentTarget {
  readonly filePath: string;
  readonly lines: ReadonlyArray<ReviewRenderableLineRow>;
  readonly lineIndex: number;
}

export interface BuildNativeReviewDiffDataInput {
  readonly parsedDiff: ReviewParsedDiff;
  readonly comments?: ReadonlyArray<ReviewInlineComment>;
}

interface CachedNativeReviewDiffData {
  readonly prepared: PreparedNativeReviewDiffData;
  readonly commentsKey: string;
  readonly data: NativeReviewDiffData;
}

interface PreparedNativeReviewFileRows {
  readonly fileId: string;
  readonly filePath: string;
  readonly lineCount: number;
  readonly truncated: boolean;
  readonly rows: ReadonlyArray<NativeReviewDiffRow>;
  readonly commentTargetsByRowId: ReadonlyMap<string, NativeReviewDiffCommentTarget>;
  readonly rowIdByCommentLineId: ReadonlyMap<string, string>;
  commentedRows: {
    readonly commentsKey: string;
    readonly rows: ReadonlyArray<NativeReviewDiffRow>;
  } | null;
}

interface PreparedNativeReviewDiffData extends Omit<NativeReviewDiffData, "rows"> {
  readonly fileRows: ReadonlyArray<PreparedNativeReviewFileRows>;
}

const nativeReviewDiffDataCache = new WeakMap<ReviewParsedDiff, CachedNativeReviewDiffData>();
const nativeReviewFileRowsCache = new WeakMap<ReviewRenderableFile, PreparedNativeReviewFileRows>();

function buildReviewCommentsCacheKey(comments: ReadonlyArray<ReviewInlineComment>): string {
  if (comments.length === 0) {
    return "none";
  }

  return JSON.stringify(
    comments.map((comment) => [
      comment.id,
      comment.sectionId,
      comment.sectionTitle,
      comment.filePath,
      comment.startIndex,
      comment.endIndex,
      comment.rangeLabel,
      comment.text,
    ]),
  );
}

export function createNativeReviewDiffTheme(
  scheme: TerminalAppearanceScheme,
  themeId: MobileThemeId,
  appTheme: MobileThemeVariables,
): NativeReviewDiffTheme {
  const terminalTheme = getMobileTerminalTheme(themeId, scheme);
  const [, terminalRed] = terminalTheme.palette;
  // Swift and Android disagree on alpha-byte order; flatten to shared opaque #RRGGBB.
  const screen = opaqueNativeHexColor(
    appTheme["--color-screen"],
    scheme === "dark" ? "#000000" : "#ffffff",
  );
  const background = opaqueNativeHexColor(appTheme["--color-md-code-bg"], screen);
  const nativeColor = (color: string) => opaqueNativeHexColor(color, background);

  if (scheme === "dark") {
    return {
      background,
      text: nativeColor(appTheme["--color-md-code-text"]),
      mutedText: nativeColor(appTheme["--color-foreground-muted"]),
      headerBackground: background,
      border: nativeColor(appTheme["--color-border"]),
      hunkBackground: nativeColor(appTheme["--color-subtle-strong"]),
      hunkText: nativeColor(appTheme["--color-foreground"]),
      addBackground: "#0d2f28",
      deleteBackground: "#391415",
      addBar: "#00cab1",
      deleteBar: terminalRed ?? "#ff2e3f",
      addText: "#5ECC71",
      deleteText: "#FF6762",
    };
  }

  return {
    background,
    text: nativeColor(appTheme["--color-md-code-text"]),
    mutedText: nativeColor(appTheme["--color-foreground-muted"]),
    headerBackground: background,
    border: nativeColor(appTheme["--color-border"]),
    hunkBackground: nativeColor(appTheme["--color-subtle-strong"]),
    hunkText: nativeColor(appTheme["--color-foreground"]),
    addBackground: "#e5f8f5",
    deleteBackground: "#ffe6e7",
    addBar: "#00cab1",
    deleteBar: terminalRed ?? "#ff2e3f",
    addText: "#199F43",
    deleteText: "#D52C36",
  };
}

function mapChangeType(file: ReviewRenderableFile): NativeReviewDiffRow["changeType"] {
  switch (file.changeType) {
    case "change":
      return "modified";
    case "new":
    case "deleted":
    case "rename-pure":
    case "rename-changed":
      return file.changeType;
    default:
      return "modified";
  }
}

function createNoticeRow(fileId: string, suffix: string, text: string): NativeReviewDiffRow {
  return {
    kind: "notice",
    id: `${fileId}:notice:${suffix}`,
    fileId,
    text,
  };
}

function noticeRowsForFile(file: ReviewRenderableFile): ReadonlyArray<NativeReviewDiffRow> {
  if (file.notice) return [createNoticeRow(file.id, "loading", file.notice)];
  if (file.rows.length > 0) {
    return [];
  }

  const previewState = getReviewFilePreviewState(file);
  if (previewState.kind === "suppressed" && previewState.reason === "non-text") {
    return [
      createNoticeRow(file.id, "non-text", "Unsupported format. Diff contents are not available."),
    ];
  }

  if (file.changeType === "rename-pure") {
    return [createNoticeRow(file.id, "rename", "This file was renamed without modifications.")];
  }

  return [];
}

function trimWordDiffRanges(
  content: string,
  ranges: NonNullable<NativeReviewDiffRow["wordDiffRanges"]>,
): NonNullable<NativeReviewDiffRow["wordDiffRanges"]> {
  return pipe(
    ranges,
    Arr.flatMap((range) => {
      let start = Math.max(0, range.start);
      let end = Math.min(content.length, range.end);

      while (start < end && /\s/.test(content[start] ?? "")) {
        start += 1;
      }
      while (end > start && /\s/.test(content[end - 1] ?? "")) {
        end -= 1;
      }

      return end > start ? [{ start, end }] : [];
    }),
  );
}

function nonWhitespaceLength(value: string) {
  return value.replace(/\s/g, "").length;
}

function shouldUseWordDiffRanges(
  content: string,
  ranges: NonNullable<NativeReviewDiffRow["wordDiffRanges"]>,
) {
  if (ranges.length === 0 || ranges.length > NATIVE_REVIEW_MAX_WORD_DIFF_RANGE_COUNT) {
    return false;
  }

  const meaningfulLength = nonWhitespaceLength(content);
  if (meaningfulLength === 0) {
    return false;
  }

  const highlightedLength = ranges.reduce(
    (total, range) => total + nonWhitespaceLength(content.slice(range.start, range.end)),
    0,
  );
  return highlightedLength / meaningfulLength <= NATIVE_REVIEW_MAX_WORD_DIFF_COVERAGE;
}

function addNativeWordDiffRanges(
  rows: ReadonlyArray<NativeReviewDiffRow>,
  lineRows: ReadonlyArray<ReviewRenderableLineRow>,
): ReadonlyArray<NativeReviewDiffRow> {
  const nextRows = [...rows];
  const addedRowIndexes = new Map<number, number>();
  const pairs: Array<{ deletionRowIndex: number; additionTokenIndex: number }> = [];
  let lineIndex = 0;

  // Pierre can split adjacent change blocks to align similar lines. Keep those pairings.
  rows.forEach((row, rowIndex) => {
    if (row.kind !== "line") return;
    const source = lineRows[lineIndex++];
    if (source?.change === "add" && source.additionTokenIndex !== null) {
      addedRowIndexes.set(source.additionTokenIndex, rowIndex);
    } else if (source?.change === "delete" && source.comparison?.change === "add") {
      pairs.push({ deletionRowIndex: rowIndex, additionTokenIndex: source.comparison.tokenIndex });
    }
  });

  let comparisonCount = 0;
  for (const { deletionRowIndex, additionTokenIndex } of pairs) {
    if (comparisonCount >= NATIVE_REVIEW_MAX_WORD_DIFF_COMPARISONS) break;
    const addedRowIndex = addedRowIndexes.get(additionTokenIndex);
    if (addedRowIndex === undefined) continue;
    const deletedRow = nextRows[deletionRowIndex];
    const addedRow = nextRows[addedRowIndex];
    if (!deletedRow?.content || !addedRow?.content) continue;

    comparisonCount += 1;
    const ranges = computeWordAltDiffRanges({
      deletionLine: deletedRow.content,
      additionLine: addedRow.content,
    });
    const deletionRanges = trimWordDiffRanges(deletedRow.content, ranges.deletion);
    const additionRanges = trimWordDiffRanges(addedRow.content, ranges.addition);

    if (shouldUseWordDiffRanges(deletedRow.content, deletionRanges)) {
      nextRows[deletionRowIndex] = { ...deletedRow, wordDiffRanges: deletionRanges };
    }
    if (shouldUseWordDiffRanges(addedRow.content, additionRanges)) {
      nextRows[addedRowIndex] = { ...addedRow, wordDiffRanges: additionRanges };
    }
  }

  return nextRows;
}

function mapLineRow(
  file: ReviewRenderableFile,
  row: ReviewRenderableLineRow,
  rowIndex: number,
): NativeReviewDiffRow {
  return {
    kind: "line",
    id: `${file.id}:line:${rowIndex}:${row.id}`,
    fileId: file.id,
    content: row.content,
    change: row.change,
    oldLineNumber: row.oldLineNumber,
    newLineNumber: row.newLineNumber,
  };
}

function prepareFileRows(file: ReviewRenderableFile): PreparedNativeReviewFileRows {
  const cached = nativeReviewFileRowsCache.get(file);
  if (cached) return cached;
  const commentTargetsByRowId = new Map<string, NativeReviewDiffCommentTarget>();
  const rowIdByCommentLineId = new Map<string, string>();
  const rows: NativeReviewDiffRow[] = [
    {
      kind: "file",
      id: `${file.id}:header`,
      fileId: file.id,
      filePath: file.path,
      previousPath: file.previousPath,
      changeType: mapChangeType(file),
      additions: file.additions,
      deletions: file.deletions,
    },
  ];

  const sourceRows =
    file.rows.length > NATIVE_REVIEW_MAX_FILE_ROWS
      ? file.rows.slice(0, NATIVE_REVIEW_MAX_FILE_ROWS)
      : file.rows;
  const lineRows = sourceRows.filter((row): row is ReviewRenderableLineRow => row.kind === "line");
  let lineIndex = 0;
  sourceRows.forEach((row, rowIndex) => {
    if (row.kind === "hunk") {
      rows.push({
        kind: "hunk",
        id: `${file.id}:hunk:${rowIndex}:${row.id}`,
        fileId: file.id,
        text: row.context ? `${row.header} ${row.context}` : row.header,
      });
      return;
    }

    const nativeRow = mapLineRow(file, row, rowIndex);
    rows.push(nativeRow);
    rowIdByCommentLineId.set(row.id, nativeRow.id);
    commentTargetsByRowId.set(nativeRow.id, {
      filePath: file.path,
      lines: lineRows,
      lineIndex,
    });
    lineIndex += 1;
  });

  if (sourceRows !== file.rows) {
    rows.push(createNoticeRow(file.id, "truncated", TRUNCATED_FILE_NOTICE));
  }
  rows.push(...noticeRowsForFile(file));
  if (file.noEOFCRAdditions || file.noEOFCRDeletions) {
    const side = file.noEOFCRAdditions
      ? file.noEOFCRDeletions
        ? "Old and new files have"
        : "New file has"
      : "Old file has";
    rows.push(createNoticeRow(file.id, "eof", `${side} no newline at end of file.`));
  }
  const prepared: PreparedNativeReviewFileRows = {
    fileId: file.id,
    filePath: file.path,
    lineCount: lineRows.length,
    truncated: sourceRows !== file.rows,
    // Pair source lines before inserting comments so comment rows cannot affect matching.
    rows: addNativeWordDiffRanges(rows, lineRows),
    commentTargetsByRowId,
    rowIdByCommentLineId,
    commentedRows: null,
  };
  nativeReviewFileRowsCache.set(file, prepared);
  return prepared;
}

function insertFileComments(
  file: PreparedNativeReviewFileRows,
  comments: ReadonlyArray<ReviewInlineComment>,
): ReadonlyArray<NativeReviewDiffRow> {
  if (comments.length === 0) {
    file.commentedRows = null;
    return file.rows;
  }
  const commentsKey = buildReviewCommentsCacheKey(comments);
  if (file.commentedRows?.commentsKey === commentsKey) {
    return file.commentedRows.rows;
  }

  const commentsByEndIndex = new Map<number, ReviewInlineComment[]>();
  for (const comment of comments) {
    if (file.truncated && comment.startIndex >= file.lineCount) continue;
    const endIndex = Math.min(comment.endIndex, file.lineCount - 1);
    if (endIndex < 0) continue;
    const existing = commentsByEndIndex.get(endIndex);
    if (existing) {
      existing.push(comment);
    } else {
      commentsByEndIndex.set(endIndex, [comment]);
    }
  }
  const rows: NativeReviewDiffRow[] = [];
  let lineIndex = 0;
  for (const row of file.rows) {
    rows.push(row);
    if (row.kind !== "line") continue;
    for (const comment of commentsByEndIndex.get(lineIndex) ?? []) {
      rows.push({
        kind: "comment",
        id: comment.id,
        fileId: file.fileId,
        filePath: file.filePath,
        commentText: comment.text,
        commentRangeLabel: comment.rangeLabel,
        commentSectionTitle: comment.sectionTitle,
      });
    }
    lineIndex += 1;
  }
  file.commentedRows = { commentsKey, rows };
  return rows;
}

function prepareNativeReviewDiffData(parsedDiff: ReviewParsedDiff): PreparedNativeReviewDiffData {
  if (parsedDiff.kind !== "files") {
    return {
      fileRows: [],
      files: [],
      commentTargetsByRowId: new Map(),
      rowIdByCommentLineId: new Map(),
      additions: 0,
      deletions: 0,
    };
  }

  const files = parsedDiff.files.map<NativeReviewDiffFile>((file) => ({
    id: file.id,
    path: file.path,
    language: resolveHighlightLanguage({ language: file.languageHint, path: file.path }),
    additions: file.additions,
    deletions: file.deletions,
  }));
  const commentTargetsByRowId = new Map<string, NativeReviewDiffCommentTarget>();
  const rowIdByCommentLineId = new Map<string, string>();
  const fileRows = parsedDiff.files.map(prepareFileRows);
  for (const file of fileRows) {
    for (const [rowId, target] of file.commentTargetsByRowId) {
      commentTargetsByRowId.set(rowId, target);
    }
    for (const [lineId, rowId] of file.rowIdByCommentLineId) {
      rowIdByCommentLineId.set(lineId, rowId);
    }
  }

  return {
    fileRows,
    files,
    commentTargetsByRowId,
    rowIdByCommentLineId,
    additions: parsedDiff.additions,
    deletions: parsedDiff.deletions,
  };
}

function buildCommentedNativeReviewDiffData(
  prepared: PreparedNativeReviewDiffData,
  comments: ReadonlyArray<ReviewInlineComment>,
): NativeReviewDiffData {
  const commentsByFilePath = new Map<string, ReviewInlineComment[]>();
  for (const comment of comments) {
    const existing = commentsByFilePath.get(comment.filePath);
    if (existing) {
      existing.push(comment);
    } else {
      commentsByFilePath.set(comment.filePath, [comment]);
    }
  }
  return {
    rows: Arr.flatMap(prepared.fileRows, (file) =>
      insertFileComments(file, commentsByFilePath.get(file.filePath) ?? []),
    ),
    files: prepared.files,
    commentTargetsByRowId: prepared.commentTargetsByRowId,
    rowIdByCommentLineId: prepared.rowIdByCommentLineId,
    additions: prepared.additions,
    deletions: prepared.deletions,
  };
}

export function buildNativeReviewDiffData(
  input: BuildNativeReviewDiffDataInput,
): NativeReviewDiffData;
export function buildNativeReviewDiffData(parsedDiff: ReviewParsedDiff): NativeReviewDiffData;
export function buildNativeReviewDiffData(
  input: ReviewParsedDiff | BuildNativeReviewDiffDataInput,
): NativeReviewDiffData {
  const parsedDiff = "parsedDiff" in input ? input.parsedDiff : input;
  const comments = "parsedDiff" in input ? (input.comments ?? []) : [];
  return buildCommentedNativeReviewDiffData(prepareNativeReviewDiffData(parsedDiff), comments);
}

export function getCachedNativeReviewDiffData(
  input: BuildNativeReviewDiffDataInput,
): NativeReviewDiffData {
  const comments = input.comments ?? [];
  const commentsKey = buildReviewCommentsCacheKey(comments);
  const cached = nativeReviewDiffDataCache.get(input.parsedDiff);
  if (cached?.commentsKey === commentsKey) {
    return cached.data;
  }

  const prepared = cached?.prepared ?? prepareNativeReviewDiffData(input.parsedDiff);
  const data = buildCommentedNativeReviewDiffData(prepared, comments);
  nativeReviewDiffDataCache.set(input.parsedDiff, { prepared, commentsKey, data });
  return data;
}