✨ feat(vscode): 代码阅读的输入参数排成带表头和边框的表格

This commit is contained in:
cruldra committed 2026-09-26 16:15:59 +08:00
1 parent 6416cdf714
commit 83adefdf5e
10 files changed
+197 -24

No files matched your search

@@ -433,7 +433,7 @@ describe('python 写在表达式里的遍历(推导式)', () => {
'CONFIG = {"ids": {k: v for k, v in pairs}}',
].join('\n')
const rows = rowTexts(await describeAll(py, 'python', pythonZhTranslator, source))
expect(rows[1]).toContain(' ids | 未标注 | 默认 ⟦列表|遍历 users 的每个 u,取 u.id⟧')
expect(rows[1]).toContain(' ids | 未标注 | ⟦列表|遍历 users 的每个 u,取 u.id⟧')
expect(rows[2]).toEqual(['名称: CONFIG', '访问级别: 公共', '值: 字典 1 项', ' "ids" | ⟦字典|遍历 pairs 的每个 k, v,取 k: v⟧'])
})
@@ -504,8 +504,8 @@ describe('属性表', () => {
'装饰器: @router.get("/{user_id}", response_model=UserDetailResponse)',
'输入参数: 3 个',
' user_id | Int4Path | ',
' db | AsyncSession | 默认 Depends(get_db)',
' current_user | User | 默认 Depends(get_current_user)',
' db | AsyncSession | Depends(get_db)',
' current_user | User | Depends(get_current_user)',
'输出参数: UserDetailResponse',
])
expect(routes[111]).toContain(' file | Annotated[UploadFile, File(..., description="头像文件")] | ')
@@ -518,7 +518,29 @@ describe('属性表', () => {
expect(port[1]).toEqual(['名称: _default_port', '访问级别: 私有', '初始值: 如果 _role 等于 "beat" 则 5680 否则 5679'])
})
it('属性名是纯文字;值按词性着色,说明词(未标注、N 个、默认)用 hint', async () => {
it('输入参数的子行排成表格:列名是名称 / 类型 / 默认值,默认值格子只写译文、没有就留空;导入项、字典的子行没有列名', async () => {
const pySource = [
'from m import a, b',
'def f(x: int = 1, y=None, *args): pass',
'D = {"k": 1}',
].join('\n')
const py1 = await describeAll(py, 'python', pythonZhTranslator, pySource)
const params = py1.get(2)?.find(row => row.label === '输入参数')
expect(params?.columns).toEqual(['名称', '类型', '默认值'])
expect(colors(params?.items?.map(item => item.cells))).toEqual([
[[{ text: 'x', role: 'variable' }], [{ text: '整数', role: 'type' }], [{ text: '1', role: 'number' }]],
[[{ text: 'y', role: 'variable' }], [{ text: '未标注', role: 'hint' }], [{ text: '空', role: 'storage' }]],
[[{ text: '*', role: 'plain' }, { text: 'args', role: 'variable' }], [{ text: '未标注', role: 'hint' }], []],
])
expect([...py1.values()].flat().filter(row => row.columns).map(row => row.label)).toEqual(['输入参数'])
const tsRows = await describeAll(ts, 'typescript', typescriptZhTranslator, 'function g(a: string, b = 2) {}')
const tsParams = tsRows.get(1)?.find(row => row.label === '输入参数')
expect(tsParams?.columns).toEqual(['名称', '类型', '默认值'])
expect(tsParams?.items?.map(item => item.cells.map(cell => text(cell)))).toEqual([['a', '字符串', ''], ['b', '未标注', '2']])
})
it('属性名是纯文字;值按词性着色,说明词(未标注、N 个)用 hint', async () => {
const env = await describeAll(ts, 'typescript', typescriptZhTranslator, readFixture('envLock.ts.txt'))
const rows = colors(env.get(18) ?? [])
expect(rows.map(row => row.label)).toEqual(['名称', '说明', '访问级别', '异步', '输入参数', '输出参数'])
@@ -547,7 +569,7 @@ describe('属性表', () => {
expect(colors(valueOf(sample.get(3), '来源'))).toEqual([{ text: 'os', role: 'type' }])
expect(colors(sample.get(39)?.find(row => row.label === '输入参数')?.items)).toEqual([
{ cells: [[{ text: 'self', role: 'variable' }], [{ text: '未标注', role: 'hint' }], []] },
{ cells: [[{ text: 'verbose', role: 'variable' }], [{ text: '布尔', role: 'type' }], [{ text: '默认 ', role: 'hint' }, { text: '假', role: 'storage' }]] },
{ cells: [[{ text: 'verbose', role: 'variable' }], [{ text: '布尔', role: 'type' }], [{ text: '假', role: 'storage' }]] },
])
})
@@ -677,7 +699,7 @@ describe('属性表', () => {
].join('\n')
expect(rowTexts(await describeAll(ts, 'typescript', typescriptZhTranslator, source))).toEqual({
1: ['名称: 匿名', '访问级别: 公共', '异步: 是', '输入参数: 无', '输出参数: 未标注'],
2: ['名称: f', '访问级别: 私有', '异步: 否', '输入参数: 2 个', ' b? | 数值 | ', ' c | 未标注 | 默认 1', '输出参数: 无'],
2: ['名称: f', '访问级别: 私有', '异步: 否', '输入参数: 2 个', ' b? | 数值 | ', ' c | 未标注 | 1', '输出参数: 无'],
3: ['名称: inner', '异步: 否', '输入参数: 无', '输出参数: 未标注'],
5: ['名称: K', '访问级别: 私有', '抽象: 否'],
6: ['名称: load', '访问级别: 受保护', '异步: 否', '装饰器: @memo()', '输入参数: 无', '输出参数: x is Foo'],
@@ -550,7 +550,10 @@ function aliased(item: Inline[], alias: Node | undefined, say: Phrases): Inline[
return [...item, { text: ' ', role: 'plain' }, { text: '作为', role: 'keyword' }, { text: ' ', role: 'plain' }, ...say.translated(alias)]
}
/** 函数:名称 / 说明 / 访问级别 / 异步(只有分这个的语言才有)/ 装饰器 / 输入参数(每个参数一行:名称、类型、默认值)/ 输出参数。 */
/**
* 函数:名称 / 说明 / 访问级别 / 异步(只有分这个的语言才有)/ 装饰器 / 输入参数 / 输出参数。
* 输入参数是一张表,列名是名称 / 类型 / 默认值,每个参数一行;没有默认值的格子留空。
*/
function functionRows(facts: FunctionFacts, say: Phrases): PropertyRow[] {
return [
{ label: '名称', value: facts.name ? say.name(facts.name, 'function') : [hint('匿名')] },
@@ -561,11 +564,12 @@ function functionRows(facts: FunctionFacts, say: Phrases): PropertyRow[] {
{
label: '输入参数',
value: count(facts.parameters.length),
columns: ['名称', '类型', '默认值'],
items: facts.parameters.map(param => ({
cells: [
param.optional ? [...say.translated(param.name), { text: '?', role: 'plain' }] : say.translated(param.name),
param.type ? say.translated(param.type) : [hint('未标注')],
param.default ? [hint('默认 '), ...say.translated(param.default)] : [],
param.default ? say.translated(param.default) : [],
],
...defining(param.name),
})),