implements PLN-0074
This commit is contained in:
@@ -45,6 +45,7 @@ let reconnectContext;
|
||||
let stoppingClient = false;
|
||||
let lastAppliedThemeSignature;
|
||||
const RECONNECT_DELAY_MS = 2000;
|
||||
const VSCODE_HOST_ID = "vscode";
|
||||
function activate(context) {
|
||||
reconnectContext = context;
|
||||
output = vscode.window.createOutputChannel("Prometeu LSP");
|
||||
@@ -207,21 +208,30 @@ function clearReconnectTimer() {
|
||||
reconnectTimer = undefined;
|
||||
}
|
||||
async function applyPrometeuVisualTheme(initializeResult) {
|
||||
const payload = extractPrometeuVisualThemesPayload(initializeResult);
|
||||
if (!payload) {
|
||||
const visualThemesPayload = extractPrometeuVisualThemesPayload(initializeResult);
|
||||
if (!visualThemesPayload) {
|
||||
output.appendLine("Prometeu visual theme payload not present in initialize result.");
|
||||
return;
|
||||
}
|
||||
const activeTheme = payload.visualThemes.find((theme) => theme.themeId === payload.activeVisualThemeId);
|
||||
const semanticProjectionPayload = extractPrometeuSemanticHostProjectionsPayload(initializeResult);
|
||||
const vscodeProjection = selectHostProjection(semanticProjectionPayload, VSCODE_HOST_ID);
|
||||
if (!semanticProjectionPayload) {
|
||||
output.appendLine("Prometeu semantic host projection payload not present in initialize result.");
|
||||
}
|
||||
else if (!vscodeProjection) {
|
||||
output.appendLine("Prometeu semantic host projection payload does not declare a vscode projection.");
|
||||
}
|
||||
const activeTheme = visualThemesPayload.visualThemes.find((theme) => theme.themeId === visualThemesPayload.activeVisualThemeId);
|
||||
if (!activeTheme) {
|
||||
output.appendLine(`Prometeu visual theme '${payload.activeVisualThemeId}' was not declared by the server.`);
|
||||
output.appendLine(`Prometeu visual theme '${visualThemesPayload.activeVisualThemeId}' was not declared by the server.`);
|
||||
return;
|
||||
}
|
||||
const themeSignature = JSON.stringify({
|
||||
languageId: payload.frontendLanguageId,
|
||||
languageId: visualThemesPayload.frontendLanguageId,
|
||||
themeId: activeTheme.themeId,
|
||||
editorPalette: activeTheme.editorPalette,
|
||||
tokenStyles: activeTheme.tokenStyles
|
||||
tokenStyles: activeTheme.tokenStyles,
|
||||
semanticProjection: vscodeProjection ?? null
|
||||
});
|
||||
if (themeSignature === lastAppliedThemeSignature) {
|
||||
output.appendLine(`Prometeu visual theme '${activeTheme.themeId}' is already applied.`);
|
||||
@@ -234,7 +244,7 @@ async function applyPrometeuVisualTheme(initializeResult) {
|
||||
const existingSemanticCustomizations = configuration.get("editor.semanticTokenColorCustomizations") ?? {};
|
||||
const semanticRules = {
|
||||
...(readObject(existingSemanticCustomizations.rules)),
|
||||
...buildSemanticTokenRules(payload.frontendLanguageId, activeTheme)
|
||||
...buildSemanticTokenRules(visualThemesPayload.frontendLanguageId, activeTheme, vscodeProjection)
|
||||
};
|
||||
await configuration.update("editor.semanticTokenColorCustomizations", {
|
||||
...existingSemanticCustomizations,
|
||||
@@ -249,7 +259,7 @@ async function applyPrometeuVisualTheme(initializeResult) {
|
||||
"editorLineNumber.foreground": activeTheme.editorPalette.lineNumberForeground
|
||||
}, configurationTarget);
|
||||
lastAppliedThemeSignature = themeSignature;
|
||||
output.appendLine(`Applied Prometeu visual theme '${activeTheme.themeId}' for language '${payload.frontendLanguageId}'.`);
|
||||
output.appendLine(`Applied Prometeu visual theme '${activeTheme.themeId}' for language '${visualThemesPayload.frontendLanguageId}'.`);
|
||||
}
|
||||
function extractPrometeuVisualThemesPayload(initializeResult) {
|
||||
if (!initializeResult || typeof initializeResult !== "object") {
|
||||
@@ -266,27 +276,63 @@ function extractPrometeuVisualThemesPayload(initializeResult) {
|
||||
}
|
||||
return payload;
|
||||
}
|
||||
function buildSemanticTokenRules(languageId, theme) {
|
||||
function extractPrometeuSemanticHostProjectionsPayload(initializeResult) {
|
||||
if (!initializeResult || typeof initializeResult !== "object") {
|
||||
return undefined;
|
||||
}
|
||||
const capabilities = readObject(initializeResult.capabilities);
|
||||
const experimental = readObject(capabilities.experimental);
|
||||
const payload = readObject(experimental.prometeuSemanticHostProjections);
|
||||
if (typeof payload.frontendLanguageId !== "string" || !Array.isArray(payload.semanticKeys) || !Array.isArray(payload.hostProjections)) {
|
||||
return undefined;
|
||||
}
|
||||
return payload;
|
||||
}
|
||||
function selectHostProjection(payload, hostId) {
|
||||
if (!payload) {
|
||||
return undefined;
|
||||
}
|
||||
return payload.hostProjections.find((projection) => projection.hostId === hostId);
|
||||
}
|
||||
function buildSemanticTokenRules(languageId, theme, hostProjection) {
|
||||
const rules = {};
|
||||
const projectionBySemanticKey = new Map((hostProjection?.tokenProjections ?? []).map((projection) => [projection.semanticKey, projection]));
|
||||
for (const tokenStyle of theme.tokenStyles) {
|
||||
const selector = `${tokenStyle.semanticKey}:${languageId}`;
|
||||
if (!tokenStyle.italic && !tokenStyle.bold && !tokenStyle.underline) {
|
||||
rules[selector] = tokenStyle.foreground;
|
||||
const styleValue = !tokenStyle.italic && !tokenStyle.bold && !tokenStyle.underline
|
||||
? tokenStyle.foreground
|
||||
: {
|
||||
foreground: tokenStyle.foreground,
|
||||
...(tokenStyle.italic ? { italic: true } : {}),
|
||||
...(tokenStyle.bold ? { bold: true } : {}),
|
||||
...(tokenStyle.underline ? { underline: true } : {})
|
||||
};
|
||||
rules[`${tokenStyle.semanticKey}:${languageId}`] = styleValue;
|
||||
const projection = projectionBySemanticKey.get(tokenStyle.semanticKey);
|
||||
if (!projection) {
|
||||
continue;
|
||||
}
|
||||
rules[selector] = {
|
||||
foreground: tokenStyle.foreground,
|
||||
...(tokenStyle.italic ? { italic: true } : {}),
|
||||
...(tokenStyle.bold ? { bold: true } : {}),
|
||||
...(tokenStyle.underline ? { underline: true } : {})
|
||||
};
|
||||
const projectedSelector = buildSemanticSelector(languageId, projection.hostTokenType, projection.hostTokenModifiers);
|
||||
if (!(projectedSelector in rules)) {
|
||||
rules[projectedSelector] = styleValue;
|
||||
}
|
||||
const fallbackSelector = buildSemanticSelector(languageId, projection.fallbackTokenType, projection.fallbackTokenModifiers);
|
||||
if (!(fallbackSelector in rules)) {
|
||||
rules[fallbackSelector] = styleValue;
|
||||
}
|
||||
}
|
||||
return rules;
|
||||
}
|
||||
function buildSemanticSelector(languageId, tokenType, modifiers) {
|
||||
const normalizedModifiers = [...modifiers].sort();
|
||||
if (normalizedModifiers.length === 0) {
|
||||
return `${tokenType}:${languageId}`;
|
||||
}
|
||||
return `${tokenType}.${normalizedModifiers.join(".")}:${languageId}`;
|
||||
}
|
||||
function readObject(value) {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
||||
return {};
|
||||
}
|
||||
return value;
|
||||
}
|
||||
//# sourceMappingURL=extension.js.map
|
||||
//# sourceMappingURL=extension.js.map
|
||||
|
||||
Reference in New Issue
Block a user