|
| 1 | +# Design Document |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +本设计文档描述了 Two Sum 可视化器中代码调试器组件的语法高亮功能实现。项目已使用 Prism.js 作为语法高亮引擎,但当前高亮效果可能未正确显示。本设计将确保 Java、Python、Go 和 JavaScript 四种语言都能正确显示语法高亮。 |
| 6 | + |
| 7 | +## Architecture |
| 8 | + |
| 9 | +```mermaid |
| 10 | +graph TD |
| 11 | + A[CodeDebugger Component] --> B[Prism.js Highlighter] |
| 12 | + B --> C[Language Grammar] |
| 13 | + C --> D[Java Grammar] |
| 14 | + C --> E[Python Grammar] |
| 15 | + C --> F[Go Grammar] |
| 16 | + C --> G[JavaScript Grammar] |
| 17 | + B --> H[Token Stream] |
| 18 | + H --> I[HTML with Token Classes] |
| 19 | + I --> J[CSS Theme Styles] |
| 20 | + J --> K[Rendered Highlighted Code] |
| 21 | +``` |
| 22 | + |
| 23 | +### 核心流程 |
| 24 | + |
| 25 | +1. CodeDebugger 组件接收代码字符串和语言类型 |
| 26 | +2. 调用 Prism.highlight() 方法解析代码 |
| 27 | +3. Prism.js 根据语言语法生成带有 token 类名的 HTML |
| 28 | +4. CSS 样式根据 token 类名应用颜色 |
| 29 | + |
| 30 | +## Components and Interfaces |
| 31 | + |
| 32 | +### 1. Prism.js 配置 |
| 33 | + |
| 34 | +```typescript |
| 35 | +// 语言导入 |
| 36 | +import Prism from 'prismjs' |
| 37 | +import 'prismjs/components/prism-java' |
| 38 | +import 'prismjs/components/prism-python' |
| 39 | +import 'prismjs/components/prism-go' |
| 40 | +import 'prismjs/components/prism-javascript' |
| 41 | + |
| 42 | +// 语言映射 |
| 43 | +const PRISM_LANGUAGE_MAP: Record<CodeLanguage, string> = { |
| 44 | + java: 'java', |
| 45 | + python: 'python', |
| 46 | + golang: 'go', |
| 47 | + javascript: 'javascript', |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +### 2. 高亮函数接口 |
| 52 | + |
| 53 | +```typescript |
| 54 | +/** |
| 55 | + * 高亮单行代码 |
| 56 | + * @param line - 代码行字符串 |
| 57 | + * @param language - 编程语言 |
| 58 | + * @returns 带有 token span 标签的 HTML 字符串 |
| 59 | + */ |
| 60 | +function highlightLine(line: string, language: CodeLanguage): string |
| 61 | +``` |
| 62 | + |
| 63 | +### 3. Token 类型映射 |
| 64 | + |
| 65 | +| Token 类型 | 颜色 | 示例 | |
| 66 | +|-----------|------|------| |
| 67 | +| keyword | #569cd6 (蓝色) | public, def, func, const | |
| 68 | +| class-name | #4ec9b0 (青色) | Map, List, int | |
| 69 | +| string | #ce9178 (橙色) | "hello", 'world' | |
| 70 | +| number | #b5cea8 (浅绿) | 42, 3.14 | |
| 71 | +| function | #dcdcaa (黄色) | twoSum, enumerate | |
| 72 | +| comment | #6a9955 (绿色) | // comment | |
| 73 | +| punctuation | #d4d4d4 (灰白) | {, }, (, ) | |
| 74 | +| operator | #d4d4d4 (灰白) | =, +, - | |
| 75 | + |
| 76 | +## Data Models |
| 77 | + |
| 78 | +### CodeLanguage 类型 |
| 79 | + |
| 80 | +```typescript |
| 81 | +type CodeLanguage = 'java' | 'python' | 'golang' | 'javascript' |
| 82 | +``` |
| 83 | + |
| 84 | +### Token 结构 (Prism.js 内部) |
| 85 | + |
| 86 | +```typescript |
| 87 | +interface PrismToken { |
| 88 | + type: string // token 类型,如 'keyword', 'string' |
| 89 | + content: string // token 内容 |
| 90 | + alias?: string // 别名 |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +## Correctness Properties |
| 95 | + |
| 96 | +*A property is a characteristic or behavior that should hold true across all valid executions of a system-essentially, a formal statement about what the system should do. Properties serve as the bridge between human-readable specifications and machine-verifiable correctness guarantees.* |
| 97 | + |
| 98 | +### Property 1: Keyword Token Classification |
| 99 | + |
| 100 | +*For any* code string containing language keywords, when highlighted by Prism.js, the output HTML SHALL contain span elements with class "token keyword" wrapping those keywords. |
| 101 | + |
| 102 | +**Validates: Requirements 1.1, 2.1, 3.1, 4.1** |
| 103 | + |
| 104 | +### Property 2: Type/Class-name Token Classification |
| 105 | + |
| 106 | +*For any* code string containing type names or class names, when highlighted by Prism.js, the output HTML SHALL contain span elements with class "token class-name" or "token builtin" wrapping those type names. |
| 107 | + |
| 108 | +**Validates: Requirements 1.2, 2.2, 3.2, 4.2** |
| 109 | + |
| 110 | +### Property 3: String Literal Token Classification |
| 111 | + |
| 112 | +*For any* code string containing string literals, when highlighted by Prism.js, the output HTML SHALL contain span elements with class "token string" wrapping those string literals. |
| 113 | + |
| 114 | +**Validates: Requirements 1.3, 2.3, 3.3, 4.3** |
| 115 | + |
| 116 | +### Property 4: Number Literal Token Classification |
| 117 | + |
| 118 | +*For any* code string containing numeric literals, when highlighted by Prism.js, the output HTML SHALL contain span elements with class "token number" wrapping those numbers. |
| 119 | + |
| 120 | +**Validates: Requirements 1.4, 2.4, 3.4, 4.4** |
| 121 | + |
| 122 | +### Property 5: Function Name Token Classification |
| 123 | + |
| 124 | +*For any* code string containing function definitions or calls, when highlighted by Prism.js, the output HTML SHALL contain span elements with class "token function" wrapping those function names. |
| 125 | + |
| 126 | +**Validates: Requirements 1.5, 2.5, 3.5, 4.5** |
| 127 | + |
| 128 | +### Property 6: Cross-Language Token Consistency |
| 129 | + |
| 130 | +*For any* equivalent token type across different languages, the CSS styling SHALL apply the same color to that token type regardless of the source language. |
| 131 | + |
| 132 | +**Validates: Requirements 5.1** |
| 133 | + |
| 134 | +## Error Handling |
| 135 | + |
| 136 | +### Prism.js 解析失败 |
| 137 | + |
| 138 | +当 Prism.js 无法解析代码时,系统应优雅降级: |
| 139 | + |
| 140 | +```typescript |
| 141 | +const highlightLine = (line: string): string => { |
| 142 | + if (!line.trim()) return ' ' |
| 143 | + try { |
| 144 | + const prismLang = PRISM_LANGUAGE_MAP[language] |
| 145 | + const grammar = Prism.languages[prismLang] |
| 146 | + if (grammar) { |
| 147 | + return Prism.highlight(line, grammar, prismLang) |
| 148 | + } |
| 149 | + return escapeHtml(line) // 降级为纯文本 |
| 150 | + } catch { |
| 151 | + return escapeHtml(line) // 错误时降级为纯文本 |
| 152 | + } |
| 153 | +} |
| 154 | +``` |
| 155 | + |
| 156 | +### 语言不支持 |
| 157 | + |
| 158 | +如果请求的语言语法未加载,返回未高亮的代码而不是抛出错误。 |
| 159 | + |
| 160 | +## Testing Strategy |
| 161 | + |
| 162 | +### 单元测试 |
| 163 | + |
| 164 | +使用 Vitest 测试框架: |
| 165 | + |
| 166 | +1. 测试 highlightLine 函数对各语言的基本高亮 |
| 167 | +2. 测试错误处理(无效输入、未知语言) |
| 168 | +3. 测试空行和特殊字符处理 |
| 169 | + |
| 170 | +### 属性测试 |
| 171 | + |
| 172 | +使用 fast-check 库进行属性测试: |
| 173 | + |
| 174 | +1. 验证关键字 token 分类的正确性 |
| 175 | +2. 验证字符串 token 分类的正确性 |
| 176 | +3. 验证数字 token 分类的正确性 |
| 177 | +4. 验证跨语言 token 一致性 |
| 178 | + |
| 179 | +每个属性测试配置运行 100 次迭代。 |
| 180 | + |
| 181 | +测试文件命名:`*.property.test.ts` |
| 182 | + |
| 183 | +测试注释格式:`**Feature: syntax-highlighting, Property {number}: {property_text}**` |
0 commit comments