@sqlrooms/sql-editor 0.7.0 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +210 -92
- package/dist/SqlEditor.d.ts.map +1 -1
- package/dist/SqlEditor.js +5 -5
- package/dist/SqlEditor.js.map +1 -1
- package/dist/SqlEditorModal.js +1 -1
- package/dist/SqlEditorModal.js.map +1 -1
- package/dist/SqlEditorSlice.js +10 -10
- package/dist/SqlEditorSlice.js.map +1 -1
- package/dist/SqlQueryDataSourcesPanel.js +3 -3
- package/dist/SqlQueryDataSourcesPanel.js.map +1 -1
- package/dist/TablesList.js +2 -2
- package/dist/TablesList.js.map +1 -1
- package/dist/hooks/useQueryTabManagement.d.ts.map +1 -1
- package/dist/hooks/useQueryTabManagement.js +1 -1
- package/dist/hooks/useQueryTabManagement.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/package.json +10 -10
- package/dist/SqlEditorSliceConfig.d.ts +0 -53
- package/dist/SqlEditorSliceConfig.d.ts.map +0 -1
- package/dist/SqlEditorSliceConfig.js +0 -15
- package/dist/SqlEditorSliceConfig.js.map +0 -1
- package/dist/components/internal/SqlMonacoEditor.d.ts +0 -36
- package/dist/components/internal/SqlMonacoEditor.d.ts.map +0 -1
- package/dist/components/internal/SqlMonacoEditor.js +0 -219
- package/dist/components/internal/SqlMonacoEditor.js.map +0 -1
- package/dist/constants/duckdb.d.ts +0 -73
- package/dist/constants/duckdb.d.ts.map +0 -1
- package/dist/constants/duckdb.js +0 -392
- package/dist/constants/duckdb.js.map +0 -1
|
@@ -1,219 +0,0 @@
|
|
|
1
|
-
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { useCallback, useEffect, useRef } from 'react';
|
|
3
|
-
import { MonacoEditor } from '@sqlrooms/monaco-editor';
|
|
4
|
-
import { DUCKDB_KEYWORDS, DUCKDB_FUNCTIONS, SQL_LANGUAGE_CONFIGURATION, } from '../../constants/duckdb-dialect';
|
|
5
|
-
/**
|
|
6
|
-
* A Monaco editor for editing SQL with DuckDB syntax highlighting and autocompletion
|
|
7
|
-
* This is an internal component used by SqlEditor
|
|
8
|
-
*/
|
|
9
|
-
export const SqlMonacoEditor = ({ customKeywords = [], customFunctions = [], tableSchemas = {}, tableSamples = {}, getLatestSchemas, onMount, className, ...props }) => {
|
|
10
|
-
// Store references to editor and monaco
|
|
11
|
-
const editorRef = useRef(null);
|
|
12
|
-
const monacoRef = useRef(null);
|
|
13
|
-
const disposableRef = useRef(null);
|
|
14
|
-
// Function to register the completion provider
|
|
15
|
-
const registerCompletionProvider = useCallback(() => {
|
|
16
|
-
if (!editorRef.current || !monacoRef.current)
|
|
17
|
-
return;
|
|
18
|
-
const monaco = monacoRef.current;
|
|
19
|
-
// Dispose previous provider if it exists
|
|
20
|
-
if (disposableRef.current) {
|
|
21
|
-
disposableRef.current.dispose();
|
|
22
|
-
}
|
|
23
|
-
// Register SQL completion provider
|
|
24
|
-
const disposable = monaco.languages.registerCompletionItemProvider('sql', {
|
|
25
|
-
triggerCharacters: [' ', '.', ',', '(', '='],
|
|
26
|
-
provideCompletionItems: (model, position) => {
|
|
27
|
-
try {
|
|
28
|
-
// Get the latest schemas and samples if the callback is provided
|
|
29
|
-
let currentSchemas = tableSchemas;
|
|
30
|
-
let currentSamples = tableSamples;
|
|
31
|
-
if (getLatestSchemas) {
|
|
32
|
-
const latest = getLatestSchemas();
|
|
33
|
-
currentSchemas = latest.tableSchemas;
|
|
34
|
-
currentSamples = latest.tableSamples;
|
|
35
|
-
}
|
|
36
|
-
const suggestions = [];
|
|
37
|
-
const word = model.getWordUntilPosition(position);
|
|
38
|
-
const range = {
|
|
39
|
-
startLineNumber: position.lineNumber,
|
|
40
|
-
endLineNumber: position.lineNumber,
|
|
41
|
-
startColumn: word.startColumn,
|
|
42
|
-
endColumn: word.endColumn,
|
|
43
|
-
};
|
|
44
|
-
// Get the text before the cursor to determine context
|
|
45
|
-
const lineContent = model.getLineContent(position.lineNumber);
|
|
46
|
-
const textBeforeCursor = lineContent
|
|
47
|
-
.substring(0, position.column - 1)
|
|
48
|
-
.trim()
|
|
49
|
-
.toLowerCase();
|
|
50
|
-
// Check if we're after a FROM, JOIN, or similar clause to prioritize table suggestions
|
|
51
|
-
const isTableContext = /\b(from|join|into|update|table)\s+\w*$/.test(textBeforeCursor);
|
|
52
|
-
// Check if we're after a table name and period to prioritize column suggestions
|
|
53
|
-
const isColumnContext = /\b(\w+)\.\w*$/.test(textBeforeCursor);
|
|
54
|
-
// Combine keywords and functions with custom ones
|
|
55
|
-
const keywords = [...DUCKDB_KEYWORDS, ...customKeywords];
|
|
56
|
-
const functions = [...DUCKDB_FUNCTIONS, ...customFunctions];
|
|
57
|
-
// Add keyword suggestions (if not in a specific context)
|
|
58
|
-
if (!isColumnContext) {
|
|
59
|
-
keywords.forEach((keyword) => {
|
|
60
|
-
suggestions.push({
|
|
61
|
-
label: keyword,
|
|
62
|
-
kind: monaco.languages.CompletionItemKind.Keyword,
|
|
63
|
-
insertText: keyword,
|
|
64
|
-
range: range,
|
|
65
|
-
detail: 'Keyword',
|
|
66
|
-
sortText: isTableContext ? 'z' + keyword : 'a' + keyword, // Lower priority in table context
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
// Add function suggestions (if not in a specific context)
|
|
71
|
-
if (!isColumnContext) {
|
|
72
|
-
functions.forEach((func) => {
|
|
73
|
-
suggestions.push({
|
|
74
|
-
label: func,
|
|
75
|
-
kind: monaco.languages.CompletionItemKind.Function,
|
|
76
|
-
insertText: func,
|
|
77
|
-
range: range,
|
|
78
|
-
detail: 'Function',
|
|
79
|
-
sortText: isTableContext ? 'z' + func : 'b' + func, // Lower priority in table context
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
// Add table and column suggestions from schemas
|
|
84
|
-
Object.entries(currentSchemas).forEach(([tableName, columns]) => {
|
|
85
|
-
// Get sample data for this table if available
|
|
86
|
-
const samples = currentSamples[tableName] || [];
|
|
87
|
-
const sampleText = samples.length > 0 ? `\nSample data:\n${samples.join('\n')}` : '';
|
|
88
|
-
// Add table suggestion
|
|
89
|
-
suggestions.push({
|
|
90
|
-
label: tableName,
|
|
91
|
-
kind: monaco.languages.CompletionItemKind.Class,
|
|
92
|
-
insertText: tableName,
|
|
93
|
-
range: range,
|
|
94
|
-
detail: 'Table',
|
|
95
|
-
documentation: {
|
|
96
|
-
value: `Table: ${tableName}${sampleText}`,
|
|
97
|
-
isTrusted: true,
|
|
98
|
-
},
|
|
99
|
-
sortText: isTableContext ? 'a' + tableName : 'c' + tableName, // Higher priority in table context
|
|
100
|
-
});
|
|
101
|
-
// Extract table name from context if we're in a column context
|
|
102
|
-
let contextTableName = '';
|
|
103
|
-
if (isColumnContext) {
|
|
104
|
-
const match = textBeforeCursor.match(/\b(\w+)\.\w*$/);
|
|
105
|
-
if (match && match[1]) {
|
|
106
|
-
contextTableName = match[1];
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
// Only add columns for the current table if we're in a column context
|
|
110
|
-
if (!isColumnContext || contextTableName === tableName) {
|
|
111
|
-
// Add column suggestions
|
|
112
|
-
Object.entries(columns).forEach(([columnName, columnType]) => {
|
|
113
|
-
// Find sample values for this column if available
|
|
114
|
-
const columnSample = samples.find((s) => s.startsWith(`${columnName}:`));
|
|
115
|
-
const sampleInfo = columnSample
|
|
116
|
-
? `\nSample: ${columnSample.split(':')[1]?.trim() || ''}`
|
|
117
|
-
: '';
|
|
118
|
-
suggestions.push({
|
|
119
|
-
label: columnName,
|
|
120
|
-
kind: monaco.languages.CompletionItemKind.Field,
|
|
121
|
-
insertText: columnName,
|
|
122
|
-
range: range,
|
|
123
|
-
detail: `Column (${columnType})`,
|
|
124
|
-
documentation: {
|
|
125
|
-
value: `Column from table ${tableName}${sampleInfo}`,
|
|
126
|
-
isTrusted: true,
|
|
127
|
-
},
|
|
128
|
-
sortText: isColumnContext && contextTableName === tableName
|
|
129
|
-
? 'a' + columnName
|
|
130
|
-
: 'd' + columnName,
|
|
131
|
-
});
|
|
132
|
-
// Only add table.column suggestions if not in a column context
|
|
133
|
-
if (!isColumnContext) {
|
|
134
|
-
suggestions.push({
|
|
135
|
-
label: `${tableName}.${columnName}`,
|
|
136
|
-
kind: monaco.languages.CompletionItemKind.Field,
|
|
137
|
-
insertText: `${tableName}.${columnName}`,
|
|
138
|
-
range: range,
|
|
139
|
-
detail: `Column (${columnType})`,
|
|
140
|
-
documentation: {
|
|
141
|
-
value: `Column from table ${tableName}${sampleInfo}`,
|
|
142
|
-
isTrusted: true,
|
|
143
|
-
},
|
|
144
|
-
sortText: 'e' + tableName + columnName,
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
});
|
|
150
|
-
return {
|
|
151
|
-
suggestions,
|
|
152
|
-
};
|
|
153
|
-
}
|
|
154
|
-
catch (error) {
|
|
155
|
-
console.error('Error in SQL completion provider:', error);
|
|
156
|
-
return { suggestions: [] };
|
|
157
|
-
}
|
|
158
|
-
},
|
|
159
|
-
});
|
|
160
|
-
// Store the disposable to clean up later
|
|
161
|
-
disposableRef.current = disposable;
|
|
162
|
-
}, [
|
|
163
|
-
customKeywords,
|
|
164
|
-
customFunctions,
|
|
165
|
-
tableSchemas,
|
|
166
|
-
tableSamples,
|
|
167
|
-
getLatestSchemas,
|
|
168
|
-
]);
|
|
169
|
-
// Re-register completion provider when tableSchemas or tableSamples change
|
|
170
|
-
useEffect(() => {
|
|
171
|
-
if (editorRef.current && monacoRef.current) {
|
|
172
|
-
registerCompletionProvider();
|
|
173
|
-
}
|
|
174
|
-
}, [tableSchemas, tableSamples, registerCompletionProvider]);
|
|
175
|
-
// Handle editor mounting to configure SQL language features
|
|
176
|
-
const handleEditorDidMount = useCallback((editor, monaco) => {
|
|
177
|
-
// Store references
|
|
178
|
-
editorRef.current = editor;
|
|
179
|
-
monacoRef.current = monaco;
|
|
180
|
-
// Register SQL language if not already registered
|
|
181
|
-
if (!monaco.languages.getLanguages().some((lang) => lang.id === 'sql')) {
|
|
182
|
-
monaco.languages.register({ id: 'sql' });
|
|
183
|
-
}
|
|
184
|
-
// Combine keywords and functions with custom ones
|
|
185
|
-
const keywords = [...DUCKDB_KEYWORDS, ...customKeywords];
|
|
186
|
-
const functions = [...DUCKDB_FUNCTIONS, ...customFunctions];
|
|
187
|
-
// Set the language configuration
|
|
188
|
-
monaco.languages.setMonarchTokensProvider('sql', {
|
|
189
|
-
...SQL_LANGUAGE_CONFIGURATION,
|
|
190
|
-
keywords,
|
|
191
|
-
builtinFunctions: functions,
|
|
192
|
-
}); // Using 'as any' to bypass the type checking issue
|
|
193
|
-
// Register the completion provider
|
|
194
|
-
registerCompletionProvider();
|
|
195
|
-
// Store the disposable to clean up later if needed
|
|
196
|
-
editor.onDidDispose(() => {
|
|
197
|
-
if (disposableRef.current) {
|
|
198
|
-
disposableRef.current.dispose();
|
|
199
|
-
}
|
|
200
|
-
});
|
|
201
|
-
// Call the original onMount if provided
|
|
202
|
-
if (onMount) {
|
|
203
|
-
onMount(editor, monaco);
|
|
204
|
-
}
|
|
205
|
-
}, [
|
|
206
|
-
customKeywords,
|
|
207
|
-
customFunctions,
|
|
208
|
-
tableSchemas,
|
|
209
|
-
tableSamples,
|
|
210
|
-
onMount,
|
|
211
|
-
registerCompletionProvider,
|
|
212
|
-
]);
|
|
213
|
-
return (_jsx(MonacoEditor, { language: "sql", onMount: handleEditorDidMount, className: className, options: {
|
|
214
|
-
formatOnPaste: true,
|
|
215
|
-
formatOnType: true,
|
|
216
|
-
wordWrap: 'on',
|
|
217
|
-
}, ...props }));
|
|
218
|
-
};
|
|
219
|
-
//# sourceMappingURL=SqlMonacoEditor.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"SqlMonacoEditor.js","sourceRoot":"","sources":["../../../src/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CAAC;QAE7B,mDAAmD;QACnD,MAAM,CAAC,YAAY,CAAC,GAAG,EAAE;YACvB,IAAI,aAAa,CAAC,OAAO,EAAE,CAAC;gBAC1B,aAAa,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC;YAClC,CAAC;QACH,CAAC,CAAC,CAAC;QAEH,wCAAwC;QACxC,IAAI,OAAO,EAAE,CAAC;YACZ,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAC1B,CAAC;IACH,CAAC,EACD;QACE,cAAc;QACd,eAAe;QACf,YAAY;QACZ,YAAY;QACZ,OAAO;QACP,0BAA0B;KAC3B,CACF,CAAC;IAEF,OAAO,CACL,KAAC,YAAY,IACX,QAAQ,EAAC,KAAK,EACd,OAAO,EAAE,oBAAoB,EAC7B,SAAS,EAAE,SAAS,EACpB,OAAO,EAAE;YACP,aAAa,EAAE,IAAI;YACnB,YAAY,EAAE,IAAI;YAClB,QAAQ,EAAE,IAAI;SACf,KACG,KAAK,GACT,CACH,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import React, {useCallback, useEffect, useRef} from 'react';\nimport {MonacoEditor} from '@sqlrooms/monaco-editor';\nimport type {MonacoEditorProps} from '@sqlrooms/monaco-editor';\nimport type {OnMount} from '@monaco-editor/react';\nimport type * as Monaco from 'monaco-editor';\nimport {\n DUCKDB_KEYWORDS,\n DUCKDB_FUNCTIONS,\n SQL_LANGUAGE_CONFIGURATION,\n} from '../../constants/duckdb-dialect';\n\nexport interface SqlMonacoEditorProps\n extends Omit<MonacoEditorProps, 'language'> {\n /**\n * Custom SQL keywords to add to the completion provider\n */\n customKeywords?: string[];\n /**\n * Custom SQL functions to add to the completion provider\n */\n customFunctions?: string[];\n /**\n * Table schemas for autocompletion\n * Format: { tableName: { columnName: columnType, ... }, ... }\n */\n tableSchemas?: Record<string, Record<string, string>>;\n /**\n * Sample data for tables to enhance autocompletion\n * Format: { tableName: [sampleDescription1, sampleDescription2, ...], ... }\n */\n tableSamples?: Record<string, string[]>;\n /**\n * Callback to get the latest table schemas and samples\n * This is called from within provideCompletionItems to ensure we have the latest data\n */\n getLatestSchemas?: () => {\n tableSchemas: Record<string, Record<string, string>>;\n tableSamples: Record<string, string[]>;\n };\n}\n\n/**\n * A Monaco editor for editing SQL with DuckDB syntax highlighting and autocompletion\n * This is an internal component used by SqlEditor\n */\nexport const SqlMonacoEditor: React.FC<SqlMonacoEditorProps> = ({\n customKeywords = [],\n customFunctions = [],\n tableSchemas = {},\n tableSamples = {},\n getLatestSchemas,\n onMount,\n className,\n ...props\n}) => {\n // Store references to editor and monaco\n const editorRef = useRef<any>(null);\n const monacoRef = useRef<any>(null);\n const disposableRef = useRef<any>(null);\n\n // Function to register the completion provider\n const registerCompletionProvider = useCallback(() => {\n if (!editorRef.current || !monacoRef.current) return;\n\n const monaco = monacoRef.current;\n\n // Dispose previous provider if it exists\n if (disposableRef.current) {\n disposableRef.current.dispose();\n }\n\n // Register SQL completion provider\n const disposable = monaco.languages.registerCompletionItemProvider('sql', {\n triggerCharacters: [' ', '.', ',', '(', '='],\n provideCompletionItems: (model: any, position: any) => {\n try {\n // Get the latest schemas and samples if the callback is provided\n let currentSchemas = tableSchemas;\n let currentSamples = tableSamples;\n\n if (getLatestSchemas) {\n const latest = getLatestSchemas();\n currentSchemas = latest.tableSchemas;\n currentSamples = latest.tableSamples;\n }\n\n const suggestions: Monaco.languages.CompletionItem[] = [];\n const word = model.getWordUntilPosition(position);\n const range = {\n startLineNumber: position.lineNumber,\n endLineNumber: position.lineNumber,\n startColumn: word.startColumn,\n endColumn: word.endColumn,\n };\n\n // Get the text before the cursor to determine context\n const lineContent = model.getLineContent(position.lineNumber);\n const textBeforeCursor = lineContent\n .substring(0, position.column - 1)\n .trim()\n .toLowerCase();\n\n // Check if we're after a FROM, JOIN, or similar clause to prioritize table suggestions\n const isTableContext = /\\b(from|join|into|update|table)\\s+\\w*$/.test(\n textBeforeCursor,\n );\n\n // Check if we're after a table name and period to prioritize column suggestions\n const isColumnContext = /\\b(\\w+)\\.\\w*$/.test(textBeforeCursor);\n\n // Combine keywords and functions with custom ones\n const keywords = [...DUCKDB_KEYWORDS, ...customKeywords];\n const functions = [...DUCKDB_FUNCTIONS, ...customFunctions];\n\n // Add keyword suggestions (if not in a specific context)\n if (!isColumnContext) {\n keywords.forEach((keyword) => {\n suggestions.push({\n label: keyword,\n kind: monaco.languages.CompletionItemKind.Keyword,\n insertText: keyword,\n range: range,\n detail: 'Keyword',\n sortText: isTableContext ? 'z' + keyword : 'a' + keyword, // Lower priority in table context\n });\n });\n }\n\n // Add function suggestions (if not in a specific context)\n if (!isColumnContext) {\n functions.forEach((func) => {\n suggestions.push({\n label: func,\n kind: monaco.languages.CompletionItemKind.Function,\n insertText: func,\n range: range,\n detail: 'Function',\n sortText: isTableContext ? 'z' + func : 'b' + func, // Lower priority in table context\n });\n });\n }\n\n // Add table and column suggestions from schemas\n Object.entries(currentSchemas).forEach(([tableName, columns]) => {\n // Get sample data for this table if available\n const samples = currentSamples[tableName] || [];\n const sampleText =\n samples.length > 0 ? `\\nSample data:\\n${samples.join('\\n')}` : '';\n\n // Add table suggestion\n suggestions.push({\n label: tableName,\n kind: monaco.languages.CompletionItemKind.Class,\n insertText: tableName,\n range: range,\n detail: 'Table',\n documentation: {\n value: `Table: ${tableName}${sampleText}`,\n isTrusted: true,\n },\n sortText: isTableContext ? 'a' + tableName : 'c' + tableName, // Higher priority in table context\n });\n\n // Extract table name from context if we're in a column context\n let contextTableName = '';\n if (isColumnContext) {\n const match = textBeforeCursor.match(/\\b(\\w+)\\.\\w*$/);\n if (match && match[1]) {\n contextTableName = match[1];\n }\n }\n\n // Only add columns for the current table if we're in a column context\n if (!isColumnContext || contextTableName === tableName) {\n // Add column suggestions\n Object.entries(columns).forEach(([columnName, columnType]) => {\n // Find sample values for this column if available\n const columnSample = samples.find((s) =>\n s.startsWith(`${columnName}:`),\n );\n const sampleInfo = columnSample\n ? `\\nSample: ${columnSample.split(':')[1]?.trim() || ''}`\n : '';\n\n suggestions.push({\n label: columnName,\n kind: monaco.languages.CompletionItemKind.Field,\n insertText: columnName,\n range: range,\n detail: `Column (${columnType})`,\n documentation: {\n value: `Column from table ${tableName}${sampleInfo}`,\n isTrusted: true,\n },\n sortText:\n isColumnContext && contextTableName === tableName\n ? 'a' + columnName\n : 'd' + columnName,\n });\n\n // Only add table.column suggestions if not in a column context\n if (!isColumnContext) {\n suggestions.push({\n label: `${tableName}.${columnName}`,\n kind: monaco.languages.CompletionItemKind.Field,\n insertText: `${tableName}.${columnName}`,\n range: range,\n detail: `Column (${columnType})`,\n documentation: {\n value: `Column from table ${tableName}${sampleInfo}`,\n isTrusted: true,\n },\n sortText: 'e' + tableName + columnName,\n });\n }\n });\n }\n });\n\n return {\n suggestions,\n };\n } catch (error) {\n console.error('Error in SQL completion provider:', error);\n return {suggestions: []};\n }\n },\n });\n\n // Store the disposable to clean up later\n disposableRef.current = disposable;\n }, [\n customKeywords,\n customFunctions,\n tableSchemas,\n tableSamples,\n getLatestSchemas,\n ]);\n\n // Re-register completion provider when tableSchemas or tableSamples change\n useEffect(() => {\n if (editorRef.current && monacoRef.current) {\n registerCompletionProvider();\n }\n }, [tableSchemas, tableSamples, registerCompletionProvider]);\n\n // Handle editor mounting to configure SQL language features\n const handleEditorDidMount = useCallback<OnMount>(\n (editor, monaco) => {\n // Store references\n editorRef.current = editor;\n monacoRef.current = monaco;\n\n // Register SQL language if not already registered\n if (\n !monaco.languages.getLanguages().some((lang: any) => lang.id === 'sql')\n ) {\n monaco.languages.register({id: 'sql'});\n }\n\n // Combine keywords and functions with custom ones\n const keywords = [...DUCKDB_KEYWORDS, ...customKeywords];\n const functions = [...DUCKDB_FUNCTIONS, ...customFunctions];\n\n // Set the language configuration\n monaco.languages.setMonarchTokensProvider('sql', {\n ...SQL_LANGUAGE_CONFIGURATION,\n keywords,\n builtinFunctions: functions,\n } as any); // Using 'as any' to bypass the type checking issue\n\n // Register the completion provider\n registerCompletionProvider();\n\n // Store the disposable to clean up later if needed\n editor.onDidDispose(() => {\n if (disposableRef.current) {\n disposableRef.current.dispose();\n }\n });\n\n // Call the original onMount if provided\n if (onMount) {\n onMount(editor, monaco);\n }\n },\n [\n customKeywords,\n customFunctions,\n tableSchemas,\n tableSamples,\n onMount,\n registerCompletionProvider,\n ],\n );\n\n return (\n <MonacoEditor\n language=\"sql\"\n onMount={handleEditorDidMount}\n className={className}\n options={{\n formatOnPaste: true,\n formatOnType: true,\n wordWrap: 'on',\n }}\n {...props}\n />\n );\n};\n"]}
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* DuckDB SQL language constants for Monaco Editor
|
|
3
|
-
*/
|
|
4
|
-
export declare const DUCKDB_KEYWORDS: string[];
|
|
5
|
-
export declare const DUCKDB_FUNCTIONS: string[];
|
|
6
|
-
export declare const SQL_OPERATORS: string[];
|
|
7
|
-
export declare const SQL_VARIABLES: string[];
|
|
8
|
-
export declare const SQL_PSEUDO_COLUMNS: string[];
|
|
9
|
-
export declare const SQL_LANGUAGE_CONFIGURATION: {
|
|
10
|
-
defaultToken: string;
|
|
11
|
-
tokenPostfix: string;
|
|
12
|
-
ignoreCase: boolean;
|
|
13
|
-
brackets: {
|
|
14
|
-
open: string;
|
|
15
|
-
close: string;
|
|
16
|
-
token: string;
|
|
17
|
-
}[];
|
|
18
|
-
keywords: string[];
|
|
19
|
-
operators: string[];
|
|
20
|
-
builtinFunctions: string[];
|
|
21
|
-
builtinVariables: string[];
|
|
22
|
-
pseudoColumns: string[];
|
|
23
|
-
tokenizer: {
|
|
24
|
-
root: ((string | RegExp)[] | {
|
|
25
|
-
include: string;
|
|
26
|
-
} | (RegExp | {
|
|
27
|
-
cases: {
|
|
28
|
-
'@keywords': string;
|
|
29
|
-
'@operators': string;
|
|
30
|
-
'@builtinFunctions': string;
|
|
31
|
-
'@builtinVariables': string;
|
|
32
|
-
'@pseudoColumns': string;
|
|
33
|
-
'@default': string;
|
|
34
|
-
};
|
|
35
|
-
})[])[];
|
|
36
|
-
whitespace: (string | RegExp)[][];
|
|
37
|
-
comments: ((string | RegExp)[] | (RegExp | {
|
|
38
|
-
token: string;
|
|
39
|
-
next: string;
|
|
40
|
-
})[])[];
|
|
41
|
-
comment: ((string | RegExp)[] | (RegExp | {
|
|
42
|
-
token: string;
|
|
43
|
-
next: string;
|
|
44
|
-
})[])[];
|
|
45
|
-
numbers: (string | RegExp)[][];
|
|
46
|
-
strings: (RegExp | {
|
|
47
|
-
token: string;
|
|
48
|
-
next: string;
|
|
49
|
-
})[][];
|
|
50
|
-
string: ((string | RegExp)[] | (RegExp | {
|
|
51
|
-
token: string;
|
|
52
|
-
next: string;
|
|
53
|
-
})[])[];
|
|
54
|
-
stringDouble: ((string | RegExp)[] | (RegExp | {
|
|
55
|
-
token: string;
|
|
56
|
-
next: string;
|
|
57
|
-
})[])[];
|
|
58
|
-
complexIdentifiers: (RegExp | {
|
|
59
|
-
token: string;
|
|
60
|
-
next: string;
|
|
61
|
-
})[][];
|
|
62
|
-
bracketedIdentifier: ((string | RegExp)[] | (RegExp | {
|
|
63
|
-
token: string;
|
|
64
|
-
next: string;
|
|
65
|
-
})[])[];
|
|
66
|
-
quotedIdentifier: ((string | RegExp)[] | (RegExp | {
|
|
67
|
-
token: string;
|
|
68
|
-
next: string;
|
|
69
|
-
})[])[];
|
|
70
|
-
scopes: never[];
|
|
71
|
-
};
|
|
72
|
-
};
|
|
73
|
-
//# sourceMappingURL=duckdb.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"duckdb.d.ts","sourceRoot":"","sources":["../../src/constants/duckdb.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,eAAO,MAAM,eAAe,UA6H3B,CAAC;AAGF,eAAO,MAAM,gBAAgB,UAgI5B,CAAC;AAGF,eAAO,MAAM,aAAa,UAyBzB,CAAC;AAGF,eAAO,MAAM,aAAa,UAUzB,CAAC;AAGF,eAAO,MAAM,kBAAkB,UAK9B,CAAC;AAGF,eAAO,MAAM,0BAA0B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAsFtC,CAAC"}
|