Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

highlights.ts

95 lines3.6 KB

1import type { T3Highlights } from "@t3tools/mobile-highlights-native";
2import { NitroModules } from "react-native-nitro-modules";
3​
4import { highlightLanguageId } from "./highlightLanguage";
5import { HIGHLIGHTS_THEME_STYLES } from "./highlights.generated";
6​
7export type HighlightScheme = "light" | "dark";
8​
9export interface HighlightedToken {
10 readonly content: string;
11 readonly color: string | null;
12 readonly fontStyle: number | null;
14​
15export type HighlightedLines = ReadonlyArray<ReadonlyArray<HighlightedToken>>;
16​
17const LINE_END = 0xffffffff;
18// Longer lines stay one plain token so a minified line does not become
19// thousands of native text runs.
20const MAX_HIGHLIGHTED_LINE_LENGTH = 1_000;
21​
22// @pierre/highlights compiled to native code (modules/t3-highlights). Missing
23// from binaries built before it shipped, where code renders as plain text.
24const nativeHighlights = NitroModules.hasHybridObject("T3Highlights")
25 ? NitroModules.createHybridObject<T3Highlights>("T3Highlights")
26 : undefined;
27​
28export interface TokenizedCode {
29 /** Retained UTF-16 source, compact native records, and line indexes. */
30 readonly byteLength: number;
31 readonly lineCount: number;
32 readonly highlightLine: (
33 index: number,
34 scheme: HighlightScheme,
35 ) => ReadonlyArray<HighlightedToken> | undefined;
37​
38/** Keep native records compact; expand only lines a client will display. */
39export function tokenizeCode(code: string, language: string): TokenizedCode {
40 const id = highlightLanguageId(language);
41 const buffer = id === 0 ? null : (nativeHighlights?.tokenize(code, id) ?? null);
42 const records = buffer ? new Uint32Array(buffer) : null;
43 const offsets = [0];
44 if (records) {
45 for (let record = 0; record < records.length; record += 2) {
46 if (records[record + 1] === LINE_END) offsets.push(record + 2);
47 }
48 } else {
49 for (let at = code.indexOf("\n"); at !== -1; at = code.indexOf("\n", at + 1))
50 offsets.push(at + 1);
51 }
52 const lineStarts = Uint32Array.from(offsets);
53 return {
54 byteLength: code.length * 2 + (records?.byteLength ?? 0) + lineStarts.byteLength,
55 lineCount: lineStarts.length,
56 highlightLine(index, scheme) {
57 if (index < 0 || index >= lineStarts.length) return undefined;
58 const first = lineStarts[index]!;
59 const next = lineStarts[index + 1];
60 if (!records) {
61 const end = next === undefined ? code.length : next - (code[next - 2] === "\r" ? 2 : 1);
62 const content = code.slice(first, end);
63 return content ? [{ content, color: null, fontStyle: null }] : [];
64 }
65 let start = first === 0 ? 0 : records[first - 2]!;
66 const last = next === undefined ? records.length : next - 2;
67 const end = last === first ? start : records[last - 2]!;
68 if (end - start > MAX_HIGHLIGHTED_LINE_LENGTH) {
69 return [{ content: code.slice(start, end), color: null, fontStyle: null }];
70 }
71 const styles = HIGHLIGHTS_THEME_STYLES[scheme];
72 const line: HighlightedToken[] = [];
73 for (let at = first; at < last; at += 2) {
74 const tokenEnd = records[at]!;
75 if (tokenEnd <= start) continue;
76 const [color, fontStyle] = styles[records[at + 1]!] ?? styles[0]!;
77 line.push({ content: code.slice(start, tokenEnd), color, fontStyle });
78 start = tokenEnd;
79 }
80 return line;
81 },
82 };
84​
85/** LF and CRLF split lines; a trailing line break yields a final empty line. */
86export function highlightCode(
87 code: string,
88 language: string,
89 scheme: HighlightScheme,
90): HighlightedLines {
91 const source = tokenizeCode(code, language);
92 return Array.from({ length: source.lineCount }, (_, index) =>
93 source.highlightLine(index, scheme)!,
94 );