mirror of
https://github.com/supabase/supabase.git
synced 2026-09-08 19:08:44 +08:00
## Context Adds an intellisense toggle for explorer notebooks similar to SQL editor + have QueryEditor render definitions via `useAddDefinition` <img width="259" height="162" alt="image" src="https://github.com/user-attachments/assets/278fdabd-1a24-4769-972e-1bce29060463" /> So intellisense will be running in the QueryEditor if intellisense is enabled + source selected is database, otherwise will not run. <img width="982" height="411" alt="image" src="https://github.com/user-attachments/assets/19497aa0-36fc-49ab-853d-cb938b5b18e7" /> Also updated `useAddDefinition` logic to flush the table columns + functions cache in react query - For context in the past we had users run into browser performance issues when definitions were loaded if their database is really big - Hence why we originally added this intellisense toggle - But we previously also required users to refresh the browser after disabling intellisense, as a manual way to flush the cache - So this change should remove the need to refresh the browser after disabling intellisense <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added PostgreSQL IntelliSense with definitions, formatting, and code completions in SQL editors. * Added a notebook option to enable or disable IntelliSense, with the preference saved between sessions. * Improved the notebook’s empty-state appearance. * **Bug Fixes** * Improved IntelliSense cleanup and prevented duplicate registrations when disabled. * Improved query execution state handling while background IntelliSense data loads. * **Tests** * Added coverage for shared registration, cleanup, preference persistence, and IntelliSense-related query handling. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
182 lines
6.7 KiB
TypeScript
182 lines
6.7 KiB
TypeScript
import { Monaco } from '@monaco-editor/react'
|
|
import { useQueryClient } from '@tanstack/react-query'
|
|
import { LOCAL_STORAGE_KEYS } from 'common'
|
|
import type { IDisposable } from 'monaco-editor'
|
|
import { useEffect } from 'react'
|
|
|
|
import getPgsqlCompletionProvider from '@/components/ui/CodeEditor/Providers/PgSQLCompletionProvider'
|
|
import getPgsqlSignatureHelpProvider from '@/components/ui/CodeEditor/Providers/PgSQLSignatureHelpProvider'
|
|
import { useDatabaseFunctionsQuery } from '@/data/database-functions/database-functions-query'
|
|
import { databaseKeys } from '@/data/database/keys'
|
|
import { useKeywordsQuery } from '@/data/database/keywords-query'
|
|
import { useSchemasQuery } from '@/data/database/schemas-query'
|
|
import { useTableColumnsQuery } from '@/data/database/table-columns-query'
|
|
import { useSchemasFilteredForHighAvailability } from '@/hooks/misc/useHighAvailability'
|
|
import { useLocalStorageQuery } from '@/hooks/misc/useLocalStorage'
|
|
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
|
|
import { formatSql } from '@/lib/formatSql'
|
|
import { useSqlEditorV2StateSnapshot } from '@/state/sql-editor/sql-editor-state'
|
|
|
|
/**
|
|
* Prevents double registration of Monaco providers (e.g registerCompletionItemProvider)
|
|
*/
|
|
const sharedRegistrations = new Map<string, { count: number; disposable?: IDisposable }>()
|
|
|
|
/**
|
|
* Shared across every `useAddDefinitions` instance so the registered pgsql completion/signature
|
|
* providers always read the freshest data from *any* currently active editor, not just whichever
|
|
* instance's `register()` factory happened to run first. Without this, the provider stays bound
|
|
* to a single per-instance ref: if that instance unmounts while a sibling editor is still active,
|
|
* `acquireSharedRegistration`'s ref-count doesn't reach zero (so nothing gets disposed), but the
|
|
* provider is left reading a ref that will never be written to again.
|
|
*/
|
|
const sharedPgInfoRef: { current: any } = { current: null }
|
|
|
|
export function acquireSharedRegistration(key: string, register: () => IDisposable) {
|
|
const existing = sharedRegistrations.get(key)
|
|
if (existing) {
|
|
existing.count += 1
|
|
} else {
|
|
sharedRegistrations.set(key, { count: 1, disposable: register() })
|
|
}
|
|
|
|
return () => {
|
|
const entry = sharedRegistrations.get(key)
|
|
if (!entry) return
|
|
|
|
entry.count -= 1
|
|
if (entry.count <= 0) {
|
|
entry.disposable?.dispose()
|
|
sharedRegistrations.delete(key)
|
|
}
|
|
}
|
|
}
|
|
|
|
export const useAddDefinitions = (
|
|
id: string,
|
|
monaco: Monaco | null,
|
|
{ enabled = true }: { enabled?: boolean } = {}
|
|
) => {
|
|
const { data: project } = useSelectedProjectQuery()
|
|
const snapV2 = useSqlEditorV2StateSnapshot()
|
|
const queryClient = useQueryClient()
|
|
|
|
const [intellisenseEnabled] = useLocalStorageQuery(
|
|
LOCAL_STORAGE_KEYS.SQL_EDITOR_INTELLISENSE,
|
|
true
|
|
)
|
|
|
|
const { data: keywords, isSuccess: isKeywordsSuccess } = useKeywordsQuery(
|
|
{
|
|
projectRef: project?.ref,
|
|
connectionString: project?.connectionString,
|
|
},
|
|
{ enabled: enabled && intellisenseEnabled }
|
|
)
|
|
const { data: functions, isSuccess: isFunctionsSuccess } = useDatabaseFunctionsQuery(
|
|
{
|
|
projectRef: project?.ref,
|
|
connectionString: project?.connectionString,
|
|
},
|
|
{ enabled: enabled && intellisenseEnabled }
|
|
)
|
|
const { data: schemas, isSuccess: isSchemasSuccess } = useSchemasQuery(
|
|
{
|
|
projectRef: project?.ref,
|
|
connectionString: project?.connectionString,
|
|
},
|
|
{ enabled: enabled && intellisenseEnabled }
|
|
)
|
|
const { data: tableColumns, isSuccess: isTableColumnsSuccess } = useTableColumnsQuery(
|
|
{
|
|
projectRef: project?.ref,
|
|
connectionString: project?.connectionString,
|
|
},
|
|
{ enabled: enabled && intellisenseEnabled }
|
|
)
|
|
|
|
const filteredSchemas = useSchemasFilteredForHighAvailability(schemas)
|
|
|
|
const isPgInfoReady =
|
|
enabled &&
|
|
intellisenseEnabled &&
|
|
isTableColumnsSuccess &&
|
|
isSchemasSuccess &&
|
|
isKeywordsSuccess &&
|
|
isFunctionsSuccess
|
|
|
|
// Keeps `sharedPgInfoRef` current for the registered pgsql completion/signature-help
|
|
// providers. Runs in an effect — not render — so only committed tree state writes the
|
|
// shared, module-level ref; mutating it directly during render risked a discarded or
|
|
// interrupted render pass leaking a write that no committed render ever produced.
|
|
useEffect(() => {
|
|
if (isPgInfoReady) {
|
|
if (sharedPgInfoRef.current === null) {
|
|
sharedPgInfoRef.current = {}
|
|
}
|
|
sharedPgInfoRef.current.tableColumns = tableColumns
|
|
sharedPgInfoRef.current.schemas = filteredSchemas
|
|
sharedPgInfoRef.current.keywords = keywords
|
|
sharedPgInfoRef.current.functions = functions
|
|
} else if (!intellisenseEnabled) {
|
|
// Release this instance's hold on the (potentially huge, for large databases)
|
|
// tableColumns/functions arrays so they're actually eligible for GC — see the
|
|
// cache-eviction effect below for why `enabled: false` alone isn't enough.
|
|
sharedPgInfoRef.current = null
|
|
}
|
|
}, [isPgInfoReady, intellisenseEnabled, tableColumns, filteredSchemas, keywords, functions])
|
|
|
|
// Actively evict the cached tableColumns/functions data when intellisense is turned off
|
|
useEffect(() => {
|
|
if (intellisenseEnabled) return
|
|
|
|
queryClient.removeQueries({
|
|
queryKey: databaseKeys.tableColumns(project?.ref, undefined, undefined),
|
|
exact: true,
|
|
})
|
|
queryClient.removeQueries({
|
|
queryKey: databaseKeys.databaseFunctions(project?.ref),
|
|
exact: true,
|
|
})
|
|
}, [intellisenseEnabled, project?.ref, queryClient])
|
|
|
|
// Enable pgsql format
|
|
useEffect(() => {
|
|
if (!monaco || !enabled) return
|
|
|
|
return acquireSharedRegistration('pgsql-format', () =>
|
|
monaco.languages.registerDocumentFormattingEditProvider('pgsql', {
|
|
async provideDocumentFormattingEdits(model) {
|
|
const value = model.getValue()
|
|
const formatted = formatSql(value)
|
|
if (id) snapV2.setSql({ id, sql: formatted })
|
|
return [{ range: model.getFullModelRange(), text: formatted }]
|
|
},
|
|
})
|
|
)
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [monaco, enabled])
|
|
|
|
// Register auto completion item provider for pgsql
|
|
useEffect(() => {
|
|
if (!isPgInfoReady || !monaco) return
|
|
|
|
return acquireSharedRegistration('pgsql-completion', () => {
|
|
const completeProvider = monaco.languages.registerCompletionItemProvider(
|
|
'pgsql',
|
|
getPgsqlCompletionProvider(monaco, sharedPgInfoRef)
|
|
)
|
|
const signatureHelpProvider = monaco.languages.registerSignatureHelpProvider(
|
|
'pgsql',
|
|
getPgsqlSignatureHelpProvider(monaco, sharedPgInfoRef)
|
|
)
|
|
return {
|
|
dispose: () => {
|
|
completeProvider.dispose()
|
|
signatureHelpProvider.dispose()
|
|
},
|
|
}
|
|
})
|
|
}, [isPgInfoReady, monaco])
|
|
}
|