Skip to content
This repository was archived by the owner on Aug 1, 2025. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions vscode/src/autoedits/renderer/image-gen/canvas/draw-decorations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,7 @@ export function drawDecorationsToCanvas(

// In order for us to draw to the canvas, we must first determine the correct
// dimensions for the canvas. We can do this with a temporary Canvas that uses the same font
const { ctx: tempCtx } = createCanvas({ height: 10, width: 10, fontSize: config.fontSize }, context)

// Iterate through each token line, and determine the required width of the canvas (maximum line length)
// and the required height of the canvas (number of lines determined by their line height)
let tempYPos = config.padding.y
let requiredWidth = 0
for (const line of diff.lines) {
const measure = tempCtx.measureText(line.text)
requiredWidth = Math.max(requiredWidth, config.padding.x + measure.width)
tempYPos += config.lineHeight
}
const { requiredWidth, tempYPos } = calculateRequiredDimensions(config, context, diff)

// Note: We limit the canvas width to avoid the image getting excessively large.
// We should consider possible strategies here, such as tweaking this value or refusing
Expand Down Expand Up @@ -176,3 +166,18 @@ export function drawDecorationsToCanvas(

return canvas
}
function calculateRequiredDimensions(config: RenderConfig, context: RenderContext, diff: VisualDiff) {
const { canvas, ctx } = createCanvas({ height: 10, width: 10, fontSize: config.fontSize }, context)

// Iterate through each token line, and determine the required width of the canvas (maximum line length)
// and the required height of the canvas (number of lines determined by their line height)
let tempYPos = config.padding.y
let requiredWidth = 0
for (const line of diff.lines) {
const measure = ctx.measureText(line.text)
requiredWidth = Math.max(requiredWidth, config.padding.x + measure.width)
tempYPos += config.lineHeight
}
canvas.dispose()
return { requiredWidth, tempYPos }
}
26 changes: 19 additions & 7 deletions vscode/src/autoedits/renderer/image-gen/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { initCanvas } from './canvas'
import { drawDecorationsToCanvas } from './canvas/draw-decorations'
import { type UserProvidedRenderConfig, getRenderConfig } from './canvas/render-config'
import {
type RenderConfig,
type UserProvidedRenderConfig,
getRenderConfig,
} from './canvas/render-config'
import { makeDecoratedDiff } from './decorated-diff'
import { initSyntaxHighlighter } from './highlight'
import type { DiffMode, VisualDiff } from './visual-diff/types'
Expand Down Expand Up @@ -38,12 +42,20 @@ export function generateSuggestionAsImage(options: ImageSuggestionOptions): Gene
const renderConfig = getRenderConfig(config)

return {
dark: drawDecorationsToCanvas(highlightedDiff.dark, 'dark', mode, renderConfig).toDataURL(
'image/png'
),
light: drawDecorationsToCanvas(highlightedDiff.light, 'light', mode, renderConfig).toDataURL(
'image/png'
),
dark: renderSuggestionImage(highlightedDiff.dark, 'dark', mode, renderConfig),
light: renderSuggestionImage(highlightedDiff.light, 'light', mode, renderConfig),
pixelRatio: renderConfig.pixelRatio,
}
}

function renderSuggestionImage(
diff: VisualDiff,
theme: 'dark' | 'light',
mode: DiffMode,
renderConfig: RenderConfig
) {
const canvas = drawDecorationsToCanvas(diff, theme, mode, renderConfig)
const image = canvas.toDataURL('image/png')
canvas.dispose()
return image
}
Loading