Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat(studio): add keyboard shortcuts to the SQL editor (#45335)

Ali Waseemfa8f49b26181
diff --git a/apps/studio/components/interfaces/SQLEditor/MonacoEditor.tsx b/apps/studio/components/interfaces/SQLEditor/MonacoEditor.tsx
index 1bf80d8aba1..394743679b7 100644
--- a/apps/studio/components/interfaces/SQLEditor/MonacoEditor.tsx
+++ b/apps/studio/components/interfaces/SQLEditor/MonacoEditor.tsx
@@ -28,6 +28,8 @@ export type MonacoEditorProps = {
monacoRef: MutableRefObject<Monaco | null>
autoFocus?: boolean
executeQuery: () => void
+ executeExplainQuery: () => void
+ prettifyQuery: () => void
onHasSelection: (value: boolean) => void
onMount?: (editor: IStandaloneCodeEditor) => void
onPrompt?: (value: {
@@ -49,6 +51,8 @@ const MonacoEditor = ({
placeholder = '',
className,
executeQuery,
+ executeExplainQuery,
+ prettifyQuery,
onHasSelection,
onPrompt,
onMount,
@@ -80,6 +84,12 @@ const MonacoEditor = ({
const executeQueryRef = useRef(executeQuery)
executeQueryRef.current = executeQuery
+ const executeExplainQueryRef = useRef(executeExplainQuery)
+ executeExplainQueryRef.current = executeExplainQuery
+
+ const prettifyQueryRef = useRef(prettifyQuery)
+ prettifyQueryRef.current = prettifyQuery
+
const aiHotkeyEnabledRef = useRef(isAIAssistantHotkeyEnabled)
aiHotkeyEnabledRef.current = isAIAssistantHotkeyEnabled
@@ -92,6 +102,29 @@ const MonacoEditor = ({
monacoRef.current.editor.setModelMarkers(model, 'owner', [])
}
+ // Blur the editor on Escape so users can hop out to the rest of the UI.
+ // The precondition defers to Monaco's own Escape consumers (suggest widget,
+ // find widget, parameter hints, snippet/rename mode, inline suggestions) and
+ // to selection/multi-cursor cancellation, so inline features keep working.
+ editor.addCommand(
+ monaco.KeyCode.Escape,
+ () => {
+ ;(document.activeElement as HTMLElement | null)?.blur()
+ },
+ [
+ 'editorTextFocus',
+ '!editorHasSelection',
+ '!editorHasMultipleSelections',
+ '!suggestWidgetVisible',
+ '!findWidgetVisible',
+ '!parameterHintsVisible',
+ '!renameInputVisible',
+ '!inSnippetMode',
+ '!accessibilityHelpWidgetVisible',
+ '!inlineSuggestionVisible',
+ ].join(' && ')
+ )
+
editor.addAction({
id: 'run-query',
label: 'Run Query',
@@ -103,6 +136,17 @@ const MonacoEditor = ({
},
})
+ editor.addAction({
+ id: 'run-explain-query',
+ label: 'Run EXPLAIN ANALYZE',
+ keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyMod.Shift | monaco.KeyCode.Enter],
+ contextMenuGroupId: 'operation',
+ contextMenuOrder: 1,
+ run: () => {
+ executeExplainQueryRef.current()
+ },
+ })
+
editor.addAction({
id: 'save-query',
label: 'Save Query',
@@ -114,6 +158,17 @@ const MonacoEditor = ({
},
})
+ editor.addAction({
+ id: 'prettify-query',
+ label: 'Prettify SQL',
+ keybindings: [monaco.KeyMod.Alt | monaco.KeyMod.Shift | monaco.KeyCode.KeyF],
+ contextMenuGroupId: 'operation',
+ contextMenuOrder: 2,
+ run: () => {
+ prettifyQueryRef.current()
+ },
+ })
+
editor.addAction({
id: 'explain-code',
label: 'Explain Code',
diff --git a/apps/studio/components/interfaces/SQLEditor/SQLEditor.tsx b/apps/studio/components/interfaces/SQLEditor/SQLEditor.tsx
index 931e9fa8627..847a3a17785 100644
--- a/apps/studio/components/interfaces/SQLEditor/SQLEditor.tsx
+++ b/apps/studio/components/interfaces/SQLEditor/SQLEditor.tsx
@@ -82,6 +82,8 @@ import {
isRoleImpersonationEnabled,
useGetImpersonatedRoleState,
} from '@/state/role-impersonation-state'
+import { SHORTCUT_IDS } from '@/state/shortcuts/registry'
+import { useShortcut } from '@/state/shortcuts/useShortcut'
import { useSidebarManagerSnapshot } from '@/state/sidebar-manager-state'
import { getSqlEditorV2StateSnapshot, useSqlEditorV2StateSnapshot } from '@/state/sql-editor-v2'
import { createTabId, useTabsStateSnapshot } from '@/state/tabs'
@@ -145,6 +147,22 @@ export const SQLEditor = () => {
})
}, [])
+ useShortcut(SHORTCUT_IDS.SQL_EDITOR_FOCUS_EDITOR, refocusEditor, {
+ registerInCommandMenu: true,
+ })
+
+ const openNewSnippet = useCallback(() => {
+ if (!ref) return
+ // skip=true bypasses the "load last visited snippet" redirect on /sql/new.
+ // Without it, the effect in pages/project/[ref]/sql/[id].tsx bounces back
+ // to the previous snippet.
+ router.push(`/project/${ref}/sql/new?skip=true`)
+ }, [ref, router])
+
+ useShortcut(SHORTCUT_IDS.SQL_EDITOR_NEW_SNIPPET, openNewSnippet, {
+ registerInCommandMenu: true,
+ })
+
const clearPendingRunRefocus = useCallback(() => {
shouldRefocusAfterRunRef.current = false
}, [])
@@ -310,6 +328,10 @@ export const SQLEditor = () => {
}
}, [id, isDiffOpen, project, snapV2])
+ useShortcut(SHORTCUT_IDS.SQL_EDITOR_FORMAT, prettifyQuery, {
+ registerInCommandMenu: true,
+ })
+
const executeQuery = useCallback(
async (force: boolean = false, sqlOverride?: string) => {
if (isDiffOpen) {
@@ -500,6 +522,10 @@ export const SQLEditor = () => {
snapV2,
])
+ useShortcut(SHORTCUT_IDS.SQL_EDITOR_EXPLAIN, executeExplainQuery, {
+ registerInCommandMenu: true,
+ })
+
const handleNewQuery = useCallback(
async (sql: string, name: string) => {
if (!ref) return console.error('Project ref is required')
@@ -925,6 +951,8 @@ export const SQLEditor = () => {
editorRef={editorRef}
monacoRef={monacoRef}
executeQuery={executeQuery}
+ executeExplainQuery={executeExplainQuery}
+ prettifyQuery={prettifyQuery}
onHasSelection={setHasSelection}
onMount={onMount}
onPrompt={({
diff --git a/apps/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityActions.tsx b/apps/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityActions.tsx
index 7c4fce807e2..d3178712ab0 100644
--- a/apps/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityActions.tsx
+++ b/apps/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityActions.tsx
@@ -1,3 +1,4 @@
+import { Hotkey } from '@tanstack/react-hotkeys'
import { LOCAL_STORAGE_KEYS, useParams } from 'common'
import { AlignLeft, Check, Heart, Keyboard, MoreVertical } from 'lucide-react'
import { toast } from 'sonner'
@@ -21,6 +22,8 @@ import { RoleImpersonationPopover } from '@/components/interfaces/RoleImpersonat
import { DatabaseSelector } from '@/components/ui/DatabaseSelector'
import { useLocalStorageQuery } from '@/hooks/misc/useLocalStorage'
import { IS_PLATFORM } from '@/lib/constants'
+import { hotkeyToKeys } from '@/state/shortcuts/formatShortcut'
+import { SHORTCUT_DEFINITIONS, SHORTCUT_IDS } from '@/state/shortcuts/registry'
import { useSqlEditorV2StateSnapshot } from '@/state/sql-editor-v2'
export type UtilityActionsProps = {
@@ -56,6 +59,10 @@ export const UtilityActions = ({
const snippet = snapV2.snippets[id]
const isFavorite = snippet !== undefined ? snippet.snippet.favorite : false
+ const hotkeySequnece: Hotkey | undefined =
+ SHORTCUT_DEFINITIONS[SHORTCUT_IDS.SQL_EDITOR_FORMAT].sequence[0]
+ const formatKeys = hotkeySequnece ? hotkeyToKeys(hotkeySequnece) : undefined
+
const toggleIntellisense = () => {
setIntellisenseEnabled(!intellisenseEnabled)
toast.success(
@@ -119,7 +126,7 @@ export const UtilityActions = ({
<AlignLeft size={14} strokeWidth={2} className="text-foreground-light" />
Prettify SQL
</span>
- <KeyboardShortcut keys={['Alt', 'Shift', 'f']} />
+ {formatKeys && <KeyboardShortcut keys={formatKeys} />}
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
@@ -180,7 +187,7 @@ export const UtilityActions = ({
<TooltipContent side="bottom" className="p-1 pl-2.5">
<div className="flex items-center gap-2.5">
<span>Prettify SQL</span>
- <KeyboardShortcut keys={['Alt', 'Shift', 'f']} />
+ {formatKeys && <KeyboardShortcut keys={formatKeys} />}
</div>
</TooltipContent>
</Tooltip>
diff --git a/apps/studio/components/ui/DownloadResultsButton.tsx b/apps/studio/components/ui/DownloadResultsButton.tsx
index 3277e12f04c..e5f2f0a89a1 100644
--- a/apps/studio/components/ui/DownloadResultsButton.tsx
+++ b/apps/studio/components/ui/DownloadResultsButton.tsx
@@ -73,7 +73,7 @@ export const DownloadResultsButton = ({
return
}
copyToClipboard(markdownData, () => {
- toast.success('Copied markdown to clipboard')
+ toast.success('Copied Markdown to clipboard')
onCopyAsMarkdown?.()
})
}
diff --git a/apps/studio/state/shortcuts/registry.ts b/apps/studio/state/shortcuts/registry.ts
index 1c6e1c66e1f..8bd41f8f968 100644
--- a/apps/studio/state/shortcuts/registry.ts
+++ b/apps/studio/state/shortcuts/registry.ts
@@ -1,3 +1,4 @@
+import { SQL_EDITOR_SHORTCUT_IDS, sqlEditorRegistry } from './registry/sql-editor'
import { TABLE_EDITOR_SHORTCUT_IDS, tableEditorRegistry } from './registry/table-editor'
import { ShortcutDefinition } from './types'
@@ -49,6 +50,9 @@ export const SHORTCUT_IDS = {
// Table editor shortcuts
...TABLE_EDITOR_SHORTCUT_IDS,
+
+ // SQL editor shortcuts
+ ...SQL_EDITOR_SHORTCUT_IDS,
} as const
/**
@@ -297,4 +301,7 @@ export const SHORTCUT_DEFINITIONS: Record<ShortcutId, ShortcutDefinition> = {
// Table editor shortcut registration
...tableEditorRegistry,
+
+ // SQL editor shortcut registration
+ ...sqlEditorRegistry,
}
diff --git a/apps/studio/state/shortcuts/registry/sql-editor.ts b/apps/studio/state/shortcuts/registry/sql-editor.ts
new file mode 100644
index 00000000000..c2d91122ed9
--- /dev/null
+++ b/apps/studio/state/shortcuts/registry/sql-editor.ts
@@ -0,0 +1,45 @@
+import { RegistryDefinations } from '../types'
+
+export const SQL_EDITOR_SHORTCUT_IDS = {
+ SQL_EDITOR_BLUR_EDITOR: 'sql-editor.blur-editor',
+ SQL_EDITOR_FOCUS_EDITOR: 'sql-editor.focus-editor',
+ SQL_EDITOR_FORMAT: 'sql-editor.format',
+ SQL_EDITOR_EXPLAIN: 'sql-editor.explain',
+ SQL_EDITOR_NEW_SNIPPET: 'sql-editor.new-snippet',
+}
+
+export type SqlEditorShortcutId =
+ (typeof SQL_EDITOR_SHORTCUT_IDS)[keyof typeof SQL_EDITOR_SHORTCUT_IDS]
+
+export const sqlEditorRegistry: RegistryDefinations<SqlEditorShortcutId> = {
+ [SQL_EDITOR_SHORTCUT_IDS.SQL_EDITOR_BLUR_EDITOR]: {
+ id: SQL_EDITOR_SHORTCUT_IDS.SQL_EDITOR_BLUR_EDITOR,
+ label: 'Blur SQL editor',
+ sequence: ['Escape'],
+ showInSettings: false,
+ },
+ [SQL_EDITOR_SHORTCUT_IDS.SQL_EDITOR_FOCUS_EDITOR]: {
+ id: SQL_EDITOR_SHORTCUT_IDS.SQL_EDITOR_FOCUS_EDITOR,
+ label: 'Focus SQL editor',
+ sequence: ['Shift+E'],
+ showInSettings: false,
+ },
+ [SQL_EDITOR_SHORTCUT_IDS.SQL_EDITOR_FORMAT]: {
+ id: SQL_EDITOR_SHORTCUT_IDS.SQL_EDITOR_FORMAT,
+ label: 'Prettify SQL',
+ sequence: ['Alt+Shift+F'],
+ showInSettings: false,
+ },
+ [SQL_EDITOR_SHORTCUT_IDS.SQL_EDITOR_EXPLAIN]: {
+ id: SQL_EDITOR_SHORTCUT_IDS.SQL_EDITOR_EXPLAIN,
+ label: 'Run EXPLAIN ANALYZE',
+ sequence: ['Mod+Shift+Enter'],
+ showInSettings: false,
+ },
+ [SQL_EDITOR_SHORTCUT_IDS.SQL_EDITOR_NEW_SNIPPET]: {
+ id: SQL_EDITOR_SHORTCUT_IDS.SQL_EDITOR_NEW_SNIPPET,
+ label: 'New SQL snippet',
+ sequence: ['Shift+N'],
+ showInSettings: false,
+ },
+}