🐛 fix(vscode): Rust 卡片照抄的类型、签名逐个名字带下标,返回类型也能悬停跳转

This commit is contained in:
cruldra committed 2026-10-15 19:48:56 +11:00
1 parent ecb6dc89c6
commit 55595e948c
17 files changed
+220 -122

No files matched your search

+64 -1
View File
@@ -1,11 +1,12 @@
import type { BlockView } from '../blockView'
import type { PreviewText } from '../contract'
import type { Outline } from '../testSupport'
import { describe, expect, it } from 'vitest'
import { blockCodes, toBlockViews } from '../blockView'
import { blockMarks } from '../diff/viewDiff'
import { rustImplRule } from '../rules/rustImpl'
import { isCrateRoot } from '../rules/rustTree'
import { commentText, nodeFacts, outline, propertyLines, readFixture, viewLines, WASM_DIR, withBlocks } from '../testSupport'
import { commentText, nodeFacts, outline, previewText, propertyLines, propertyRow, readFixture, typeNames, viewLines, WASM_DIR, withBlocks, wordsOf } from '../testSupport'
import { rustZhTranslator } from '../translators/rustZh'
import { createRustAdapter } from './rust'
@@ -136,6 +137,68 @@ describe('rust 语言卡片', () => {
})
})
it('照抄的类型逐个名字带自己的下标、按词性着色:参数类型、返回;跨行写的压成一行;没写返回类型的 () 是普通文字', async () => {
const source = [
'fn f(state: State<\'_, AppState>) -> Result<(), String> {}',
'fn g(',
' map: HashMap<',
' String,',
' Vec<u8>,',
' >,',
') -> Option<',
' String,',
'> {}',
'fn h() {}',
'',
].join('\n')
const [f, g, h] = await views(source)
const paramType = (block: BlockView | undefined): PreviewText => propertyRow(block, '输入参数').items?.[0]?.cells[1] ?? []
const returned = (block: BlockView | undefined): PreviewText => propertyRow(block, '返回').value
expect(previewText(paramType(f))).toBe('State<\'_, AppState>')
expect(wordsOf(paramType(f), 'type')).toEqual([['State', source.indexOf('State')], ['AppState', source.indexOf('AppState')]])
expect(previewText(returned(f))).toBe('Result<(), String>')
expect(wordsOf(returned(f), 'type')).toEqual([['Result', source.indexOf('Result')], ['String', source.indexOf('String')]])
expect(previewText(paramType(g))).toBe('HashMap< String, Vec<u8>, >')
expect(previewText(returned(g))).toBe('Option< String, >')
expect([...typeNames(paramType(g), source), ...typeNames(returned(g), source)]).toEqual(['HashMap', 'String', 'Vec', 'u8', 'Option', 'String'])
expect(returned(h)).toEqual([{ text: '()', role: 'plain' }])
})
it('方法表的参数和返回、字段类型、变体携带的数据同样逐个名字带下标,导入来源逐节带下标', async () => {
const source = [
'use std::collections::HashMap;',
'struct S {',
' map: HashMap<String, Vec<u8>>,',
'}',
'enum E {',
' Pair(Option<String>, Box<S>),',
' Named { id: Uuid },',
'}',
'impl S {',
' fn m(',
' &self,',
' state: State<\'_, AppState>,',
' ) -> Result<(), String> {}',
'}',
'',
].join('\n')
const list = await views(source)
const from = propertyRow(card(list, 'import'), '来源').value
expect(previewText(from)).toBe('std::collections')
expect(wordsOf(from, 'type')).toEqual([['std', source.indexOf('std')], ['::', undefined], ['collections', source.indexOf('collections')]])
expect(typeNames(propertyRow(card(list, 'class'), '字段').items?.[0]?.cells[1], source)).toEqual(['HashMap', 'String', 'Vec', 'u8'])
const [pair, named] = propertyRow(card(list, 'enum'), '变体').items ?? []
expect(previewText(pair?.cells[1] ?? [])).toBe('Option<String>, Box<S>')
expect(typeNames(pair?.cells[1], source)).toEqual(['Option', 'String', 'Box', 'S'])
expect(previewText(named?.cells[1] ?? [])).toBe('{ id: Uuid }')
expect(typeNames(named?.cells[1], source)).toEqual(['Uuid'])
const [method] = propertyRow(card(list, 'rust.impl'), '方法').items ?? []
expect(previewText(method?.cells[1] ?? [])).toBe('&self, state: State<\'_, AppState>')
expect(typeNames(method?.cells[1], source)).toEqual(['State', 'AppState'])
expect(previewText(method?.cells[2] ?? [])).toBe('Result<(), String>')
expect(typeNames(method?.cells[2], source)).toEqual(['Result', 'String'])
})
it('带身体的模块拆成子块,常量和外部函数是卡片', async () => {
const source = [
'/// 平台相关。',