feat(editor): add whitespace display toggle for SQL editor

This commit is contained in:
Melody Ruch authored and GitHub committed 2026-09-20 00:50:44 +08:00
1 parent fff07eff00
commit fa3925ca52
18 files changed
+184

No files matched your search

@@ -0,0 +1,52 @@
// @vitest-environment happy-dom
import { EditorState } from "@codemirror/state";
import * as viewModule from "@codemirror/view";
import { EditorView } from "@codemirror/view";
import { afterEach, describe, expect, it } from "vitest";
import { NEWLINE_MARKER, NEWLINE_MARKER_CLASS, createShowWhitespaceExtension } from "@/lib/editor/codemirrorShowWhitespace";
let view: EditorView | null = null;
function mount(doc: string, enabled: boolean) {
const parent = document.createElement("div");
document.body.appendChild(parent);
view = new EditorView({ parent, state: EditorState.create({ doc, extensions: [createShowWhitespaceExtension(viewModule, enabled)] }) });
return view;
}
afterEach(() => {
view?.destroy();
view = null;
document.body.innerHTML = "";
});
describe("createShowWhitespaceExtension", () => {
it("adds nothing when disabled", () => {
expect(createShowWhitespaceExtension(viewModule, false)).toEqual([]);
const v = mount("select 1\n\tfrom t", false);
expect(v.dom.querySelectorAll(`.${NEWLINE_MARKER_CLASS}`)).toHaveLength(0);
expect(v.dom.querySelector(".cm-highlightSpace, .cm-highlightTab")).toBeNull();
});
it("marks a line break after every line except the last", () => {
const v = mount("select 1\n\tfrom t\nwhere x", true);
const markers = v.dom.querySelectorAll(`.${NEWLINE_MARKER_CLASS}`);
expect(markers).toHaveLength(2);
expect(markers[0]!.textContent).toBe(NEWLINE_MARKER);
});
it("marks spaces and tabs and never changes the document text", () => {
const doc = "select 1\n\tfrom t";
const v = mount(doc, true);
expect(v.dom.querySelector(".cm-highlightSpace")).not.toBeNull();
expect(v.dom.querySelector(".cm-highlightTab")).not.toBeNull();
expect(v.state.doc.toString()).toBe(doc);
});
it("keeps marking empty lines and follows edits", () => {
const v = mount("a\n\nb", true);
expect(v.dom.querySelectorAll(`.${NEWLINE_MARKER_CLASS}`)).toHaveLength(2);
v.dispatch({ changes: { from: v.state.doc.length, insert: "\nc" } });
expect(v.dom.querySelectorAll(`.${NEWLINE_MARKER_CLASS}`)).toHaveLength(3);
});
});
@@ -0,0 +1,71 @@
import type { Extension } from "@codemirror/state";
type ViewModule = typeof import("@codemirror/view");
type EditorViewType = import("@codemirror/view").EditorView;
type DecorationSet = import("@codemirror/view").DecorationSet;
export const NEWLINE_MARKER = "↵";
export const NEWLINE_MARKER_CLASS = "cm-dbx-newline-marker";
/**
* Display-only whitespace markers for the SQL editor: spaces as "·", tabs as "→"
* (CodeMirror's highlightWhitespace) plus a "↵" at every line end that is followed
* by a line break. The document text itself is never touched.
*/
export function createShowWhitespaceExtension(view: Pick<ViewModule, "highlightWhitespace" | "ViewPlugin" | "Decoration" | "WidgetType" | "EditorView">, enabled: boolean): Extension {
if (!enabled) return [];
const { highlightWhitespace, ViewPlugin, Decoration, WidgetType, EditorView } = view;
class NewlineMarkerWidget extends WidgetType {
eq(): boolean {
return true;
}
toDOM(): HTMLElement {
const el = document.createElement("span");
el.className = NEWLINE_MARKER_CLASS;
el.setAttribute("aria-hidden", "true");
el.textContent = NEWLINE_MARKER;
return el;
}
ignoreEvent(): boolean {
return true;
}
}
const marker = Decoration.widget({ widget: new NewlineMarkerWidget(), side: 1 });
function buildDecorations(editorView: EditorViewType): DecorationSet {
const doc = editorView.state.doc;
const ranges = [];
let lastLine = -1;
for (const { from, to } of editorView.visibleRanges) {
for (let pos = from; pos <= to; ) {
const line = doc.lineAt(pos);
if (line.number !== lastLine) {
lastLine = line.number;
if (line.number < doc.lines) ranges.push(marker.range(line.to));
}
pos = line.to + 1;
}
}
return Decoration.set(ranges);
}
const newlinePlugin = ViewPlugin.fromClass(
class {
decorations: DecorationSet;
constructor(editorView: EditorViewType) {
this.decorations = buildDecorations(editorView);
}
update(update: import("@codemirror/view").ViewUpdate) {
if (update.docChanged || update.viewportChanged) this.decorations = buildDecorations(update.view);
}
},
{ decorations: (plugin) => plugin.decorations },
);
const markerTheme = EditorView.baseTheme({
[`.${NEWLINE_MARKER_CLASS}`]: { opacity: "0.35", pointerEvents: "none", userSelect: "none" },
});
return [highlightWhitespace(), newlinePlugin, markerTheme];
}