|
| 1 | +import type { SQLDialect } from "@codemirror/lang-sql"; |
| 2 | +import type { Extension } from "@codemirror/state"; |
| 3 | +import { aliasColumnCompletionSource } from "./alias-completion-source.js"; |
| 4 | +import { unqualifiedColumnCompletionSource } from "./column-completion-source.js"; |
| 5 | +import { createCteCompletionSource } from "./cte-completion-source.js"; |
| 6 | +import { NodeSqlParser } from "./parser.js"; |
| 7 | +import { QueryContextAnalyzer } from "./query-context.js"; |
| 8 | +import type { SqlSchemaSource } from "./schema-facet.js"; |
| 9 | +import type { SqlParser } from "./types.js"; |
| 10 | + |
| 11 | +/** |
| 12 | + * Configuration for {@link sqlCompletion}, the convenience helper that |
| 13 | + * registers every schema-aware SQL completion source at once. |
| 14 | + */ |
| 15 | +export interface SqlCompletionConfig { |
| 16 | + /** |
| 17 | + * The SQL dialect whose language the completion sources are registered on |
| 18 | + * (e.g. `PostgreSQL`, or a dialect from `./dialects`). This must match the |
| 19 | + * dialect passed to `sql({ dialect })` for the sources to activate. |
| 20 | + */ |
| 21 | + dialect: SQLDialect; |
| 22 | + /** |
| 23 | + * Database schema to complete columns from. Falls back to the shared |
| 24 | + * `sqlSchemaFacet` when not provided. Not used by CTE completion, which |
| 25 | + * derives columns from the statement itself. |
| 26 | + */ |
| 27 | + schema?: SqlSchemaSource; |
| 28 | + /** |
| 29 | + * Custom SQL parser shared by all completion sources. Defaults to a new |
| 30 | + * `NodeSqlParser`. Pass the same instance used by the linter/hover so |
| 31 | + * dialect-specific setups only configure the parser once. |
| 32 | + */ |
| 33 | + parser?: SqlParser; |
| 34 | + /** |
| 35 | + * Query-context analyzer shared by all completion sources, so each edit is |
| 36 | + * analyzed once. Defaults to one built from `parser`. |
| 37 | + */ |
| 38 | + contextAnalyzer?: QueryContextAnalyzer; |
| 39 | + |
| 40 | + /** Whether to enable CTE name/column completion (default: true) */ |
| 41 | + enableCteCompletion?: boolean; |
| 42 | + /** Whether to enable alias-qualified column completion (default: true) */ |
| 43 | + enableAliasCompletion?: boolean; |
| 44 | + /** Whether to enable unqualified column completion (default: true) */ |
| 45 | + enableColumnCompletion?: boolean; |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Registers every schema-aware SQL completion source in one call, so you don't |
| 50 | + * have to wire up each `dialect.language.data.of({ autocomplete })` by hand: |
| 51 | + * - {@link createCteCompletionSource} — CTE names and their output columns |
| 52 | + * - {@link aliasColumnCompletionSource} — `u.` → columns of `users` in |
| 53 | + * `SELECT ... FROM users u` |
| 54 | + * - {@link unqualifiedColumnCompletionSource} — `SELECT e` → `email` from the |
| 55 | + * statement's FROM/JOIN tables |
| 56 | + * |
| 57 | + * A single parser and query-context analyzer are shared across the sources so |
| 58 | + * each edit is analyzed only once. This complements `sqlExtension`, which |
| 59 | + * covers linting, hover, gutter, and navigation but not completion. |
| 60 | + * |
| 61 | + * @example |
| 62 | + * ```ts |
| 63 | + * import { sql, PostgreSQL } from '@codemirror/lang-sql'; |
| 64 | + * import { sqlCompletion } from '@marimo-team/codemirror-sql'; |
| 65 | + * |
| 66 | + * const schema = { users: ['id', 'name', 'email'] }; |
| 67 | + * const extensions = [ |
| 68 | + * sql({ dialect: PostgreSQL, schema }), |
| 69 | + * sqlCompletion({ dialect: PostgreSQL, schema }), |
| 70 | + * ]; |
| 71 | + * ``` |
| 72 | + */ |
| 73 | +export function sqlCompletion(config: SqlCompletionConfig): Extension[] { |
| 74 | + const { |
| 75 | + dialect, |
| 76 | + schema, |
| 77 | + parser = new NodeSqlParser(), |
| 78 | + enableCteCompletion = true, |
| 79 | + enableAliasCompletion = true, |
| 80 | + enableColumnCompletion = true, |
| 81 | + } = config; |
| 82 | + const contextAnalyzer = config.contextAnalyzer ?? new QueryContextAnalyzer(parser); |
| 83 | + |
| 84 | + const extensions: Extension[] = []; |
| 85 | + |
| 86 | + if (enableCteCompletion) { |
| 87 | + extensions.push( |
| 88 | + dialect.language.data.of({ |
| 89 | + autocomplete: createCteCompletionSource({ parser, contextAnalyzer }), |
| 90 | + }), |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + if (enableAliasCompletion) { |
| 95 | + extensions.push( |
| 96 | + dialect.language.data.of({ |
| 97 | + autocomplete: aliasColumnCompletionSource({ schema, parser, contextAnalyzer }), |
| 98 | + }), |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + if (enableColumnCompletion) { |
| 103 | + extensions.push( |
| 104 | + dialect.language.data.of({ |
| 105 | + autocomplete: unqualifiedColumnCompletionSource({ schema, parser, contextAnalyzer }), |
| 106 | + }), |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + return extensions; |
| 111 | +} |
0 commit comments