✨ feat(vscode): 代码阅读的块种类只画图标(lucide,种类色),种类名放进鼠标停上去的提示和读屏文字;库规则给自己的图标

This commit is contained in:
cruldra committed 2026-09-27 20:02:32 +08:00
1 parent 33a1b5d57c
commit 1dfc0c7b1e
19 files changed
+185 -56

No files matched your search

+2 -2
View File
@@ -46,14 +46,14 @@ export function blockAttributes(block: CodeBlock): BlockAttributes {
interface Attributed {
attributes: BlockAttributes
/** 库规则认领的块的种类标签:这个种类在筛选栏里也叫它。 */
/** 库规则认领的块的种类(名字、颜色、图标):这个种类在筛选栏里也叫它的名字。 */
badge?: { label: string }
children: Attributed[]
}
/**
* 整份文件(所有层的块)里出现过的属性,每个值有几块。通用属性按 COMMON_ATTRIBUTES 的顺序排在前面,
* 语言自己的属性按第一次出现的先后排在后面。显示名取翻译插件的 names,没给的用 id;库规则的种类用规则给的标签。
* 语言自己的属性按第一次出现的先后排在后面。显示名取翻译插件的 names,没给的用 id;库规则的种类用规则给的名字(badge.label)。
*/
export function fileAttributes(blocks: readonly Attributed[], names: AttributeNames | undefined): FileAttribute[] {
const counts = new Map<string, Map<string, number>>()
+1 -1
View File
@@ -26,7 +26,7 @@ interface BlockViewBase {
defines?: SourceRange
/** 筛选属性(含 kind),见 blockAttributes;库规则认领的块叠上规则给的,kind 是规则的种类 id。 */
attributes: BlockAttributes
/** 库规则的卡片才有:种类标签的文字和颜色(RuleCard.badge)。预览同类块一样默认收起它的子块。 */
/** 库规则的卡片才有:种类的名字、颜色和图标(RuleCard.badge)。预览同类块一样默认收起它的子块。 */
badge?: KindBadge
children: BlockView[]
}
+8 -5
View File
@@ -382,7 +382,7 @@ export interface SourceDocument {
markers: SourceRange[]
}
/** 筛选属性和值的显示名,筛选栏、种类标签用它;没给名字的显示 id。 */
/** 筛选属性和值的显示名,筛选栏、块的种类(图标的悬停提示)用它;没给名字的显示 id。 */
export interface AttributeNames {
/** 属性 id → 属性名,例如 access → 访问级别。 */
attributes: Record<string, string>
@@ -412,8 +412,11 @@ export interface Translator {
/** 一种颜色在暗色、亮色主题下各用哪个(CSS 颜色值);高对比主题跟着暗 / 亮走。 */
export interface ThemeColor { dark: string, light: string }
/** 种类标签:文字和颜色。 */
export interface KindBadge { label: string, color: ThemeColor }
/**
* 库规则的种类在预览里的样子:块左边只画图标(种类色),名字是悬停提示和读屏念的文字,筛选栏里这个种类也叫它。
* icon 是 lucide 图标名(https://lucide.dev/icons 上的 kebab-case 名字,如 `database`)。
*/
export interface KindBadge { label: string, color: ThemeColor, icon: string }
/**
* 库规则认领一块后给出的替身,预览里它代替原来那块,下面挂着原块留下的子块:
@@ -421,11 +424,11 @@ export interface KindBadge { label: string, color: ThemeColor }
*/
export type RuleBlock = RuleCard | RuleFacts
/** 规则自己的卡片:一张属性表,种类标签、每一行都由规则给(用词归规则)。 */
/** 规则自己的卡片:一张属性表,种类的图标、颜色、名字和每一行都由规则给(用词归规则)。 */
export interface RuleCard {
/** 种类 id,写成「规则 id.名字」(sqlalchemy.table):筛选的 kind 属性值就是它,筛选记住的也是它,要稳定。 */
kind: string
/** 种类标签的文字和颜色:筛选栏里这个种类也叫这个名字。 */
/** 种类的名字、颜色和图标:筛选栏里这个种类也叫这个名字。 */
badge: KindBadge
rows: PropertyRow[]
/** 块自己的名字(类名)在源码里的范围:跳到的定义就是它时闪整块。 */
@@ -296,7 +296,7 @@ describe('异常卡片(TypeScript)', () => {
})
describe('异常卡片(宿主、筛选、中文名)', () => {
it('筛选:种类是 exception、叫「异常」,访问级别同原来的类;没有种类标签(badge),库规则不认', async () => {
it('筛选:种类是 exception、叫「异常」,访问级别同原来的类;没有规则给的种类(badge),库规则不认', async () => {
const list = await pyViews(PY_CASES)
expect(named(list, 'Base').attributes).toEqual({ kind: 'exception', access: 'public' })
expect(named(list, 'Base').badge).toBeUndefined()
+5 -5
View File
@@ -32,7 +32,7 @@ function cardRule(id: string, overrides: Partial<RuleBlock> = {}): LibraryRule {
languageIds: ['python'],
libraries: ['sqlalchemy'],
claim: block => block.kind === 'class'
? { kind: `${id}.card`, badge: { label: `${id} 卡片`, color: { dark: '#fff', light: '#000' } }, rows: [], absorbs: block.children.slice(0, 1), ...overrides }
? { kind: `${id}.card`, badge: { label: `${id} 卡片`, color: { dark: '#fff', light: '#000' }, icon: 'box' }, rows: [], absorbs: block.children.slice(0, 1), ...overrides }
: undefined,
}
}
@@ -91,14 +91,14 @@ describe('库规则:启用', () => {
})
describe('库规则:认领', () => {
it('认领的块换成替身:种类、标签、编辑范围是整个类;规则没并进来的子块(夹在中间的注释、方法)照源码顺序留下', async () => {
it('认领的块换成替身:种类、badge、编辑范围是整个类;规则没并进来的子块(夹在中间的注释、方法)照源码顺序留下', async () => {
const rule: LibraryRule = {
...cardRule('orm'),
claim: (block) => {
if (block.kind !== 'class')
return undefined
const [table, , column] = block.children
return { kind: 'orm.card', badge: { label: '卡片', color: { dark: '#fff', light: '#000' } }, rows: [{ label: '名称', value: [{ text: 'User', role: 'type' }] }], absorbs: [table, column] }
return { kind: 'orm.card', badge: { label: '卡片', color: { dark: '#fff', light: '#000' }, icon: 'box' }, rows: [{ label: '名称', value: [{ text: 'User', role: 'type' }] }], absorbs: [table, column] }
},
}
const [, card] = await views(MODEL, [rule])
@@ -128,7 +128,7 @@ describe('库规则:认领', () => {
'',
].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') }
? { kind: 'orm.card', badge: { label: '卡片', color: { dark: '#fff', light: '#000' }, icon: 'box' }, 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]])
@@ -198,7 +198,7 @@ describe('库规则:认领', () => {
expect(reports).toEqual(['库规则 lib 给第 3 行的 class 块的替身不合约定,这块照常显示:要并进来的块不是这块的子块'])
})
it('筛选:规则的种类和别的种类一样计数,名字取规则给的标签', async () => {
it('筛选:规则的种类和别的种类一样计数,名字取规则给的名字(badge.label)', async () => {
const list = await views(MODEL, [cardRule('orm')])
expect(fileAttributes(list, pythonZhTranslator.attributeNames).find(attribute => attribute.id === 'kind')?.values).toEqual([
{ id: 'import', name: '导入', count: 1 },
+2 -2
View File
@@ -46,7 +46,7 @@ function segmentsOf(rows: PropertyRow[]): Segment[] {
describe('celery 库规则:应用', () => {
it('luffy 的 celery_app:名称 / 应用名 / 消息队列 / 结果存储', async () => {
const app = await card(SNAPSHOT, 'celery.app')
expect(app.badge).toEqual({ label: 'Celery 应用', color: { dark: '#98c379', light: '#4d7f17' } })
expect(app.badge).toEqual({ label: 'Celery 应用', color: { dark: '#98c379', light: '#4d7f17' }, icon: 'server-cog' })
expect(propertyLines(properties(app))).toEqual([
'名称: celery_app',
'应用名: "luffy_agent"',
@@ -90,7 +90,7 @@ describe('celery 库规则:应用', () => {
describe('celery 库规则:定时任务', () => {
it('luffy 的 beat_schedule:应用、任务表,频率换算成人话,说明取注释,展开另起一行', async () => {
const beat = await card(SNAPSHOT, 'celery.beat')
expect(beat.badge).toEqual({ label: '定时任务', color: { dark: '#56b6c2', light: '#127f8c' } })
expect(beat.badge).toEqual({ label: '定时任务', color: { dark: '#56b6c2', light: '#127f8c' }, icon: 'calendar-clock' })
expect(propertyLines(properties(beat))).toEqual([
'应用: celery_app',
'任务: 12 条,另有展开 1 处',
+4 -2
View File
@@ -21,6 +21,8 @@ const APP_KIND = 'celery.app'
const BEAT_KIND = 'celery.beat'
const APP_COLOR: ThemeColor = { dark: '#98c379', light: '#4d7f17' }
const BEAT_COLOR: ThemeColor = { dark: '#56b6c2', light: '#127f8c' }
const APP_ICON = 'server-cog'
const BEAT_ICON = 'calendar-clock'
/** `Celery(...)` 的位置参数依次是哪个参数(celery.app.base.Celery.__init__)。 */
const APP_PARAMETERS = ['main', 'loader', 'backend', 'amqp', 'events', 'log', 'control', 'set_as_current', 'tasks', 'broker', 'include', 'changes', 'config_source', 'fixups', 'task_cls', 'autofinalize', 'namespace', 'strict_typing']
@@ -500,7 +502,7 @@ function beatBlock(app: Node, schedule: Node, settings: [string, Node][], doc: N
items: entries.map(entry => entryItem(entry, withOptions, env)),
},
]
return { kind: BEAT_KIND, badge: { label: words.beatKind, color: BEAT_COLOR }, rows, absorbs: [] }
return { kind: BEAT_KIND, badge: { label: words.beatKind, color: BEAT_COLOR, icon: BEAT_ICON }, rows, absorbs: [] }
}
// ─── Celery 应用 ──────────────────────────────────────────────────────────────
@@ -527,7 +529,7 @@ function appBlock(target: Node, call: Call, doc: Node | undefined, env: Env): Ru
]
return {
kind: APP_KIND,
badge: { label: words.appKind, color: APP_COLOR },
badge: { label: words.appKind, color: APP_COLOR, icon: APP_ICON },
rows,
...(target.type === 'identifier' && { defines: range(target) }),
absorbs: [],
@@ -128,11 +128,11 @@ describe('dataclasses 库规则(Python)', () => {
expect(sandbox.children.map(child => [child.kind, child.startLine])).toEqual([['function', 52]])
})
it('筛选:种类是 dataclasses.dataclass、叫「数据类」,访问级别同原来的类;种类标签有自己的颜色,默认收起子块', async () => {
it('筛选:种类是 dataclasses.dataclass、叫「数据类」,访问级别同原来的类;种类有自己的颜色和图标,默认收起子块', async () => {
const list = await views(SNAPSHOT)
const sandbox = named(list, 'SandboxAccess')
expect(sandbox.attributes).toEqual({ kind: 'dataclasses.dataclass', access: 'public' })
expect(sandbox.badge).toEqual({ label: '数据类', color: { dark: '#ef7fb0', light: '#b3306c' } })
expect(sandbox.badge).toEqual({ label: '数据类', color: { dark: '#ef7fb0', light: '#b3306c' }, icon: 'package' })
expect(fileAttributes(list, pythonZhTranslator.attributeNames).find(attribute => attribute.id === 'kind')?.values)
.toContainEqual({ id: 'dataclasses.dataclass', name: '数据类', count: 4 })
})
+2 -1
View File
@@ -16,6 +16,7 @@ const LIBRARY = 'dataclasses'
const TYPE_LIBRARIES = ['dataclasses', 'typing', 'typing_extensions']
const KIND = 'dataclasses.dataclass'
const COLOR: ThemeColor = { dark: '#ef7fb0', light: '#b3306c' }
const ICON = 'package'
/** `@dataclass(...)` 各参数不写时的值:写成这个值等于没写,「选项」里不讲。 */
const OPTION_DEFAULTS: Record<string, boolean> = {
@@ -261,7 +262,7 @@ function dataclassCard(block: ClassBlock, decorator: Node, call: Call | undefine
]
return {
kind: KIND,
badge: { label: words.kind, color: COLOR },
badge: { label: words.kind, color: COLOR, icon: ICON },
rows,
...(facts.name && { defines: range(facts.name) }),
absorbs: withLeadingComments(children, [...fields.map(field => field.block), ...markers]),
@@ -47,7 +47,7 @@ describe('sqlAlchemy 库规则', () => {
it('referralRelation:和原型一样的数据表卡片', async () => {
const table = await model(SNAPSHOT, 'ReferralRelation')
expect(table.kind).toBe('sqlalchemy.table')
expect(table.badge).toEqual({ label: '数据表', color: { dark: '#e8a860', light: '#a3590d' } })
expect(table.badge).toEqual({ label: '数据表', color: { dark: '#e8a860', light: '#a3590d' }, icon: 'database' })
expect(propertyLines(properties(table))).toEqual([
'名称: ReferralRelation',
'表名: referral_relations',
+2 -1
View File
@@ -20,6 +20,7 @@ const SCHEMA_ITEMS = new Set(['ForeignKey', 'Sequence', 'Identity', 'Computed',
const SIZE_KEYWORDS = ['length', 'precision', 'scale']
const TABLE_KIND = 'sqlalchemy.table'
const TABLE_COLOR: ThemeColor = { dark: '#e8a860', light: '#a3590d' }
const TABLE_ICON = 'database'
/** 一种显示语言的用词。 */
interface Words {
@@ -435,7 +436,7 @@ function tableBlock(block: ClassBlock, model: Model, env: Env): RuleCard {
const members = [model.table, model.args, ...model.columns.map(column => column.member), ...model.relationships.map(relationship => relationship.member)]
return {
kind: TABLE_KIND,
badge: { label: words.kind, color: TABLE_COLOR },
badge: { label: words.kind, color: TABLE_COLOR, icon: TABLE_ICON },
rows,
...(facts.name && { defines: range(facts.name) }),
absorbs: withLeadingComments(block.children, members.filter((member): member is Member => member !== undefined).map(member => member.block)),