✨ feat(vscode): 代码阅读把字典、对象字面量的值展开成键值树
This commit is contained in:
1 parent
51f2ef7bfa
commit
4930dc0d42
20 files changed
+578
-103
No files matched your search
@@ -1,6 +1,6 @@
|
||||
import type { LanguageAdapter, Translator } from '../../../src/codeReader/contract'
|
||||
import type { RevealPlan } from './reveal'
|
||||
import type { BlockKind, BlockView, PropertyRow, Segment } from './types'
|
||||
import type { BlockKind, BlockView, PropertyItem, PropertyRow, Segment } from './types'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { toBlockViews } from '../../../src/codeReader/blockView'
|
||||
import { createPythonAdapter } from '../../../src/codeReader/languages/python'
|
||||
@@ -138,13 +138,25 @@ describe('跳到定义:TS / Python 源码里闪哪一处', () => {
|
||||
|
||||
/** 不填 defines 的第三方翻译插件:属性表照旧,行和子行都不标讲的是哪个定义。 */
|
||||
function withoutDefines(translator: Translator): Translator {
|
||||
const strip = (items: PropertyItem[]): PropertyItem[] => items.map(({ cells, items: nested }) => nested ? { cells, items: strip(nested) } : { cells })
|
||||
return {
|
||||
...translator,
|
||||
describe: (block, source) => translator.describe(block, source).map(({ label, value, items }) =>
|
||||
items ? { label, value, items: items.map(({ cells }) => ({ cells })) } : { label, value }),
|
||||
items ? { label, value, items: strip(items) } : { label, value }),
|
||||
}
|
||||
}
|
||||
|
||||
/** parentId 下面一层层的子行里 id 是 target 的那个。 */
|
||||
function findItem(items: PropertyItem[] | undefined, parentId: string, target: string): PropertyItem | undefined {
|
||||
for (const [index, item] of (items ?? []).entries()) {
|
||||
const id = propertyItemId(parentId, index)
|
||||
const found = id === target ? item : findItem(item.items, id, target)
|
||||
if (found)
|
||||
return found
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
function flatten(list: BlockView[]): BlockView[] {
|
||||
return list.flatMap(view => [view, ...flatten(view.children)])
|
||||
}
|
||||
@@ -163,7 +175,7 @@ describe('跳到定义:TS / Python 源码里闪哪一处', () => {
|
||||
const rowId = propertyRowId(block.id, index)
|
||||
if (plan.row === rowId)
|
||||
return `${row.label}: ${text(row.value)}`
|
||||
const item = row.items?.find((_, i) => plan.row === propertyItemId(rowId, i))
|
||||
const item = findItem(row.items, rowId, plan.row)
|
||||
if (item)
|
||||
return item.cells.map(text).join(' | ')
|
||||
}
|
||||
@@ -267,6 +279,27 @@ describe('跳到定义:TS / Python 源码里闪哪一处', () => {
|
||||
expect(await atPy('run')).toEqual({ plan: { id: '0.0', expand: ['0'] }, flashed: 'def run(self):' })
|
||||
})
|
||||
|
||||
it('字典 / 对象:定义落在对象的属性名上只闪那一项,嵌套的连外层各层一起展开;值里只是显示着的名字闪整块,展开到它那一层', async () => {
|
||||
const source = [
|
||||
'export const routes = {',
|
||||
' home: { path: \'/\', page: Home },',
|
||||
' about,',
|
||||
'}',
|
||||
'use(routes.home.path)',
|
||||
].join('\n')
|
||||
const at = (needle: string): ReturnType<typeof jump> => jump(ts, 'typescript', typescriptZhTranslator, source, needle)
|
||||
expect(await at('home:')).toEqual({ plan: { id: '0', row: '0/p2/0', expand: ['0/p2'] }, flashed: 'home | 对象 2 项' })
|
||||
expect(await at('path:')).toEqual({ plan: { id: '0', row: '0/p2/0/0', expand: ['0/p2', '0/p2/0'] }, flashed: 'path | \'/\'' })
|
||||
expect(await at('about,')).toEqual({ plan: { id: '0', row: '0/p2/1', expand: ['0/p2'] }, flashed: 'about | about' })
|
||||
expect(await at('Home')).toEqual({ plan: { id: '0', expand: ['0/p2', '0/p2/0'] }, flashed: 'export const routes = {' })
|
||||
const plain = (needle: string): ReturnType<typeof jump> => jump(ts, 'typescript', withoutDefines(typescriptZhTranslator), source, needle)
|
||||
expect(await plain('path:')).toEqual({ plan: { id: '0', expand: ['0/p2', '0/p2/0'] }, flashed: 'export const routes = {' })
|
||||
|
||||
const pySource = 'CONFIG = {"db": {"host": host}}'
|
||||
const atPy = (needle: string): ReturnType<typeof jump> => jump(py, 'python', pythonZhTranslator, pySource, needle)
|
||||
expect(await atPy('host}')).toEqual({ plan: { id: '0', expand: ['0/p2', '0/p2/0'] }, flashed: 'CONFIG = {"db": {"host": host}}' })
|
||||
})
|
||||
|
||||
it('typeScript:翻译插件没标 defines 时闪整块', async () => {
|
||||
const at = (needle: string): ReturnType<typeof jump> => jump(ts, 'typescript', withoutDefines(typescriptZhTranslator), TS, needle)
|
||||
expect(await at('path: string')).toEqual({ plan: { id: '1', expand: ['1/p3'] }, flashed: 'export function load(path: string, { encoding }: Options): string {' })
|
||||
|
||||
Reference in new issue
Block a user