@mhjuma/traverse 0.2.0 → 0.3.0
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/CHANGELOG.md +30 -0
- package/package.json +7 -4
- package/src/components/Explorer/Explorer.spec.tsx +11 -3
- package/src/components/Explorer/Explorer.tsx +11 -0
- package/src/components/ExplorerWorkspace/ExplorerWorkspace.spec.tsx +53 -17
- package/src/components/ExplorerWorkspace/ExplorerWorkspace.tsx +78 -74
- package/src/components/ExplorerWorkspace/bootstrapKustoLanguage.spec.ts +150 -0
- package/src/components/ExplorerWorkspace/bootstrapKustoLanguage.ts +95 -0
- package/src/components/ExplorerWorkspace/kustoSchemaQueue.spec.ts +322 -0
- package/src/components/ExplorerWorkspace/kustoSchemaQueue.ts +188 -0
- package/src/components/ExplorerWorkspace/monacoLoader.spec.ts +82 -0
- package/src/components/ExplorerWorkspace/monacoLoader.ts +57 -0
- package/src/components/ExplorerWorkspace/monacoWorkers.spec.ts +81 -0
- package/src/components/ExplorerWorkspace/monacoWorkers.ts +90 -0
- package/src/index.ts +2 -0
- package/src/services/schema.spec.ts +107 -28
- package/src/services/schema.ts +66 -25
- package/src/test-setup.ts +3 -0
- package/vitest.config.ts +7 -0
- package/src/components/ExplorerWorkspace/kqlLanguage.spec.ts +0 -321
- package/src/components/ExplorerWorkspace/kqlLanguage.ts +0 -155
|
@@ -1,321 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
-
import { getSchema } from '../../services/schema';
|
|
3
|
-
import { registerKqlLanguage, setKqlSchemaResolver } from './kqlLanguage';
|
|
4
|
-
|
|
5
|
-
vi.mock('../../services/schema', () => ({
|
|
6
|
-
getSchema: vi.fn().mockReturnValue([
|
|
7
|
-
{
|
|
8
|
-
name: 'TestTable',
|
|
9
|
-
folder: '',
|
|
10
|
-
description: '',
|
|
11
|
-
kind: 'table',
|
|
12
|
-
columns: [
|
|
13
|
-
{ name: 'Col1', type: 'string' },
|
|
14
|
-
{ name: 'Col2', type: 'int' },
|
|
15
|
-
],
|
|
16
|
-
},
|
|
17
|
-
{
|
|
18
|
-
name: 'OtherTable',
|
|
19
|
-
folder: '',
|
|
20
|
-
description: '',
|
|
21
|
-
kind: 'table',
|
|
22
|
-
columns: [
|
|
23
|
-
{ name: 'ColA', type: 'string' },
|
|
24
|
-
],
|
|
25
|
-
},
|
|
26
|
-
]),
|
|
27
|
-
KQL_FUNCTIONS: ['where', 'project', 'extend', 'summarize', 'take', 'top'],
|
|
28
|
-
KQL_AGGREGATIONS: ['count', 'sum', 'avg', 'min', 'max'],
|
|
29
|
-
KQL_SCALAR_FUNCTIONS: ['ago', 'now', 'bin', 'strlen', 'tostring'],
|
|
30
|
-
}));
|
|
31
|
-
|
|
32
|
-
interface MockMonacoLanguage {
|
|
33
|
-
id: string;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function createMockMonaco() {
|
|
37
|
-
const registeredLanguages: MockMonacoLanguage[] = [];
|
|
38
|
-
let monarchProvider: Record<string, unknown> | null = null;
|
|
39
|
-
let completionProvider: { provideCompletionItems: (model: unknown, position: unknown) => unknown } | null = null;
|
|
40
|
-
|
|
41
|
-
return {
|
|
42
|
-
languages: {
|
|
43
|
-
getLanguages: () => registeredLanguages,
|
|
44
|
-
register: (lang: MockMonacoLanguage) => { registeredLanguages.push(lang); },
|
|
45
|
-
setMonarchTokensProvider: (_id: string, provider: Record<string, unknown>) => { monarchProvider = provider; },
|
|
46
|
-
registerCompletionItemProvider: (_id: string, provider: { provideCompletionItems: (model: unknown, position: unknown) => unknown }) => { completionProvider = provider; },
|
|
47
|
-
CompletionItemKind: {
|
|
48
|
-
Keyword: 14,
|
|
49
|
-
Function: 1,
|
|
50
|
-
Field: 5,
|
|
51
|
-
Class: 7,
|
|
52
|
-
},
|
|
53
|
-
},
|
|
54
|
-
_getMonarchProvider: () => monarchProvider,
|
|
55
|
-
_getCompletionProvider: () => completionProvider,
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
describe('registerKqlLanguage', () => {
|
|
60
|
-
let mockMonaco: ReturnType<typeof createMockMonaco>;
|
|
61
|
-
|
|
62
|
-
beforeEach(() => {
|
|
63
|
-
mockMonaco = createMockMonaco();
|
|
64
|
-
setKqlSchemaResolver(() => getSchema());
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it('registers kql language', () => {
|
|
68
|
-
registerKqlLanguage(mockMonaco as never);
|
|
69
|
-
const langs = mockMonaco.languages.getLanguages();
|
|
70
|
-
expect(langs.some((l) => l.id === 'kql')).toBe(true);
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it('does not register twice', () => {
|
|
74
|
-
registerKqlLanguage(mockMonaco as never);
|
|
75
|
-
registerKqlLanguage(mockMonaco as never);
|
|
76
|
-
|
|
77
|
-
const kqlCount = mockMonaco.languages.getLanguages().filter((l) => l.id === 'kql').length;
|
|
78
|
-
expect(kqlCount).toBe(1);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
describe('Monarch tokenizer', () => {
|
|
82
|
-
it('sets up monarch provider with expected structure', () => {
|
|
83
|
-
registerKqlLanguage(mockMonaco as never);
|
|
84
|
-
const provider = mockMonaco._getMonarchProvider();
|
|
85
|
-
|
|
86
|
-
expect(provider).not.toBeNull();
|
|
87
|
-
expect(provider?.['ignoreCase']).toBe(true);
|
|
88
|
-
expect(provider?.['keywords']).toEqual(expect.arrayContaining(['where', 'project']));
|
|
89
|
-
expect(provider?.['aggregations']).toEqual(expect.arrayContaining(['count', 'sum']));
|
|
90
|
-
expect(provider?.['scalarFunctions']).toEqual(expect.arrayContaining(['ago', 'now']));
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
it('has operators array with comparison operators', () => {
|
|
94
|
-
registerKqlLanguage(mockMonaco as never);
|
|
95
|
-
const provider = mockMonaco._getMonarchProvider();
|
|
96
|
-
const operators = provider?.['operators'] as string[];
|
|
97
|
-
|
|
98
|
-
expect(operators).toContain('==');
|
|
99
|
-
expect(operators).toContain('!=');
|
|
100
|
-
expect(operators).toContain('has');
|
|
101
|
-
expect(operators).toContain('contains');
|
|
102
|
-
expect(operators).toContain('and');
|
|
103
|
-
expect(operators).toContain('or');
|
|
104
|
-
expect(operators).toContain('not');
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
it('has tokenizer with root rules', () => {
|
|
108
|
-
registerKqlLanguage(mockMonaco as never);
|
|
109
|
-
const provider = mockMonaco._getMonarchProvider();
|
|
110
|
-
const tokenizer = provider?.['tokenizer'] as { root: unknown[] };
|
|
111
|
-
|
|
112
|
-
expect(tokenizer).toBeDefined();
|
|
113
|
-
expect(tokenizer.root).toBeInstanceOf(Array);
|
|
114
|
-
expect(tokenizer.root.length).toBeGreaterThan(0);
|
|
115
|
-
});
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
describe('Completion provider', () => {
|
|
119
|
-
it('registers a completion provider with trigger characters', () => {
|
|
120
|
-
registerKqlLanguage(mockMonaco as never);
|
|
121
|
-
const provider = mockMonaco._getCompletionProvider();
|
|
122
|
-
expect(provider).not.toBeNull();
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
it('provides KQL function suggestions after pipe', () => {
|
|
126
|
-
registerKqlLanguage(mockMonaco as never);
|
|
127
|
-
const provider = mockMonaco._getCompletionProvider()!;
|
|
128
|
-
|
|
129
|
-
const model = {
|
|
130
|
-
getWordUntilPosition: () => ({ startColumn: 1, endColumn: 1, word: '' }),
|
|
131
|
-
getLineContent: (line: number) => line === 1 ? '| ' : '',
|
|
132
|
-
getLineCount: () => 1,
|
|
133
|
-
};
|
|
134
|
-
const position = { lineNumber: 1, column: 3 };
|
|
135
|
-
|
|
136
|
-
const result = provider.provideCompletionItems(model, position) as { suggestions: { label: string }[] };
|
|
137
|
-
const labels = result.suggestions.map((s) => s.label);
|
|
138
|
-
expect(labels).toContain('where');
|
|
139
|
-
expect(labels).toContain('project');
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
it('provides column suggestions after column operator', () => {
|
|
143
|
-
registerKqlLanguage(mockMonaco as never);
|
|
144
|
-
const provider = mockMonaco._getCompletionProvider()!;
|
|
145
|
-
|
|
146
|
-
const model = {
|
|
147
|
-
getWordUntilPosition: () => ({ startColumn: 12, endColumn: 12, word: '' }),
|
|
148
|
-
getLineContent: (line: number) => {
|
|
149
|
-
if (line === 1) {
|
|
150
|
-
return 'TestTable';
|
|
151
|
-
}
|
|
152
|
-
if (line === 2) {
|
|
153
|
-
return '| where ';
|
|
154
|
-
}
|
|
155
|
-
return '';
|
|
156
|
-
},
|
|
157
|
-
getLineCount: () => 2,
|
|
158
|
-
};
|
|
159
|
-
const position = { lineNumber: 2, column: 9 };
|
|
160
|
-
|
|
161
|
-
const result = provider.provideCompletionItems(model, position) as { suggestions: { label: string; detail?: string }[] };
|
|
162
|
-
const labels = result.suggestions.map((s) => s.label);
|
|
163
|
-
expect(labels).toContain('Col1');
|
|
164
|
-
expect(labels).toContain('Col2');
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
it('provides table suggestions in general context', () => {
|
|
168
|
-
registerKqlLanguage(mockMonaco as never);
|
|
169
|
-
const provider = mockMonaco._getCompletionProvider()!;
|
|
170
|
-
|
|
171
|
-
const model = {
|
|
172
|
-
getWordUntilPosition: () => ({ startColumn: 1, endColumn: 1, word: '' }),
|
|
173
|
-
getLineContent: () => '',
|
|
174
|
-
getLineCount: () => 1,
|
|
175
|
-
};
|
|
176
|
-
const position = { lineNumber: 1, column: 1 };
|
|
177
|
-
|
|
178
|
-
const result = provider.provideCompletionItems(model, position) as { suggestions: { label: string }[] };
|
|
179
|
-
const labels = result.suggestions.map((s) => s.label);
|
|
180
|
-
expect(labels).toContain('TestTable');
|
|
181
|
-
expect(labels).toContain('OtherTable');
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
it('deduplicates suggestions by label', () => {
|
|
185
|
-
registerKqlLanguage(mockMonaco as never);
|
|
186
|
-
const provider = mockMonaco._getCompletionProvider()!;
|
|
187
|
-
|
|
188
|
-
const model = {
|
|
189
|
-
getWordUntilPosition: () => ({ startColumn: 1, endColumn: 1, word: '' }),
|
|
190
|
-
getLineContent: () => '',
|
|
191
|
-
getLineCount: () => 1,
|
|
192
|
-
};
|
|
193
|
-
const position = { lineNumber: 1, column: 1 };
|
|
194
|
-
|
|
195
|
-
const result = provider.provideCompletionItems(model, position) as { suggestions: { label: string }[] };
|
|
196
|
-
const labels = result.suggestions.map((s) => s.label);
|
|
197
|
-
const uniqueLabels = [...new Set(labels)];
|
|
198
|
-
expect(labels.length).toBe(uniqueLabels.length);
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
it('provides suggestions after "by" keyword', () => {
|
|
202
|
-
registerKqlLanguage(mockMonaco as never);
|
|
203
|
-
const provider = mockMonaco._getCompletionProvider()!;
|
|
204
|
-
|
|
205
|
-
const model = {
|
|
206
|
-
getWordUntilPosition: () => ({ startColumn: 30, endColumn: 30, word: '' }),
|
|
207
|
-
getLineContent: (line: number) => {
|
|
208
|
-
if (line === 1) {
|
|
209
|
-
return 'TestTable';
|
|
210
|
-
}
|
|
211
|
-
if (line === 2) {
|
|
212
|
-
return '| summarize count() by ';
|
|
213
|
-
}
|
|
214
|
-
return '';
|
|
215
|
-
},
|
|
216
|
-
getLineCount: () => 2,
|
|
217
|
-
};
|
|
218
|
-
const position = { lineNumber: 2, column: 24 };
|
|
219
|
-
|
|
220
|
-
const result = provider.provideCompletionItems(model, position) as { suggestions: { label: string }[] };
|
|
221
|
-
const labels = result.suggestions.map((s) => s.label);
|
|
222
|
-
expect(labels).toContain('Col1');
|
|
223
|
-
});
|
|
224
|
-
|
|
225
|
-
it('provides suggestions inside function calls', () => {
|
|
226
|
-
registerKqlLanguage(mockMonaco as never);
|
|
227
|
-
const provider = mockMonaco._getCompletionProvider()!;
|
|
228
|
-
|
|
229
|
-
const model = {
|
|
230
|
-
getWordUntilPosition: () => ({ startColumn: 20, endColumn: 20, word: '' }),
|
|
231
|
-
getLineContent: (line: number) => {
|
|
232
|
-
if (line === 1) {
|
|
233
|
-
return 'TestTable';
|
|
234
|
-
}
|
|
235
|
-
if (line === 2) {
|
|
236
|
-
return '| where strlen(';
|
|
237
|
-
}
|
|
238
|
-
return '';
|
|
239
|
-
},
|
|
240
|
-
getLineCount: () => 2,
|
|
241
|
-
};
|
|
242
|
-
const position = { lineNumber: 2, column: 17 };
|
|
243
|
-
|
|
244
|
-
const result = provider.provideCompletionItems(model, position) as { suggestions: { label: string }[] };
|
|
245
|
-
const labels = result.suggestions.map((s) => s.label);
|
|
246
|
-
expect(labels).toContain('Col1');
|
|
247
|
-
});
|
|
248
|
-
|
|
249
|
-
it('uses the latest schema resolver for completions after registration', () => {
|
|
250
|
-
registerKqlLanguage(mockMonaco as never);
|
|
251
|
-
const provider = mockMonaco._getCompletionProvider()!;
|
|
252
|
-
|
|
253
|
-
setKqlSchemaResolver(() => [{
|
|
254
|
-
name: 'DynamicTable',
|
|
255
|
-
folder: 'Folder',
|
|
256
|
-
description: '',
|
|
257
|
-
kind: 'table',
|
|
258
|
-
columns: [{ name: 'DynamicColumn', type: 'string' }],
|
|
259
|
-
}]);
|
|
260
|
-
|
|
261
|
-
const result = provider.provideCompletionItems({
|
|
262
|
-
getWordUntilPosition: () => ({ startColumn: 12, endColumn: 12, word: '' }),
|
|
263
|
-
getLineContent: (line: number) => {
|
|
264
|
-
if (line === 1) {
|
|
265
|
-
return 'DynamicTable';
|
|
266
|
-
}
|
|
267
|
-
if (line === 2) {
|
|
268
|
-
return '| where ';
|
|
269
|
-
}
|
|
270
|
-
return '';
|
|
271
|
-
},
|
|
272
|
-
getLineCount: () => 2,
|
|
273
|
-
}, { lineNumber: 2, column: 9 }) as { suggestions: { label: string }[] };
|
|
274
|
-
|
|
275
|
-
expect(result.suggestions.map((suggestion) => suggestion.label)).toContain('DynamicColumn');
|
|
276
|
-
});
|
|
277
|
-
});
|
|
278
|
-
});
|
|
279
|
-
|
|
280
|
-
describe('kqlLanguage additional branch coverage', () => {
|
|
281
|
-
let mockMonaco: ReturnType<typeof createMockMonaco>;
|
|
282
|
-
|
|
283
|
-
beforeEach(() => {
|
|
284
|
-
mockMonaco = createMockMonaco();
|
|
285
|
-
setKqlSchemaResolver(() => getSchema());
|
|
286
|
-
});
|
|
287
|
-
|
|
288
|
-
it('suggests columns from statementTable in general context', () => {
|
|
289
|
-
registerKqlLanguage(mockMonaco as never);
|
|
290
|
-
const provider = mockMonaco._getCompletionProvider()!;
|
|
291
|
-
const model = {
|
|
292
|
-
getWordAtPosition: () => null,
|
|
293
|
-
getWordUntilPosition: () => ({ word: '', startColumn: 1, endColumn: 1 }),
|
|
294
|
-
getLineContent: (n: number) => n === 1 ? 'TestTable' : ' ',
|
|
295
|
-
getLineCount: () => 2,
|
|
296
|
-
};
|
|
297
|
-
const position = { lineNumber: 2, column: 3 };
|
|
298
|
-
|
|
299
|
-
const result = provider.provideCompletionItems(model, position) as { suggestions: { label: string; detail?: string }[] };
|
|
300
|
-
const colSuggestions = result.suggestions.filter((s) => s.detail?.includes('TestTable'));
|
|
301
|
-
expect(colSuggestions.length).toBeGreaterThan(0);
|
|
302
|
-
expect(colSuggestions.map((s) => s.label)).toContain('Col1');
|
|
303
|
-
});
|
|
304
|
-
|
|
305
|
-
it('suggests columns from mentioned tables when no statementTable found', () => {
|
|
306
|
-
registerKqlLanguage(mockMonaco as never);
|
|
307
|
-
const provider = mockMonaco._getCompletionProvider()!;
|
|
308
|
-
const model = {
|
|
309
|
-
getWordAtPosition: () => null,
|
|
310
|
-
getWordUntilPosition: () => ({ word: '', startColumn: 1, endColumn: 1 }),
|
|
311
|
-
getLineContent: (n: number) => n === 1 ? 'let x = OtherTable' : ' ',
|
|
312
|
-
getLineCount: () => 2,
|
|
313
|
-
};
|
|
314
|
-
const position = { lineNumber: 2, column: 3 };
|
|
315
|
-
|
|
316
|
-
const result = provider.provideCompletionItems(model, position) as { suggestions: { label: string; detail?: string }[] };
|
|
317
|
-
const colSuggestions = result.suggestions.filter((s) => s.detail?.includes('OtherTable'));
|
|
318
|
-
expect(colSuggestions.length).toBeGreaterThan(0);
|
|
319
|
-
expect(colSuggestions.map((s) => s.label)).toContain('ColA');
|
|
320
|
-
});
|
|
321
|
-
});
|
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
import type { Monaco } from '@monaco-editor/react';
|
|
2
|
-
|
|
3
|
-
import { getSchema, KQL_FUNCTIONS, KQL_AGGREGATIONS, KQL_SCALAR_FUNCTIONS } from '../../services/schema';
|
|
4
|
-
import type { SchemaTable } from '../../services/schema';
|
|
5
|
-
|
|
6
|
-
let schemaResolver: () => SchemaTable[] = () => getSchema();
|
|
7
|
-
|
|
8
|
-
export function setKqlSchemaResolver(resolver: () => SchemaTable[]): void {
|
|
9
|
-
schemaResolver = resolver;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export function registerKqlLanguage(monaco: Monaco) {
|
|
13
|
-
if (monaco.languages.getLanguages().some((l: { id: string }) => l.id === 'kql')) {
|
|
14
|
-
return;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
monaco.languages.register({ id: 'kql' });
|
|
18
|
-
|
|
19
|
-
monaco.languages.setMonarchTokensProvider('kql', {
|
|
20
|
-
ignoreCase: true,
|
|
21
|
-
keywords: KQL_FUNCTIONS,
|
|
22
|
-
aggregations: KQL_AGGREGATIONS,
|
|
23
|
-
scalarFunctions: KQL_SCALAR_FUNCTIONS,
|
|
24
|
-
operators: ['==', '!=', '<', '>', '<=', '>=', '!~', '=~', 'in', 'has', 'contains', 'startswith', 'endswith', 'matches regex', 'between', 'and', 'or', 'not'],
|
|
25
|
-
tokenizer: {
|
|
26
|
-
root: [
|
|
27
|
-
[/\/\/.*$/, 'comment'],
|
|
28
|
-
[/"[^"]*"/, 'string'],
|
|
29
|
-
[/'[^']*'/, 'string'],
|
|
30
|
-
[/\b(true|false)\b/, 'keyword'],
|
|
31
|
-
[/\b(datetime|timespan|long|int|real|string|bool|dynamic)\b/, 'type'],
|
|
32
|
-
[/\b(ago|now|bin)\s*\(/, { token: 'function', next: '@root' }],
|
|
33
|
-
[/\b\d+(\.\d+)?\b/, 'number'],
|
|
34
|
-
[/[a-zA-Z_]\w*/, {
|
|
35
|
-
cases: {
|
|
36
|
-
'@keywords': 'keyword',
|
|
37
|
-
'@aggregations': 'function',
|
|
38
|
-
'@scalarFunctions': 'function',
|
|
39
|
-
'@default': 'identifier',
|
|
40
|
-
},
|
|
41
|
-
}],
|
|
42
|
-
[/\|/, 'delimiter'],
|
|
43
|
-
[/[{}()[\]]/, 'bracket'],
|
|
44
|
-
[/[,;]/, 'delimiter'],
|
|
45
|
-
],
|
|
46
|
-
},
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
monaco.languages.registerCompletionItemProvider('kql', {
|
|
50
|
-
triggerCharacters: ['.', '|', ' ', ','],
|
|
51
|
-
provideCompletionItems: (model: any, position: any) => {
|
|
52
|
-
const word = model.getWordUntilPosition(position);
|
|
53
|
-
const range = {
|
|
54
|
-
startLineNumber: position.lineNumber, endLineNumber: position.lineNumber,
|
|
55
|
-
startColumn: word.startColumn, endColumn: word.endColumn,
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
const suggestions: {
|
|
59
|
-
label: string; kind: number; insertText: string; detail?: string;
|
|
60
|
-
sortText?: string;
|
|
61
|
-
range: { startLineNumber: number; endLineNumber: number; startColumn: number; endColumn: number };
|
|
62
|
-
}[] = [];
|
|
63
|
-
|
|
64
|
-
// Find the current statement block around the cursor (delimited by blank lines)
|
|
65
|
-
let blockStart = position.lineNumber;
|
|
66
|
-
while (blockStart > 1 && model.getLineContent(blockStart - 1).trim() !== '') {
|
|
67
|
-
blockStart--;
|
|
68
|
-
}
|
|
69
|
-
let blockEnd = position.lineNumber;
|
|
70
|
-
const totalLines = model.getLineCount();
|
|
71
|
-
while (blockEnd < totalLines && model.getLineContent(blockEnd + 1).trim() !== '') {
|
|
72
|
-
blockEnd++;
|
|
73
|
-
}
|
|
74
|
-
const blockLines: string[] = [];
|
|
75
|
-
for (let i = blockStart; i <= blockEnd; i++) {
|
|
76
|
-
blockLines.push(model.getLineContent(i));
|
|
77
|
-
}
|
|
78
|
-
const blockText = blockLines.join('\n');
|
|
79
|
-
const schema = schemaResolver();
|
|
80
|
-
|
|
81
|
-
// Find the table referenced in this statement block (first non-keyword identifier)
|
|
82
|
-
const tableMatch = blockText.match(/^\s*(?:let\s+\w+\s*=\s*)?(\w+)/);
|
|
83
|
-
const statementTable = tableMatch ? schema.find((t) => t.name === tableMatch[1]) : null;
|
|
84
|
-
|
|
85
|
-
// Text from start of current line to cursor position
|
|
86
|
-
const lineText = model.getLineContent(position.lineNumber);
|
|
87
|
-
const textBeforeCursor = lineText.substring(0, position.column - 1);
|
|
88
|
-
|
|
89
|
-
// Detect if we're right after a pipe + operator (e.g., "| where ", "| project ")
|
|
90
|
-
const afterPipe = /\|\s*\w*$/.test(textBeforeCursor);
|
|
91
|
-
// Detect if we're after a column-accepting operator on the current line
|
|
92
|
-
const afterColumnOperator = /\|\s*(?:where|project|extend|project-away|project-rename|project-reorder|sort\s+by|order\s+by|distinct|summarize|mv-expand|parse)\s+/i.test(textBeforeCursor);
|
|
93
|
-
// Detect if we're after "by" keyword (e.g., "summarize count() by ")
|
|
94
|
-
const afterBy = /\bby\s+/i.test(textBeforeCursor);
|
|
95
|
-
// Detect if we're inside a function call (after an open paren or comma)
|
|
96
|
-
const insideFunc = /(?:\(|,)\s*\w*$/.test(textBeforeCursor);
|
|
97
|
-
|
|
98
|
-
if (afterPipe) {
|
|
99
|
-
// Right after pipe — suggest KQL tabular operators
|
|
100
|
-
KQL_FUNCTIONS.forEach((fn) => {
|
|
101
|
-
suggestions.push({ label: fn, kind: monaco.languages.CompletionItemKind.Keyword, insertText: fn + ' ', range });
|
|
102
|
-
});
|
|
103
|
-
} else if (afterColumnOperator || afterBy || insideFunc) {
|
|
104
|
-
// Context where columns are expected — prioritize columns from the statement's table
|
|
105
|
-
if (statementTable) {
|
|
106
|
-
statementTable.columns.forEach((col) => {
|
|
107
|
-
suggestions.push({ label: col.name, kind: monaco.languages.CompletionItemKind.Field, insertText: col.name, detail: `${col.type} · ${statementTable.name}`, sortText: '0' + col.name, range });
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
// Also suggest aggregation and scalar functions (useful inside summarize, extend, etc.)
|
|
111
|
-
KQL_AGGREGATIONS.forEach((fn) => {
|
|
112
|
-
suggestions.push({ label: fn, kind: monaco.languages.CompletionItemKind.Function, insertText: fn + '()', detail: 'aggregation', sortText: '1' + fn, range });
|
|
113
|
-
});
|
|
114
|
-
KQL_SCALAR_FUNCTIONS.forEach((fn) => {
|
|
115
|
-
suggestions.push({ label: fn, kind: monaco.languages.CompletionItemKind.Function, insertText: fn + '()', detail: 'function', sortText: '1' + fn, range });
|
|
116
|
-
});
|
|
117
|
-
} else {
|
|
118
|
-
// General context — suggest tables first, then columns from mentioned tables, then functions
|
|
119
|
-
schema.forEach((table) => {
|
|
120
|
-
suggestions.push({ label: table.name, kind: monaco.languages.CompletionItemKind.Class, insertText: table.name, detail: `${table.columns.length} cols`, sortText: '0' + table.name, range });
|
|
121
|
-
});
|
|
122
|
-
if (statementTable) {
|
|
123
|
-
statementTable.columns.forEach((col) => {
|
|
124
|
-
suggestions.push({ label: col.name, kind: monaco.languages.CompletionItemKind.Field, insertText: col.name, detail: `${col.type} · ${statementTable.name}`, sortText: '1' + col.name, range });
|
|
125
|
-
});
|
|
126
|
-
} else {
|
|
127
|
-
// No specific table found — suggest columns from all mentioned tables
|
|
128
|
-
const mentioned = schema.filter((t) => blockText.includes(t.name));
|
|
129
|
-
mentioned.forEach((table) => {
|
|
130
|
-
table.columns.forEach((col) => {
|
|
131
|
-
suggestions.push({ label: col.name, kind: monaco.languages.CompletionItemKind.Field, insertText: col.name, detail: `${col.type} · ${table.name}`, sortText: '1' + col.name, range });
|
|
132
|
-
});
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
KQL_AGGREGATIONS.forEach((fn) => {
|
|
136
|
-
suggestions.push({ label: fn, kind: monaco.languages.CompletionItemKind.Function, insertText: fn + '()', detail: 'aggregation', sortText: '2' + fn, range });
|
|
137
|
-
});
|
|
138
|
-
KQL_SCALAR_FUNCTIONS.forEach((fn) => {
|
|
139
|
-
suggestions.push({ label: fn, kind: monaco.languages.CompletionItemKind.Function, insertText: fn + '()', detail: 'function', sortText: '2' + fn, range });
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// Deduplicate by label
|
|
144
|
-
const seen = new Set<string>();
|
|
145
|
-
const unique = suggestions.filter((s) => {
|
|
146
|
-
if (seen.has(s.label)) {
|
|
147
|
-
return false;
|
|
148
|
-
}
|
|
149
|
-
seen.add(s.label);
|
|
150
|
-
return true;
|
|
151
|
-
});
|
|
152
|
-
return { suggestions: unique };
|
|
153
|
-
},
|
|
154
|
-
});
|
|
155
|
-
}
|