11import { acceptCompletion } from "@codemirror/autocomplete" ;
2- import { PostgreSQL , sql } from "@codemirror/lang-sql" ;
2+ import { keywordCompletionSource , PostgreSQL , sql } from "@codemirror/lang-sql" ;
33import { keymap } from "@codemirror/view" ;
4+
45import { basicSetup , EditorView } from "codemirror" ;
56import { cteCompletionSource } from "../src/sql/cte-completion-source.js" ;
67import { sqlExtension } from "../src/sql/extension.js" ;
8+ import { DefaultSqlTooltipRenders } from "../src/sql/hover.js" ;
79import { type Schema , tableTooltipRenderer } from "./custom-renderers.js" ;
810
911// Default SQL content for the demo
@@ -130,6 +132,19 @@ const defaultKeymap = [
130132 } ,
131133] ;
132134
135+ // e.g. lazily load keyword docs
136+ const getKeywordDocs = async ( ) => {
137+ // For compatibility with Vite and other bundlers, `import` returns a JS module and not a JSON object
138+ // So we need to nest the keywords under a json key to access them,
139+ // otherwise a keyword can conflict with a JS reserved keyword (e.g. `default` or `with`)
140+ const keywords = await import ( "@marimo-team/codemirror-sql/data/common-keywords.json" ) ;
141+ const duckdbKeywords = await import ( "@marimo-team/codemirror-sql/data/duckdb-keywords.json" ) ;
142+ return {
143+ ...keywords . default . keywords ,
144+ ...duckdbKeywords . default . keywords ,
145+ } ;
146+ } ;
147+
133148// Initialize the SQL editor
134149function initializeEditor ( ) {
135150 const extensions = [
@@ -142,6 +157,26 @@ function initializeEditor() {
142157 schema : schema ,
143158 // Enable uppercase keywords for more traditional SQL style
144159 upperCaseKeywords : true ,
160+ keywordCompletion : ( label , type ) => {
161+ // console.log("label", label, type);
162+ return {
163+ label,
164+ keyword : label ,
165+ info : async ( ) => {
166+ const dom = document . createElement ( "div" ) ;
167+ const keywordDocs = await getKeywordDocs ( ) ;
168+ const description = keywordDocs [ label . toLocaleLowerCase ( ) ] ;
169+ if ( ! description ) {
170+ return null ;
171+ }
172+ dom . innerHTML = DefaultSqlTooltipRenders . keyword ( {
173+ keyword : label ,
174+ info : description ,
175+ } ) ;
176+ return dom ;
177+ } ,
178+ } ;
179+ } ,
145180 } ) ,
146181 sqlExtension ( {
147182 // Linter extension configuration
@@ -164,8 +199,8 @@ function initializeEditor() {
164199 enableTables : true , // Show table information
165200 enableColumns : true , // Show column information
166201 keywords : async ( ) => {
167- const keywords = await import ( "../src/data/duckdb-keywords.json" ) ;
168- return keywords . default ;
202+ const keywords = await getKeywordDocs ( ) ;
203+ return keywords ;
169204 } ,
170205 tooltipRenderers : {
171206 // Custom renderer for tables
0 commit comments