✨ feat(vscode): 代码阅读预览 Rust 函数体按语句拆成子块

let / let-else、if / else、match 各支、循环、隐式返回、闭包、unsafe / async 块照 Python / TypeScript 的约定拆块;特质同实现,方法卡挂在方法表那一行;函数卡去掉分情况表;Rust 语言插件给出调用结构
This commit is contained in:
cruldra committed 2026-10-15 17:07:07 +11:00
1 parent d5c627c54a
commit 86422e4671
7 files changed
+682 -135

No files matched your search

+20 -12
View File
@@ -1,33 +1,41 @@
import type { ImplBlock, KindBadge, LibraryRule, RuleCard, RuleContext, ThemeColor } from '../contract'
import { implRows, plainSay } from './rustRows'
import type { ImplBlock, KindBadge, LibraryRule, PropertyRow, RuleCard, TraitBlock } from '../contract'
import { implRows, plainSay, traitRows } from './rustRows'
/** 和函数卡同一个黄,实现块看起来仍是函数那一挂。 */
const COLOR: ThemeColor = { dark: '#dcdcaa', light: '#795e26' }
const BADGE: KindBadge = { label: '实现', color: COLOR, icon: 'square-function' }
const IMPL: KindBadge = { label: '实现', color: { dark: '#dcdcaa', light: '#795e26' }, icon: 'square-function' }
/** 和声明关键字同一个蓝,同特质这个种类在预览里的颜色。 */
const TRAIT: KindBadge = { label: '特质', color: { dark: '#569cd6', light: '#0000ff' }, icon: 'network' }
/**
* impl 的方法表:点方法名展开那张函数卡。
* impl、trait 的方法表:点方法名展开那张函数卡,函数体各句在卡里。
* 方法挂在方法表的子行上(RuleSlot)。不看导入,Rust 文件都启用。
*/
export const rustImplRule: LibraryRule = {
id: 'rust',
label: 'Rust 实现',
label: 'Rust 实现与特质',
languageIds: ['rust'],
libraries: [],
claim: (block, context) => block.kind === 'impl' ? implCard(block, context) : undefined,
claim: (block, context) => {
const say = plainSay(context.doc)
if (block.kind === 'impl')
return methodsCard(block, 'rust.impl', IMPL, implRows(block, say))
if (block.kind === 'trait')
return methodsCard(block, 'rust.trait', TRAIT, traitRows(block, say))
return undefined
},
attributeNames: () => ({
attributes: {},
values: { kind: { 'rust.impl': '实现' } },
values: { kind: { 'rust.impl': '实现', 'rust.trait': '特质' } },
}),
}
function implCard(block: ImplBlock, context: RuleContext): RuleCard {
const rows = implRows(block, plainSay(context.doc))
/** 带方法表的卡片:子块里的每个函数挂在「方法」那一行讲它的子行上,跟 rows 里的顺序一一对上。 */
function methodsCard(block: ImplBlock | TraitBlock, kind: string, badge: KindBadge, rows: PropertyRow[]): RuleCard {
const row = rows.findIndex(item => item.label === '方法')
const methods = block.children.filter(child => child.kind === 'function')
return {
kind: 'rust.impl',
badge: BADGE,
kind,
badge,
rows,
...(block.facts.name && { defines: { startIndex: block.facts.name.startIndex, endIndex: block.facts.name.endIndex } }),
absorbs: [],