✨ feat(vscode): 代码阅读函数、导入、常量、变量块预览改成属性表

This commit is contained in:
cruldra committed 2026-09-25 04:56:22 +08:00
1 parent 0c4c9b0fba
commit dd9b39ac5d
19 files changed
+1458 -213

No files matched your search

+16 -7
View File
@@ -1,4 +1,4 @@
import type { BlockKind, CodeBlock, Node, SourceSpan } from '../contract'
import type { BlockShape, CodeBlock, Node, SourceSpan, TextKind } from '../contract'
/** 语言插件共用的建树工具:取子节点、算头部、把一串语句切成块。 */
@@ -10,6 +10,11 @@ export function children(node: Node): Node[] {
return node.children.filter((child): child is Node => child !== null)
}
/** 具名子节点,去掉注释这类语法之外的节点。 */
export function syntaxChildren(node: Node | null | undefined): Node[] {
return named(node).filter(child => !child.isExtra)
}
export function nodeSpan(node: Node): SourceSpan {
return {
startIndex: node.startIndex,
@@ -19,12 +24,16 @@ export function nodeSpan(node: Node): SourceSpan {
}
}
export function leafBlock(kind: BlockKind, node: Node): CodeBlock {
return { kind, node, header: nodeSpan(node), children: [] }
export function leafShape(node: Node): BlockShape {
return { node, header: nodeSpan(node), children: [] }
}
export function leafBlock(kind: TextKind, node: Node): CodeBlock {
return { kind, ...leafShape(node) }
}
/** 头部往前扩到 from(装饰器)。 */
export function extendHeader(block: CodeBlock, from: Node): CodeBlock {
export function extendHeader<B extends CodeBlock>(block: B, from: Node): B {
return { ...block, header: { ...block.header, startIndex: from.startIndex, startRow: from.startPosition.row } }
}
@@ -67,13 +76,13 @@ function commentsIn(node: Node, from: number, to: number): Node[] {
export type ListBlocks = (items: Node[], afterRow: number) => CodeBlock[]
/**
* 复合语句块:头部到 bodyStart 之前,子块来自 items。
* 复合语句块(不含种类):头部到 bodyStart 之前,子块来自 items。
* 头部和主体之间的注释(例如 Python 冒号后、主体前的注释)也归子块。
*/
export function compoundBlock(kind: BlockKind, outer: Node, bodyStart: number, items: Node[], list: ListBlocks): CodeBlock {
export function compoundShape(outer: Node, bodyStart: number, items: Node[], list: ListBlocks): BlockShape {
const header = headerSpan(outer, bodyStart)
const between = commentsIn(outer, header.endIndex, bodyStart)
return { kind, node: outer, header, children: list([...between, ...items], header.endRow) }
return { node: outer, header, children: list([...between, ...items], header.endRow) }
}
/**