⚡ perf(vscode): 代码阅读预览发给页面的块只记相对位置:块在源码里占的地方记成离前一块多远、自己多长,块里名字的下标从块的开头算,页面从文件开头累加回文件里的位置、和扩展端打交道的地方都在一处换算;靠前改了代码,后面内容没变的块发过去一模一样,扩展端自己的块和发给页面的块分成两种类型

This commit is contained in:
cruldra committed 2026-10-13 16:32:18 +11:00
1 parent 3a2815c19c
commit ad5f559e92
43 files changed
+980 -424

No files matched your search

+12 -9
View File
@@ -1,4 +1,5 @@
import type { BlockView, ChangedBlock, DiffStats, DocumentDiff, RemovedView } from './types'
import type { Placement } from './placement'
import type { BlockView, CardView, ChangedBlock, DiffStats, DocumentDiff, RemovedView } from './types'
import { inlineText } from './segments'
import { unfoldToShow } from './viewState'
@@ -63,7 +64,7 @@ export function statsParts(stats: DiffStats): { change: keyof DiffStats, text: s
}
/** 删掉的块收起时写的名字:属性表是第一行的值(函数名、变量名、导入的来源),一行译文是译文;只取第一行。 */
export function removedTitle(block: BlockView): string {
export function removedTitle(block: CardView): string {
const text = 'properties' in block
? block.properties[0] && ('markdown' in block.properties[0].value ? block.properties[0].value.markdown : inlineText(block.properties[0].value))
: 'markdown' in block.translation ? block.translation.markdown : inlineText(block.translation)
@@ -140,8 +141,8 @@ export interface ChangeStop {
ancestors: string[]
}
/** 页面上的各处改动(见 ChangeStop);hidden 里的块(被筛掉的)连同子孙不算。 */
export function changeStops(blocks: readonly BlockView[], tree: DiffTree, hidden: ReadonlySet<string>): ChangeStop[] {
/** 页面上的各处改动(见 ChangeStop,行号是文件里的,按 placement 换算);hidden 里的块(被筛掉的)连同子孙不算。 */
export function changeStops(blocks: readonly BlockView[], placement: Placement, tree: DiffTree, hidden: ReadonlySet<string>): ChangeStop[] {
const stops: ChangeStop[] = []
/** 一层块:owner 是父块 id(顶层 ''),ancestorsOf 给出要让某个子块看得见得展开的,removedAncestors 是这一层删掉的块要展开的 */
const visit = (list: readonly BlockView[], owner: string, ancestorsOf: (block: BlockView) => string[], removedAncestors: string[]): void => {
@@ -154,11 +155,13 @@ export function changeStops(blocks: readonly BlockView[], tree: DiffTree, hidden
for (const block of list) {
const ancestors = ancestorsOf(block)
const change = hidden.has(block.id) ? undefined : tree.changes.get(block.id)
if (change?.change === 'add' && change.outermost)
stops.push({ id: block.id, change: 'add', from: block.startLine, to: block.endLine, ancestors })
if (change?.change === 'mod' && change.own) {
const own = block.children[0] ? Math.max(block.startLine, block.children[0].startLine - 1) : block.endLine
stops.push({ id: block.id, change: 'mod', from: block.startLine, to: own, ancestors })
const place = placement.of(block.id)
if (place && change?.change === 'add' && change.outermost)
stops.push({ id: block.id, change: 'add', from: place.line, to: place.endLine, ancestors })
if (place && change?.change === 'mod' && change.own) {
const first = block.children[0] && placement.of(block.children[0].id)
const own = first ? Math.max(place.line, first.line - 1) : place.endLine
stops.push({ id: block.id, change: 'mod', from: place.line, to: own, ancestors })
}
if (change?.change === 'mod')
visit(block.children, block.id, child => [...ancestors, ...unfoldToShow(block.id, child)], [...ancestors, block.id])