implements PLN-0074
This commit is contained in:
@@ -51,6 +51,27 @@ type PrometeuVisualThemesPayload = {
|
||||
visualThemes: PrometeuVisualTheme[];
|
||||
};
|
||||
|
||||
type PrometeuSemanticHostProjectionEntry = {
|
||||
semanticKey: string;
|
||||
hostTokenType: string;
|
||||
hostTokenModifiers: string[];
|
||||
fallbackTokenType: string;
|
||||
fallbackTokenModifiers: string[];
|
||||
};
|
||||
|
||||
type PrometeuSemanticHostProjection = {
|
||||
hostId: string;
|
||||
tokenProjections: PrometeuSemanticHostProjectionEntry[];
|
||||
};
|
||||
|
||||
type PrometeuSemanticHostProjectionsPayload = {
|
||||
frontendLanguageId: string;
|
||||
semanticKeys: string[];
|
||||
hostProjections: PrometeuSemanticHostProjection[];
|
||||
};
|
||||
|
||||
const VSCODE_HOST_ID = "vscode";
|
||||
|
||||
export function activate(context: vscode.ExtensionContext): void {
|
||||
reconnectContext = context;
|
||||
output = vscode.window.createOutputChannel("Prometeu LSP");
|
||||
@@ -255,23 +276,31 @@ function clearReconnectTimer(): void {
|
||||
}
|
||||
|
||||
async function applyPrometeuVisualTheme(initializeResult: unknown): Promise<void> {
|
||||
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 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 = payload.visualThemes.find((theme) => theme.themeId === payload.activeVisualThemeId);
|
||||
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.`);
|
||||
@@ -286,7 +315,7 @@ async function applyPrometeuVisualTheme(initializeResult: unknown): Promise<void
|
||||
const existingSemanticCustomizations = configuration.get<Record<string, unknown>>("editor.semanticTokenColorCustomizations") ?? {};
|
||||
const semanticRules = {
|
||||
...(readObject(existingSemanticCustomizations.rules)),
|
||||
...buildSemanticTokenRules(payload.frontendLanguageId, activeTheme)
|
||||
...buildSemanticTokenRules(visualThemesPayload.frontendLanguageId, activeTheme, vscodeProjection)
|
||||
};
|
||||
|
||||
await configuration.update(
|
||||
@@ -312,7 +341,7 @@ async function applyPrometeuVisualTheme(initializeResult: unknown): Promise<void
|
||||
);
|
||||
|
||||
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: unknown): PrometeuVisualThemesPayload | undefined {
|
||||
@@ -334,27 +363,82 @@ function extractPrometeuVisualThemesPayload(initializeResult: unknown): Prometeu
|
||||
return payload as unknown as PrometeuVisualThemesPayload;
|
||||
}
|
||||
|
||||
function extractPrometeuSemanticHostProjectionsPayload(initializeResult: unknown): PrometeuSemanticHostProjectionsPayload | undefined {
|
||||
if (!initializeResult || typeof initializeResult !== "object") {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const capabilities = readObject((initializeResult as { capabilities?: unknown }).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 as unknown as PrometeuSemanticHostProjectionsPayload;
|
||||
}
|
||||
|
||||
function selectHostProjection(
|
||||
payload: PrometeuSemanticHostProjectionsPayload | undefined,
|
||||
hostId: string
|
||||
): PrometeuSemanticHostProjection | undefined {
|
||||
if (!payload) {
|
||||
return undefined;
|
||||
}
|
||||
return payload.hostProjections.find((projection) => projection.hostId === hostId);
|
||||
}
|
||||
|
||||
function buildSemanticTokenRules(
|
||||
languageId: string,
|
||||
theme: PrometeuVisualTheme
|
||||
theme: PrometeuVisualTheme,
|
||||
hostProjection: PrometeuSemanticHostProjection | undefined
|
||||
): Record<string, string | { foreground: string; italic?: boolean; bold?: boolean; underline?: boolean }> {
|
||||
const rules: Record<string, string | { foreground: string; italic?: boolean; bold?: boolean; underline?: boolean }> = {};
|
||||
const projectionBySemanticKey = new Map<string, PrometeuSemanticHostProjectionEntry>(
|
||||
(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: string,
|
||||
tokenType: string,
|
||||
modifiers: string[]
|
||||
): string {
|
||||
const normalizedModifiers = [...modifiers].sort();
|
||||
if (normalizedModifiers.length === 0) {
|
||||
return `${tokenType}:${languageId}`;
|
||||
}
|
||||
return `${tokenType}.${normalizedModifiers.join(".")}:${languageId}`;
|
||||
}
|
||||
|
||||
function readObject(value: unknown): Record<string, unknown> {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
||||
return {};
|
||||
|
||||
Reference in New Issue
Block a user