feat(editor): add SQL block comment action

This commit is contained in:
Melody Ruch authored and GitHub committed 2026-08-16 11:29:03 +08:00
1 parent 34a4f5d78a
commit e18222628d
14 files changed
+127 -3

No files matched your search

@@ -9,6 +9,7 @@ import {
supportsDatabaseNameCompletion,
supportsDatabaseSchemaQualifier,
supportsQueryTargetDatabaseListing,
supportsQueryEditorBlockComments,
supportsSqlInListPaste,
supportsTableImport,
supportsTransaction,
@@ -123,6 +124,21 @@ describe("supportsSqlInListPaste", () => {
});
});
describe("supportsQueryEditorBlockComments", () => {
it("allows block comments in generic and SQL editors", () => {
expect(supportsQueryEditorBlockComments(undefined)).toBe(true);
expect(supportsQueryEditorBlockComments("mysql")).toBe(true);
expect(supportsQueryEditorBlockComments("postgres")).toBe(true);
expect(supportsQueryEditorBlockComments("sqlserver")).toBe(true);
});
it("hides block comments in non-SQL editors", () => {
expect(supportsQueryEditorBlockComments("redis")).toBe(false);
expect(supportsQueryEditorBlockComments("mongodb")).toBe(false);
expect(supportsQueryEditorBlockComments("elasticsearch")).toBe(false);
});
});
describe("supportsTableImport", () => {
it("enables OceanBase Oracle table import", () => {
expect(supportsTableImport("oceanbase-oracle")).toBe(true);
@@ -0,0 +1,52 @@
import { readFileSync } from "node:fs";
import { toggleBlockComment } from "@codemirror/commands";
import { sql } from "@codemirror/lang-sql";
import { EditorSelection, EditorState, type Transaction } from "@codemirror/state";
import { describe, expect, it, vi } from "vitest";
const queryEditorSource = readFileSync(new URL("../../../components/editor/QueryEditor.vue", import.meta.url), "utf8");
function runToggleBlockComment(doc: string, selection: EditorSelection | { anchor: number; head?: number }) {
let state = EditorState.create({ doc, selection, extensions: [sql()] });
const dispatch = vi.fn((transaction: Transaction) => {
state = transaction.state;
});
const handled = toggleBlockComment({
get state() {
return state;
},
dispatch,
} as never);
return { dispatch, handled, state };
}
describe("QueryEditor block comment", () => {
it("wires toggleBlockComment into the right-click context menu, the keymap, and the editor module load", () => {
expect(queryEditorSource).toContain('let codeMirrorToggleBlockComment: typeof import("@codemirror/commands").toggleBlockComment | null = null;');
expect(queryEditorSource).toContain("codeMirrorToggleBlockComment?.(currentView);");
expect(queryEditorSource).toContain('label: t("editor.contextMenu.blockCommentSelection")');
expect(queryEditorSource).toContain("shortcut: shortcuts.toggleBlockComment");
expect(queryEditorSource).toContain("...binding(shortcuts.toggleBlockComment, (view) => {");
expect(queryEditorSource).toContain("!supportsQueryEditorBlockComments(props.databaseType)");
expect(queryEditorSource).toContain("defaultKeymap.filter((item) => item.run !== toggleBlockComment)");
expect(queryEditorSource).toContain("codeMirrorToggleBlockComment = toggleBlockComment;");
});
it("wraps the selected SQL in a block comment", () => {
const doc = "SELECT id FROM users;";
const selection = EditorSelection.range(doc.indexOf("id"), doc.indexOf("id") + "id".length);
const result = runToggleBlockComment(doc, selection);
expect(result.handled).toBe(true);
expect(result.state.doc.toString()).toBe("SELECT /* id */ FROM users;");
});
it("removes an existing block comment around the selection", () => {
const doc = "SELECT /* id */ FROM users;";
const selection = EditorSelection.range(doc.indexOf("/*"), doc.indexOf("*/") + "*/".length);
const result = runToggleBlockComment(doc, selection);
expect(result.state.doc.toString()).toBe("SELECT id FROM users;");
});
});
@@ -5,6 +5,7 @@ describe("shortcutRegistry editor actions", () => {
const formatterEditorActionIds: ShortcutActionId[] = [
"formatSql",
"toggleLineComment",
"toggleBlockComment",
"indentMore",
"indentLess",
"joinLines",
@@ -126,6 +127,7 @@ describe("shortcutRegistry editor actions", () => {
expect(shortcuts.executeSql).toBe("Mod+Shift+Enter");
expect(shortcuts.formatSql).toBe("Shift+Mod+F");
expect(shortcuts.toggleLineComment).toBe("Mod+/");
expect(shortcuts.toggleBlockComment).toBe("Shift+Alt+A");
expect(shortcuts.indentMore).toBe("");
expect(shortcuts.indentLess).toBe("Shift+Tab");
expect(shortcuts.joinLines).toBe("Mod+J");
@@ -131,6 +131,11 @@ export function supportsSqlInListPaste(dbType?: DatabaseType): boolean {
return supportsSqlFileExecution(dbType) && !NON_SQL_IN_LIST_PASTE_TYPES.has(dbType);
}
export function supportsQueryEditorBlockComments(dbType?: DatabaseType): boolean {
if (!dbType) return true;
return supportsSqlFileExecution(dbType);
}
export function supportsSchemaDiagram(dbType?: DatabaseType): boolean {
return supportsDatabaseFeature(dbType, "diagram");
}
@@ -6,6 +6,7 @@ export type ShortcutActionId =
| "formatSql"
| "expandSelectStar"
| "toggleLineComment"
| "toggleBlockComment"
| "saveSql"
| "acceptCompletion"
| "indentMore"
@@ -119,6 +120,12 @@ export const SHORTCUT_DEFINITIONS: ShortcutDefinition[] = [
scope: "editor",
defaultShortcut: "Mod+/",
},
{
id: "toggleBlockComment",
labelKey: "settings.shortcutToggleBlockComment",
scope: "editor",
defaultShortcut: "Shift+Alt+A",
},
{
id: "saveSql",
labelKey: "settings.shortcutSaveSql",