✨ feat(vscode): 代码阅读加 Celery 库规则,定时任务表里的任务名能跳到任务函数

This commit is contained in:
cruldra committed 2026-09-26 19:41:07 +08:00
1 parent 905d7845b5
commit 68104e2cba
22 files changed
+2228 -139

No files matched your search

+59 -2
View File
@@ -1,10 +1,10 @@
import type { BlockView, RuleRun } from './blockView'
import type { CodeBlock, LibraryRule, RuleBlock } from './contract'
import type { CodeBlock, LibraryRule, RuleBlock, RuleLocation, RuleWorkspace, Segment } from './contract'
import { describe, expect, it } from 'vitest'
import { fileAttributes } from './attributes'
import { toBlockViews } from './blockView'
import { createPythonAdapter } from './languages/python'
import { activeRules, fromLibrary, importsIn } from './libraryRules'
import { activeRules, fromLibrary, importsIn, resolveReference } from './libraryRules'
import { WASM_DIR, withBlocks } from './testSupport'
import { pythonZhTranslator } from './translators/pythonZh'
@@ -164,3 +164,60 @@ describe('库规则:认领', () => {
])
})
})
describe('库规则:自己查定义的引用', () => {
const workspace: RuleWorkspace = { lookup: async () => [] }
const found: RuleLocation[] = [{ uri: 'file:///w/tasks.py', start: { line: 3, character: 4 }, end: { line: 3, character: 8 } }]
/** 认领类块、在名称那一行标一个引用的规则。 */
function referencing(id: string, reference: Segment, resolve?: LibraryRule['resolve']): LibraryRule {
return {
...cardRule(id, { rows: [{ label: '名称', value: [reference] }] }),
...(resolve && { resolve }),
}
}
it('引用要标自己的规则、规则要给 resolve、段要带下标,不然这块照常显示', async () => {
const reports: string[] = []
const resolve = async (): Promise<RuleLocation[]> => found
const list = await views(MODEL, [
referencing('other', { text: 'x', role: 'string', offset: 20, reference: { rule: 'celery', key: 'x' } }, resolve),
referencing('lazy', { text: 'x', role: 'string', offset: 20, reference: { rule: 'lazy', key: 'x' } }),
referencing('loose', { text: 'x', role: 'string', reference: { rule: 'loose', key: 'x' } }, resolve),
], reports)
expect(list[1].kind).toBe('class')
expect(reports.map(report => report.replace(/^.*照常显示:/, ''))).toEqual([
'引用「x」标的规则是「celery」,不是自己',
'标了引用「x」却没给 resolve',
'引用「x」没带源码下标',
])
const good = await views(MODEL, [referencing('good', { text: 'x', role: 'string', offset: 20, reference: { rule: 'good', key: 'x' } }, resolve)])
expect(good[1].kind).toBe('good.card')
})
it('查引用交给标它的规则;规则不在、没给 resolve、出错时当作没有定义并记下来', async () => {
const reports: string[] = []
const asked: [string, RuleWorkspace][] = []
const rule = referencing('good', { text: 'x', role: 'string', offset: 20 }, async (key, given) => {
asked.push([key, given])
return found
})
const broken = referencing('broken', { text: 'x', role: 'string', offset: 20 }, async () => {
throw new Error('索引坏了')
})
const rules = [rule, broken, cardRule('plain')]
const report = (message: string): number => reports.push(message)
expect(await resolveReference({ rule: 'good', key: 'a.b' }, rules, workspace, report)).toEqual(found)
expect(asked).toEqual([['a.b', workspace]])
expect(await resolveReference({ rule: 'broken', key: 'a.b' }, rules, workspace, report)).toEqual([])
expect(await resolveReference({ rule: 'plain', key: 'a.b' }, rules, workspace, report)).toEqual([])
expect(await resolveReference({ rule: 'gone', key: 'a.b' }, rules, workspace, report)).toEqual([])
expect(reports).toEqual([
'库规则 broken 查「a.b」的定义时出错:索引坏了',
'没有能查引用「a.b」的库规则 plain',
'没有能查引用「a.b」的库规则 gone',
])
})
})