✨ feat(vscode): 代码阅读预览 Rust 函数体按语句拆成子块
let / let-else、if / else、match 各支、循环、隐式返回、闭包、unsafe / async 块照 Python / TypeScript 的约定拆块;特质同实现,方法卡挂在方法表那一行;函数卡去掉分情况表;Rust 语言插件给出调用结构
This commit is contained in:
1 parent
d5c627c54a
commit
86422e4671
7 files changed
+682
-135
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import type { FactBlock, Inline, Node, PropertyRow, SourceDocument, SourceSpan, Translator } from '../contract'
|
||||
import type { FactBlock, Inline, Node, PropertyRow, SourceDocument, SourceSpan, TextBlock, TextTranslation, Translator } from '../contract'
|
||||
import { attributeRows, enumRows, functionRows, implRows, importRows, moduleRows, plainSay, structRows, traitRows } from '../rules/rustRows'
|
||||
import { isCrateRoot } from '../rules/rustTree'
|
||||
import { createZhTemplateTranslator, KIND_ZH, ZH_ATTRIBUTE_NAMES } from './zhTemplate'
|
||||
@@ -58,7 +58,7 @@ const template = createZhTemplateTranslator({
|
||||
})
|
||||
|
||||
/**
|
||||
* Rust 的中文直译。属性表的用词按代码阅读的 Rust 原型:结构体、可见性、变体、分情况。
|
||||
* Rust 的中文直译。属性表的用词按代码阅读的 Rust 原型:结构体、可见性、变体。
|
||||
* 关键字和注释仍走中文直译那一套。常量和变量的行不另写一套,走模板里现成的那套。
|
||||
*/
|
||||
export const rustZhTranslator: Translator = {
|
||||
@@ -77,9 +77,21 @@ export const rustZhTranslator: Translator = {
|
||||
},
|
||||
},
|
||||
},
|
||||
translate,
|
||||
describe,
|
||||
}
|
||||
|
||||
/** 写了 `return` 的返回块:`return x;` 整句,或 match 一支的值 `=> return x`。 */
|
||||
const RETURN_WRITTEN = new Set(['expression_statement', 'return_expression'])
|
||||
|
||||
/** 一层最后那个不带分号的式子是返回值,源码里没有 return 这个词,译文照写了的那样在前面补「返回」。 */
|
||||
function translate(block: TextBlock, document: SourceDocument): TextTranslation {
|
||||
const translation = template.translate(block, document)
|
||||
if (block.kind !== 'return' || RETURN_WRITTEN.has(block.node.type) || !Array.isArray(translation.text))
|
||||
return translation
|
||||
return { ...translation, text: [{ text: '返回', role: 'keyword' }, { text: ' ', role: 'plain' }, ...translation.text] }
|
||||
}
|
||||
|
||||
function describe(block: FactBlock, document: SourceDocument): PropertyRow[] {
|
||||
const say = plainSay(node => template.doc(node, document))
|
||||
switch (block.kind) {
|
||||
|
||||
Reference in new issue
Block a user