forked from OtterMind/Chat2DB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfield.ts
More file actions
109 lines (93 loc) · 2.97 KB
/
Copy pathfield.ts
File metadata and controls
109 lines (93 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
import sqlService from '@/service/sql';
import i18n from '@/i18n';
let fieldList: Record<string, Array<{ name: string; tableName: string }>> = {};
/** 当前库下的表 */
let intelliSenseField = monaco.languages.registerCompletionItemProvider('sql', {
provideCompletionItems: () => {
return {
suggestions: [],
};
},
});
export const resetSenseField = () => {
intelliSenseField.dispose();
}
const addIntelliSenseField = async (props: {
tableName: string;
dataSourceId: number;
databaseName: string;
schemaName?: string;
}) => {
const { tableName, dataSourceId, databaseName, schemaName } = props;
if (!fieldList[tableName]) {
const data = await sqlService.getAllFieldByTable({
dataSourceId,
databaseName,
schemaName,
tableName,
});
fieldList[tableName] = data;
}
};
function checkFieldContext(text) {
const normalizedText = text.trim().toUpperCase();
const columnKeywords = ['SELECT', 'WHERE', 'AND', 'OR', 'GROUP BY', 'ORDER BY', 'SET'];
for (const keyword of columnKeywords) {
if (normalizedText.endsWith(keyword)) {
return true;
}
}
return false;
}
const registerIntelliSenseField = (tableList: string[], dataSourceId, databaseName, schemaName) => {
resetSenseField();
fieldList = {};
intelliSenseField = monaco.languages.registerCompletionItemProvider('sql', {
triggerCharacters: [' ', ',', '.', '('],
provideCompletionItems: async (model, position) => {
// 获取到当前行文本
const textUntilPosition = model.getValueInRange({
startLineNumber: position.lineNumber,
startColumn: 1,
endLineNumber: position.lineNumber,
endColumn: position.column,
});
const isFieldContext = checkFieldContext(textUntilPosition);
const match = textUntilPosition.match(/(\b\w+\b)[^\w]*$/);
let word;
if (match) {
word = match[1];
}
if (!word) {
return; // 如果没有匹配到,直接返回
}
if (word && tableList.includes(word) && !fieldList[word]) {
const data = await sqlService.getAllFieldByTable({
dataSourceId,
databaseName,
schemaName,
tableName: word,
});
fieldList[word] = data;
}
const suggestions: monaco.languages.CompletionItem[] = Object.keys(fieldList).reduce((acc, cur) => {
const arr = fieldList[cur].map((fieldObj) => ({
label: {
label: fieldObj.name,
detail: `(${fieldObj.tableName})`,
description: i18n('sqlEditor.text.fieldName'),
},
kind: monaco.languages.CompletionItemKind.Field,
insertText: fieldObj.name,
sortText: isFieldContext ? '01' : '08',
}));
return [...acc, ...arr];
}, []);
return {
suggestions,
};
},
});
};
export { intelliSenseField, registerIntelliSenseField, addIntelliSenseField };