11import type { ExtensionContext } from "vscode" ;
22import * as vscode from "vscode" ;
3- import { getUserLangs , getUserTheme } from "vscode-shiki-bridge" ;
3+ import { getTheme , getUserLangs , getUserTheme } from "vscode-shiki-bridge" ;
44import { createHighlighterCore } from "shiki/core" ;
55import { createOnigurumaEngine } from "shiki/engine/oniguruma" ;
66import { MarkdownWebviewProvider } from "./markdownWebviewProvider" ;
@@ -18,6 +18,9 @@ import { SUPPORTED_LANGUAGE_IDS } from "../supportedLanguageIds";
1818const NO_DIAGNOSTICS_MESSAGE =
1919 "Select code with an error to show the prettified diagnostic in this view." ;
2020
21+ const SIDEBAR_CACHE_SIZE_MAX = 100 ;
22+ const sidebarHtmlCache = new Map < string , string > ( ) ;
23+
2124type ViewMode = "cursor" | "locked" ;
2225
2326interface DiagnosticItem {
@@ -72,8 +75,20 @@ export function registerWebviewViewProvider(context: ExtensionContext) {
7275async function diagnosticToItem (
7376 formattedDiagnostic : FormattedDiagnostic
7477) : Promise < DiagnosticItem > {
78+ const cacheKey = formattedDiagnostic . lspDiagnostic . message ;
79+ let html = sidebarHtmlCache . get ( cacheKey ) ;
80+ if ( ! html ) {
81+ html = await prettifyDiagnosticForSidebar (
82+ formattedDiagnostic . lspDiagnostic
83+ ) ;
84+ if ( sidebarHtmlCache . size > SIDEBAR_CACHE_SIZE_MAX ) {
85+ const firstKey = sidebarHtmlCache . keys ( ) . next ( ) . value ! ;
86+ sidebarHtmlCache . delete ( firstKey ) ;
87+ }
88+ sidebarHtmlCache . set ( cacheKey , html ) ;
89+ }
7590 return {
76- html : await prettifyDiagnosticForSidebar ( formattedDiagnostic . lspDiagnostic ) ,
91+ html,
7792 range : formattedDiagnostic . range ,
7893 } ;
7994}
@@ -94,7 +109,22 @@ class MarkdownWebviewViewProvider implements vscode.WebviewViewProvider {
94109
95110 private async ensureInitialized ( ) {
96111 if ( ! this . initialized ) {
97- const [ theme , themes ] = await getUserTheme ( ) ;
112+ let theme : string ;
113+ let themes : Parameters < typeof createHighlighterCore > [ 0 ] [ "themes" ] ;
114+ try {
115+ [ theme , themes ] = await getUserTheme ( ) ;
116+ } catch {
117+ // User's theme not found in extension registry (e.g. custom themes).
118+ // Fall back to a built-in VS Code theme matching the user's color theme kind.
119+ const isDark =
120+ vscode . window . activeColorTheme . kind === vscode . ColorThemeKind . Dark ||
121+ vscode . window . activeColorTheme . kind ===
122+ vscode . ColorThemeKind . HighContrast ;
123+
124+ [ theme , themes ] = await getTheme (
125+ isDark ? "Default Dark Modern" : "Default Light Modern"
126+ ) ;
127+ }
98128
99129 const langs = await getUserLangs ( [ "type" , "ts" ] ) ;
100130 const highlighter = await createHighlighterCore ( {
@@ -225,7 +255,6 @@ class MarkdownWebviewViewProvider implements vscode.WebviewViewProvider {
225255 } ) ,
226256 webviewView . onDidChangeVisibility ( ( ) => {
227257 if ( webviewView . visible ) {
228- this . lastContent = null ;
229258 this . refresh ( webviewView . webview ) ;
230259 }
231260 } )
0 commit comments