Files
superwork/vscode/src/codeReader/rules/rustImpl.ts
T

37 lines
1.3 KiB
TypeScript

import type { ImplBlock, KindBadge, LibraryRule, RuleCard, ThemeColor } from '../contract'
import { implRows, plainSay } from './rustRows'
/** 和函数卡同一个黄,实现块看起来仍是函数那一挂。 */
const COLOR: ThemeColor = { dark: '#dcdcaa', light: '#795e26' }
const BADGE: KindBadge = { label: '实现', color: COLOR, icon: 'square-function' }
/**
* impl 的方法表:点方法名展开那张函数卡。
* 方法挂在方法表的子行上(RuleSlot)。不看导入,Rust 文件都启用。
*/
export const rustImplRule: LibraryRule = {
id: 'rust',
label: 'Rust 实现',
languageIds: ['rust'],
libraries: [],
claim: block => block.kind === 'impl' ? implCard(block) : undefined,
attributeNames: () => ({
attributes: {},
values: { kind: { 'rust.impl': '实现' } },
}),
}
function implCard(block: ImplBlock): RuleCard {
const rows = implRows(block, plainSay())
const row = rows.findIndex(item => item.label === '方法')
const methods = block.children.filter(child => child.kind === 'function')
return {
kind: 'rust.impl',
badge: BADGE,
rows,
...(block.facts.name && { defines: { startIndex: block.facts.name.startIndex, endIndex: block.facts.name.endIndex } }),
absorbs: [],
...(row >= 0 && { slots: methods.map((child, item) => ({ block: child, row, item })) }),
}
}