@cloudcare/codemirror-dql 0.0.2
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/LICENSE +21 -0
- package/build.sh +7 -0
- package/dist/cjs/client/dql.d.ts +23 -0
- package/dist/cjs/client/dql.js +156 -0
- package/dist/cjs/client/dql.js.map +1 -0
- package/dist/cjs/client/index.d.ts +1 -0
- package/dist/cjs/client/index.js +6 -0
- package/dist/cjs/client/index.js.map +1 -0
- package/dist/cjs/complete/dql.terms.d.ts +20 -0
- package/dist/cjs/complete/dql.terms.js +209 -0
- package/dist/cjs/complete/dql.terms.js.map +1 -0
- package/dist/cjs/complete/hybrid.d.ts +50 -0
- package/dist/cjs/complete/hybrid.js +832 -0
- package/dist/cjs/complete/hybrid.js.map +1 -0
- package/dist/cjs/complete/index.d.ts +7 -0
- package/dist/cjs/complete/index.js +33 -0
- package/dist/cjs/complete/index.js.map +1 -0
- package/dist/cjs/dql.d.ts +16 -0
- package/dist/cjs/dql.js +66 -0
- package/dist/cjs/dql.js.map +1 -0
- package/dist/cjs/index.d.ts +2 -0
- package/dist/cjs/index.js +13 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/parser/index.d.ts +2 -0
- package/dist/cjs/parser/index.js +12 -0
- package/dist/cjs/parser/index.js.map +1 -0
- package/dist/cjs/parser/matcher.d.ts +7 -0
- package/dist/cjs/parser/matcher.js +104 -0
- package/dist/cjs/parser/matcher.js.map +1 -0
- package/dist/cjs/parser/path-finder.d.ts +5 -0
- package/dist/cjs/parser/path-finder.js +92 -0
- package/dist/cjs/parser/path-finder.js.map +1 -0
- package/dist/cjs/theme.d.ts +6 -0
- package/dist/cjs/theme.js +252 -0
- package/dist/cjs/theme.js.map +1 -0
- package/dist/cjs/types/function.d.ts +11 -0
- package/dist/cjs/types/function.js +16 -0
- package/dist/cjs/types/function.js.map +1 -0
- package/dist/cjs/types/index.d.ts +1 -0
- package/dist/cjs/types/index.js +6 -0
- package/dist/cjs/types/index.js.map +1 -0
- package/dist/cjs/types/matcher.d.ts +7 -0
- package/dist/cjs/types/matcher.js +24 -0
- package/dist/cjs/types/matcher.js.map +1 -0
- package/dist/esm/client/dql.d.ts +23 -0
- package/dist/esm/client/dql.js +150 -0
- package/dist/esm/client/dql.js.map +1 -0
- package/dist/esm/client/index.d.ts +1 -0
- package/dist/esm/client/index.js +2 -0
- package/dist/esm/client/index.js.map +1 -0
- package/dist/esm/complete/dql.terms.d.ts +20 -0
- package/dist/esm/complete/dql.terms.js +206 -0
- package/dist/esm/complete/dql.terms.js.map +1 -0
- package/dist/esm/complete/hybrid.d.ts +50 -0
- package/dist/esm/complete/hybrid.js +772 -0
- package/dist/esm/complete/hybrid.js.map +1 -0
- package/dist/esm/complete/index.d.ts +7 -0
- package/dist/esm/complete/index.js +29 -0
- package/dist/esm/complete/index.js.map +1 -0
- package/dist/esm/dql.d.ts +16 -0
- package/dist/esm/dql.js +59 -0
- package/dist/esm/dql.js.map +1 -0
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/parser/index.d.ts +2 -0
- package/dist/esm/parser/index.js +3 -0
- package/dist/esm/parser/index.js.map +1 -0
- package/dist/esm/parser/matcher.d.ts +7 -0
- package/dist/esm/parser/matcher.js +74 -0
- package/dist/esm/parser/matcher.js.map +1 -0
- package/dist/esm/parser/path-finder.d.ts +5 -0
- package/dist/esm/parser/path-finder.js +74 -0
- package/dist/esm/parser/path-finder.js.map +1 -0
- package/dist/esm/theme.d.ts +6 -0
- package/dist/esm/theme.js +249 -0
- package/dist/esm/theme.js.map +1 -0
- package/dist/esm/types/function.d.ts +11 -0
- package/dist/esm/types/function.js +13 -0
- package/dist/esm/types/function.js.map +1 -0
- package/dist/esm/types/index.d.ts +1 -0
- package/dist/esm/types/index.js +2 -0
- package/dist/esm/types/index.js.map +1 -0
- package/dist/esm/types/matcher.d.ts +7 -0
- package/dist/esm/types/matcher.js +19 -0
- package/dist/esm/types/matcher.js.map +1 -0
- package/eslint.config.mjs +40 -0
- package/package.json +60 -0
|
@@ -0,0 +1,772 @@
|
|
|
1
|
+
import { syntaxTree } from '@codemirror/language';
|
|
2
|
+
import { containsChild, isMatcher, walkBackward, getChildNodeText, createMatcher } from '../parser';
|
|
3
|
+
import { Order, SOrder, Limit, Slimit, Asc, Desc, In, NotIn, By, And, Or, DQL, EvalCallBody, ArgItem, Identifier, EqlSingle, StringLiteral, Number, FunctionCallBody, Pow, Mul, Div, Mod, Add, Sub, Eql, Gte, Gtr, Lte, Lss, InValue, AsExpr, Query, Namespace, IndexExpr, IndexItem, DataSourceList, DataSourceItem, Projection, ProjectionField, FieldName, FilterClause, FilterExpr, InFieldValue, BinaryExpr, DColon, Neq, DAnd, DOr, EqlRegex, NeqRegex, LimitClause, TimeClause, TimeRange, TimeExpr, ByClause, FieldListExpr, OrderClause, OffsetClause, SLimitClause, SOffsetClause, SOrderClause, OuterFunctionCallBody, ShowIdentifier, } from '@cloudcare/lezer-dql';
|
|
4
|
+
import { functionIdentifierTerms, matchOpTerms, binOpTerms, durationTerms, snippets, connectOpTerms } from './dql.terms';
|
|
5
|
+
const autocompleteNodes = {
|
|
6
|
+
matchOp: matchOpTerms,
|
|
7
|
+
connectOp: connectOpTerms,
|
|
8
|
+
binOp: binOpTerms,
|
|
9
|
+
duration: durationTerms,
|
|
10
|
+
functionIdentifier: functionIdentifierTerms,
|
|
11
|
+
number: [],
|
|
12
|
+
};
|
|
13
|
+
// ContextKind is the different possible value determinate by the autocompletion
|
|
14
|
+
export var ContextKind;
|
|
15
|
+
(function (ContextKind) {
|
|
16
|
+
// dynamic autocompletion (required a distant server)
|
|
17
|
+
ContextKind[ContextKind["Namespace"] = 0] = "Namespace";
|
|
18
|
+
ContextKind[ContextKind["Index"] = 1] = "Index";
|
|
19
|
+
ContextKind[ContextKind["DataSource"] = 2] = "DataSource";
|
|
20
|
+
ContextKind[ContextKind["ShowFunction"] = 3] = "ShowFunction";
|
|
21
|
+
ContextKind[ContextKind["FieldName"] = 4] = "FieldName";
|
|
22
|
+
ContextKind[ContextKind["FieldValue"] = 5] = "FieldValue";
|
|
23
|
+
// static autocompletion
|
|
24
|
+
ContextKind[ContextKind["Function"] = 6] = "Function";
|
|
25
|
+
ContextKind[ContextKind["OuterFunction"] = 7] = "OuterFunction";
|
|
26
|
+
ContextKind[ContextKind["BinOp"] = 8] = "BinOp";
|
|
27
|
+
ContextKind[ContextKind["MatchOp"] = 9] = "MatchOp";
|
|
28
|
+
ContextKind[ContextKind["ConnectOp"] = 10] = "ConnectOp";
|
|
29
|
+
ContextKind[ContextKind["Duration"] = 11] = "Duration";
|
|
30
|
+
ContextKind[ContextKind["Offset"] = 12] = "Offset";
|
|
31
|
+
ContextKind[ContextKind["SOffset"] = 13] = "SOffset";
|
|
32
|
+
ContextKind[ContextKind["Limit"] = 14] = "Limit";
|
|
33
|
+
ContextKind[ContextKind["SLimit"] = 15] = "SLimit";
|
|
34
|
+
ContextKind[ContextKind["Orderby"] = 16] = "Orderby";
|
|
35
|
+
ContextKind[ContextKind["SOrderby"] = 17] = "SOrderby";
|
|
36
|
+
ContextKind[ContextKind["By"] = 18] = "By";
|
|
37
|
+
ContextKind[ContextKind["Number"] = 19] = "Number";
|
|
38
|
+
ContextKind[ContextKind["Desc"] = 20] = "Desc";
|
|
39
|
+
ContextKind[ContextKind["Asc"] = 21] = "Asc";
|
|
40
|
+
})(ContextKind || (ContextKind = {}));
|
|
41
|
+
function trimQuotes(str) {
|
|
42
|
+
// 1. 判断字符串首尾是否是同一个配对的引号
|
|
43
|
+
if (!str)
|
|
44
|
+
return str;
|
|
45
|
+
// 支持 " ' `
|
|
46
|
+
const first = str[0];
|
|
47
|
+
const last = str[str.length - 1];
|
|
48
|
+
if (first === last && `"'\``.includes(first)) {
|
|
49
|
+
return str.slice(1, -1);
|
|
50
|
+
}
|
|
51
|
+
return str;
|
|
52
|
+
}
|
|
53
|
+
function computeStartCompleteWithQueted(node, pos) {
|
|
54
|
+
// 有范围标志符号的 比如 {a,b,c}
|
|
55
|
+
let start = node.from + 1;
|
|
56
|
+
if (node.firstChild !== null) {
|
|
57
|
+
start = pos;
|
|
58
|
+
}
|
|
59
|
+
return start;
|
|
60
|
+
}
|
|
61
|
+
function computeStartCompleteWithUnQueted(node, pos) {
|
|
62
|
+
// 没有范围标志符号的 比如 a,b,c
|
|
63
|
+
let start = node.from;
|
|
64
|
+
if (node.firstChild !== null) {
|
|
65
|
+
start = pos;
|
|
66
|
+
}
|
|
67
|
+
return start;
|
|
68
|
+
}
|
|
69
|
+
export function computeStartCompletePosition(state, node, pos) {
|
|
70
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
71
|
+
let start = node.from;
|
|
72
|
+
if ((node.type.id === Query && !containsChild(node, DataSourceList)) || node.type.id === DColon) {
|
|
73
|
+
// L:: and not container L::humber
|
|
74
|
+
start = pos;
|
|
75
|
+
}
|
|
76
|
+
else if (node.type.id === Number) {
|
|
77
|
+
if (((_a = node.parent) === null || _a === void 0 ? void 0 : _a.type.id) === TimeExpr) {
|
|
78
|
+
start = pos;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
else if (node.type.id === IndexExpr ||
|
|
82
|
+
node.type.id === InValue ||
|
|
83
|
+
node.type.id === Projection ||
|
|
84
|
+
node.type.id === FilterClause ||
|
|
85
|
+
node.type.id === InFieldValue) {
|
|
86
|
+
// IndexExpr, InValue, Projection, FilterClause, InFieldValue
|
|
87
|
+
// 这些节点的开始位置是从第一个子节点开始的
|
|
88
|
+
start = computeStartCompleteWithQueted(node, pos);
|
|
89
|
+
}
|
|
90
|
+
else if (node.type.id === DataSourceList || node.type.id === FieldListExpr) {
|
|
91
|
+
// DataSourceList
|
|
92
|
+
// 这个节点的开始位置是从第一个子节点开始的
|
|
93
|
+
start = computeStartCompleteWithUnQueted(node, pos);
|
|
94
|
+
}
|
|
95
|
+
else if ((node.type.id === OuterFunctionCallBody || node.type.id === EvalCallBody || node.type.id === FunctionCallBody) &&
|
|
96
|
+
node.firstChild === null) {
|
|
97
|
+
start++;
|
|
98
|
+
}
|
|
99
|
+
else if (node.type.id === StringLiteral &&
|
|
100
|
+
(isMatcher(node) ||
|
|
101
|
+
((_b = node.parent) === null || _b === void 0 ? void 0 : _b.type.id) === IndexItem ||
|
|
102
|
+
((_c = node.parent) === null || _c === void 0 ? void 0 : _c.type.id) === DataSourceItem ||
|
|
103
|
+
((_d = node.parent) === null || _d === void 0 ? void 0 : _d.type.id) === InValue ||
|
|
104
|
+
((_e = node.parent) === null || _e === void 0 ? void 0 : _e.type.id) === AsExpr ||
|
|
105
|
+
((_f = node.parent) === null || _f === void 0 ? void 0 : _f.type.id) === FieldName ||
|
|
106
|
+
((_g = node.parent) === null || _g === void 0 ? void 0 : _g.type.id) === InFieldValue)) {
|
|
107
|
+
start++;
|
|
108
|
+
}
|
|
109
|
+
else if (node.type.id === ByClause ||
|
|
110
|
+
node.type.id === OrderClause ||
|
|
111
|
+
node.type.id === SOrderClause ||
|
|
112
|
+
node.type.id === LimitClause ||
|
|
113
|
+
node.type.id === SLimitClause ||
|
|
114
|
+
node.type.id === OffsetClause ||
|
|
115
|
+
node.type.id === SOffsetClause) {
|
|
116
|
+
start = pos;
|
|
117
|
+
}
|
|
118
|
+
const currentText = state.doc.slice(start, pos).toString();
|
|
119
|
+
console.log('====currentText222', currentText);
|
|
120
|
+
return start;
|
|
121
|
+
}
|
|
122
|
+
function arrayToCompletionResult(data, from, to, includeSnippet = false, span = true) {
|
|
123
|
+
const options = data;
|
|
124
|
+
if (includeSnippet) {
|
|
125
|
+
options.push(...snippets);
|
|
126
|
+
}
|
|
127
|
+
return {
|
|
128
|
+
from: from,
|
|
129
|
+
to: to,
|
|
130
|
+
options: options,
|
|
131
|
+
validFor: span ? /^[a-zA-Z0-9_:]+$/ : undefined,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
function getNodeDocByExpr(tree, state, finder) {
|
|
135
|
+
const currentNode = tree.getChild(finder);
|
|
136
|
+
if (!currentNode) {
|
|
137
|
+
return '';
|
|
138
|
+
}
|
|
139
|
+
return state.sliceDoc(currentNode.from, currentNode.to);
|
|
140
|
+
}
|
|
141
|
+
function getNamespaceByQuery(tree, state) {
|
|
142
|
+
let currentNode = walkBackward(tree, Query);
|
|
143
|
+
if (!currentNode) {
|
|
144
|
+
// Weird case that shouldn't happen, because "VectorSelector" is by definition the parent of the LabelMatchers.
|
|
145
|
+
return '';
|
|
146
|
+
}
|
|
147
|
+
let namespace = '';
|
|
148
|
+
currentNode.cursor().iterate((node) => {
|
|
149
|
+
// Continue until we find the VectorSelector, then look up the metric name.
|
|
150
|
+
if (node.type.id === Namespace) {
|
|
151
|
+
namespace = getNodeDocByExpr(node.node, state, Identifier);
|
|
152
|
+
if (namespace) {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
return namespace;
|
|
158
|
+
}
|
|
159
|
+
function getIndexsByQuery(tree, state) {
|
|
160
|
+
let currentNode = walkBackward(tree, Query);
|
|
161
|
+
if (!currentNode) {
|
|
162
|
+
// Weird case that shouldn't happen, because "VectorSelector" is by definition the parent of the LabelMatchers.
|
|
163
|
+
return [];
|
|
164
|
+
}
|
|
165
|
+
let indexs = [];
|
|
166
|
+
currentNode.cursor().iterate((node) => {
|
|
167
|
+
// Continue until we find the VectorSelector, then look up the metric name.
|
|
168
|
+
if (node.type.id === IndexExpr) {
|
|
169
|
+
const IndexNodes = getChildNodeText(node.node, Identifier, StringLiteral);
|
|
170
|
+
IndexNodes.forEach((indexNode) => {
|
|
171
|
+
if (indexNode.type.id === StringLiteral) {
|
|
172
|
+
indexs.push(`${state.sliceDoc(indexNode.from, indexNode.to).slice(1, -1)}`);
|
|
173
|
+
}
|
|
174
|
+
else if (indexNode.type.id === Identifier) {
|
|
175
|
+
indexs.push(state.sliceDoc(indexNode.from, indexNode.to));
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
return indexs;
|
|
182
|
+
}
|
|
183
|
+
function getSourceListByQuery(tree, state) {
|
|
184
|
+
let currentNode = walkBackward(tree, Query);
|
|
185
|
+
if (!currentNode) {
|
|
186
|
+
// Weird case that shouldn't happen, because "VectorSelector" is by definition the parent of the LabelMatchers.
|
|
187
|
+
return [];
|
|
188
|
+
}
|
|
189
|
+
let sourcelist = [];
|
|
190
|
+
currentNode.cursor().iterate((node) => {
|
|
191
|
+
// Continue until we find the VectorSelector, then look up the metric name.
|
|
192
|
+
if (node.type.id === DataSourceList) {
|
|
193
|
+
const SourceNodes = getChildNodeText(node.node, Identifier, StringLiteral);
|
|
194
|
+
SourceNodes.forEach((sourceNode) => {
|
|
195
|
+
if (sourceNode.type.id === StringLiteral) {
|
|
196
|
+
sourcelist.push(`${state.sliceDoc(sourceNode.from, sourceNode.to).slice(1, -1)}`);
|
|
197
|
+
}
|
|
198
|
+
else if (sourceNode.type.id === Identifier) {
|
|
199
|
+
sourcelist.push(state.sliceDoc(sourceNode.from, sourceNode.to));
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
if (sourcelist.length === 0) {
|
|
206
|
+
sourcelist = ['*'];
|
|
207
|
+
}
|
|
208
|
+
return sourcelist;
|
|
209
|
+
}
|
|
210
|
+
// analyzeCompletion is going to determinate what should be autocompleted.
|
|
211
|
+
// The value of the autocompletion is then calculate by the function buildCompletion.
|
|
212
|
+
// Note: this method is exported for testing purpose only. Do not use it directly.
|
|
213
|
+
export function analyzeCompletion(state, node, pos) {
|
|
214
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
|
|
215
|
+
const result = [];
|
|
216
|
+
const currentText = state.doc.slice(node.from, pos).toString();
|
|
217
|
+
console.log(`currentText: ${currentText}`, 'nodeName: ', node.type.name, getNamespaceByQuery(node, state), getIndexsByQuery(node, state), getSourceListByQuery(node, state));
|
|
218
|
+
switch (node.type.id) {
|
|
219
|
+
case 0: {
|
|
220
|
+
// 0 is the id of the error node
|
|
221
|
+
}
|
|
222
|
+
case DColon:
|
|
223
|
+
case Query: {
|
|
224
|
+
result.push({ kind: ContextKind.DataSource, Namespace: getNamespaceByQuery(node, state), Index: getIndexsByQuery(node, state) });
|
|
225
|
+
break;
|
|
226
|
+
}
|
|
227
|
+
case DQL: {
|
|
228
|
+
if (node.firstChild !== null && node.firstChild.type.id === 0) {
|
|
229
|
+
// this situation can happen when there is nothing in the text area and the user is explicitly triggering the autocompletion (with ctrl + space)
|
|
230
|
+
result.push({ kind: ContextKind.Namespace }, { kind: ContextKind.Function }, { kind: ContextKind.ShowFunction });
|
|
231
|
+
}
|
|
232
|
+
break;
|
|
233
|
+
}
|
|
234
|
+
case IndexExpr: {
|
|
235
|
+
result.push({ kind: ContextKind.Index, Namespace: getNamespaceByQuery(node, state) });
|
|
236
|
+
break;
|
|
237
|
+
}
|
|
238
|
+
case DataSourceList: {
|
|
239
|
+
result.push({ kind: ContextKind.DataSource, Namespace: getNamespaceByQuery(node, state), Index: getIndexsByQuery(node, state) });
|
|
240
|
+
break;
|
|
241
|
+
}
|
|
242
|
+
case Projection: {
|
|
243
|
+
result.push({
|
|
244
|
+
kind: ContextKind.FieldName,
|
|
245
|
+
Namespace: getNamespaceByQuery(node, state),
|
|
246
|
+
Index: getIndexsByQuery(node, state),
|
|
247
|
+
DataSource: getSourceListByQuery(node, state),
|
|
248
|
+
});
|
|
249
|
+
break;
|
|
250
|
+
}
|
|
251
|
+
case FilterClause: {
|
|
252
|
+
// 缺少 链接符的情况 {host = 'host' }
|
|
253
|
+
// if (node.firstChild) {
|
|
254
|
+
// result.push({
|
|
255
|
+
// kind: ContextKind.ConnectOp,
|
|
256
|
+
// });
|
|
257
|
+
// } else {
|
|
258
|
+
// result.push({
|
|
259
|
+
// kind: ContextKind.FieldName,
|
|
260
|
+
// Namespace: getNamespaceByQuery(node, state),
|
|
261
|
+
// Index: getIndexsByQuery(node, state),
|
|
262
|
+
// DataSource: getSourceListByQuery(node, state),
|
|
263
|
+
// });
|
|
264
|
+
// }
|
|
265
|
+
break;
|
|
266
|
+
}
|
|
267
|
+
case InFieldValue: {
|
|
268
|
+
break;
|
|
269
|
+
}
|
|
270
|
+
case TimeClause: {
|
|
271
|
+
result.push({ kind: ContextKind.Number });
|
|
272
|
+
break;
|
|
273
|
+
}
|
|
274
|
+
case TimeRange: {
|
|
275
|
+
result.push({ kind: ContextKind.Number });
|
|
276
|
+
break;
|
|
277
|
+
}
|
|
278
|
+
case FieldListExpr: {
|
|
279
|
+
result.push({
|
|
280
|
+
kind: ContextKind.FieldName,
|
|
281
|
+
Namespace: getNamespaceByQuery(node, state),
|
|
282
|
+
Index: getIndexsByQuery(node, state),
|
|
283
|
+
DataSource: getSourceListByQuery(node, state),
|
|
284
|
+
});
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
287
|
+
case Identifier: {
|
|
288
|
+
// Identifier is the root node of the DQL language
|
|
289
|
+
// It can be used to complete the namespace, index, datasource, fieldname, fieldvalue
|
|
290
|
+
if (((_a = node.parent) === null || _a === void 0 ? void 0 : _a.type.id) === Namespace) {
|
|
291
|
+
const queryNode = walkBackward(node, Query);
|
|
292
|
+
if (queryNode === null || queryNode === void 0 ? void 0 : queryNode.getChildren(DColon).length) {
|
|
293
|
+
//
|
|
294
|
+
result.push({
|
|
295
|
+
kind: ContextKind.Namespace,
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
else {
|
|
299
|
+
result.push({
|
|
300
|
+
kind: ContextKind.Namespace,
|
|
301
|
+
}, {
|
|
302
|
+
kind: ContextKind.ShowFunction,
|
|
303
|
+
}, {
|
|
304
|
+
kind: ContextKind.OuterFunction,
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
else if (((_b = node.parent) === null || _b === void 0 ? void 0 : _b.type.id) === FieldName && ((_d = (_c = node.parent) === null || _c === void 0 ? void 0 : _c.parent) === null || _d === void 0 ? void 0 : _d.type.id) === ProjectionField) {
|
|
309
|
+
// L::xxx:(avg(), host)
|
|
310
|
+
result.push({
|
|
311
|
+
kind: ContextKind.FieldName,
|
|
312
|
+
Namespace: getNamespaceByQuery(node, state),
|
|
313
|
+
Index: getIndexsByQuery(node, state),
|
|
314
|
+
DataSource: getSourceListByQuery(node, state),
|
|
315
|
+
}, { kind: ContextKind.Function });
|
|
316
|
+
}
|
|
317
|
+
else if (((_e = node.parent) === null || _e === void 0 ? void 0 : _e.type.id) === ArgItem) {
|
|
318
|
+
//Avg(xxx)
|
|
319
|
+
result.push({
|
|
320
|
+
kind: ContextKind.FieldName,
|
|
321
|
+
Namespace: getNamespaceByQuery(node, state),
|
|
322
|
+
Index: getIndexsByQuery(node, state),
|
|
323
|
+
DataSource: getSourceListByQuery(node, state),
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
else if (((_f = node.parent) === null || _f === void 0 ? void 0 : _f.type.id) === IndexItem) {
|
|
327
|
+
result.push({
|
|
328
|
+
kind: ContextKind.Index,
|
|
329
|
+
Namespace: getNamespaceByQuery(node, state),
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
else if (((_g = node.parent) === null || _g === void 0 ? void 0 : _g.type.id) === DataSourceItem) {
|
|
333
|
+
result.push({
|
|
334
|
+
kind: ContextKind.DataSource,
|
|
335
|
+
Namespace: getNamespaceByQuery(node, state),
|
|
336
|
+
Index: getIndexsByQuery(node, state),
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
else if (((_h = node.parent) === null || _h === void 0 ? void 0 : _h.type.id) === FieldName) {
|
|
340
|
+
result.push({
|
|
341
|
+
kind: ContextKind.FieldName,
|
|
342
|
+
Namespace: getNamespaceByQuery(node, state),
|
|
343
|
+
Index: getIndexsByQuery(node, state),
|
|
344
|
+
DataSource: getSourceListByQuery(node, state),
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
else if (((_j = node.parent) === null || _j === void 0 ? void 0 : _j.type.id) === FilterExpr) {
|
|
348
|
+
result.push({
|
|
349
|
+
kind: ContextKind.FieldName,
|
|
350
|
+
Namespace: getNamespaceByQuery(node, state),
|
|
351
|
+
Index: getIndexsByQuery(node, state),
|
|
352
|
+
DataSource: getSourceListByQuery(node, state),
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
break;
|
|
356
|
+
}
|
|
357
|
+
case StringLiteral: {
|
|
358
|
+
if (((_k = node.parent) === null || _k === void 0 ? void 0 : _k.type.id) === IndexItem) {
|
|
359
|
+
result.push({
|
|
360
|
+
kind: ContextKind.Index,
|
|
361
|
+
Namespace: getNamespaceByQuery(node, state),
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
else if (((_l = node.parent) === null || _l === void 0 ? void 0 : _l.type.id) === DataSourceItem) {
|
|
365
|
+
result.push({
|
|
366
|
+
kind: ContextKind.DataSource,
|
|
367
|
+
Namespace: getNamespaceByQuery(node, state),
|
|
368
|
+
Index: getIndexsByQuery(node, state),
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
else if (((_m = node.parent) === null || _m === void 0 ? void 0 : _m.type.id) === InFieldValue) {
|
|
372
|
+
if (isMatcher(node)) {
|
|
373
|
+
result.push({
|
|
374
|
+
kind: ContextKind.FieldValue,
|
|
375
|
+
Namespace: getNamespaceByQuery(node, state),
|
|
376
|
+
Index: getIndexsByQuery(node, state),
|
|
377
|
+
DataSource: getSourceListByQuery(node, state),
|
|
378
|
+
matcher: createMatcher(node, state),
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
else if (((_o = node.parent) === null || _o === void 0 ? void 0 : _o.type.id) === FieldName) {
|
|
383
|
+
result.push({
|
|
384
|
+
kind: ContextKind.FieldName,
|
|
385
|
+
Namespace: getNamespaceByQuery(node, state),
|
|
386
|
+
Index: getIndexsByQuery(node, state),
|
|
387
|
+
DataSource: getSourceListByQuery(node, state),
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
else if (((_p = node.parent) === null || _p === void 0 ? void 0 : _p.type.id) === FilterExpr) {
|
|
391
|
+
if (isMatcher(node)) {
|
|
392
|
+
result.push({
|
|
393
|
+
kind: ContextKind.FieldValue,
|
|
394
|
+
Namespace: getNamespaceByQuery(node, state),
|
|
395
|
+
Index: getIndexsByQuery(node, state),
|
|
396
|
+
DataSource: getSourceListByQuery(node, state),
|
|
397
|
+
matcher: createMatcher(node, state),
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
else {
|
|
401
|
+
result.push({
|
|
402
|
+
kind: ContextKind.FieldName,
|
|
403
|
+
Namespace: getNamespaceByQuery(node, state),
|
|
404
|
+
Index: getIndexsByQuery(node, state),
|
|
405
|
+
DataSource: getSourceListByQuery(node, state),
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
break;
|
|
410
|
+
}
|
|
411
|
+
case Number: {
|
|
412
|
+
if (((_q = node.parent) === null || _q === void 0 ? void 0 : _q.type.id) === TimeExpr) {
|
|
413
|
+
result.push({
|
|
414
|
+
kind: ContextKind.Duration,
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
else {
|
|
418
|
+
result.push({
|
|
419
|
+
kind: ContextKind.Number,
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
break;
|
|
423
|
+
}
|
|
424
|
+
case ShowIdentifier: {
|
|
425
|
+
result.push({
|
|
426
|
+
kind: ContextKind.ShowFunction,
|
|
427
|
+
});
|
|
428
|
+
break;
|
|
429
|
+
}
|
|
430
|
+
case EvalCallBody:
|
|
431
|
+
case FunctionCallBody: {
|
|
432
|
+
break;
|
|
433
|
+
}
|
|
434
|
+
case OuterFunctionCallBody: {
|
|
435
|
+
// result.push({
|
|
436
|
+
// kind: ContextKind.Function,
|
|
437
|
+
// });
|
|
438
|
+
break;
|
|
439
|
+
}
|
|
440
|
+
case InValue: {
|
|
441
|
+
break;
|
|
442
|
+
}
|
|
443
|
+
case EqlSingle:
|
|
444
|
+
case Neq:
|
|
445
|
+
case EqlRegex:
|
|
446
|
+
case NeqRegex:
|
|
447
|
+
case In:
|
|
448
|
+
case NotIn: {
|
|
449
|
+
result.push({ kind: ContextKind.MatchOp });
|
|
450
|
+
break;
|
|
451
|
+
}
|
|
452
|
+
case By:
|
|
453
|
+
break;
|
|
454
|
+
case OrderClause: {
|
|
455
|
+
if (!node.getChildren(By).length) {
|
|
456
|
+
result.push({ kind: ContextKind.By });
|
|
457
|
+
}
|
|
458
|
+
else {
|
|
459
|
+
result.push({
|
|
460
|
+
kind: ContextKind.FieldName,
|
|
461
|
+
Namespace: getNamespaceByQuery(node, state),
|
|
462
|
+
Index: getIndexsByQuery(node, state),
|
|
463
|
+
DataSource: getSourceListByQuery(node, state),
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
break;
|
|
467
|
+
}
|
|
468
|
+
case Order: {
|
|
469
|
+
result.push({ kind: ContextKind.Orderby }, { kind: ContextKind.By });
|
|
470
|
+
break;
|
|
471
|
+
}
|
|
472
|
+
case SOrderClause: {
|
|
473
|
+
if (!node.getChildren(By).length) {
|
|
474
|
+
result.push({ kind: ContextKind.By });
|
|
475
|
+
}
|
|
476
|
+
else {
|
|
477
|
+
result.push({
|
|
478
|
+
kind: ContextKind.FieldName,
|
|
479
|
+
Namespace: getNamespaceByQuery(node, state),
|
|
480
|
+
Index: getIndexsByQuery(node, state),
|
|
481
|
+
DataSource: getSourceListByQuery(node, state),
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
break;
|
|
485
|
+
}
|
|
486
|
+
case SOrder:
|
|
487
|
+
result.push({ kind: ContextKind.SOrderby }, { kind: ContextKind.By });
|
|
488
|
+
break;
|
|
489
|
+
case Limit:
|
|
490
|
+
result.push({ kind: ContextKind.Limit });
|
|
491
|
+
break;
|
|
492
|
+
case Slimit:
|
|
493
|
+
result.push({ kind: ContextKind.SLimit });
|
|
494
|
+
break;
|
|
495
|
+
case Asc:
|
|
496
|
+
result.push({ kind: ContextKind.Asc });
|
|
497
|
+
break;
|
|
498
|
+
case Desc:
|
|
499
|
+
result.push({ kind: ContextKind.Desc });
|
|
500
|
+
break;
|
|
501
|
+
case And:
|
|
502
|
+
case DAnd:
|
|
503
|
+
case Or:
|
|
504
|
+
case DOr: {
|
|
505
|
+
result.push({ kind: ContextKind.ConnectOp });
|
|
506
|
+
break;
|
|
507
|
+
}
|
|
508
|
+
case Pow:
|
|
509
|
+
case Mul:
|
|
510
|
+
case Div:
|
|
511
|
+
case Mod:
|
|
512
|
+
case Add:
|
|
513
|
+
case Sub:
|
|
514
|
+
case Eql:
|
|
515
|
+
case Gte:
|
|
516
|
+
case Gtr:
|
|
517
|
+
case Lte:
|
|
518
|
+
case Lss:
|
|
519
|
+
result.push({ kind: ContextKind.BinOp });
|
|
520
|
+
break;
|
|
521
|
+
case BinaryExpr:
|
|
522
|
+
// if ()
|
|
523
|
+
// result.push({ kind: ContextKind.BinOp });
|
|
524
|
+
break;
|
|
525
|
+
}
|
|
526
|
+
return result;
|
|
527
|
+
}
|
|
528
|
+
export class HybridComplete {
|
|
529
|
+
constructor(dqlClient) {
|
|
530
|
+
this.dqlClient = dqlClient;
|
|
531
|
+
}
|
|
532
|
+
DQL(context) {
|
|
533
|
+
const { state, pos } = context;
|
|
534
|
+
const tree = syntaxTree(state).resolve(pos, -1);
|
|
535
|
+
// The lines above can help you to print the current lezer tree.
|
|
536
|
+
// It's useful when you are trying to understand why it doesn't autocomplete.
|
|
537
|
+
// console.log(syntaxTree(state).topNode.toString());
|
|
538
|
+
// console.log(`current node: ${tree.type.name}`);
|
|
539
|
+
const contexts = analyzeCompletion(state, tree, pos);
|
|
540
|
+
let asyncResult = Promise.resolve([]);
|
|
541
|
+
let completeSnippet = false;
|
|
542
|
+
let span = true;
|
|
543
|
+
for (const context of contexts) {
|
|
544
|
+
switch (context.kind) {
|
|
545
|
+
case ContextKind.Namespace: {
|
|
546
|
+
asyncResult = asyncResult.then((result) => {
|
|
547
|
+
return this.autocompleteNamespace(result, context);
|
|
548
|
+
});
|
|
549
|
+
break;
|
|
550
|
+
}
|
|
551
|
+
case ContextKind.Index: {
|
|
552
|
+
asyncResult = asyncResult.then((result) => {
|
|
553
|
+
return this.autocompleteIndex(result, context);
|
|
554
|
+
});
|
|
555
|
+
break;
|
|
556
|
+
}
|
|
557
|
+
case ContextKind.DataSource: {
|
|
558
|
+
asyncResult = asyncResult.then((result) => {
|
|
559
|
+
return this.autocompleteDataSource(result, context);
|
|
560
|
+
});
|
|
561
|
+
break;
|
|
562
|
+
}
|
|
563
|
+
case ContextKind.ShowFunction: {
|
|
564
|
+
asyncResult = asyncResult.then((result) => {
|
|
565
|
+
return this.autocompleteShowFuncs(result, context);
|
|
566
|
+
});
|
|
567
|
+
break;
|
|
568
|
+
}
|
|
569
|
+
case ContextKind.FieldName: {
|
|
570
|
+
asyncResult = asyncResult.then((result) => {
|
|
571
|
+
return this.autocompleteFieldNames(result, context);
|
|
572
|
+
});
|
|
573
|
+
break;
|
|
574
|
+
}
|
|
575
|
+
case ContextKind.FieldValue: {
|
|
576
|
+
asyncResult = asyncResult.then((result) => {
|
|
577
|
+
return this.autocomplteFieldValues(result, context);
|
|
578
|
+
});
|
|
579
|
+
break;
|
|
580
|
+
}
|
|
581
|
+
case ContextKind.OuterFunction: {
|
|
582
|
+
asyncResult = asyncResult.then((result) => {
|
|
583
|
+
return result.concat(autocompleteNodes.functionIdentifier);
|
|
584
|
+
});
|
|
585
|
+
break;
|
|
586
|
+
}
|
|
587
|
+
case ContextKind.Function: {
|
|
588
|
+
completeSnippet = true;
|
|
589
|
+
asyncResult = asyncResult.then((result) => {
|
|
590
|
+
return result.concat(autocompleteNodes.functionIdentifier);
|
|
591
|
+
});
|
|
592
|
+
break;
|
|
593
|
+
}
|
|
594
|
+
case ContextKind.ConnectOp: {
|
|
595
|
+
asyncResult = asyncResult.then((result) => {
|
|
596
|
+
return result.concat(autocompleteNodes.connectOp);
|
|
597
|
+
});
|
|
598
|
+
break;
|
|
599
|
+
}
|
|
600
|
+
case ContextKind.BinOp: {
|
|
601
|
+
asyncResult = asyncResult.then((result) => {
|
|
602
|
+
return result.concat(autocompleteNodes.binOp);
|
|
603
|
+
});
|
|
604
|
+
break;
|
|
605
|
+
}
|
|
606
|
+
case ContextKind.MatchOp: {
|
|
607
|
+
asyncResult = asyncResult.then((result) => {
|
|
608
|
+
return result.concat(autocompleteNodes.matchOp);
|
|
609
|
+
});
|
|
610
|
+
break;
|
|
611
|
+
}
|
|
612
|
+
case ContextKind.Duration: {
|
|
613
|
+
span = false;
|
|
614
|
+
asyncResult = asyncResult.then((result) => {
|
|
615
|
+
return result.concat(autocompleteNodes.duration);
|
|
616
|
+
});
|
|
617
|
+
break;
|
|
618
|
+
}
|
|
619
|
+
case ContextKind.Offset: {
|
|
620
|
+
asyncResult = asyncResult.then((result) => {
|
|
621
|
+
return result.concat([{ label: 'offset' }]);
|
|
622
|
+
});
|
|
623
|
+
break;
|
|
624
|
+
}
|
|
625
|
+
case ContextKind.SOffset: {
|
|
626
|
+
asyncResult = asyncResult.then((result) => {
|
|
627
|
+
return result.concat([{ label: 'soffset' }]);
|
|
628
|
+
});
|
|
629
|
+
break;
|
|
630
|
+
}
|
|
631
|
+
case ContextKind.Limit: {
|
|
632
|
+
asyncResult = asyncResult.then((result) => {
|
|
633
|
+
return result.concat([{ label: 'limit' }]);
|
|
634
|
+
});
|
|
635
|
+
break;
|
|
636
|
+
}
|
|
637
|
+
case ContextKind.SLimit: {
|
|
638
|
+
asyncResult = asyncResult.then((result) => {
|
|
639
|
+
return result.concat([{ label: 'slimit' }]);
|
|
640
|
+
});
|
|
641
|
+
break;
|
|
642
|
+
}
|
|
643
|
+
case ContextKind.Orderby: {
|
|
644
|
+
asyncResult = asyncResult.then((result) => {
|
|
645
|
+
return result.concat([{ label: 'order by' }]);
|
|
646
|
+
});
|
|
647
|
+
break;
|
|
648
|
+
}
|
|
649
|
+
case ContextKind.SOrderby: {
|
|
650
|
+
asyncResult = asyncResult.then((result) => {
|
|
651
|
+
return result.concat([{ label: 'sorder by' }]);
|
|
652
|
+
});
|
|
653
|
+
break;
|
|
654
|
+
}
|
|
655
|
+
case ContextKind.By: {
|
|
656
|
+
asyncResult = asyncResult.then((result) => {
|
|
657
|
+
return result.concat([{ label: 'by' }]);
|
|
658
|
+
});
|
|
659
|
+
break;
|
|
660
|
+
}
|
|
661
|
+
case ContextKind.Number: {
|
|
662
|
+
break;
|
|
663
|
+
}
|
|
664
|
+
case ContextKind.Asc: {
|
|
665
|
+
asyncResult = asyncResult.then((result) => {
|
|
666
|
+
return result.concat([{ label: 'asc' }]);
|
|
667
|
+
});
|
|
668
|
+
break;
|
|
669
|
+
}
|
|
670
|
+
case ContextKind.Desc: {
|
|
671
|
+
asyncResult = asyncResult.then((result) => {
|
|
672
|
+
return result.concat([{ label: 'desc' }]);
|
|
673
|
+
});
|
|
674
|
+
break;
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
return asyncResult.then((result) => {
|
|
679
|
+
return arrayToCompletionResult(result, computeStartCompletePosition(state, tree, pos), pos, completeSnippet, span);
|
|
680
|
+
});
|
|
681
|
+
}
|
|
682
|
+
autocompleteNamespace(result, context) {
|
|
683
|
+
if (!this.dqlClient)
|
|
684
|
+
return result;
|
|
685
|
+
const namespaceCompetion = [];
|
|
686
|
+
return this.dqlClient.namespace().then((namespaces) => {
|
|
687
|
+
namespaces.forEach((namespace) => {
|
|
688
|
+
namespaceCompetion.push({
|
|
689
|
+
label: namespace.name,
|
|
690
|
+
info: namespace.help,
|
|
691
|
+
type: 'constant',
|
|
692
|
+
});
|
|
693
|
+
});
|
|
694
|
+
return namespaceCompetion;
|
|
695
|
+
});
|
|
696
|
+
}
|
|
697
|
+
autocompleteIndex(result, context) {
|
|
698
|
+
if (!this.dqlClient)
|
|
699
|
+
return result;
|
|
700
|
+
const indexCompetion = [];
|
|
701
|
+
return this.dqlClient.indexValues(context.Namespace).then((indexs) => {
|
|
702
|
+
indexs.forEach((index) => {
|
|
703
|
+
indexCompetion.push({
|
|
704
|
+
label: index,
|
|
705
|
+
type: 'keyword',
|
|
706
|
+
});
|
|
707
|
+
});
|
|
708
|
+
return indexCompetion;
|
|
709
|
+
});
|
|
710
|
+
}
|
|
711
|
+
autocompleteDataSource(result, context) {
|
|
712
|
+
if (!this.dqlClient)
|
|
713
|
+
return result;
|
|
714
|
+
const datasourceCompetion = [];
|
|
715
|
+
return this.dqlClient.datasourceValues(context.Namespace, context.Index).then((datasources) => {
|
|
716
|
+
datasources.forEach((datasource) => {
|
|
717
|
+
datasourceCompetion.push({
|
|
718
|
+
label: datasource,
|
|
719
|
+
type: 'keyword',
|
|
720
|
+
});
|
|
721
|
+
});
|
|
722
|
+
return datasourceCompetion;
|
|
723
|
+
});
|
|
724
|
+
}
|
|
725
|
+
autocompleteShowFuncs(result, context) {
|
|
726
|
+
if (!this.dqlClient)
|
|
727
|
+
return result;
|
|
728
|
+
const showFuncCompetion = [];
|
|
729
|
+
return this.dqlClient.showFunctions().then((showfuns) => {
|
|
730
|
+
showfuns.forEach((showfun) => {
|
|
731
|
+
showFuncCompetion.push({
|
|
732
|
+
label: showfun.name,
|
|
733
|
+
type: 'function',
|
|
734
|
+
detail: 'function',
|
|
735
|
+
info: showfun.info,
|
|
736
|
+
});
|
|
737
|
+
});
|
|
738
|
+
return showFuncCompetion;
|
|
739
|
+
});
|
|
740
|
+
}
|
|
741
|
+
autocompleteFieldNames(result, context) {
|
|
742
|
+
if (!this.dqlClient)
|
|
743
|
+
return result;
|
|
744
|
+
const fieldNamesCompetion = [];
|
|
745
|
+
return this.dqlClient.fieldNames(context.Namespace, context.Index, context.DataSource).then((fieldNames) => {
|
|
746
|
+
fieldNames.forEach((fieldName) => {
|
|
747
|
+
fieldNamesCompetion.push({
|
|
748
|
+
label: fieldName.name,
|
|
749
|
+
detail: fieldName.type,
|
|
750
|
+
type: 'keyword',
|
|
751
|
+
info: fieldName.info,
|
|
752
|
+
});
|
|
753
|
+
});
|
|
754
|
+
return fieldNamesCompetion;
|
|
755
|
+
});
|
|
756
|
+
}
|
|
757
|
+
autocomplteFieldValues(result, context) {
|
|
758
|
+
if (!this.dqlClient)
|
|
759
|
+
return result;
|
|
760
|
+
const fieldValuesCompetion = [];
|
|
761
|
+
return this.dqlClient.fieldValues(context.Namespace, context.Index, context.DataSource, context.matcher).then((fieldValues) => {
|
|
762
|
+
fieldValues.forEach((fieldValue) => {
|
|
763
|
+
fieldValuesCompetion.push({
|
|
764
|
+
label: fieldValue,
|
|
765
|
+
type: 'text',
|
|
766
|
+
});
|
|
767
|
+
});
|
|
768
|
+
return fieldValuesCompetion;
|
|
769
|
+
});
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
//# sourceMappingURL=hybrid.js.map
|