feat(code-runner): 高亮层补 bun 别名、CodeMirror 加 TS 模式
syntect 高亮别名表(highlight.rs)加 ('bun','ts'):bun 运行器跑的是
TypeScript,归一化后用 ts 扩展名命中 TypeScript.sublime-syntax。
CodeMirror buildLanguageExtension(codemirror-editor/editor.ts)新增
bun/typescript/ts 分支 → javascript({ typescript: true }),让 bun 代码块
在阅读器侧编辑器获得 TS 类型感知高亮。归一化后 CodeRunner 实际只传
canonical key,但保留别名分支作防御(CodeMirror 也在 SQL 控制台等场景用)。
两边都补了测试:highlight_code_resolves_bun_alias_to_typescript 锁定
'bun' 与 'ts' 输出一致;CodeMirror 经 tsc --noEmit + vite build 通过。
This commit is contained in:
parent
22d226d4e8
commit
864c813c6d
@ -107,9 +107,14 @@ export class EditorOptions {
|
||||
/**
|
||||
* 把语言标识映射成 CodeMirror 语言 Extension。
|
||||
* - `python` → @codemirror/lang-python
|
||||
* - `node` / `javascript` / `js` → @codemirror/lang-javascript
|
||||
* - `node` / `javascript` / `js` → @codemirror/lang-javascript(JS 模式)
|
||||
* - `bun` / `typescript` / `ts` → @codemirror/lang-javascript(TypeScript 模式)
|
||||
* - `sql` / 缺省 → @codemirror/lang-sql(PostgreSQL 方言 + schema 补全)
|
||||
*
|
||||
* 归一化后(src/api/code_runner/languages.rs::normalize_lang)CodeRunner 只会
|
||||
* 传来 canonical key(node/bun/python/go/rust),但这里仍保留别名分支作防御——
|
||||
* CodeMirror 也用在 SQL 控制台等场景,调用方可能直接传别名。
|
||||
*
|
||||
* SQL 分支使用 schema 补全;非 SQL 语言忽略 schema(补全仅对 SQL 有意义)。
|
||||
*/
|
||||
function buildLanguageExtension(lang: string, schema: SqlSchema): Extension {
|
||||
@ -120,6 +125,10 @@ function buildLanguageExtension(lang: string, schema: SqlSchema): Extension {
|
||||
if (normalized === 'node' || normalized === 'javascript' || normalized === 'js') {
|
||||
return javascript();
|
||||
}
|
||||
if (normalized === 'bun' || normalized === 'typescript' || normalized === 'ts') {
|
||||
// bun 跑 TypeScript,编辑器用 TS 模式获得类型感知高亮(接口/类型注解等)。
|
||||
return javascript({ typescript: true });
|
||||
}
|
||||
// sql / 缺省:保留原有 PostgreSQL + schema 补全行为
|
||||
return sql({
|
||||
dialect: PostgreSQL,
|
||||
|
||||
@ -85,6 +85,8 @@ pub mod server {
|
||||
("js", "js"),
|
||||
("javascript", "js"),
|
||||
("typescript", "ts"),
|
||||
// bun 运行器跑的是 TypeScript,归一化后用 ts 语法高亮。
|
||||
("bun", "ts"),
|
||||
("py", "py"),
|
||||
("python", "py"),
|
||||
("rb", "rb"),
|
||||
@ -486,4 +488,21 @@ const message = ref('Hello Vue!')
|
||||
// 大写标识经别名表 eq_ignore_ascii_case 回退,输出须与小写一致。
|
||||
assert_eq!(by_alias, by_upper);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn highlight_code_resolves_bun_alias_to_typescript() {
|
||||
// bun 运行器跑 TypeScript;别名表把 "bun" 归一为 "ts" 扩展名。
|
||||
// 用类型注解(TS 特有语法)验证命中的是 TypeScript 语法而非纯 JS。
|
||||
let code = "const x: number = 1;";
|
||||
let result = highlight_code(code, Some("bun"));
|
||||
// 不应回退纯文本(纯文本无 <span> 高亮 span)。
|
||||
assert!(
|
||||
result.contains("<span"),
|
||||
"bun 别名应触发语法高亮, got: {}",
|
||||
result
|
||||
);
|
||||
// 与直接传 ts 的输出一致——别名表正确归一。
|
||||
let by_ts = highlight_code(code, Some("ts"));
|
||||
assert_eq!(result, by_ts);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user