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
35 changes: 35 additions & 0 deletions src/sql/__tests__/theme-sharing.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { EditorState, type Extension } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import { expect, it } from "vitest";
import { defaultSqlHoverTheme } from "../hover.js";
import { sqlStructureGutter } from "../structure-extension.js";

function styleModules(extensions: Extension) {
return EditorState.create({ extensions }).facet(EditorView.styleModule);
}

it.each(["light", "dark"] as const)("shares the %s hover style modules", (mode) => {
const first = styleModules(defaultSqlHoverTheme(mode));
const second = styleModules(defaultSqlHoverTheme(mode));
expect(first.length).toBeGreaterThan(0);
expect(second).toHaveLength(first.length);
first.forEach((module, index) => expect(second[index]).toBe(module));
});

it("shares gutter styles across editors with different widths", () => {
const first = styleModules(sqlStructureGutter());
const second = styleModules(sqlStructureGutter({ width: 8 }));
expect(first.length).toBeGreaterThan(0);
expect(second).toHaveLength(first.length);
first.forEach((module, index) => expect(second[index]).toBe(module));

const views = [undefined, 8].map((width) =>
new EditorView({ extensions: sqlStructureGutter({ width }) }),
);
try {
expect(views.map((view) => view.dom.style.getPropertyValue("--cm-sql-gutter-width")))
.toEqual(["3px", "8px"]);
} finally {
views.forEach((view) => view.destroy());
}
});
11 changes: 10 additions & 1 deletion src/sql/hover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ export const DefaultSqlTooltipRenders = {
/**
* Default CSS styles for hover tooltips
*/
export const defaultSqlHoverTheme = (theme: "light" | "dark" = "light"): Extension => {
function createSqlHoverTheme(theme: "light" | "dark"): Extension {
// Theme-dependent color variables
const lightTheme = {
tooltipBg: "#ffffff",
Expand Down Expand Up @@ -713,4 +713,13 @@ export const defaultSqlHoverTheme = (theme: "light" | "dark" = "light"): Extensi
color: colors.em,
},
});
}

const sqlHoverThemes = {
light: createSqlHoverTheme("light"),
dark: createSqlHoverTheme("dark"),
};

/** Default CSS styles for hover tooltips. */
export const defaultSqlHoverTheme = (theme: "light" | "dark" = "light"): Extension =>
sqlHoverThemes[theme];
51 changes: 26 additions & 25 deletions src/sql/structure-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,30 +211,26 @@ function createStructurePlugin(analyzer: SqlStructureAnalyzer, config: SqlGutter
});
}

function createGutterTheme(config: SqlGutterConfig): Extension {
const gutterWidth = config.width || 3;

return EditorView.baseTheme({
".cm-sql-gutter": {
width: `${gutterWidth}px`,
minWidth: `${gutterWidth}px`,
},
".cm-sql-gutter .cm-gutterElement": {
width: `${gutterWidth}px`,
padding: "0",
margin: "0",
},
".cm-sql-gutter-marker": {
width: "100%",
height: "100%",
display: "block",
},
".cm-lineNumbers .cm-gutterElement": {
paddingLeft: "8px",
paddingRight: "8px",
},
});
}
const gutterTheme = EditorView.baseTheme({
".cm-sql-gutter": {
width: "var(--cm-sql-gutter-width, 3px)",
minWidth: "var(--cm-sql-gutter-width, 3px)",
},
".cm-sql-gutter .cm-gutterElement": {
width: "var(--cm-sql-gutter-width, 3px)",
padding: "0",
margin: "0",
},
".cm-sql-gutter-marker": {
width: "100%",
height: "100%",
display: "block",
},
".cm-lineNumbers .cm-gutterElement": {
paddingLeft: "8px",
paddingRight: "8px",
},
});

function createSqlGutter(config: SqlGutterConfig): Extension {
return gutter({
Expand All @@ -254,7 +250,12 @@ export function sqlStructureGutter(config: SqlGutterConfig = {}): Extension[] {
return [
sqlGutterStateField,
createStructurePlugin(analyzer, config),
createGutterTheme(config),
[
gutterTheme,
EditorView.editorAttributes.of({
style: `--cm-sql-gutter-width: ${config.width || 3}px`,
}),
],
createSqlGutter(config),
];
}
Loading