✨ 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

@@ -109,4 +109,74 @@ describe('rust 语言卡片', () => {
items: { changed: { 1: { added: true } }, removed: [] },
})
})
it('带身体的模块拆成子块,常量和外部函数是卡片', async () => {
const source = [
'/// 平台相关。',
'pub mod platform {',
' /// 读入。',
' use std::io::Read;',
' /// 上限。',
' pub const LIMIT: u32 = 8;',
' static NAME: &str = "a";',
' pub fn open() -> u32 { LIMIT }',
' extern "C" {',
' /// 外部。',
' pub fn bar(x: i32) -> i32;',
' }',
' let local = 1;',
' println!("hi");',
'}',
'mod other;',
'',
'impl Agent {',
' pub const N: u8 = 2;',
'}',
'',
].join('\n')
const list = await views(source)
const mod = list.find(block => block.kind === 'module')
expect(mod?.kind).toBe('module')
expect(list.some(block => block.kind === 'statement' && block.code.includes('const LIMIT'))).toBe(false)
expect(list.filter(block => block.kind === 'statement').map(block => block.code)).toEqual(['mod other;'])
expect(mod?.children.map(block => block.kind)).toEqual(['import', 'const', 'variable', 'function', 'function', 'statement', 'statement'])
expect(viewLines(mod ? [mod] : [])).toEqual([
'module 1',
' 名称: platform',
' 说明: 平台相关。',
' 可见性: 公开',
' import 3',
' 来源: std::io',
' 导入项: 1 个',
' Read | —',
' const 5',
' 名称: LIMIT',
' 说明: 上限。',
' 访问级别: 公共',
' 类型: u32',
' 值: 8',
' variable 7',
' 名称: NAME',
' 类型: &str',
' 初始值: "a"',
' function 8',
' 名称: open',
' 可见性: 公开',
' 返回: u32',
' function 10',
' 名称: bar',
' 说明: 外部。',
' 可见性: 公开',
' 输入参数: 1 个',
' x | i32',
' 返回: i32',
' statement 13: 令 local = 1;',
' statement 14: println!("hi")',
])
const impl = list.find(block => block.kind === 'rust.impl')
const inner = impl?.children.find(block => block.kind === 'const')
expect(inner?.kind).toBe('const')
expect(viewLines(inner ? [inner] : []).join('\n')).toContain('名称: N')
expect(viewLines(inner ? [inner] : []).join('\n')).toMatch(/类型: u8|值: 2/)
})
})