✨ 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

+32 -5
View File
@@ -2,6 +2,7 @@ import type { BlockView } from '../blockView'
import type { Inline, PropertyRow, Segment } from '../contract'
import { describe, expect, it } from 'vitest'
import { toBlockViews } from '../blockView'
import { replaceLines } from '../editText'
import { createPythonAdapter } from '../languages/python'
import { flatSegments, propertyLines, readFixture, WASM_DIR, withBlocks } from '../testSupport'
import { pythonZhTranslator } from '../translators/pythonZh'
@@ -122,7 +123,7 @@ describe('sqlAlchemy 库规则', () => {
expect(view.children, view.id).toEqual([])
})
it('并进来的行只在卡片里能编辑,卡片的编辑范围是整个类', async () => {
it('卡片的编辑范围是整个类,每行只在一块里能编辑', async () => {
const lines = SNAPSHOT.split('\n')
const table = await model(SNAPSHOT, 'ReferralRelation')
expect(lines[table.startLine]).toBe('class ReferralRelation(IntPK, TimestampMixin, Base):')
@@ -137,6 +138,32 @@ describe('sqlAlchemy 库规则', () => {
expect(owners.get(lines.indexOf(' __tablename__ = "referral_relations"'))).toEqual([table.id])
})
it('两个字段中间写着的方法照常是子块,卡片编辑范围盖住它、写回整个类;字段前面的注释并进卡片', async () => {
const source = [
'from sqlalchemy.orm import Mapped, mapped_column',
'',
'',
'class Order(Base):',
' __tablename__ = "orders"',
'',
' # 金额',
' amount: Mapped[int] = mapped_column(Integer)',
'',
' # 算税',
' def tax(self) -> int:',
' return self.amount // 10',
'',
' note: Mapped[str] = mapped_column(Text)',
'',
].join('\n')
const [, order] = await views(source)
expect(propertyLines(properties(order)).slice(-3)).toEqual(['字段: 2 个', ' amount | 整数 | | ', ' note | 长文本 | | '])
expect(order.children.map(child => [child.kind, child.startLine])).toEqual([['comment', 9], ['function', 10]])
expect([order.startLine, order.endLine]).toEqual([3, 13])
expect(replaceLines(source, order.startLine, order.endLine, order.code)).toBe(source)
expect(replaceLines(source, order.startLine, order.endLine, order.code.replace('// 10', '// 8'))).toBe(source.replace('// 10', '// 8'))
})
it('跳到定义、高亮出现处:卡片自己是类名的定义,字段行是字段名的定义,带下标的词都指着源码里的这个名字', async () => {
const table = await model(SNAPSHOT, 'ReferralRelation')
expect(SNAPSHOT.slice(table.defines?.startIndex, table.defines?.endIndex)).toBe('ReferralRelation')
@@ -162,7 +189,7 @@ describe('sqlAlchemy 库规则', () => {
expect(table.attributes).toEqual({ kind: 'sqlalchemy.table', access: 'public' })
})
it('import sqlalchemy as sa:老写法 Column、类型全名、只写注解的字段、不认得的类型照常翻、docstring 当说明;方法照常是子块,夹在中间的注释并进来', async () => {
it('import sqlalchemy as sa:老写法 Column、类型全名、只写注解的字段、不认得的类型照常翻、docstring 当说明;方法照常是子块,字段前面的注释并进来', async () => {
const source = [
'import sqlalchemy as sa',
'from sqlalchemy.orm import DeclarativeBase, relationship',
@@ -205,11 +232,11 @@ describe('sqlAlchemy 库规则', () => {
'关系: 1 个',
' orders | Order | 对方字段 user · 单个对象',
])
expect([user.startLine, user.endLine]).toEqual([8, 18])
expect([user.startLine, user.endLine]).toEqual([8, 21])
expect(user.children.map(child => [child.kind, child.startLine])).toEqual([['function', 20]])
})
it('字段的属性 docstring:没写 comment= 时当说明,写了以 comment= 为准;它并进卡片,编辑范围到最后一个字段的说明为止', async () => {
it('字段的属性 docstring:没写 comment= 时当说明,写了以 comment= 为准;它并进卡片', async () => {
const source = [
'from sqlalchemy.orm import Mapped, mapped_column',
'',
@@ -234,7 +261,7 @@ describe('sqlAlchemy 库规则', () => {
' summary | 长文本 | | 案例描述',
' status | 字符串(16) | 可空 | 案例状态',
])
expect([table.startLine, table.endLine]).toEqual([3, 11])
expect([table.startLine, table.endLine]).toEqual([3, 14])
expect(table.children.map(child => [child.kind, child.startLine])).toEqual([['function', 13]])
})