Skip to content

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

Add prettify feature in sql editor

Joshen Limcfb3c658adff
diff --git a/studio/components/interfaces/SQLEditor/SQLEditor.tsx b/studio/components/interfaces/SQLEditor/SQLEditor.tsx
index ba145a7528d..ad5816d7139 100644
--- a/studio/components/interfaces/SQLEditor/SQLEditor.tsx
+++ b/studio/components/interfaces/SQLEditor/SQLEditor.tsx
@@ -26,6 +26,8 @@ import { useSqlTitleGenerateMutation } from 'data/ai/sql-title-mutation'
import { SqlSnippet } from 'data/content/sql-snippets-query'
import { useEntityDefinitionsQuery } from 'data/database/entity-definitions-query'
import { useExecuteSqlMutation } from 'data/sql/execute-sql-mutation'
+import { useFormatQueryMutation } from 'data/sql/format-sql-query'
+import { useProjectSubscriptionV2Query } from 'data/subscriptions/project-subscription-v2-query'
import { isError } from 'data/utils/error-check'
import {
useFlag,
@@ -40,6 +42,7 @@ import { removeCommentsFromSql, uuidv4 } from 'lib/helpers'
import { useProfile } from 'lib/profile'
import Telemetry from 'lib/telemetry'
import { getSqlEditorStateSnapshot, useSqlEditorStateSnapshot } from 'state/sql-editor'
+import { subscriptionHasHipaaAddon } from '../BillingV2/Subscription/Subscription.utils'
import AISchemaSuggestionPopover from './AISchemaSuggestionPopover'
import AISettingsModal from './AISettingsModal'
import {
@@ -60,8 +63,6 @@ import {
getDiffTypeDropdownLabel,
} from './SQLEditor.utils'
import UtilityPanel from './UtilityPanel/UtilityPanel'
-import { subscriptionHasHipaaAddon } from '../BillingV2/Subscription/Subscription.utils'
-import { useProjectSubscriptionV2Query } from 'data/subscriptions/project-subscription-v2-query'
// Load the monaco editor client-side only (does not behave well server-side)
const MonacoEditor = dynamic(() => import('./MonacoEditor'), { ssr: false })
@@ -98,6 +99,7 @@ const SQLEditor = () => {
const project = useSelectedProject()
const snap = useSqlEditorStateSnapshot()
+ const { mutate: formatQuery } = useFormatQueryMutation()
const { mutateAsync: generateSql, isLoading: isGenerateSqlLoading } = useSqlGenerateMutation()
const { mutateAsync: editSql, isLoading: isEditSqlLoading } = useSqlEditMutation()
const { mutateAsync: titleSql } = useSqlTitleGenerateMutation()
@@ -203,6 +205,44 @@ const SQLEditor = () => {
[generateSqlTitle, snap]
)
+ const prettifyQuery = useCallback(async () => {
+ if (isDiffOpen) return
+
+ // use the latest state
+ const state = getSqlEditorStateSnapshot()
+ const snippet = state.snippets[id]
+
+ if (editorRef.current && project) {
+ const editor = editorRef.current
+ const selection = editor.getSelection()
+ const selectedValue = selection ? editor.getModel()?.getValueInRange(selection) : undefined
+ const sql = snippet
+ ? (selectedValue || editorRef.current?.getValue()) ?? snippet.snippet.content.sql
+ : selectedValue || editorRef.current?.getValue()
+ formatQuery(
+ {
+ projectRef: project.ref,
+ connectionString: project.connectionString,
+ sql,
+ },
+ {
+ onSuccess: (res) => {
+ const editorModel = editorRef?.current?.getModel()
+ if (editorRef.current && editorModel) {
+ editorRef.current.executeEdits('apply-prettify-edit', [
+ {
+ text: res.result,
+ range: editorModel.getFullModelRange(),
+ },
+ ])
+ snap.setSql(id, res.result)
+ }
+ },
+ }
+ )
+ }
+ }, [formatQuery, id, isDiffOpen, project, snap])
+
const executeQuery = useCallback(
async (force: boolean = false) => {
if (isDiffOpen) return
@@ -886,6 +926,7 @@ const SQLEditor = () => {
id={id}
isExecuting={isExecuting}
isDisabled={isDiffOpen}
+ prettifyQuery={prettifyQuery}
executeQuery={executeQuery}
/>
)}
diff --git a/studio/components/interfaces/SQLEditor/UtilityPanel/SizeToggleButton.tsx b/studio/components/interfaces/SQLEditor/UtilityPanel/SizeToggleButton.tsx
index a63c91b1be9..4df607c54dd 100644
--- a/studio/components/interfaces/SQLEditor/UtilityPanel/SizeToggleButton.tsx
+++ b/studio/components/interfaces/SQLEditor/UtilityPanel/SizeToggleButton.tsx
@@ -1,6 +1,6 @@
-import { Button, IconChevronDown, IconChevronUp } from 'ui'
-import { useSqlEditorStateSnapshot } from 'state/sql-editor'
import * as Tooltip from '@radix-ui/react-tooltip'
+import { useSqlEditorStateSnapshot } from 'state/sql-editor'
+import { Button, IconMaximize2, IconMinimize2 } from 'ui'
export interface SizeToggleButton {
id: string
@@ -22,7 +22,7 @@ const SizeToggleButton = ({ id }: SizeToggleButton) => {
type="text"
size="tiny"
onClick={restorePanelSize}
- icon={<IconChevronUp className="text-gray-1100" size="tiny" strokeWidth={2} />}
+ icon={<IconMaximize2 className="text-gray-1100" size="tiny" strokeWidth={2} />}
/>
</Tooltip.Trigger>
<Tooltip.Portal>
@@ -46,7 +46,7 @@ const SizeToggleButton = ({ id }: SizeToggleButton) => {
type="text"
size="tiny"
onClick={maximizeEditor}
- icon={<IconChevronDown className="text-gray-1100" size="tiny" strokeWidth={2} />}
+ icon={<IconMinimize2 className="text-gray-1100" size="tiny" strokeWidth={2} />}
/>
</Tooltip.Trigger>
<Tooltip.Portal>
diff --git a/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityActions.tsx b/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityActions.tsx
index ff73bbb09c2..1e19d344e13 100644
--- a/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityActions.tsx
+++ b/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityActions.tsx
@@ -1,14 +1,17 @@
-import { Button, IconCommand, IconCornerDownLeft } from 'ui'
import { IS_PLATFORM } from 'lib/constants'
+import { detectOS } from 'lib/helpers'
+import { Button, IconAlignLeft, IconCommand, IconCornerDownLeft } from 'ui'
+import * as Tooltip from '@radix-ui/react-tooltip'
+
import FavoriteButton from './FavoriteButton'
import SavingIndicator from './SavingIndicator'
import SizeToggleButton from './SizeToggleButton'
-import { detectOS } from 'lib/helpers'
export type UtilityActionsProps = {
id: string
isExecuting?: boolean
isDisabled?: boolean
+ prettifyQuery: () => void
executeQuery: () => void
}
@@ -16,6 +19,7 @@ const UtilityActions = ({
id,
isExecuting = false,
isDisabled = false,
+ prettifyQuery,
executeQuery,
}: UtilityActionsProps) => {
const os = detectOS()
@@ -25,6 +29,28 @@ const UtilityActions = ({
<SavingIndicator id={id} />
{IS_PLATFORM && <FavoriteButton id={id} />}
<SizeToggleButton id={id} />
+ <Tooltip.Root delayDuration={0}>
+ <Tooltip.Trigger asChild>
+ <Button
+ type="text"
+ onClick={() => prettifyQuery()}
+ icon={<IconAlignLeft size="tiny" strokeWidth={2} className="text-gray-1100" />}
+ />
+ </Tooltip.Trigger>
+ <Tooltip.Portal>
+ <Tooltip.Content side="bottom">
+ <Tooltip.Arrow className="radix-tooltip-arrow" />
+ <div
+ className={[
+ 'rounded bg-scale-100 py-1 px-2 leading-none shadow',
+ 'border border-scale-200',
+ ].join(' ')}
+ >
+ <span className="text-xs text-scale-1200">Prettify SQL</span>
+ </div>
+ </Tooltip.Content>
+ </Tooltip.Portal>
+ </Tooltip.Root>
<Button
onClick={() => executeQuery()}
disabled={isDisabled || isExecuting}
diff --git a/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityPanel.tsx b/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityPanel.tsx
index 59537d7b637..1c96bd69099 100644
--- a/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityPanel.tsx
+++ b/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityPanel.tsx
@@ -6,10 +6,17 @@ export type UtilityPanelProps = {
id: string
isExecuting?: boolean
isDisabled?: boolean
+ prettifyQuery: () => void
executeQuery: () => void
}
-const UtilityPanel = ({ id, isExecuting, isDisabled, executeQuery }: UtilityPanelProps) => {
+const UtilityPanel = ({
+ id,
+ isExecuting,
+ isDisabled,
+ prettifyQuery,
+ executeQuery,
+}: UtilityPanelProps) => {
return (
<>
<div className="flex justify-between overflow-visible px-6 py-2">
@@ -20,6 +27,7 @@ const UtilityPanel = ({ id, isExecuting, isDisabled, executeQuery }: UtilityPane
id={id}
isExecuting={isExecuting}
isDisabled={isDisabled}
+ prettifyQuery={prettifyQuery}
executeQuery={executeQuery}
/>
</div>
diff --git a/studio/state/sql-editor.ts b/studio/state/sql-editor.ts
index 0f16cdd6d4a..fdc98e1b0ab 100644
--- a/studio/state/sql-editor.ts
+++ b/studio/state/sql-editor.ts
@@ -1,11 +1,12 @@
-import { upsertContent, UpsertContentPayload } from 'data/content/content-upsert-mutation'
-import { SqlSnippet } from 'data/content/sql-snippets-query'
import { debounce, memoize } from 'lodash'
import { useMemo } from 'react'
-import { SqlSnippets } from 'types'
import { proxy, snapshot, subscribe, useSnapshot } from 'valtio'
import { devtools, proxySet } from 'valtio/utils'
+import { upsertContent, UpsertContentPayload } from 'data/content/content-upsert-mutation'
+import { SqlSnippet } from 'data/content/sql-snippets-query'
+import { SqlSnippets } from 'types'
+
export type StateSnippet = {
snippet: SqlSnippet
splitSizes: number[]