|
| 1 | +import { DatabaseTypeCode } from '@/constants'; |
| 2 | +import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'; |
| 3 | +import i18n from '@/i18n'; |
| 4 | + |
| 5 | +/** 当前库下的表 */ |
| 6 | +let intelliSenseView = monaco.languages.registerCompletionItemProvider('sql', { |
| 7 | + provideCompletionItems: (model, position) => { |
| 8 | + return { |
| 9 | + suggestions: [], |
| 10 | + }; |
| 11 | + }, |
| 12 | +}); |
| 13 | + |
| 14 | +const checkViewContext = (text) => { |
| 15 | + const normalizedText = text.trim().toUpperCase(); |
| 16 | + const tableKeywords = ['FROM', 'JOIN', 'INNER JOIN', 'LEFT JOIN', 'RIGHT JOIN', 'UPDATE']; |
| 17 | + |
| 18 | + for (const keyword of tableKeywords) { |
| 19 | + if (normalizedText.endsWith(keyword)) { |
| 20 | + return true; |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + return false; |
| 25 | +}; |
| 26 | + |
| 27 | +const registerIntelliSenseView = ( |
| 28 | + viewList: string[], |
| 29 | + databaseName?: string | null, |
| 30 | +) => { |
| 31 | + intelliSenseView.dispose(); |
| 32 | + intelliSenseView = monaco.languages.registerCompletionItemProvider('sql', { |
| 33 | + triggerCharacters: [' '], |
| 34 | + provideCompletionItems: (model, position) => { |
| 35 | + const lineContentUntilPosition = model.getValueInRange({ |
| 36 | + startLineNumber: position.lineNumber, |
| 37 | + startColumn: 1, |
| 38 | + endLineNumber: position.lineNumber, |
| 39 | + endColumn: position.column, |
| 40 | + }); |
| 41 | + |
| 42 | + const isViewContext = checkViewContext(lineContentUntilPosition); |
| 43 | + |
| 44 | + return { |
| 45 | + suggestions: (viewList || []).map((viewName) => { |
| 46 | + return { |
| 47 | + label: { |
| 48 | + label: viewName, |
| 49 | + detail: databaseName ? `(${databaseName})` : null, |
| 50 | + description: i18n('sqlEditor.text.viewName'), |
| 51 | + }, |
| 52 | + kind: monaco.languages.CompletionItemKind.Unit, |
| 53 | + insertText: viewName, |
| 54 | + // range: monaco.Range.fromPositions(position), |
| 55 | + // documentation: tableName.comment, |
| 56 | + sortText: isViewContext ? '01' : '08' |
| 57 | + }; |
| 58 | + }), |
| 59 | + }; |
| 60 | + }, |
| 61 | + }); |
| 62 | +}; |
| 63 | + |
| 64 | +export { intelliSenseView, registerIntelliSenseView }; |
0 commit comments