✨ feat(vscode): 代码阅读预览 Rust 的语言卡片

This commit is contained in:
cruldra committed 2026-10-04 14:24:55 +08:00
1 parent 4480f936f3
commit 6d58b681cf
15 files changed
+1016 -6

No files matched your search

+105
View File
@@ -0,0 +1,105 @@
import type { FactBlock, Node, PropertyRow, SourceDocument, Translator } from '../contract'
import { enumRows, functionRows, implRows, importRows, plainSay, structRows, traitRows } from '../rustRows'
import { createZhTemplateTranslator, KIND_ZH, ZH_ATTRIBUTE_NAMES } from './zhTemplate'
const say = plainSay()
/**
* Rust 的中文直译。属性表的用词按代码阅读的 Rust 原型:结构体、可见性、变体、分情况。
* 关键字和注释仍走中文直译那一套。
*/
export const rustZhTranslator: Translator = {
...createZhTemplateTranslator({
id: 'rust.zh-template',
label: '中文直译',
languageIds: ['rust'],
keywords: {
fn: '函数',
let: '令',
mut: '可变',
pub: '公开',
struct: '结构体',
enum: '枚举',
impl: '实现',
trait: '特质',
use: '导入',
mod: '模块',
match: '匹配',
if: '如果',
else: '否则',
for: '遍历',
while: '当',
loop: '循环',
return: '返回',
async: '异步',
await: '等待',
break: '跳出',
continue: '继续',
where: '其中',
type: '类型',
const: '常量',
static: '静态',
unsafe: '不安全',
as: '作为',
in: '在',
ref: '引用',
move: '移动',
dyn: '动态',
crate: '本 crate',
super: '父模块',
self: '自己',
Self: '本类型',
},
literals: { true: '真', false: '假' },
builtinTypes: {},
isTypePosition: () => false,
operators: {},
operatorParents: [],
emptyLiterals: {},
mapLiterals: {},
stringNodes: ['string_literal'],
commentText,
nodeTemplates: {},
}),
attributeNames: {
attributes: { ...ZH_ATTRIBUTE_NAMES.attributes, access: '可见性' },
values: {
...ZH_ATTRIBUTE_NAMES.values,
kind: { ...KIND_ZH, class: '结构体' },
access: {
...ZH_ATTRIBUTE_NAMES.values.access,
public: '公开',
module: '本模块',
super: '父模块',
crate: '本 crate',
},
},
},
describe,
}
function describe(block: FactBlock, _document: SourceDocument): PropertyRow[] {
switch (block.kind) {
case 'import':
return importRows(block, say)
case 'class':
return structRows(block, say)
case 'enum':
return enumRows(block, say)
case 'function':
return functionRows(block, say)
case 'trait':
return traitRows(block, say)
case 'impl':
return implRows(block, say)
default:
return []
}
}
function commentText(node: Node, text: string): string {
const doc = node.childForFieldName('doc')
if (doc)
return doc.text.trim()
return text.replace(/^\/\/[/!]?\s?/, '').replace(/^\/\*[*!]?\s?/, '').replace(/\s*\*\/$/, '').trim()
}