Fix comment parsing to handle end of line inline comments

This commit is contained in:
Michael C. McMinn 2024-12-13 14:50:32 -05:00
parent 4c8f2cd228
commit 97cfc8ea9b

View file

@ -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 ?? "{}");
}