Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions demo/shared/theme.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { EditorState } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import { expect, it } from "vitest";
import { registerEditor, setThemeMode, themeExtension } from "./theme.js";

it("reuses demo styles across editors and theme toggles", () => {
const views = [0, 1].map(
() =>
new EditorView({
state: EditorState.create({ extensions: themeExtension() }),
}),
);
const unregister = views.map(registerEditor);
const modules = () =>
views.map((view) => view.state.facet(EditorView.styleModule));
try {
setThemeMode("light");
const light = modules();
setThemeMode("dark");
const dark = modules();
setThemeMode("light");
const restored = modules();
for (const pair of [light, dark, restored]) {
expect(pair[0].length).toBeGreaterThan(0);
expect(pair[1]).toHaveLength(pair[0].length);
pair[0].forEach((module, index) =>
expect(pair[1][index]).toBe(module),
);
}
light[0].forEach((module, index) =>
expect(restored[0][index]).toBe(module),
);
} finally {
for (const dispose of unregister) dispose();
for (const view of views) view.destroy();
}
});
8 changes: 6 additions & 2 deletions demo/shared/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,9 +529,13 @@ function lspTheme(p: Palette): Extension {
});
}

const themes: Record<ThemeMode, Extension> = {
light: [editorTheme(LIGHT), highlightStyle(LIGHT), lspTheme(LIGHT)],
dark: [editorTheme(DARK), highlightStyle(DARK), lspTheme(DARK)],
};

function themeFor(mode: ThemeMode): Extension {
const p = mode === "dark" ? DARK : LIGHT;
return [editorTheme(p), highlightStyle(p), lspTheme(p)];
return themes[mode];
}

// ---------------------------------------------------------------------------
Expand Down
26 changes: 26 additions & 0 deletions src/__tests__/theme-sharing.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { EditorState } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import { expect, it } from "vitest";
import { languageServerWithClient } from "../plugin.js";
import { createFakeClient } from "./test-utils.js";

it.each([
{ diagnosticsEnabled: true, completionEnabled: false },
{ diagnosticsEnabled: false, completionEnabled: true },
{ diagnosticsEnabled: true, completionEnabled: true },
])("shares style modules with %j", (features) => {
const createModules = () =>
EditorState.create({
extensions: languageServerWithClient({
client: createFakeClient(),
documentUri: "file:///test.py",
languageId: "python",
...features,
}),
}).facet(EditorView.styleModule);
const first = createModules();
const second = createModules();
expect(first.length).toBeGreaterThan(0);
expect(second).toHaveLength(first.length);
first.forEach((module, index) => expect(second[index]).toBe(module));
});
50 changes: 25 additions & 25 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2444,6 +2444,29 @@ export function languageServer(options: LanguageServerWebsocketOptions) {
});
}

const diagnosticsTheme = EditorView.baseTheme({
".cm-lsp-unnecessary": { opacity: "0.6" },
".cm-lsp-deprecated": { textDecoration: "line-through" },
".cm-diagnostic-related": {
marginTop: "4px",
display: "flex",
flexDirection: "column",
gap: "2px",
},
".cm-diagnostic-related-clickable": { cursor: "pointer" },
".cm-diagnostic-related-location": { opacity: "0.7" },
".cm-diagnostic-code-link": {
display: "inline-block",
marginTop: "4px",
},
});

const deprecatedCompletionTheme = EditorView.baseTheme({
".cm-tooltip-autocomplete li.cm-deprecated .cm-completionLabel": {
textDecoration: "line-through",
},
});

export function languageServerWithClient(options: LanguageServerOptions) {
const shortcuts = {
rename: "F2",
Expand Down Expand Up @@ -2502,25 +2525,7 @@ export function languageServerWithClient(options: LanguageServerOptions) {
// plus the field that keeps related-info click targets mapped through
// edits. Hosts can override these classes via their own theme.
if (featuresOptions.diagnosticsEnabled) {
extensions.push(
relatedLocationAnchors,
EditorView.baseTheme({
".cm-lsp-unnecessary": { opacity: "0.6" },
".cm-lsp-deprecated": { textDecoration: "line-through" },
".cm-diagnostic-related": {
marginTop: "4px",
display: "flex",
flexDirection: "column",
gap: "2px",
},
".cm-diagnostic-related-clickable": { cursor: "pointer" },
".cm-diagnostic-related-location": { opacity: "0.7" },
".cm-diagnostic-code-link": {
display: "inline-block",
marginTop: "4px",
},
}),
);
extensions.push(relatedLocationAnchors, diagnosticsTheme);
}

// Add shortcuts
Expand Down Expand Up @@ -2750,12 +2755,7 @@ export function languageServerWithClient(options: LanguageServerOptions) {
const userOptionClass = options.completionConfig?.optionClass;
extensions.push(
// Hosts can override via `.cm-tooltip-autocomplete li.cm-deprecated`
EditorView.baseTheme({
".cm-tooltip-autocomplete li.cm-deprecated .cm-completionLabel":
{
textDecoration: "line-through",
},
}),
deprecatedCompletionTheme,
autocompletion({
...options.completionConfig,
optionClass: (completion) =>
Expand Down
Loading