From 5999b4cb7d768bdc2fa77106138e00f26242fe31 Mon Sep 17 00:00:00 2001 From: QQ Date: Tue, 17 Nov 2026 08:32:57 +0800 Subject: [PATCH] Import apps/android/src/ui/code-block.tsx --- apps/android/src/ui/code-block.tsx | 94 ++++++++++++++++++------------ 1 file changed, 56 insertions(+), 38 deletions(-) diff --git a/apps/android/src/ui/code-block.tsx b/apps/android/src/ui/code-block.tsx index 9631293..f48dee8 100644 --- a/apps/android/src/ui/code-block.tsx +++ b/apps/android/src/ui/code-block.tsx @@ -10,6 +10,8 @@ import { } from 'react-native' import * as Clipboard from 'expo-clipboard' import { useTheme } from './theme-context' +import { resolveChatMetrics } from './theme' +import { DARK_CODE_PALETTE, highlightCode, LIGHT_CODE_PALETTE } from './syntax-highlight' import { IconWrapFill, IconNowrapFill, IconCopyOutline, IconCheckOutline } from './dsh-icons' export interface CodeBlockProps { @@ -22,14 +24,18 @@ const MONOSPACE_FONT = 'monospace' const COPY_RESET_MS = 2000 export const CodeBlockView = memo(function CodeBlockView({ code, language, style }: CodeBlockProps) { - const { colors, density } = useTheme() + const { colors, scheme, density, appearance } = useTheme() const isCompact = density === 'compact' + const metrics = resolveChatMetrics(density, appearance) + const useDarkCode = appearance.codeTheme === 'dark' || (appearance.codeTheme === 'auto' && scheme === 'dark') + const palette = useDarkCode ? DARK_CODE_PALETTE : LIGHT_CODE_PALETTE const [wrap, setWrap] = useState(false) const [copied, setCopied] = useState(false) const timeoutRef = useRef | null>(null) const cleanCode = (code || '').replace(/\n+$/u, '') const label = (language || 'code').toLowerCase() + const tokens = React.useMemo(() => highlightCode(cleanCode, language), [cleanCode, language]) useEffect(() => { return () => { @@ -52,10 +58,42 @@ export const CodeBlockView = memo(function CodeBlockView({ code, language, style } }, [cleanCode]) + const renderedTokens = ( + + {tokens.map((token, index) => ( + token.kind === 'plain' + ? token.text + : ( + + {token.text} + + ) + ))} + + ) + return ( - - - + + + {label} @@ -70,9 +108,9 @@ export const CodeBlockView = memo(function CodeBlockView({ code, language, style ]} > {wrap ? ( - + ) : ( - + )} {copied ? ( - + ) : ( - + )} {wrap ? ( - - - {cleanCode} - + + {renderedTokens} ) : ( - - {cleanCode} - + {renderedTokens} )} @@ -181,4 +193,10 @@ const styles = StyleSheet.create({ fontFamily: MONOSPACE_FONT, includeFontPadding: false, }, + commentToken: { + fontStyle: 'italic', + }, + keywordToken: { + fontWeight: '600', + }, })