🐛 fix(vscode): 代码阅读跳到参数、导入项、多变量声明里的一个时只闪属性表里那一行,按源码位置判断

This commit is contained in:
cruldra committed 2026-09-25 14:11:44 +08:00
1 parent 5b4ca903c5
commit 3ac1c783fb
14 files changed
+367 -47

No files matched your search

@@ -55,9 +55,9 @@ function texts(map: Map<number, Translated>): Record<number, string> {
return Object.fromEntries([...map].map(([line, { segments }]) => [line, text(segments)]))
}
/** 只看文字和着色:去掉各段的源码下标(下标另有用例)。 */
/** 只看文字和着色:去掉各段的源码下标、属性行讲的定义(这两样另有用例)。 */
function colors<T>(value: T): T {
return JSON.parse(JSON.stringify(value, (key, field: unknown) => key === 'offset' ? undefined : field)) as T
return JSON.parse(JSON.stringify(value, (key, field: unknown) => key === 'offset' || key === 'defines' ? undefined : field)) as T
}
const CJK = /\p{Script=Han}/u
@@ -578,6 +578,35 @@ describe('源码下标(跳到定义用)', () => {
expect(linked(map.get(4)?.segments)).toEqual([['opts', at('opts ===')]])
})
it('属性行、子行标出它讲的定义:参数名(连前缀、整个解构模式)、导入项的名称到别名、每个声明的变量名,其余行不标', async () => {
/** 标了 defines 的行写成「属性名: 那段源码」,子行缩进两格只写那段源码。 */
const marked = (source: string, rows: PropertyRow[] | undefined): string[] => (rows ?? []).flatMap(row => [
...(row.defines ? [`${row.label}: ${source.slice(row.defines.startIndex, row.defines.endIndex)}`] : []),
...(row.items ?? []).flatMap(item => item.defines ? [` ${source.slice(item.defines.startIndex, item.defines.endIndex)}`] : []),
])
const pySource = [
'from m import a as b, c',
'import os.path as p',
'def f(self, *args, key: int = 1, **kw): pass',
'x = 1',
].join('\n')
const pyRows = await describeAll(py, 'python', pythonZhTranslator, pySource)
expect(marked(pySource, pyRows.get(1))).toEqual([' a as b', ' c'])
expect(marked(pySource, pyRows.get(2))).toEqual([' os.path as p'])
expect(marked(pySource, pyRows.get(3))).toEqual([' self', ' *args', ' key', ' **kw'])
expect(marked(pySource, pyRows.get(4))).toEqual(['名称: x'])
const tsSource = [
'import d, * as ns from \'m\'',
'function g({ u, v }: T, w?: number) {}',
'let i = 0, j',
].join('\n')
const tsRows = await describeAll(ts, 'typescript', typescriptZhTranslator, tsSource)
expect(marked(tsSource, tsRows.get(1))).toEqual([' d', ' ns'])
expect(marked(tsSource, tsRows.get(2))).toEqual([' { u, v }', ' w'])
expect(marked(tsSource, tsRows.get(3))).toEqual(['名称: i', '名称: j'])
})
it('样例全文:带下标的只有照抄的名字和字符串,下标处的源码就是这段;变量名、函数名都带,译词、说明词都不带', async () => {
const samples: [LanguageAdapter, string, Translator, string][] = [
[ts, 'typescript', typescriptZhTranslator, 'envLock.ts.txt'],
@@ -1,4 +1,4 @@
import type { Access, FactBlock, FunctionFacts, ImportFacts, ImportItem, Node, PropertyRow, Segment, SourceSpan, TextBlock, TokenRole, Translator, VariableBlock } from '../contract'
import type { Access, Defines, FactBlock, FunctionFacts, ImportFacts, ImportItem, Node, PropertyRow, Segment, SourceSpan, TextBlock, TokenRole, Translator, VariableBlock } from '../contract'
import { commonIndent, splitLines } from '../editText'
/** 按所在语法节点区分译法的关键字,例如 Python 的 else:if 后「否则」,for / while 后「未中断时」。 */
@@ -388,6 +388,25 @@ function accessRows(access: Access | undefined): PropertyRow[] {
return access ? [{ label: '访问级别', value: [{ text: ACCESS_ZH[access], role: 'keyword' }] }] : []
}
/** 这一行 / 子行讲的定义:几个节点(导入项是名称和别名)合起来在源码里的范围;一个节点都没有就不填。 */
function defining(...nodes: (Node | undefined)[]): Defines {
const present = nodes.filter((node): node is Node => node !== undefined)
if (present.length === 0)
return {}
return { defines: { startIndex: Math.min(...present.map(node => node.startIndex)), endIndex: Math.max(...present.map(node => node.endIndex)) } }
}
function importNodes(item: ImportItem): (Node | undefined)[] {
switch (item.kind) {
case 'named':
return [item.name, item.alias]
case 'module':
return [item.module, item.alias]
default:
return [item.alias]
}
}
function aliased(item: Segment[], alias: Node | undefined, say: Phrases): Segment[] {
if (!alias)
return item
@@ -413,6 +432,7 @@ function functionRows(facts: FunctionFacts, say: Phrases): PropertyRow[] {
param.type ? say.translated(param.type) : [hint('未标注')],
param.default ? [hint('默认 '), ...say.translated(param.default)] : [],
],
...defining(param.name),
})),
},
{ label: '输出参数', value: facts.returnType ? say.translated(facts.returnType) : [hint('未标注')] },
@@ -437,7 +457,7 @@ function importRows(facts: ImportFacts, say: Phrases): PropertyRow[] {
return [
{ label: '来源', value: facts.sources.length > 0 ? joined(facts.sources.map(say.module), ', ') : [hint('无')] },
...(facts.typeOnly === undefined ? [] : [{ label: '仅类型', value: yesNo(facts.typeOnly) }]),
{ label: '导入项', value: count(facts.items.length), items: facts.items.map(item => ({ cells: [itemText(item)] })) },
{ label: '导入项', value: count(facts.items.length), items: facts.items.map(item => ({ cells: [itemText(item)], ...defining(...importNodes(item)) })) },
]
}
@@ -445,7 +465,7 @@ function importRows(facts: ImportFacts, say: Phrases): PropertyRow[] {
function variableRows(block: VariableBlock, say: Phrases): PropertyRow[] {
const valueLabel = block.kind === 'const' ? '值' : '初始值'
return block.facts.declarators.flatMap(declarator => [
{ label: '名称', value: say.translated(declarator.name) },
{ label: '名称', value: say.translated(declarator.name), ...defining(declarator.name) },
...accessRows(block.facts.access),
...(declarator.type ? [{ label: '类型', value: say.translated(declarator.type) }] : []),
{ label: valueLabel, value: declarator.value ? say.translated(declarator.value) : [hint('无')] },