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
|
||||
|
||||
@@ -53,100 +53,6 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"semanticTokenTypes": [
|
||||
{
|
||||
"id": "pbs-comment",
|
||||
"description": "PBS comment token"
|
||||
},
|
||||
{
|
||||
"id": "pbs-string",
|
||||
"description": "PBS string token"
|
||||
},
|
||||
{
|
||||
"id": "pbs-number",
|
||||
"description": "PBS number token"
|
||||
},
|
||||
{
|
||||
"id": "pbs-literal",
|
||||
"description": "PBS literal token"
|
||||
},
|
||||
{
|
||||
"id": "pbs-lifecycle",
|
||||
"description": "PBS lifecycle token"
|
||||
},
|
||||
{
|
||||
"id": "pbs-keyword",
|
||||
"description": "PBS keyword token"
|
||||
},
|
||||
{
|
||||
"id": "pbs-operator",
|
||||
"description": "PBS operator token"
|
||||
},
|
||||
{
|
||||
"id": "pbs-punctuation",
|
||||
"description": "PBS punctuation token"
|
||||
},
|
||||
{
|
||||
"id": "pbs-function",
|
||||
"description": "PBS function token"
|
||||
},
|
||||
{
|
||||
"id": "pbs-method",
|
||||
"description": "PBS method token"
|
||||
},
|
||||
{
|
||||
"id": "pbs-constructor",
|
||||
"description": "PBS constructor token"
|
||||
},
|
||||
{
|
||||
"id": "pbs-struct",
|
||||
"description": "PBS struct token"
|
||||
},
|
||||
{
|
||||
"id": "pbs-contract",
|
||||
"description": "PBS contract token"
|
||||
},
|
||||
{
|
||||
"id": "pbs-host",
|
||||
"description": "PBS host token"
|
||||
},
|
||||
{
|
||||
"id": "pbs-builtin-type",
|
||||
"description": "PBS builtin type token"
|
||||
},
|
||||
{
|
||||
"id": "pbs-service",
|
||||
"description": "PBS service token"
|
||||
},
|
||||
{
|
||||
"id": "pbs-error",
|
||||
"description": "PBS error token"
|
||||
},
|
||||
{
|
||||
"id": "pbs-enum",
|
||||
"description": "PBS enum token"
|
||||
},
|
||||
{
|
||||
"id": "pbs-callback",
|
||||
"description": "PBS callback token"
|
||||
},
|
||||
{
|
||||
"id": "pbs-global",
|
||||
"description": "PBS global token"
|
||||
},
|
||||
{
|
||||
"id": "pbs-const",
|
||||
"description": "PBS const token"
|
||||
},
|
||||
{
|
||||
"id": "pbs-implements",
|
||||
"description": "PBS implements token"
|
||||
},
|
||||
{
|
||||
"id": "pbs-identifier",
|
||||
"description": "PBS identifier token"
|
||||
}
|
||||
],
|
||||
"configurationDefaults": {
|
||||
"editor.semanticHighlighting.enabled": true
|
||||
}
|
||||
|
||||
@@ -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