✨ feat(vscode): 代码阅读库规则卡片不再吞掉字段中间的方法,编辑时收起子块

This commit is contained in:
cruldra committed 2026-09-26 19:58:43 +08:00
1 parent c803fd457f
commit 2251e67ef8
15 files changed
+280 -54

No files matched your search

+35 -10
View File
@@ -3,6 +3,7 @@ import type { CodeBlock, LibraryRule, RuleBlock, RuleLocation, RuleWorkspace, Se
import { describe, expect, it } from 'vitest'
import { fileAttributes } from './attributes'
import { toBlockViews } from './blockView'
import { replaceLines } from './editText'
import { createPythonAdapter } from './languages/python'
import { activeRules, fromLibrary, importsIn, resolveReference } from './libraryRules'
import { WASM_DIR, withBlocks } from './testSupport'
@@ -90,7 +91,7 @@ describe('库规则:启用', () => {
})
describe('库规则:认领', () => {
it('认领的块换成替身:种类、标签、编辑范围到最后一个并进来的子块,夹在中间的注释也并进来,之后的子块留下', async () => {
it('认领的块换成替身:种类、标签、编辑范围是整个类;规则没并进来的子块(夹在中间的注释、方法)照源码顺序留下', async () => {
const rule: LibraryRule = {
...cardRule('orm'),
claim: (block) => {
@@ -105,22 +106,46 @@ describe('库规则:认领', () => {
kind: 'orm.card',
badge: { label: '卡片' },
startLine: 2,
endLine: 5,
code: 'class User(Base):\n __tablename__ = "users"\n # 基本信息\n name = mapped_column(String)',
endLine: 8,
code: 'class User(Base):\n __tablename__ = "users"\n # 基本信息\n name = mapped_column(String)\n\n def greet(self) -> str:\n return self.name',
attributes: { kind: 'orm.card', access: 'public' },
properties: [{ label: '名称' }],
})
expect(card.children.map(child => [child.id, child.kind, child.startLine])).toEqual([['1.0', 'function', 7]])
expect(card.children.map(child => [child.id, child.kind, child.startLine])).toEqual([['1.0', 'comment', 4], ['1.1', 'function', 7]])
})
it('每一行最多在一块里能编辑:并进来的行只在替身里', async () => {
it('两个字段中间写着的方法:预览里照常是卡片的子块;卡片「编辑」写回整个类', async () => {
const source = [
'from sqlalchemy.orm import mapped_column',
'',
'class User(Base):',
' id = mapped_column(Integer)',
'',
' def greet(self) -> str:',
' return self.name',
'',
' name = mapped_column(String)',
'',
].join('\n')
const fields: LibraryRule = { ...cardRule('orm'), claim: block => block.kind === 'class'
? { kind: 'orm.card', badge: { label: '卡片', color: { dark: '#fff', light: '#000' } }, rows: [], absorbs: block.children.filter(child => child.kind === 'variable') }
: undefined }
const [, card] = await views(source, [fields])
expect(card.children.map(child => [child.kind, child.startLine, child.endLine])).toEqual([['function', 5, 5]])
expect([card.startLine, card.endLine]).toEqual([2, 8])
expect(replaceLines(source, card.startLine, card.endLine, card.code)).toBe(source)
const edited = replaceLines(source, card.startLine, card.endLine, card.code.replace('greet', 'hello').replace('String', 'Text'))
expect(edited).toBe(source.replace('greet', 'hello').replace('String', 'Text'))
})
it('能编辑的范围只在块和它的子孙之间重叠:卡片盖住的是它自己留下的子块,别的块互不重叠', async () => {
const list = flatten(await views(MODEL, [cardRule('orm', {})]))
const owners = new Map<number, string[]>()
for (const view of list) {
for (let line = view.startLine; line <= view.endLine; line++)
owners.set(line, [...owners.get(line) ?? [], view.id])
const related = (a: string, b: string): boolean => a.startsWith(`${b}.`) || b.startsWith(`${a}.`)
for (const a of list) {
for (const b of list.filter(other => other !== a && !related(a.id, other.id)))
expect(a.startLine > b.endLine || b.startLine > a.endLine, `${a.id} / ${b.id}`).toBe(true)
}
expect([...owners.values()].filter(ids => ids.length > 1)).toEqual([])
expect(list.filter(view => view.id.startsWith('1.')).every(view => view.startLine >= list[1].startLine && view.endLine <= list[1].endLine)).toBe(true)
})
it('几个规则都认同一块时先注册的算', async () => {