From 97cfc8ea9b9b11075fd3bae11b1f910c87302da8 Mon Sep 17 00:00:00 2001 From: "Michael C. McMinn" Date: Fri, 13 Dec 2024 14:50:32 -0500 Subject: [PATCH] Fix comment parsing to handle end of line inline comments --- extensions/vscode/src/util/getTheme.ts | 27 ++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/extensions/vscode/src/util/getTheme.ts b/extensions/vscode/src/util/getTheme.ts index a4a570a19..113a5047f 100644 --- a/extensions/vscode/src/util/getTheme.ts +++ b/extensions/vscode/src/util/getTheme.ts @@ -11,12 +11,39 @@ import * as vscode from "vscode"; /** * Strip comments from theme */ + +function stripInLineComment(line: string): string { + let inString = false; + let pCh = ''; + + for (let i = 0; i < line.length-1; i++) { + const ch = line[i]; + const nCh = line[i + 1]; + + // If we're not in a string and we see '//' this is a comment. + if (!inString && ch === '/' && nCh === '/') { + // Stop processing this line from here. + return line.substring(0, i); + } + + // Toggle inString state if we see a double quote not escaped by a backslash. + if (ch === '"' && pCh !== '\\') { + inString = !inString; + } + + pCh = ch; + } + + return line; +} + function parseThemeString(themeString: string | undefined): any { themeString = themeString ?.split("\n") .filter((line) => { return !line.trim().startsWith("//"); }) + .map(stripInLineComment) .join("\n"); return JSON.parse(themeString ?? "{}"); }