diff --git a/core/syntaxHighlighting/SyntaxHighlighter.ts b/core/syntaxHighlighting/SyntaxHighlighter.ts index b05092913..b63d7cd2b 100644 --- a/core/syntaxHighlighting/SyntaxHighlighter.ts +++ b/core/syntaxHighlighting/SyntaxHighlighter.ts @@ -1,9 +1,8 @@ import * as fs from "fs/promises"; import { JSDOM } from "jsdom"; -import path from "path"; import satori from "satori"; // import puppeteer, { Browser, ScreenshotOptions } from "puppeteer"; -import { BundledTheme, codeToHtml } from "shiki"; +import { BundledTheme, codeToHtml, getSingletonHighlighter } from "shiki"; import { escapeForSVG, kebabOfStr } from "../util/text"; interface SyntaxHighlighterOptions { @@ -43,13 +42,12 @@ type PngUri = string; type SvgUri = string; export class SyntaxHighlighter { - private themesDir: string = path.join(__dirname, "themes"); // We may need this in case we start supporting custom colors. - private currentTheme: string; private static instance: SyntaxHighlighter; + private currentTheme: string = "dark-plus"; + private editorBackground: string = "#000000"; + private editorForeground: string = "#FFFFFF"; - private constructor(options: SyntaxHighlighterOptions = {}) { - this.currentTheme = options.theme || "dark-plus"; - } + private constructor(options: SyntaxHighlighterOptions = {}) {} static getInstance(options?: SyntaxHighlighterOptions): SyntaxHighlighter { if (!SyntaxHighlighter.instance) { @@ -58,6 +56,28 @@ export class SyntaxHighlighter { return SyntaxHighlighter.instance; } + public async setTheme(themeName: string): Promise { + if ( + this.themeExists(kebabOfStr(themeName)) || + themeName === "Default Dark Modern" + ) { + this.currentTheme = + themeName === "Default Dark Modern" + ? "dark-plus" + : kebabOfStr(themeName); + + const highlighter = await getSingletonHighlighter({ + themes: [this.currentTheme], + }); + + const th = highlighter.getTheme(this.currentTheme); + this.editorBackground = th.bg; + this.editorForeground = th.fg; + } else { + this.currentTheme = "dark-plus"; + } + } + async init(): Promise {} async close(): Promise {} @@ -129,14 +149,6 @@ export class SyntaxHighlighter { return themeArray.includes(themeNameKebab as BundledTheme); } - async setTheme(themeName: string): Promise { - if (this.themeExists(kebabOfStr(themeName))) { - this.currentTheme = themeName; - } else { - this.currentTheme = "dark-plus"; - } - } - async highlightCode( code: string, language: string = "javascript", @@ -204,6 +216,7 @@ export class SyntaxHighlighter { code: string, language: string = "javascript", fontSize: number, + fontFamily: string, dimensions: Dimensions, lineHeight: number, options: ConversionOptions, @@ -213,13 +226,14 @@ export class SyntaxHighlighter { const guts = this.convertShikiHtmlToSvgGut( highlightedCodeHtml, fontSize, + fontFamily, lineHeight, ); const backgroundColor = this.getBackgroundColor(highlightedCodeHtml); const svg = ` - + ${guts} `; @@ -275,6 +289,7 @@ export class SyntaxHighlighter { convertShikiHtmlToSvgGut( shikiHtml: string, fontSize: number, + fontFamily: string, lineHeight: number, ): string { const dom = new JSDOM(shikiHtml); @@ -298,7 +313,7 @@ export class SyntaxHighlighter { .join(""); const y = (index + 1) * lineHeight; - return `${spans}`; + return `${spans}`; }); return ` @@ -327,6 +342,7 @@ export class SyntaxHighlighter { code: string, language: string = "javascript", fontSize: number, + fontFamily: string, dimensions: Dimensions, lineHeight: number, options: ConversionOptions, @@ -347,6 +363,7 @@ export class SyntaxHighlighter { code, language, fontSize, + fontFamily, dimensions, lineHeight, options, @@ -375,16 +392,4 @@ export class SyntaxHighlighter { return `data:image/svg+xml;base64,${svgBuffer.toString("base64")}`; } } - - // Utility method to list available themes - async getAvailableThemes(): Promise { - try { - const files = await fs.readdir(this.themesDir); - return files - .filter((file) => file.endsWith(".css")) - .map((file) => file.replace(".css", "")); - } catch (error) { - return []; - } - } } diff --git a/extensions/vscode/src/activation/NextEditWindowManager.ts b/extensions/vscode/src/activation/NextEditWindowManager.ts index d0a254884..90db6328c 100644 --- a/extensions/vscode/src/activation/NextEditWindowManager.ts +++ b/extensions/vscode/src/activation/NextEditWindowManager.ts @@ -6,7 +6,7 @@ import * as vscode from "vscode"; import { myersDiff } from "core/diff/myers"; import { getRenderableDiff } from "core/nextEdit/diff/diff"; import { SyntaxHighlighter } from "core/syntaxHighlighting/SyntaxHighlighter"; -import { getTheme } from "../util/getTheme"; +import { getThemeString } from "../util/getTheme"; export interface TextApplier { applyText( @@ -16,16 +16,6 @@ export interface TextApplier { ): Promise; } -// Fallback theme colors -const FALLBACK_THEME = { - colors: { - "editor.foreground": "#FFFFFF", - "editor.background": "#333333", - // "editor.foreground": "var(--vscode-editor-foreground)", - // "editor.background": "var(--vscode-editor-background)", - }, -}; - const SVG_CONFIG = { stroke: "#999998", strokeWidth: 1, @@ -86,7 +76,9 @@ export class NextEditWindowManager { private readonly excludedURIPrefixes = ["output:", "vscode://inline-chat"]; private readonly hideCommand = "continue.hideNextEditWindow"; - private theme = getTheme() || FALLBACK_THEME; + private theme: string; + private fontSize: number; + private fontFamily: string; private syntaxHighlighter: SyntaxHighlighter; // Current active decoration @@ -122,6 +114,8 @@ export class NextEditWindowManager { } private constructor() { + this.theme = getThemeString(); + console.log( "Next Edit Theme initialized:", this.theme @@ -129,32 +123,47 @@ export class NextEditWindowManager { : "Theme is undefined", ); this.setupListeners(); - this.syntaxHighlighter = SyntaxHighlighter.getInstance({ - theme: "one-dark-pro", - }); + this.syntaxHighlighter = SyntaxHighlighter.getInstance(); + this.syntaxHighlighter.setTheme(this.theme); + + const editorConfig = vscode.workspace.getConfiguration("editor"); + this.fontSize = editorConfig.get("fontSize") ?? 14; + this.fontFamily = editorConfig.get("fontFamily") ?? "monospace"; } - public setupNextEditWindowManager( + public async setupNextEditWindowManager( context: vscode.ExtensionContext, textApplier?: TextApplier, ) { - // Register the command to hide tooltips - const hideTooltipsCommand = vscode.commands.registerCommand( - HIDE_TOOLTIP_COMMAND, - async () => { - await this.hideAllTooltips(); - }, + await vscode.commands.executeCommand( + "setContext", + "nextEditWindowActive", + false, ); - context.subscriptions.push(hideTooltipsCommand); - // Register the command to accept next edit - const acceptNextEditCommand = vscode.commands.registerCommand( - ACCEPT_NEXT_EDIT_COMMAND, - () => { - this.acceptNextEdit(); - }, - ); - context.subscriptions.push(acceptNextEditCommand); + const existingCommands = await vscode.commands.getCommands(); + + if (!existingCommands.includes(HIDE_TOOLTIP_COMMAND)) { + // Register the command to hide tooltips + const hideTooltipsCommand = vscode.commands.registerCommand( + HIDE_TOOLTIP_COMMAND, + async () => { + await this.hideAllTooltips(); + }, + ); + context.subscriptions.push(hideTooltipsCommand); + } + + if (!existingCommands.includes(ACCEPT_NEXT_EDIT_COMMAND)) { + // Register the command to accept next edit + const acceptNextEditCommand = vscode.commands.registerCommand( + ACCEPT_NEXT_EDIT_COMMAND, + () => { + this.acceptNextEdit(); + }, + ); + context.subscriptions.push(acceptNextEditCommand); + } // Add this class to context disposables context.subscriptions.push(this); @@ -315,46 +324,62 @@ export class NextEditWindowManager { private setupListeners() { // Theme change listener - const themeListener = vscode.workspace.onDidChangeConfiguration((e) => { + vscode.workspace.onDidChangeConfiguration((e) => { if ( e.affectsConfiguration("workbench.colorTheme") || e.affectsConfiguration("editor.fontSize") || - e.affectsConfiguration("editor.fontFamily") + e.affectsConfiguration("editor.fontFamily") || + e.affectsConfiguration("window.autoDetectColorScheme") || + e.affectsConfiguration("window.autoDetectHighContrast") || + e.affectsConfiguration("workbench.preferredDarkColorTheme") || + e.affectsConfiguration("workbench.preferredLightColorTheme") || + e.affectsConfiguration("workbench.preferredHighContrastColorTheme") || + e.affectsConfiguration("workbench.preferredHighContrastLightColorTheme") ) { - const newTheme = getTheme(); - this.theme = newTheme || FALLBACK_THEME; + this.theme = getThemeString(); + this.syntaxHighlighter.setTheme(this.theme); console.log( "Theme updated:", this.theme ? "Theme exists" : "Theme is undefined", ); + const editorConfig = vscode.workspace.getConfiguration("editor"); + this.fontSize = editorConfig.get("fontSize") ?? 14; + this.fontFamily = editorConfig.get("fontFamily") ?? "monospace"; } }); - this.disposables.push(themeListener); + // this.disposables.push(themeListener); + + // Listen for active color theme changes + vscode.window.onDidChangeActiveColorTheme(() => { + this.theme = getThemeString(); + this.syntaxHighlighter.setTheme(this.theme); + console.log( + "Active theme changed:", + this.theme ? "Theme exists" : "Theme is undefined", + ); + }); + // this.disposables.push(activeThemeListener); // Listen for editor changes to clean up decorations when editor closes - const editorCloseListener = vscode.window.onDidChangeVisibleTextEditors( - async () => { - // If our active editor is no longer visible, clear decorations - if ( - this.activeEditor && - !vscode.window.visibleTextEditors.includes(this.activeEditor) - ) { - await this.hideAllTooltips(); - } - }, - ); - this.disposables.push(editorCloseListener); + vscode.window.onDidChangeVisibleTextEditors(async () => { + // If our active editor is no longer visible, clear decorations + if ( + this.activeEditor && + !vscode.window.visibleTextEditors.includes(this.activeEditor) + ) { + await this.hideAllTooltips(); + } + }); + // this.disposables.push(editorCloseListener); // Listen for selection changes to hide tooltip when cursor moves - const selectionListener = vscode.window.onDidChangeTextEditorSelection( - async (e) => { - // If the selection changed in our active editor, hide the tooltip - if (this.activeEditor && e.textEditor === this.activeEditor) { - await this.hideAllTooltips(); - } - }, - ); - this.disposables.push(selectionListener); + vscode.window.onDidChangeTextEditorSelection(async (e) => { + // If the selection changed in our active editor, hide the tooltip + if (this.activeEditor && e.textEditor === this.activeEditor) { + await this.hideAllTooltips(); + } + }); + // this.disposables.push(selectionListener); } private shouldRenderTip(uri: vscode.Uri): boolean { @@ -381,11 +406,11 @@ export class NextEditWindowManager { | undefined > { console.log("createSvgTooltip"); - const baseTextConfig = { - "font-family": SVG_CONFIG.fontFamily, - "font-size": SVG_CONFIG.fontSize, - fill: this.theme.colors["editor.foreground"], - }; + // const baseTextConfig = { + // "font-family": SVG_CONFIG.fontFamily, + // "font-size": SVG_CONFIG.fontSize, + // fill: this.theme.colors["editor.foreground"], + // }; try { const tipWidth = SVG_CONFIG.getTipWidth(text); @@ -408,7 +433,8 @@ export class NextEditWindowManager { const uri = await this.syntaxHighlighter.getDataUri( text, "typescript", - SVG_CONFIG.fontSize, + this.fontSize, + this.fontFamily, dimensions, SVG_CONFIG.lineHeight, { @@ -443,12 +469,8 @@ export class NextEditWindowManager { const { uri, dimensions } = uriAndDimensions; - const blankUri = await this.syntaxHighlighter.getBlankDataUri(dimensions, { - imageType: "svg", - }); - // Use theme or fallback - const backgroundColour = this.theme.colors["editor.background"]; + // const backgroundColour = this.theme.colors["editor.background"]; // const tipWidth = SVG_CONFIG.getTipWidth(text); // const tipHeight = SVG_CONFIG.getTipHeight(text); const tipWidth = dimensions.width; @@ -469,7 +491,7 @@ export class NextEditWindowManager { // filter: ${SVG_CONFIG.filter}; // margin-left: ${SVG_CONFIG.cursorOffset * 8}px;`, border: `transparent; position: absolute; z-index: 1000; - box-shadow: inset 0 0 0 ${SVG_CONFIG.strokeWidth}px ${SVG_CONFIG.stroke}, inset 0 0 0 ${tipHeight}px ${backgroundColour}; + box-shadow: inset 0 0 0 ${SVG_CONFIG.strokeWidth}px ${SVG_CONFIG.stroke}, inset 0 0 0 ${tipHeight}px; filter: ${SVG_CONFIG.filter}; border-radius: ${SVG_CONFIG.radius}px; margin-left: ${SVG_CONFIG.cursorOffset * 8}px;`, @@ -629,11 +651,11 @@ export class NextEditWindowManager { } } -export default function setupNextEditWindowManager( +export default async function setupNextEditWindowManager( context: vscode.ExtensionContext, textApplier?: TextApplier, ) { - NextEditWindowManager.getInstance().setupNextEditWindowManager( + await NextEditWindowManager.getInstance().setupNextEditWindowManager( context, textApplier, );