✨ feat(vscode): Rust 的模块、常量和外部函数拆成卡片

This commit is contained in:
cruldra committed 2026-10-04 20:16:51 +08:00
1 parent e6345e3309
commit 04c4b9f710
13 files changed
+220 -92

No files matched your search

+62 -55
View File
@@ -1,66 +1,68 @@
import type { FactBlock, Node, PropertyRow, SourceDocument, Translator } from '../contract'
import { enumRows, functionRows, implRows, importRows, plainSay, structRows, traitRows } from '../rules/rustRows'
import { enumRows, functionRows, implRows, importRows, moduleRows, plainSay, structRows, traitRows } from '../rules/rustRows'
import { createZhTemplateTranslator, KIND_ZH, ZH_ATTRIBUTE_NAMES } from './zhTemplate'
const say = plainSay()
const template = 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: {},
})
/**
* 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: {},
}),
...template,
attributeNames: {
attributes: { ...ZH_ATTRIBUTE_NAMES.attributes, access: '可见性' },
values: {
@@ -78,7 +80,7 @@ export const rustZhTranslator: Translator = {
describe,
}
function describe(block: FactBlock, _document: SourceDocument): PropertyRow[] {
function describe(block: FactBlock, document: SourceDocument): PropertyRow[] {
switch (block.kind) {
case 'import':
return importRows(block, say)
@@ -92,6 +94,11 @@ function describe(block: FactBlock, _document: SourceDocument): PropertyRow[] {
return traitRows(block, say)
case 'impl':
return implRows(block, say)
case 'module':
return moduleRows(block, say)
case 'const':
case 'variable':
return template.describe(block, document)
default:
return []
}
@@ -214,6 +214,7 @@ export const KIND_ZH: Record<BlockKind, string> = {
class: '类',
trait: '特质',
impl: '实现',
module: '模块',
enum: '枚举',
exception: '异常',
type: '类型',
@@ -708,6 +709,7 @@ export function createZhTemplateTranslator(table: ZhTable): Translator {
case 'class':
case 'trait':
case 'impl':
case 'module':
return classRows(block.facts, phrases)
case 'enum':
return enumRows(block.facts, phrases)