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