implements PLN-0071
This commit is contained in:
@@ -16,9 +16,41 @@ let output: vscode.OutputChannel;
|
||||
let reconnectTimer: NodeJS.Timeout | undefined;
|
||||
let reconnectContext: vscode.ExtensionContext | undefined;
|
||||
let stoppingClient = false;
|
||||
let lastAppliedThemeSignature: string | undefined;
|
||||
|
||||
const RECONNECT_DELAY_MS = 2000;
|
||||
|
||||
type PrometeuTokenStyle = {
|
||||
semanticKey: string;
|
||||
foreground: string;
|
||||
italic: boolean;
|
||||
bold: boolean;
|
||||
underline: boolean;
|
||||
};
|
||||
|
||||
type PrometeuEditorPalette = {
|
||||
baseForeground: string;
|
||||
selectionBackground: string;
|
||||
activeHighlightBackground: string;
|
||||
lineNumberForeground: string;
|
||||
statusChipBackground: string;
|
||||
statusChipBorder: string;
|
||||
statusChipForeground: string;
|
||||
};
|
||||
|
||||
type PrometeuVisualTheme = {
|
||||
themeId: string;
|
||||
displayName: string;
|
||||
editorPalette: PrometeuEditorPalette;
|
||||
tokenStyles: PrometeuTokenStyle[];
|
||||
};
|
||||
|
||||
type PrometeuVisualThemesPayload = {
|
||||
frontendLanguageId: string;
|
||||
activeVisualThemeId: string;
|
||||
visualThemes: PrometeuVisualTheme[];
|
||||
};
|
||||
|
||||
export function activate(context: vscode.ExtensionContext): void {
|
||||
reconnectContext = context;
|
||||
output = vscode.window.createOutputChannel("Prometeu LSP");
|
||||
@@ -160,6 +192,7 @@ async function startClient(context: vscode.ExtensionContext): Promise<void> {
|
||||
|
||||
try {
|
||||
await nextClient.start();
|
||||
await applyPrometeuVisualTheme(nextClient.initializeResult);
|
||||
|
||||
output.appendLine("Prometeu Studio LSP client started.");
|
||||
vscode.window.showInformationMessage("Connected to Prometeu Studio LSP.");
|
||||
@@ -220,3 +253,111 @@ function clearReconnectTimer(): void {
|
||||
clearTimeout(reconnectTimer);
|
||||
reconnectTimer = undefined;
|
||||
}
|
||||
|
||||
async function applyPrometeuVisualTheme(initializeResult: unknown): Promise<void> {
|
||||
const payload = extractPrometeuVisualThemesPayload(initializeResult);
|
||||
if (!payload) {
|
||||
output.appendLine("Prometeu visual theme payload not present in initialize result.");
|
||||
return;
|
||||
}
|
||||
|
||||
const activeTheme = payload.visualThemes.find((theme) => theme.themeId === payload.activeVisualThemeId);
|
||||
if (!activeTheme) {
|
||||
output.appendLine(`Prometeu visual theme '${payload.activeVisualThemeId}' was not declared by the server.`);
|
||||
return;
|
||||
}
|
||||
|
||||
const themeSignature = JSON.stringify({
|
||||
languageId: payload.frontendLanguageId,
|
||||
themeId: activeTheme.themeId,
|
||||
editorPalette: activeTheme.editorPalette,
|
||||
tokenStyles: activeTheme.tokenStyles
|
||||
});
|
||||
if (themeSignature === lastAppliedThemeSignature) {
|
||||
output.appendLine(`Prometeu visual theme '${activeTheme.themeId}' is already applied.`);
|
||||
return;
|
||||
}
|
||||
|
||||
const configurationTarget = vscode.workspace.workspaceFolders?.length
|
||||
? vscode.ConfigurationTarget.Workspace
|
||||
: vscode.ConfigurationTarget.Global;
|
||||
const configuration = vscode.workspace.getConfiguration();
|
||||
|
||||
const existingSemanticCustomizations = configuration.get<Record<string, unknown>>("editor.semanticTokenColorCustomizations") ?? {};
|
||||
const semanticRules = {
|
||||
...(readObject(existingSemanticCustomizations.rules)),
|
||||
...buildSemanticTokenRules(payload.frontendLanguageId, activeTheme)
|
||||
};
|
||||
|
||||
await configuration.update(
|
||||
"editor.semanticTokenColorCustomizations",
|
||||
{
|
||||
...existingSemanticCustomizations,
|
||||
enabled: true,
|
||||
rules: semanticRules
|
||||
},
|
||||
configurationTarget
|
||||
);
|
||||
|
||||
const existingWorkbenchColors = configuration.get<Record<string, unknown>>("workbench.colorCustomizations") ?? {};
|
||||
await configuration.update(
|
||||
"workbench.colorCustomizations",
|
||||
{
|
||||
...existingWorkbenchColors,
|
||||
"editor.foreground": activeTheme.editorPalette.baseForeground,
|
||||
"editor.selectionBackground": activeTheme.editorPalette.selectionBackground,
|
||||
"editorLineNumber.foreground": activeTheme.editorPalette.lineNumberForeground
|
||||
},
|
||||
configurationTarget
|
||||
);
|
||||
|
||||
lastAppliedThemeSignature = themeSignature;
|
||||
output.appendLine(`Applied Prometeu visual theme '${activeTheme.themeId}' for language '${payload.frontendLanguageId}'.`);
|
||||
}
|
||||
|
||||
function extractPrometeuVisualThemesPayload(initializeResult: unknown): PrometeuVisualThemesPayload | 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.prometeuVisualThemes);
|
||||
|
||||
if (typeof payload.frontendLanguageId !== "string" || typeof payload.activeVisualThemeId !== "string") {
|
||||
return undefined;
|
||||
}
|
||||
if (!Array.isArray(payload.visualThemes)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return payload as unknown as PrometeuVisualThemesPayload;
|
||||
}
|
||||
|
||||
function buildSemanticTokenRules(
|
||||
languageId: string,
|
||||
theme: PrometeuVisualTheme
|
||||
): Record<string, string | { foreground: string; italic?: boolean; bold?: boolean; underline?: boolean }> {
|
||||
const rules: Record<string, string | { foreground: string; italic?: boolean; bold?: boolean; underline?: boolean }> = {};
|
||||
for (const tokenStyle of theme.tokenStyles) {
|
||||
const selector = `${tokenStyle.semanticKey}:${languageId}`;
|
||||
if (!tokenStyle.italic && !tokenStyle.bold && !tokenStyle.underline) {
|
||||
rules[selector] = tokenStyle.foreground;
|
||||
continue;
|
||||
}
|
||||
rules[selector] = {
|
||||
foreground: tokenStyle.foreground,
|
||||
...(tokenStyle.italic ? { italic: true } : {}),
|
||||
...(tokenStyle.bold ? { bold: true } : {}),
|
||||
...(tokenStyle.underline ? { underline: true } : {})
|
||||
};
|
||||
}
|
||||
return rules;
|
||||
}
|
||||
|
||||
function readObject(value: unknown): Record<string, unknown> {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
||||
return {};
|
||||
}
|
||||
return value as Record<string, unknown>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user