Skip to content

Commit 22e3dc8

Browse files
committed
feat: add onHighlightComplete callback to Code and Diff renderables
1 parent 37d4071 commit 22e3dc8

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

packages/core/src/renderables/Code.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export interface CodeOptions extends TextBufferOptions {
1515
conceal?: boolean
1616
drawUnstyledText?: boolean
1717
streaming?: boolean
18+
onHighlightComplete?: () => void
1819
}
1920

2021
export class CodeRenderable extends TextBufferRenderable {
@@ -31,6 +32,7 @@ export class CodeRenderable extends TextBufferRenderable {
3132
private _streaming: boolean
3233
private _hadInitialContent: boolean = false
3334
private _lastHighlights: SimpleHighlight[] = []
35+
private _onHighlightComplete?: () => void
3436

3537
protected _contentDefaultOptions = {
3638
content: "",
@@ -49,6 +51,7 @@ export class CodeRenderable extends TextBufferRenderable {
4951
this._conceal = options.conceal ?? this._contentDefaultOptions.conceal
5052
this._drawUnstyledText = options.drawUnstyledText ?? this._contentDefaultOptions.drawUnstyledText
5153
this._streaming = options.streaming ?? this._contentDefaultOptions.streaming
54+
this._onHighlightComplete = options.onHighlightComplete
5255

5356
// Set initial content immediately so lineCount is correct for measure functions
5457
// This prevents width glitches in parent components like LineNumberRenderable
@@ -148,6 +151,14 @@ export class CodeRenderable extends TextBufferRenderable {
148151
}
149152
}
150153

154+
get onHighlightComplete(): (() => void) | undefined {
155+
return this._onHighlightComplete
156+
}
157+
158+
set onHighlightComplete(value: (() => void) | undefined) {
159+
this._onHighlightComplete = value
160+
}
161+
151162
private ensureVisibleTextBeforeHighlight(): void {
152163
const content = this._content
153164

@@ -247,6 +258,7 @@ export class CodeRenderable extends TextBufferRenderable {
247258
this._isHighlighting = false
248259
this._highlightsDirty = false
249260
this.requestRender()
261+
this._onHighlightComplete?.()
250262
} catch (error) {
251263
// Check if this result is stale
252264
if (snapshotId !== this._highlightSnapshotId) {
@@ -261,6 +273,7 @@ export class CodeRenderable extends TextBufferRenderable {
261273
this._isHighlighting = false
262274
this._highlightsDirty = false
263275
this.requestRender()
276+
this._onHighlightComplete?.()
264277
}
265278
}
266279

packages/core/src/renderables/Diff.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export interface DiffRenderableOptions extends RenderableOptions<DiffRenderable>
4747
removedSignColor?: string | RGBA
4848
addedLineNumberBg?: string | RGBA
4949
removedLineNumberBg?: string | RGBA
50+
onHighlightComplete?: () => void
5051
}
5152

5253
export class DiffRenderable extends Renderable {
@@ -106,6 +107,9 @@ export class DiffRenderable extends Renderable {
106107
private errorTextRenderable: TextRenderable | null = null
107108
private errorCodeRenderable: CodeRenderable | null = null
108109

110+
private _onHighlightComplete?: () => void
111+
private _pendingHighlights = 0
112+
109113
constructor(ctx: RenderContext, options: DiffRenderableOptions) {
110114
super(ctx, {
111115
...options,
@@ -141,6 +145,7 @@ export class DiffRenderable extends Renderable {
141145
this._removedSignColor = parseColor(options.removedSignColor ?? "#ef4444")
142146
this._addedLineNumberBg = parseColor(options.addedLineNumberBg ?? "transparent")
143147
this._removedLineNumberBg = parseColor(options.removedLineNumberBg ?? "transparent")
148+
this._onHighlightComplete = options.onHighlightComplete
144149

145150
// Only parse and build if diff is provided
146151
if (this._diff) {
@@ -240,6 +245,13 @@ export class DiffRenderable extends Renderable {
240245
super.destroyRecursively()
241246
}
242247

248+
private handleHighlightComplete(): void {
249+
this._pendingHighlights--
250+
if (this._pendingHighlights === 0 && this._onHighlightComplete) {
251+
this._onHighlightComplete()
252+
}
253+
}
254+
243255
/**
244256
* Create or update a CodeRenderable with the given content and options.
245257
* Reuses existing instances to avoid expensive recreation.
@@ -315,6 +327,10 @@ export class DiffRenderable extends Renderable {
315327
): CodeRenderable {
316328
const existingRenderable = side === "left" ? this.leftCodeRenderable : this.rightCodeRenderable
317329

330+
if (this._filetype) {
331+
this._pendingHighlights++
332+
}
333+
318334
if (!existingRenderable) {
319335
// Create new CodeRenderable
320336
const codeOptions: CodeOptions = {
@@ -326,6 +342,7 @@ export class DiffRenderable extends Renderable {
326342
syntaxStyle: this._syntaxStyle ?? SyntaxStyle.create(),
327343
width: "100%",
328344
height: "100%",
345+
onHighlightComplete: () => this.handleHighlightComplete(),
329346
...(this._fg !== undefined && { fg: this._fg }),
330347
...(drawUnstyledText !== undefined && { drawUnstyledText }),
331348
...(this._selectionBg !== undefined && { selectionBg: this._selectionBg }),
@@ -346,6 +363,7 @@ export class DiffRenderable extends Renderable {
346363
existingRenderable.content = content
347364
existingRenderable.wrapMode = wrapMode ?? "none"
348365
existingRenderable.conceal = this._conceal
366+
existingRenderable.onHighlightComplete = () => this.handleHighlightComplete()
349367
if (drawUnstyledText !== undefined) {
350368
existingRenderable.drawUnstyledText = drawUnstyledText
351369
}

0 commit comments

Comments
 (0)