Skip to content

Commit f7cce74

Browse files
authored
demo: add keyword completion, fixup tests (#15)
1 parent 58e4eee commit f7cce74

4 files changed

Lines changed: 510 additions & 427 deletions

File tree

demo/index.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { acceptCompletion } from "@codemirror/autocomplete";
2-
import { PostgreSQL, sql } from "@codemirror/lang-sql";
2+
import { keywordCompletionSource, PostgreSQL, sql } from "@codemirror/lang-sql";
33
import { keymap } from "@codemirror/view";
4+
45
import { basicSetup, EditorView } from "codemirror";
56
import { cteCompletionSource } from "../src/sql/cte-completion-source.js";
67
import { sqlExtension } from "../src/sql/extension.js";
8+
import { DefaultSqlTooltipRenders } from "../src/sql/hover.js";
79
import { 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
134149
function 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

Comments
 (0)