Skip to content

Commit f978a09

Browse files
authored
Create sidebar cache + handle theme not found cases (#190)
1 parent af522db commit f978a09

3 files changed

Lines changed: 35 additions & 57 deletions

File tree

apps/vscode-extension/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"displayName": "Pretty TypeScript Errors",
44
"publisher": "YoavBls",
55
"description": "Make TypeScript errors prettier and more human-readable in VSCode",
6-
"version": "0.8.4",
6+
"version": "0.8.5",
77
"icon": "assets/icon.png",
88
"repository": {
99
"type": "git",

apps/vscode-extension/src/provider/webviewViewProvider.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { ExtensionContext } from "vscode";
22
import * as vscode from "vscode";
3-
import { getUserLangs, getUserTheme } from "vscode-shiki-bridge";
3+
import { getTheme, getUserLangs, getUserTheme } from "vscode-shiki-bridge";
44
import { createHighlighterCore } from "shiki/core";
55
import { createOnigurumaEngine } from "shiki/engine/oniguruma";
66
import { MarkdownWebviewProvider } from "./markdownWebviewProvider";
@@ -18,6 +18,9 @@ import { SUPPORTED_LANGUAGE_IDS } from "../supportedLanguageIds";
1818
const 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+
2124
type ViewMode = "cursor" | "locked";
2225

2326
interface DiagnosticItem {
@@ -72,8 +75,20 @@ export function registerWebviewViewProvider(context: ExtensionContext) {
7275
async 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
})

package-lock.json

Lines changed: 1 addition & 52 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)