fix(mongodb): use line comments in editor
This commit is contained in:
1 parent
6cbb10439e
commit
fa4238c5d3
8 files changed
+370
-4
No files matched your search
@@ -0,0 +1,72 @@
|
||||
import type { Extension } from "@codemirror/state";
|
||||
import { tags } from "@lezer/highlight";
|
||||
import { shellLineCommentRanges } from "@/lib/editor/shellLineCommentRanges";
|
||||
|
||||
type EditorViewType = import("@codemirror/view").EditorView;
|
||||
type DecorationSet = import("@codemirror/view").DecorationSet;
|
||||
interface SyntaxNodeLike {
|
||||
name: string;
|
||||
parent: SyntaxNodeLike | null;
|
||||
}
|
||||
|
||||
export const SHELL_LINE_COMMENT_CLASS = "cm-shell-line-comment";
|
||||
|
||||
interface ShellLineCommentHighlightDeps {
|
||||
ViewPlugin: typeof import("@codemirror/view").ViewPlugin;
|
||||
Decoration: typeof import("@codemirror/view").Decoration;
|
||||
highlightingFor: typeof import("@codemirror/language").highlightingFor;
|
||||
syntaxTree: typeof import("@codemirror/language").syntaxTree;
|
||||
}
|
||||
|
||||
/** Reuses the active theme's comment colour so `//` matches what the SQL grammar gives `--`. */
|
||||
export function shellLineCommentClass(state: import("@codemirror/state").EditorState, highlightingFor: ShellLineCommentHighlightDeps["highlightingFor"]): string {
|
||||
const themeClass = highlightingFor(state, [tags.lineComment, tags.comment]);
|
||||
return themeClass ? `${SHELL_LINE_COMMENT_CLASS} ${themeClass}` : SHELL_LINE_COMMENT_CLASS;
|
||||
}
|
||||
|
||||
export function createShellLineCommentHighlight({ ViewPlugin, Decoration, highlightingFor, syntaxTree }: ShellLineCommentHighlightDeps): Extension {
|
||||
function buildDecorations(view: EditorViewType, className: string): DecorationSet {
|
||||
const tree = syntaxTree(view.state);
|
||||
const ranges = new Map<number, number>();
|
||||
for (const visibleRange of view.visibleRanges) {
|
||||
const from = view.state.doc.lineAt(visibleRange.from).from;
|
||||
const to = view.state.doc.lineAt(visibleRange.to).to;
|
||||
const text = view.state.doc.sliceString(from, to);
|
||||
for (const range of shellLineCommentRanges(text)) {
|
||||
const absoluteFrom = from + range.from;
|
||||
const absoluteTo = from + range.to;
|
||||
if (absoluteFrom >= visibleRange.to || absoluteTo <= visibleRange.from) continue;
|
||||
let node: SyntaxNodeLike | null = tree.resolveInner(absoluteFrom, 1);
|
||||
let excluded = false;
|
||||
while (node) {
|
||||
if (node.name === "BlockComment" || node.name === "String" || node.name === "QuotedIdentifier") {
|
||||
excluded = true;
|
||||
break;
|
||||
}
|
||||
node = node.parent;
|
||||
}
|
||||
if (!excluded) ranges.set(absoluteFrom, absoluteTo);
|
||||
}
|
||||
}
|
||||
const decoration = Decoration.mark({ class: className });
|
||||
return Decoration.set([...ranges].sort(([left], [right]) => left - right).map(([from, to]) => decoration.range(from, to)));
|
||||
}
|
||||
|
||||
return ViewPlugin.fromClass(
|
||||
class {
|
||||
decorations: DecorationSet;
|
||||
className: string;
|
||||
constructor(view: EditorViewType) {
|
||||
this.className = shellLineCommentClass(view.state, highlightingFor);
|
||||
this.decorations = buildDecorations(view, this.className);
|
||||
}
|
||||
update(update: import("@codemirror/view").ViewUpdate) {
|
||||
const className = shellLineCommentClass(update.state, highlightingFor);
|
||||
if (!update.docChanged && !update.viewportChanged && className === this.className) return;
|
||||
this.className = className;
|
||||
this.decorations = buildDecorations(update.view, className);
|
||||
}
|
||||
},
|
||||
{ decorations: (plugin) => plugin.decorations },
|
||||
);
|
||||
}
|
||||
@@ -30,6 +30,21 @@ export function sqlSemanticHighlightTheme(EditorView: typeof import("@codemirror
|
||||
});
|
||||
}
|
||||
|
||||
// MongoDB and other shell-style editors keep the SQL grammar, so their `//` comments are decorated
|
||||
// manually. The decoration carries the active theme's comment highlight class, so it only has to
|
||||
// keep the SQL grammar's colours from leaking through on the tokens it wraps.
|
||||
export function shellLineCommentTheme(EditorView: typeof import("@codemirror/view").EditorView): Extension {
|
||||
return EditorView.theme({
|
||||
".cm-shell-line-comment": {
|
||||
fontStyle: "italic",
|
||||
},
|
||||
".cm-shell-line-comment *": {
|
||||
color: "inherit !important",
|
||||
fontStyle: "italic",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const SUPPORTS_COLOR_MIX = typeof CSS !== "undefined" && typeof CSS.supports === "function" && CSS.supports("color", "color-mix(in oklch, black 50%, white)");
|
||||
const SUPPORTS_OKLCH = typeof CSS !== "undefined" && typeof CSS.supports === "function" && CSS.supports("color", "oklch(0.62 0.19 255)");
|
||||
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
import type { DatabaseType } from "@/types/database";
|
||||
|
||||
const SQL_LINE_COMMENT_TOKEN = "--";
|
||||
|
||||
// Editors that speak a shell/script dialect instead of SQL keep their native line comment marker.
|
||||
const LINE_COMMENT_TOKENS: Partial<Record<DatabaseType, string>> = {
|
||||
mongodb: "//",
|
||||
};
|
||||
|
||||
export function queryEditorLineCommentToken(dbType?: DatabaseType): string {
|
||||
if (!dbType) return SQL_LINE_COMMENT_TOKEN;
|
||||
return LINE_COMMENT_TOKENS[dbType] ?? SQL_LINE_COMMENT_TOKEN;
|
||||
}
|
||||
|
||||
export function queryEditorCommentTokens(dbType?: DatabaseType) {
|
||||
return {
|
||||
line: queryEditorLineCommentToken(dbType),
|
||||
block: { open: "/*", close: "*/" },
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
export interface ShellLineCommentRange {
|
||||
from: number;
|
||||
to: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds `//` line comments in shell-style text (the MongoDB editor keeps the SQL grammar for
|
||||
* highlighting, so its comments are not tokenized by the parser). Quoted strings and block
|
||||
* comments are skipped so markers such as `"https://host"` stay uncommented.
|
||||
*/
|
||||
export function shellLineCommentRanges(text: string, to = text.length): ShellLineCommentRange[] {
|
||||
const ranges: ShellLineCommentRange[] = [];
|
||||
const limit = Math.min(to, text.length);
|
||||
let index = 0;
|
||||
|
||||
while (index < limit) {
|
||||
const char = text[index];
|
||||
|
||||
if (char === '"' || char === "'" || char === "`") {
|
||||
index = skipString(text, index, char);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (char === "/" && text[index + 1] === "*") {
|
||||
const end = text.indexOf("*/", index + 2);
|
||||
index = end === -1 ? text.length : end + 2;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (char === "/" && text[index + 1] === "/") {
|
||||
let end = index + 2;
|
||||
while (end < text.length && text[end] !== "\n" && text[end] !== "\r") end += 1;
|
||||
ranges.push({ from: index, to: end });
|
||||
index = end;
|
||||
continue;
|
||||
}
|
||||
|
||||
index += 1;
|
||||
}
|
||||
|
||||
return ranges;
|
||||
}
|
||||
|
||||
function skipString(text: string, start: number, quote: string): number {
|
||||
let index = start + 1;
|
||||
while (index < text.length) {
|
||||
const char = text[index];
|
||||
if (char === "\\") {
|
||||
index += 2;
|
||||
continue;
|
||||
}
|
||||
if (char === quote) return index + 1;
|
||||
// Single- and double-quoted strings do not span lines; bail out so an unterminated quote
|
||||
// does not swallow the rest of the document.
|
||||
if ((quote === '"' || quote === "'") && (char === "\n" || char === "\r")) return index;
|
||||
index += 1;
|
||||
}
|
||||
return index;
|
||||
}
|
||||
Reference in new issue
Block a user