File size: 4,763 Bytes
39e315a | 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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | import { c as _c } from "react-compiler-runtime";
import { extname } from 'path';
import React, { Suspense, use, useMemo } from 'react';
import { Ansi, Text } from '../../ink.js';
import { getCliHighlightPromise } from '../../utils/cliHighlight.js';
import { logForDebugging } from '../../utils/debug.js';
import { convertLeadingTabsToSpaces } from '../../utils/file.js';
import { hashPair } from '../../utils/hash.js';
type Props = {
code: string;
filePath: string;
dim?: boolean;
skipColoring?: boolean;
};
// Module-level highlight cache — hl.highlight() is the hot cost on virtual-
// scroll remounts. useMemo doesn't survive unmount→remount. Keyed by hash
// of code+language to avoid retaining full source strings (#24180 RSS fix).
const HL_CACHE_MAX = 500;
const hlCache = new Map<string, string>();
function cachedHighlight(hl: NonNullable<Awaited<ReturnType<typeof getCliHighlightPromise>>>, code: string, language: string): string {
const key = hashPair(language, code);
const hit = hlCache.get(key);
if (hit !== undefined) {
hlCache.delete(key);
hlCache.set(key, hit);
return hit;
}
const out = hl.highlight(code, {
language
});
if (hlCache.size >= HL_CACHE_MAX) {
const first = hlCache.keys().next().value;
if (first !== undefined) hlCache.delete(first);
}
hlCache.set(key, out);
return out;
}
export function HighlightedCodeFallback(t0) {
const $ = _c(20);
const {
code,
filePath,
dim: t1,
skipColoring: t2
} = t0;
const dim = t1 === undefined ? false : t1;
const skipColoring = t2 === undefined ? false : t2;
let t3;
if ($[0] !== code) {
t3 = convertLeadingTabsToSpaces(code);
$[0] = code;
$[1] = t3;
} else {
t3 = $[1];
}
const codeWithSpaces = t3;
if (skipColoring) {
let t4;
if ($[2] !== codeWithSpaces) {
t4 = <Ansi>{codeWithSpaces}</Ansi>;
$[2] = codeWithSpaces;
$[3] = t4;
} else {
t4 = $[3];
}
let t5;
if ($[4] !== dim || $[5] !== t4) {
t5 = <Text dimColor={dim}>{t4}</Text>;
$[4] = dim;
$[5] = t4;
$[6] = t5;
} else {
t5 = $[6];
}
return t5;
}
let t4;
if ($[7] !== filePath) {
t4 = extname(filePath).slice(1);
$[7] = filePath;
$[8] = t4;
} else {
t4 = $[8];
}
const language = t4;
let t5;
if ($[9] !== codeWithSpaces) {
t5 = <Ansi>{codeWithSpaces}</Ansi>;
$[9] = codeWithSpaces;
$[10] = t5;
} else {
t5 = $[10];
}
let t6;
if ($[11] !== codeWithSpaces || $[12] !== language) {
t6 = <Highlighted codeWithSpaces={codeWithSpaces} language={language} />;
$[11] = codeWithSpaces;
$[12] = language;
$[13] = t6;
} else {
t6 = $[13];
}
let t7;
if ($[14] !== t5 || $[15] !== t6) {
t7 = <Suspense fallback={t5}>{t6}</Suspense>;
$[14] = t5;
$[15] = t6;
$[16] = t7;
} else {
t7 = $[16];
}
let t8;
if ($[17] !== dim || $[18] !== t7) {
t8 = <Text dimColor={dim}>{t7}</Text>;
$[17] = dim;
$[18] = t7;
$[19] = t8;
} else {
t8 = $[19];
}
return t8;
}
function Highlighted(t0) {
const $ = _c(10);
const {
codeWithSpaces,
language
} = t0;
let t1;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
t1 = getCliHighlightPromise();
$[0] = t1;
} else {
t1 = $[0];
}
const hl = use(t1);
let t2;
if ($[1] !== codeWithSpaces || $[2] !== hl || $[3] !== language) {
bb0: {
if (!hl) {
t2 = codeWithSpaces;
break bb0;
}
let highlightLang = "markdown";
if (language) {
if (hl.supportsLanguage(language)) {
highlightLang = language;
} else {
logForDebugging(`Language not supported while highlighting code, falling back to markdown: ${language}`);
}
}
;
try {
t2 = cachedHighlight(hl, codeWithSpaces, highlightLang);
} catch (t3) {
const e = t3;
if (e instanceof Error && e.message.includes("Unknown language")) {
logForDebugging(`Language not supported while highlighting code, falling back to markdown: ${e}`);
let t4;
if ($[5] !== codeWithSpaces || $[6] !== hl) {
t4 = cachedHighlight(hl, codeWithSpaces, "markdown");
$[5] = codeWithSpaces;
$[6] = hl;
$[7] = t4;
} else {
t4 = $[7];
}
t2 = t4;
break bb0;
}
t2 = codeWithSpaces;
}
}
$[1] = codeWithSpaces;
$[2] = hl;
$[3] = language;
$[4] = t2;
} else {
t2 = $[4];
}
const out = t2;
let t3;
if ($[8] !== out) {
t3 = <Ansi>{out}</Ansi>;
$[8] = out;
$[9] = t3;
} else {
t3 = $[9];
}
return t3;
}
|