Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

feat(editor): support ctrl-n/p in completion lists

Xsyl.way105da7dfea5f
diff --git a/apps/desktop/src/components/editor/QueryEditor.vue b/apps/desktop/src/components/editor/QueryEditor.vue
index f91bb8c3a8..31f9dbcf9a 100644
--- a/apps/desktop/src/components/editor/QueryEditor.vue
+++ b/apps/desktop/src/components/editor/QueryEditor.vue
@@ -137,6 +137,7 @@ import { selectLineEnds } from "@/lib/editor/selectLineEnds";
import { scrollEditorLine } from "@/lib/editor/scrollEditorLine";
import { createQueryEditorStringMouseSelection } from "@/lib/editor/queryEditorStringMouseSelection";
import { createQueryEditorCompletionShortcutBindings } from "@/lib/editor/queryEditorCompletionShortcut";
+import { handleQueryEditorCompletionNavigation } from "@/lib/editor/queryEditorCompletionNavigation";
import { createQueryEditorSelectionCaseShortcutBindings } from "@/lib/editor/queryEditorSelectionCaseShortcut";
import { createQueryEditorExecutionShortcutBindings, createQueryEditorExecutionShortcutHandler, createQueryEditorPostCompositionKeyGuard } from "@/lib/editor/queryEditorExecutionShortcut";
import { supportsQueryEditorBlockComments, supportsSqlInListPaste } from "@/lib/database/databaseFeatureSupport";
@@ -2171,6 +2172,11 @@ const codeMirrorLifecycle = useQueryEditorCodeMirror({
EditorView.domEventHandlers({
keydown(event, currentView) {
const shortcuts = normalizeShortcutSettings(settingsStore.editorSettings.shortcuts);
+ // The configured Quick Open shortcut (Ctrl-P on Windows/Linux)
+ // must keep working even while a completion list is open; yield
+ // before completion navigation can consume and preventDefault it.
+ if (matchesShortcut(event, shortcuts.quickOpen)) return false;
+ if (handleQueryEditorCompletionNavigation(currentView, event, initializedRuntime.codeMirrorMoveCompletionSelection)) return true;
if (matchesShortcut(event, shortcuts.selectLineEnds)) {
event.preventDefault();
return selectLineEnds(currentView);
diff --git a/apps/desktop/src/lib/__tests__/editor/queryEditorCompletionNavigation.spec.ts b/apps/desktop/src/lib/__tests__/editor/queryEditorCompletionNavigation.spec.ts
new file mode 100644
index 0000000000..d1f0da5908
--- /dev/null
+++ b/apps/desktop/src/lib/__tests__/editor/queryEditorCompletionNavigation.spec.ts
@@ -0,0 +1,45 @@
+// @vitest-environment happy-dom
+
+import type { EditorView } from "@codemirror/view";
+import { describe, expect, it, vi } from "vitest";
+import { handleQueryEditorCompletionNavigation, queryEditorCompletionNavigationDirection } from "@/lib/editor/queryEditorCompletionNavigation";
+
+function event(key: string, modifiers: Partial<KeyboardEventInit> = {}): KeyboardEvent {
+ return new KeyboardEvent("keydown", { key, ctrlKey: true, cancelable: true, ...modifiers });
+}
+
+describe("queryEditorCompletionNavigation", () => {
+ it("maps only plain Ctrl-N and Ctrl-P to completion navigation", () => {
+ expect(queryEditorCompletionNavigationDirection(event("n"))).toBe(true);
+ expect(queryEditorCompletionNavigationDirection(event("P"))).toBe(false);
+ expect(queryEditorCompletionNavigationDirection(event("n", { ctrlKey: false }))).toBeNull();
+ expect(queryEditorCompletionNavigationDirection(event("n", { metaKey: true }))).toBeNull();
+ expect(queryEditorCompletionNavigationDirection(event("n", { altKey: true }))).toBeNull();
+ expect(queryEditorCompletionNavigationDirection(event("n", { shiftKey: true }))).toBeNull();
+ expect(queryEditorCompletionNavigationDirection(event("x"))).toBeNull();
+ });
+
+ it("ignores keys while an IME composition is active", () => {
+ expect(queryEditorCompletionNavigationDirection({ key: "n", ctrlKey: true, metaKey: false, altKey: false, shiftKey: false, isComposing: true })).toBeNull();
+ expect(queryEditorCompletionNavigationDirection({ key: "p", ctrlKey: true, metaKey: false, altKey: false, shiftKey: false, isComposing: true })).toBeNull();
+ });
+
+ it("moves the active completion and consumes the key only when CodeMirror handled it", () => {
+ const view = {} as EditorView;
+ const next = vi.fn(() => true);
+ const previous = vi.fn(() => false);
+ const moveCompletionSelection = vi.fn((forward: boolean) => (forward ? next : previous));
+
+ const nextEvent = event("n");
+ expect(handleQueryEditorCompletionNavigation(view, nextEvent, moveCompletionSelection)).toBe(true);
+ expect(moveCompletionSelection).toHaveBeenCalledWith(true);
+ expect(next).toHaveBeenCalledWith(view);
+ expect(nextEvent.defaultPrevented).toBe(true);
+
+ const previousEvent = event("p");
+ expect(handleQueryEditorCompletionNavigation(view, previousEvent, moveCompletionSelection)).toBe(false);
+ expect(moveCompletionSelection).toHaveBeenCalledWith(false);
+ expect(previous).toHaveBeenCalledWith(view);
+ expect(previousEvent.defaultPrevented).toBe(false);
+ });
+});
diff --git a/apps/desktop/src/lib/editor/queryEditorCompletionNavigation.ts b/apps/desktop/src/lib/editor/queryEditorCompletionNavigation.ts
new file mode 100644
index 0000000000..f460f5daad
--- /dev/null
+++ b/apps/desktop/src/lib/editor/queryEditorCompletionNavigation.ts
@@ -0,0 +1,22 @@
+import type { EditorView } from "@codemirror/view";
+
+type CompletionSelectionMover = (forward: boolean) => (view: EditorView) => boolean;
+
+export function queryEditorCompletionNavigationDirection(event: Pick<KeyboardEvent, "key" | "ctrlKey" | "metaKey" | "altKey" | "shiftKey" | "isComposing">): boolean | null {
+ // IME composition keys must never drive editor shortcuts (repo convention:
+ // composition-period keys are ignored everywhere else in the editor).
+ if (event.isComposing) return null;
+ if (!event.ctrlKey || event.metaKey || event.altKey || event.shiftKey) return null;
+ const key = event.key.toLowerCase();
+ if (key === "n") return true;
+ if (key === "p") return false;
+ return null;
+}
+
+export function handleQueryEditorCompletionNavigation(view: EditorView, event: KeyboardEvent, moveCompletionSelection: CompletionSelectionMover | null | undefined): boolean {
+ const direction = queryEditorCompletionNavigationDirection(event);
+ if (direction === null || !moveCompletionSelection) return false;
+ const handled = moveCompletionSelection(direction)(view);
+ if (handled) event.preventDefault();
+ return handled;
+}