Open History to see the last commit
1import type {
2 NativeReviewDiffRow,
3 NativeReviewDiffStyle,
4 NativeReviewDiffToken,
5} from "../diffs/nativeReviewDiffSurface";
6import { MOBILE_CODE_SURFACE, MOBILE_TYPOGRAPHY } from "../../lib/typography";
7import type { SourceHighlightTokens } from "./sourceHighlightingState";
8
9export const NATIVE_SOURCE_ROW_HEIGHT = MOBILE_CODE_SURFACE.rowHeight;
10export const NATIVE_SOURCE_CONTENT_WIDTH = 32_000;
11
12export const NATIVE_SOURCE_STYLE: NativeReviewDiffStyle = {
13 rowHeight: NATIVE_SOURCE_ROW_HEIGHT,
14 contentWidth: NATIVE_SOURCE_CONTENT_WIDTH,
15 changeBarWidth: 0,
16 gutterWidth: MOBILE_CODE_SURFACE.gutterWidth,
17 codePadding: MOBILE_CODE_SURFACE.codePadding,
18 textVerticalInset: MOBILE_CODE_SURFACE.textVerticalInset,
19 codeFontSize: MOBILE_CODE_SURFACE.fontSize,
20 codeFontWeight: "regular",
21 lineNumberFontSize: MOBILE_CODE_SURFACE.lineNumberFontSize,
22 lineNumberFontWeight: "regular",
23 emptyStateFontSize: MOBILE_TYPOGRAPHY.label.fontSize,
24 emptyStateFontWeight: "medium",
25};
26
27const SOURCE_FILE_ID = "source-file";
28
29function expandTabs(value: string): string {
30 return value.replace(/\t/g, " ");
31}
32
33export function nativeSourceRowId(index: number): string {
34 return `source-line:${index}`;
35}
36
37export function buildNativeSourceRows(
38 lines: ReadonlyArray<string>,
39): ReadonlyArray<NativeReviewDiffRow> {
40 return lines.map((line, index) => ({
41 kind: "line",
42 id: nativeSourceRowId(index),
43 fileId: SOURCE_FILE_ID,
44 content: expandTabs(line),
45 change: "context",
46 newLineNumber: index + 1,
47 }));
48}
49
50export function buildNativeSourceTokens(
51 tokenLines: SourceHighlightTokens | null,
52): Readonly<Record<string, ReadonlyArray<NativeReviewDiffToken>>> {
53 if (tokenLines === null) {
54 return {};
55 }
56
57 return Object.fromEntries(
58 tokenLines.map((tokens, index) => [
59 nativeSourceRowId(index),
60 tokens.map((token) => ({
61 content: expandTabs(token.content),
62 color: token.color,
63 fontStyle: token.fontStyle,
64 })),
65 ]),
66 );
67}