@kusto/monaco-kusto 3.2.4 → 3.2.7

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.
@@ -1,1562 +1,1585 @@
1
- var __assign = (this && this.__assign) || function () {
2
- __assign = Object.assign || function(t) {
3
- for (var s, i = 1, n = arguments.length; i < n; i++) {
4
- s = arguments[i];
5
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
- t[p] = s[p];
7
- }
8
- return t;
9
- };
10
- return __assign.apply(this, arguments);
11
- };
12
- /// <reference path="../../node_modules/@kusto/language-service/Kusto.JavaScript.Client.d.ts" />
13
- /// <reference path="../../node_modules/@kusto/language-service-next/Kusto.Language.Bridge.d.ts" />
14
- /// <reference path="../typings/refs.d.ts" />
15
- import * as s from './schema';
16
- // polyfill string endsWith
17
- if (!String.prototype.endsWith) {
18
- String.prototype.endsWith = function (search, this_len) {
19
- if (this_len === undefined || this_len > this.length) {
20
- this_len = this.length;
21
- }
22
- return this.substring(this_len - search.length, this_len) === search;
23
- };
24
- }
25
- // If we're running in a web worker - which doesn't share global context with the main thread -
26
- // we need to manually load dependencies that are not explicit- meaning our non-module dependencies
27
- // generated by Bridge.Net
28
- if (typeof document == 'undefined') {
29
- // monaco will run the worker from vs/base/worker so the relative path needs to be from there (hence going up 2 dirs)
30
- importScripts('../../language/kusto/bridge.min.js');
31
- importScripts('../../language/kusto/kusto.javascript.client.min.js');
32
- importScripts('../../language/kusto/Kusto.Language.Bridge.min.js');
33
- }
34
- import * as ls from '../_deps/vscode-languageserver-types/main';
35
- import * as XRegExp from '../_deps/xregexp/xregexp-all';
36
- var k = Kusto.Data.IntelliSense;
37
- var parsing = Kusto.Language.Parsing;
38
- var k2 = Kusto.Language.Editor;
39
- var sym = Kusto.Language.Symbols;
40
- var GlobalState = Kusto.Language.GlobalState;
41
- import { getCslTypeNameFromClrType, getEntityDataTypeFromCslType } from './schema';
42
- var List = System.Collections.Generic.List$1;
43
- function assertNever(x) {
44
- throw new Error('Unexpected object: ' + x);
45
- }
46
- var ParseProperties = /** @class */ (function () {
47
- function ParseProperties(version, uri, rulesProvider, parseMode) {
48
- this.version = version;
49
- this.uri = uri;
50
- this.rulesProvider = rulesProvider;
51
- this.parseMode = parseMode;
52
- }
53
- ParseProperties.prototype.isParseNeeded = function (document, rulesProvider, parseMode) {
54
- if (document.uri === this.uri &&
55
- (!rulesProvider || rulesProvider === this.rulesProvider) &&
56
- document.version <= this.version &&
57
- parseMode &&
58
- parseMode <= this.parseMode) {
59
- return false;
60
- }
61
- return true;
62
- };
63
- return ParseProperties;
64
- }());
65
- export var TokenKind;
66
- (function (TokenKind) {
67
- TokenKind[TokenKind["TableToken"] = 2] = "TableToken";
68
- TokenKind[TokenKind["TableColumnToken"] = 4] = "TableColumnToken";
69
- TokenKind[TokenKind["OperatorToken"] = 8] = "OperatorToken";
70
- TokenKind[TokenKind["SubOperatorToken"] = 16] = "SubOperatorToken";
71
- TokenKind[TokenKind["CalculatedColumnToken"] = 32] = "CalculatedColumnToken";
72
- TokenKind[TokenKind["StringLiteralToken"] = 64] = "StringLiteralToken";
73
- TokenKind[TokenKind["FunctionNameToken"] = 128] = "FunctionNameToken";
74
- TokenKind[TokenKind["UnknownToken"] = 256] = "UnknownToken";
75
- TokenKind[TokenKind["CommentToken"] = 512] = "CommentToken";
76
- TokenKind[TokenKind["PlainTextToken"] = 1024] = "PlainTextToken";
77
- TokenKind[TokenKind["DataTypeToken"] = 2048] = "DataTypeToken";
78
- TokenKind[TokenKind["ControlCommandToken"] = 4096] = "ControlCommandToken";
79
- TokenKind[TokenKind["CommandPartToken"] = 8192] = "CommandPartToken";
80
- TokenKind[TokenKind["QueryParametersToken"] = 16384] = "QueryParametersToken";
81
- TokenKind[TokenKind["CslCommandToken"] = 32768] = "CslCommandToken";
82
- TokenKind[TokenKind["LetVariablesToken"] = 65536] = "LetVariablesToken";
83
- TokenKind[TokenKind["PluginToken"] = 131072] = "PluginToken";
84
- TokenKind[TokenKind["BracketRangeToken"] = 262144] = "BracketRangeToken";
85
- TokenKind[TokenKind["ClientDirectiveToken"] = 524288] = "ClientDirectiveToken";
86
- })(TokenKind || (TokenKind = {}));
87
- /**
88
- * convert the bridge.net object to a plain javascript object that only contains data.
89
- * @param k2Classifications @kusto/language-service-next bridge.net object
90
- */
91
- function toClassifiedRange(k2Classifications) {
92
- return k2Classifications.map(function (classification) { return ({
93
- start: classification.Start,
94
- end: classification.End,
95
- length: classification.Length,
96
- kind: classification.Kind,
97
- }); });
98
- }
99
- /**
100
- * Kusto Language service translates the kusto object model (transpiled from C# by Bridge.Net)
101
- * to the vscode language server types, which are used by vscode lanugage extensions.
102
- * This should make things easier in the future to provide a vscode extension based on this translation layer.
103
- *
104
- * Further translations, if needed, to support specific editors (Atom, sublime, Etc)
105
- * should be done on top of this API, since it is (at least meant to be) a standard that is supported by multiple editors.
106
- *
107
- * Note1: Currenlty monaco isn't using this object model so further translation will be necessary on calling modules.
108
- *
109
- * Note2: This file is responsible for interacting with the kusto object model and exposing Microsoft language service types.
110
- * An exception to that rule is tokenization (and syntax highlighting which depends on it) -
111
- * since it's not currently part of the Microosft language service protocol. Thus tokenize() _does_ 'leak' kusto types to the callers.
112
- */
113
- var KustoLanguageService = /** @class */ (function () {
114
- function KustoLanguageService(schema, languageSettings) {
115
- var _a, _b, _c, _d;
116
- var _this = this;
117
- this._toOptionKind = (_a = {},
118
- _a[k2.CompletionKind.AggregateFunction] = k.OptionKind.FunctionAggregation,
119
- _a[k2.CompletionKind.BuiltInFunction] = k.OptionKind.FunctionServerSide,
120
- _a[k2.CompletionKind.Cluster] = k.OptionKind.Database,
121
- _a[k2.CompletionKind.Column] = k.OptionKind.Column,
122
- _a[k2.CompletionKind.CommandPrefix] = k.OptionKind.None,
123
- _a[k2.CompletionKind.Database] = k.OptionKind.Database,
124
- _a[k2.CompletionKind.DatabaseFunction] = k.OptionKind.FunctionServerSide,
125
- _a[k2.CompletionKind.Example] = k.OptionKind.None,
126
- _a[k2.CompletionKind.Identifier] = k.OptionKind.None,
127
- _a[k2.CompletionKind.Keyword] = k.OptionKind.None,
128
- _a[k2.CompletionKind.LocalFunction] = k.OptionKind.FunctionLocal,
129
- _a[k2.CompletionKind.MaterialiedView] = k.OptionKind.MaterializedView,
130
- _a[k2.CompletionKind.Parameter] = k.OptionKind.Parameter,
131
- _a[k2.CompletionKind.Punctuation] = k.OptionKind.None,
132
- _a[k2.CompletionKind.QueryPrefix] = k.OptionKind.Operator,
133
- _a[k2.CompletionKind.RenderChart] = k.OptionKind.Operator,
134
- _a[k2.CompletionKind.ScalarInfix] = k.OptionKind.None,
135
- _a[k2.CompletionKind.ScalarPrefix] = k.OptionKind.None,
136
- _a[k2.CompletionKind.ScalarType] = k.OptionKind.DataType,
137
- _a[k2.CompletionKind.Syntax] = k.OptionKind.None,
138
- _a[k2.CompletionKind.Table] = k.OptionKind.Table,
139
- _a[k2.CompletionKind.TabularPrefix] = k.OptionKind.None,
140
- _a[k2.CompletionKind.TabularSuffix] = k.OptionKind.None,
141
- _a[k2.CompletionKind.Unknown] = k.OptionKind.None,
142
- _a[k2.CompletionKind.Variable] = k.OptionKind.Parameter,
143
- _a[k2.CompletionKind.Option] = k.OptionKind.Option,
144
- _a);
145
- this.disabledCompletionItemsV2 = {
146
- // render charts
147
- ladderchart: k2.CompletionKind.RenderChart,
148
- pivotchart: k2.CompletionKind.RenderChart,
149
- timeline: k2.CompletionKind.RenderChart,
150
- timepivot: k2.CompletionKind.RenderChart,
151
- '3Dchart': k2.CompletionKind.RenderChart,
152
- list: k2.CompletionKind.RenderChart,
153
- };
154
- this.isIntellisenseV2 = function () {
155
- return _this._languageSettings.useIntellisenseV2 && _this._schema && _this._schema.clusterType === 'Engine';
156
- };
157
- this.disabledCompletionItemsV1 = {
158
- capacity: k.OptionKind.Policy,
159
- callout: k.OptionKind.Policy,
160
- encoding: k.OptionKind.Policy,
161
- batching: k.OptionKind.Policy,
162
- querythrottling: k.OptionKind.Policy,
163
- merge: k.OptionKind.Policy,
164
- querylimit: k.OptionKind.Policy,
165
- rowstore: k.OptionKind.Policy,
166
- streamingingestion: k.OptionKind.Policy,
167
- restricted_view_access: k.OptionKind.Policy,
168
- sharding: k.OptionKind.Policy,
169
- 'restricted-viewers': k.OptionKind.Policy,
170
- attach: k.OptionKind.Command,
171
- purge: k.OptionKind.Command,
172
- };
173
- this._kustoKindtolsKind = (_b = {},
174
- _b[k.OptionKind.None] = ls.CompletionItemKind.Interface,
175
- _b[k.OptionKind.Operator] = ls.CompletionItemKind.Method,
176
- _b[k.OptionKind.Command] = ls.CompletionItemKind.Method,
177
- _b[k.OptionKind.Service] = ls.CompletionItemKind.Class,
178
- _b[k.OptionKind.Policy] = ls.CompletionItemKind.Reference,
179
- _b[k.OptionKind.Database] = ls.CompletionItemKind.Class,
180
- _b[k.OptionKind.Table] = ls.CompletionItemKind.Class,
181
- _b[k.OptionKind.DataType] = ls.CompletionItemKind.Class,
182
- _b[k.OptionKind.Literal] = ls.CompletionItemKind.Property,
183
- _b[k.OptionKind.Parameter] = ls.CompletionItemKind.Variable,
184
- _b[k.OptionKind.IngestionMapping] = ls.CompletionItemKind.Variable,
185
- _b[k.OptionKind.ExpressionFunction] = ls.CompletionItemKind.Variable,
186
- _b[k.OptionKind.Option] = ls.CompletionItemKind.Interface,
187
- _b[k.OptionKind.OptionKind] = ls.CompletionItemKind.Interface,
188
- _b[k.OptionKind.OptionRender] = ls.CompletionItemKind.Interface,
189
- _b[k.OptionKind.Column] = ls.CompletionItemKind.Function,
190
- _b[k.OptionKind.ColumnString] = ls.CompletionItemKind.Field,
191
- _b[k.OptionKind.ColumnNumeric] = ls.CompletionItemKind.Field,
192
- _b[k.OptionKind.ColumnDateTime] = ls.CompletionItemKind.Field,
193
- _b[k.OptionKind.ColumnTimespan] = ls.CompletionItemKind.Field,
194
- _b[k.OptionKind.FunctionServerSide] = ls.CompletionItemKind.Field,
195
- _b[k.OptionKind.FunctionAggregation] = ls.CompletionItemKind.Field,
196
- _b[k.OptionKind.FunctionFilter] = ls.CompletionItemKind.Field,
197
- _b[k.OptionKind.FunctionScalar] = ls.CompletionItemKind.Field,
198
- _b[k.OptionKind.ClientDirective] = ls.CompletionItemKind.Enum,
199
- _b);
200
- this._kustoKindToLsKindV2 = (_c = {},
201
- _c[k2.CompletionKind.AggregateFunction] = ls.CompletionItemKind.Field,
202
- _c[k2.CompletionKind.BuiltInFunction] = ls.CompletionItemKind.Field,
203
- _c[k2.CompletionKind.Cluster] = ls.CompletionItemKind.Class,
204
- _c[k2.CompletionKind.Column] = ls.CompletionItemKind.Function,
205
- _c[k2.CompletionKind.CommandPrefix] = ls.CompletionItemKind.Field,
206
- _c[k2.CompletionKind.Database] = ls.CompletionItemKind.Class,
207
- _c[k2.CompletionKind.DatabaseFunction] = ls.CompletionItemKind.Field,
208
- _c[k2.CompletionKind.Example] = ls.CompletionItemKind.Text,
209
- _c[k2.CompletionKind.Identifier] = ls.CompletionItemKind.Method,
210
- _c[k2.CompletionKind.Keyword] = ls.CompletionItemKind.Method,
211
- _c[k2.CompletionKind.LocalFunction] = ls.CompletionItemKind.Field,
212
- _c[k2.CompletionKind.MaterialiedView] = ls.CompletionItemKind.Class,
213
- _c[k2.CompletionKind.Parameter] = ls.CompletionItemKind.Variable,
214
- _c[k2.CompletionKind.Punctuation] = ls.CompletionItemKind.Interface,
215
- _c[k2.CompletionKind.QueryPrefix] = ls.CompletionItemKind.Function,
216
- _c[k2.CompletionKind.RenderChart] = ls.CompletionItemKind.Method,
217
- _c[k2.CompletionKind.ScalarInfix] = ls.CompletionItemKind.Field,
218
- _c[k2.CompletionKind.ScalarPrefix] = ls.CompletionItemKind.Field,
219
- _c[k2.CompletionKind.ScalarType] = ls.CompletionItemKind.TypeParameter,
220
- _c[k2.CompletionKind.Syntax] = ls.CompletionItemKind.Method,
221
- _c[k2.CompletionKind.Table] = ls.CompletionItemKind.Class,
222
- _c[k2.CompletionKind.TabularPrefix] = ls.CompletionItemKind.Field,
223
- // datatable, externaldata
224
- _c[k2.CompletionKind.TabularSuffix] = ls.CompletionItemKind.Field,
225
- _c[k2.CompletionKind.Unknown] = ls.CompletionItemKind.Interface,
226
- _c[k2.CompletionKind.Variable] = ls.CompletionItemKind.Variable,
227
- _c[k2.CompletionKind.Option] = ls.CompletionItemKind.Text,
228
- _c);
229
- this._tokenKindToClassificationKind = (_d = {},
230
- _d[TokenKind.TableToken] = k2.ClassificationKind.Table,
231
- _d[TokenKind.TableColumnToken] = k2.ClassificationKind.Column,
232
- _d[TokenKind.OperatorToken] = k2.ClassificationKind.QueryOperator,
233
- _d[TokenKind.SubOperatorToken] = k2.ClassificationKind.Function,
234
- _d[TokenKind.CalculatedColumnToken] = k2.ClassificationKind.Column,
235
- _d[TokenKind.StringLiteralToken] = k2.ClassificationKind.Literal,
236
- _d[TokenKind.FunctionNameToken] = k2.ClassificationKind.Function,
237
- _d[TokenKind.UnknownToken] = k2.ClassificationKind.PlainText,
238
- _d[TokenKind.CommentToken] = k2.ClassificationKind.Comment,
239
- _d[TokenKind.PlainTextToken] = k2.ClassificationKind.PlainText,
240
- _d[TokenKind.DataTypeToken] = k2.ClassificationKind.Type,
241
- _d[TokenKind.ControlCommandToken] = k2.ClassificationKind.PlainText,
242
- _d[TokenKind.CommandPartToken] = k2.ClassificationKind.PlainText,
243
- _d[TokenKind.QueryParametersToken] = k2.ClassificationKind.QueryParameter,
244
- _d[TokenKind.CslCommandToken] = k2.ClassificationKind.Keyword,
245
- _d[TokenKind.LetVariablesToken] = k2.ClassificationKind.Identifier,
246
- _d[TokenKind.PluginToken] = k2.ClassificationKind.Function,
247
- _d[TokenKind.BracketRangeToken] = k2.ClassificationKind.Keyword,
248
- _d[TokenKind.ClientDirectiveToken] = k2.ClassificationKind.Keyword,
249
- _d);
250
- this._schemaCache = {};
251
- this._kustoJsSchema = KustoLanguageService.convertToKustoJsSchema(schema);
252
- this._kustoJsSchemaV2 = this.convertToKustoJsSchemaV2(schema);
253
- this._schema = schema;
254
- this.configure(languageSettings);
255
- this._newlineAppendPipePolicy = new Kusto.Data.IntelliSense.ApplyPolicy();
256
- this._newlineAppendPipePolicy.Text = '\n| ';
257
- }
258
- KustoLanguageService.prototype.configure = function (languageSettings) {
259
- this._languageSettings = languageSettings;
260
- // Since we're still reverting to V1 intellisense for control commands, we need to update the rules provider
261
- // (which is a notion of V1 intellisense).
262
- this.createRulesProvider(this._kustoJsSchema, this._schema.clusterType);
263
- };
264
- KustoLanguageService.prototype.doComplete = function (document, position) {
265
- return this.isIntellisenseV2() ? this.doCompleteV2(document, position) : this.doCompleteV1(document, position);
266
- };
267
- KustoLanguageService.prototype.doCompleteV2 = function (document, position) {
268
- var _this = this;
269
- if (!document) {
270
- return Promise.resolve(ls.CompletionList.create([]));
271
- }
272
- var script = this.parseDocumentV2(document);
273
- var cursorOffset = document.offsetAt(position);
274
- var currentcommand = this.getCurrentCommandV2(script, cursorOffset);
275
- var completionItems = currentcommand.Service.GetCompletionItems(cursorOffset);
276
- var disabledItems = this.disabledCompletionItemsV2;
277
- if (this._languageSettings.disabledCompletionItems) {
278
- this._languageSettings.disabledCompletionItems.map(function (item) {
279
- // logic will treat unknown as a '*' wildcard, meaning that if the key is in the object
280
- // the completion item will be suppressed.
281
- disabledItems[item] = k2.CompletionKind.Unknown;
282
- });
283
- }
284
- var items = this.toArray(completionItems.Items)
285
- .filter(function (item) {
286
- return !(item &&
287
- item.MatchText &&
288
- disabledItems[item.MatchText] !== undefined &&
289
- (disabledItems[item.MatchText] === k2.CompletionKind.Unknown ||
290
- disabledItems[item.MatchText] === item.Kind));
291
- })
292
- .map(function (kItem, i) {
293
- var v1CompletionOption = new k.CompletionOption(_this._toOptionKind[kItem.Kind] || k.OptionKind.None, kItem.DisplayText);
294
- var helpTopic = _this.getTopic(v1CompletionOption);
295
- // If we have AfterText it means that the cursor should no be placed at end of suggested text.
296
- // In that case we switch to snippet format and represent the point where the cursor should be as
297
- // as '\$0'
298
- var _a = kItem.AfterText && kItem.AfterText.length > 0
299
- ? {
300
- textToInsert: kItem.EditText + '$0' + kItem.AfterText,
301
- format: ls.InsertTextFormat.Snippet,
302
- }
303
- : {
304
- textToInsert: kItem.EditText,
305
- format: ls.InsertTextFormat.PlainText,
306
- }, textToInsert = _a.textToInsert, format = _a.format;
307
- var lsItem = ls.CompletionItem.create(kItem.DisplayText);
308
- var startPosition = document.positionAt(completionItems.EditStart);
309
- var endPosition = document.positionAt(completionItems.EditStart + completionItems.EditLength);
310
- lsItem.textEdit = ls.TextEdit.replace(ls.Range.create(startPosition, endPosition), textToInsert);
311
- lsItem.sortText = _this.getSortText(i + 1);
312
- lsItem.kind = _this.kustoKindToLsKindV2(kItem.Kind);
313
- lsItem.insertTextFormat = format;
314
- lsItem.detail = helpTopic ? helpTopic.ShortDescription : undefined;
315
- lsItem.documentation = helpTopic
316
- ? { value: helpTopic.LongDescription, kind: ls.MarkupKind.Markdown }
317
- : undefined;
318
- return lsItem;
319
- });
320
- return Promise.resolve(ls.CompletionList.create(items));
321
- };
322
- /**
323
- * when trying to get a topic we need the funtion name (abs, tolower, ETC).
324
- * The problem is that the 'Value' string also contains the arguments (e.g abs(nubmer)), which means that we are
325
- * not able to correlate the option with its documentation.
326
- * This piece of code tries to strip this hwne getting topic.
327
- * @param completionOption the Completion option
328
- */
329
- KustoLanguageService.prototype.getTopic = function (completionOption) {
330
- if (completionOption.Kind == k.OptionKind.FunctionScalar ||
331
- completionOption.Kind == k.OptionKind.FunctionAggregation) {
332
- // from a value like 'abs(number)' remove the '(number)' so that only 'abs' will remain
333
- var indexOfParen = completionOption.Value.indexOf('(');
334
- if (indexOfParen >= 0) {
335
- completionOption = new k.CompletionOption(completionOption.Kind, completionOption.Value.substring(0, indexOfParen));
336
- }
337
- }
338
- return k.CslDocumentation.Instance.GetTopic(completionOption);
339
- };
340
- KustoLanguageService.prototype.doCompleteV1 = function (document, position) {
341
- var _this = this;
342
- // TODO: fix typing in CslCommandParser to allow rulesProvider to be query only.
343
- var caretAbsolutePosition = document.offsetAt(position);
344
- // find out what's the current command to only parse this one.
345
- this.parseDocumentV1(document, k.ParseMode.CommandTokensOnly);
346
- var currentCommand = this.getCurrentCommand(document, caretAbsolutePosition);
347
- var commandTextUntilCursor = '';
348
- if (currentCommand) {
349
- var commandStartOffset = currentCommand.AbsoluteStart;
350
- this.parseTextV1(currentCommand.Text, k.ParseMode.TokenizeAllText);
351
- var caretRelativePosition = caretAbsolutePosition - currentCommand.AbsoluteStart;
352
- commandTextUntilCursor = currentCommand.Text.substring(currentCommand.CslExpressionStartPosition, caretRelativePosition);
353
- }
354
- var commandTextWithoutLastWord = this.getCommandWithoutLastWord(commandTextUntilCursor);
355
- var context = this._rulesProvider.AnalyzeCommand$1(commandTextUntilCursor, currentCommand).Context;
356
- var result = { v: null };
357
- this._rulesProvider.TryMatchAnyRule(commandTextWithoutLastWord, result);
358
- var rule = result.v;
359
- if (rule) {
360
- var completionOptions = this.toArray(rule.GetCompletionOptions(context));
361
- // TODO once AppendPipePolicy becomes a public static member of ApplyPolicy in our c# code, and bridge.Net transplies this,
362
- // remove the 'as any' part..
363
- // Also = DefaultApplyPolicy is internal in c# code, so not exposed in d.ts, so we cast it to any.
364
- if (this._languageSettings.newlineAfterPipe &&
365
- rule.DefaultAfterApplyPolicy === Kusto.Data.IntelliSense.ApplyPolicy.AppendPipePolicy) {
366
- rule.DefaultAfterApplyPolicy = this._newlineAppendPipePolicy;
367
- }
368
- var options = completionOptions
369
- .filter(function (option) {
370
- return !(option && option.Value && _this.disabledCompletionItemsV1[option.Value] === option.Kind);
371
- })
372
- .map(function (option, ordinal) {
373
- var _a = _this.getTextToInsert(rule, option), insertText = _a.insertText, insertTextFormat = _a.insertTextFormat;
374
- var helpTopic = k.CslDocumentation.Instance.GetTopic(option);
375
- var item = ls.CompletionItem.create(option.Value);
376
- item.kind = _this.kustoKindToLsKind(option.Kind);
377
- item.insertText = insertText;
378
- item.insertTextFormat = insertTextFormat;
379
- item.sortText = _this.getSortText(ordinal + 1);
380
- item.detail = helpTopic ? helpTopic.ShortDescription : undefined;
381
- item.documentation = helpTopic
382
- ? { value: helpTopic.LongDescription, kind: ls.MarkupKind.Markdown }
383
- : undefined;
384
- return item;
385
- });
386
- return Promise.resolve(ls.CompletionList.create(options));
387
- }
388
- return Promise.resolve(ls.CompletionList.create([]));
389
- };
390
- KustoLanguageService.prototype.doRangeFormat = function (document, range) {
391
- if (!document) {
392
- return Promise.resolve([]);
393
- }
394
- var rangeStartOffset = document.offsetAt(range.start);
395
- var rangeEndOffset = document.offsetAt(range.end);
396
- var commands = this.getFormattedCommandsInDocumentV2(document, rangeStartOffset, rangeEndOffset);
397
- if (!commands.originalRange || commands.formattedCommands.length === 0) {
398
- return Promise.resolve([]);
399
- }
400
- return Promise.resolve([ls.TextEdit.replace(commands.originalRange, commands.formattedCommands.join(''))]);
401
- };
402
- KustoLanguageService.prototype.doDocumentFormat = function (document) {
403
- if (!document) {
404
- return Promise.resolve([]);
405
- }
406
- var startPos = document.positionAt(0);
407
- var endPos = document.positionAt(document.getText().length);
408
- var fullDocRange = ls.Range.create(startPos, endPos);
409
- var formattedDoc = this.getFormattedCommandsInDocumentV2(document).formattedCommands.join('');
410
- return Promise.resolve([ls.TextEdit.replace(fullDocRange, formattedDoc)]);
411
- };
412
- // Method is not triggered, instead doRangeFormat is invoked with the range of the caret's line.
413
- KustoLanguageService.prototype.doCurrentCommandFormat = function (document, caretPosition) {
414
- var offset = document.offsetAt(caretPosition);
415
- var range = this.createRange(document, offset - 1, offset + 1);
416
- return this.doRangeFormat(document, range);
417
- };
418
- KustoLanguageService.prototype.doFolding = function (document) {
419
- if (!document) {
420
- return Promise.resolve([]);
421
- }
422
- return this.getCommandsInDocument(document).then(function (commands) {
423
- return commands.map(function (command) {
424
- // don't count the last empty line as part of the folded range (consider linux, mac, pc newlines)
425
- if (command.text.endsWith('\r\n')) {
426
- command.absoluteEnd -= 2;
427
- }
428
- else if (command.text.endsWith('\r') || command.text.endsWith('\n')) {
429
- --command.absoluteEnd;
430
- }
431
- var startPosition = document.positionAt(command.absoluteStart);
432
- var endPosition = document.positionAt(command.absoluteEnd);
433
- return {
434
- startLine: startPosition.line,
435
- startCharacter: startPosition.character,
436
- endLine: endPosition.line,
437
- endCharacter: endPosition.character,
438
- };
439
- });
440
- });
441
- };
442
- KustoLanguageService.prototype.doValidation = function (document, changeIntervals) {
443
- var _this = this;
444
- // didn't implement validation for v1.
445
- if (!document || !this.isIntellisenseV2()) {
446
- return Promise.resolve([]);
447
- }
448
- var script = this.parseDocumentV2(document);
449
- var blocks = this.toArray(script.Blocks);
450
- if (changeIntervals.length > 0) {
451
- blocks = this.getAffectedBlocks(blocks, changeIntervals);
452
- }
453
- var diagnostics = blocks
454
- .map(function (block) {
455
- var diagnostics = _this.toArray(block.Service.GetDiagnostics());
456
- if (diagnostics) {
457
- return diagnostics;
458
- }
459
- return [];
460
- })
461
- .reduce(function (prev, curr) { return prev.concat(curr); }, []);
462
- var lsDiagnostics = this.toLsDiagnostics(diagnostics, document);
463
- return Promise.resolve(lsDiagnostics);
464
- };
465
- KustoLanguageService.prototype.toLsDiagnostics = function (diagnostics, document) {
466
- return diagnostics
467
- .filter(function (diag) { return diag.HasLocation; })
468
- .map(function (diag) {
469
- var start = document.positionAt(diag.Start);
470
- var end = document.positionAt(diag.Start + diag.Length);
471
- var range = ls.Range.create(start, end);
472
- return ls.Diagnostic.create(range, diag.Message, ls.DiagnosticSeverity.Error);
473
- });
474
- };
475
- /**
476
- * Colorize one or more kusto blocks (a.k.a commands), or just the entire document.
477
- * Supports multi-cursor editing (colorizes blocks on multiple changes).
478
- * @param document The document to colorize
479
- * @param changeIntervals an array containing 0 or more changed intervals. if the array is empty - just colorize the entire row.
480
- * if the array contains a single change - just color the kusto blocks that wraps this change. If multiple changes are provided,
481
- * colorize all blocks that intersect these changes.
482
- * The code will try to only parse once if this is the same command.
483
- */
484
- KustoLanguageService.prototype.doColorization = function (document, changeIntervals) {
485
- var _this = this;
486
- if (!document || !this._languageSettings.useSemanticColorization) {
487
- return Promise.resolve([]);
488
- }
489
- // V1 intellisense
490
- if (!this.isIntellisenseV2()) {
491
- // Handle specific ranges changes (and not the whole doc)
492
- if (changeIntervals.length > 0) {
493
- this.parseDocumentV1(document, k.ParseMode.CommandTokensOnly);
494
- var affectedCommands = this.toArray(this._parser.Results).filter(function (command) {
495
- // a command is affected if it intersects at least on of changed ranges.
496
- return command // command can be null. we're filtering all nulls in the array.
497
- ? changeIntervals.some(function (_a) {
498
- var changeStart = _a.start, changeEnd = _a.end;
499
- // both intervals intersect if either the start or the end of interval A is inside interval B.
500
- // If we deleted something at the end of a command, the interval will not intersect the current command.
501
- // so we also want consider affected commands commands the end where the interval begins.
502
- // hence the + 1.
503
- return (command.AbsoluteStart >= changeStart && command.AbsoluteStart <= changeEnd) ||
504
- (changeStart >= command.AbsoluteStart && changeStart <= command.AbsoluteEnd + 1);
505
- })
506
- : false;
507
- });
508
- // We're not on any command so don't return any classifications.
509
- // this can happen if we're at the and of the file and deleting empty rows (for example).
510
- if (!affectedCommands || affectedCommands.length === 0) {
511
- return Promise.resolve([
512
- {
513
- classifications: [],
514
- absoluteStart: changeIntervals[0].start,
515
- absoluteEnd: changeIntervals[0].end,
516
- },
517
- ]);
518
- }
519
- var results = affectedCommands.map(function (command) {
520
- _this.parseTextV1(command.Text, k.ParseMode.TokenizeAllText);
521
- var k2Classifications = _this.getClassificationsFromParseResult(command.AbsoluteStart);
522
- var classifications = toClassifiedRange(k2Classifications);
523
- return {
524
- classifications: classifications,
525
- absoluteStart: command.AbsoluteStart,
526
- absoluteEnd: command.AbsoluteEnd,
527
- };
528
- });
529
- return Promise.resolve(results);
530
- }
531
- // Entire document requested
532
- this.parseDocumentV1(document, k.ParseMode.TokenizeAllText);
533
- var classifications_1 = this.getClassificationsFromParseResult();
534
- return Promise.resolve([
535
- {
536
- classifications: toClassifiedRange(classifications_1),
537
- absoluteStart: 0,
538
- absoluteEnd: document.getText().length,
539
- },
540
- ]);
541
- }
542
- // V2 intellisense
543
- var script = this.parseDocumentV2(document);
544
- if (changeIntervals.length > 0) {
545
- var blocks_1 = this.toArray(script.Blocks);
546
- var affectedBlocks = this.getAffectedBlocks(blocks_1, changeIntervals);
547
- var result = affectedBlocks.map(function (block) { return ({
548
- classifications: toClassifiedRange(_this.toArray(block.Service.GetClassifications(block.Start, block.End).Classifications)),
549
- absoluteStart: block.Start,
550
- absoluteEnd: block.End,
551
- }); });
552
- return Promise.resolve(result);
553
- }
554
- // Entire document requested
555
- var blocks = this.toArray(script.Blocks);
556
- var classifications = blocks
557
- .map(function (block) {
558
- return _this.toArray(block.Service.GetClassifications(block.Start, block.Length).Classifications);
559
- })
560
- .reduce(function (prev, curr) { return prev.concat(curr); }, []);
561
- return Promise.resolve([
562
- {
563
- classifications: toClassifiedRange(classifications),
564
- absoluteStart: 0,
565
- absoluteEnd: document.getText().length,
566
- },
567
- ]);
568
- };
569
- KustoLanguageService.prototype.getAffectedBlocks = function (blocks, changeIntervals) {
570
- return blocks.filter(function (block) {
571
- // a command is affected if it intersects at least on of changed ranges.
572
- return block // command can be null. we're filtering all nulls in the array.
573
- ? changeIntervals.some(function (_a) {
574
- var changeStart = _a.start, changeEnd = _a.end;
575
- // both intervals intersect if either the start or the end of interval A is inside interval B.
576
- return (block.Start >= changeStart && block.Start <= changeEnd) ||
577
- (changeStart >= block.Start && changeStart <= block.End + 1);
578
- })
579
- : false;
580
- });
581
- };
582
- KustoLanguageService.prototype.setSchema = function (schema) {
583
- var _this = this;
584
- this._schema = schema;
585
- if (this._languageSettings.useIntellisenseV2) {
586
- var kustoJsSchemaV2 = schema && schema.clusterType === 'Engine' ? this.convertToKustoJsSchemaV2(schema) : null;
587
- this._kustoJsSchemaV2 = kustoJsSchemaV2;
588
- this._script = undefined;
589
- this._parsePropertiesV2 = undefined;
590
- }
591
- // since V2 doesn't support control commands, we're initializing V1 intellisense for both cases and we'll going to use V1 intellisense for contorl commands.
592
- return new Promise(function (resolve, reject) {
593
- var kustoJsSchema = schema ? KustoLanguageService.convertToKustoJsSchema(schema) : undefined;
594
- _this._kustoJsSchema = kustoJsSchema;
595
- _this.createRulesProvider(kustoJsSchema, schema.clusterType);
596
- resolve(undefined);
597
- });
598
- };
599
- KustoLanguageService.prototype.setParameters = function (parameters) {
600
- if (!this._languageSettings.useIntellisenseV2 || this._schema.clusterType !== 'Engine') {
601
- throw new Error('setParameters requires intellisense V2 and Engine cluster');
602
- }
603
- this._schema.globalParameters = parameters;
604
- var symbols = parameters.map(function (param) { return KustoLanguageService.createParameterSymbol(param); });
605
- this._kustoJsSchemaV2 = this._kustoJsSchemaV2.WithParameters(KustoLanguageService.toBridgeList(symbols));
606
- return Promise.resolve(undefined);
607
- };
608
- /**
609
- * A combination of normalizeSchema and setSchema
610
- * @param schema schema json as received from .show schema as json
611
- * @param clusterConnectionString cluster connection string
612
- * @param databaseInContextName name of database in context
613
- */
614
- KustoLanguageService.prototype.setSchemaFromShowSchema = function (schema, clusterConnectionString, databaseInContextName, globalParameters) {
615
- var _this = this;
616
- return this.normalizeSchema(schema, clusterConnectionString, databaseInContextName).then(function (normalized) {
617
- return _this.setSchema(__assign(__assign({}, normalized), { globalParameters: globalParameters }));
618
- });
619
- };
620
- /**
621
- * Converts the result of .show schema as json to a normalized schema used by kusto lagnuage service.
622
- * @param schema result of show schema
623
- * @param clusterConnectionString cluster connection string`
624
- * @param databaseInContextName database in context name
625
- */
626
- KustoLanguageService.prototype.normalizeSchema = function (schema, clusterConnectionString, databaseInContextName) {
627
- var databases = Object.keys(schema.Databases)
628
- .map(function (key) { return schema.Databases[key]; })
629
- .map(function (_a) {
630
- var Name = _a.Name, Tables = _a.Tables, Functions = _a.Functions, MinorVersion = _a.MinorVersion, MajorVersion = _a.MajorVersion;
631
- return ({
632
- name: Name,
633
- minorVersion: MinorVersion,
634
- majorVersion: MajorVersion,
635
- tables: Object.keys(Tables)
636
- .map(function (key) { return Tables[key]; })
637
- .map(function (_a) {
638
- var Name = _a.Name, OrderedColumns = _a.OrderedColumns, DocString = _a.DocString, EntityType = _a.EntityType;
639
- return ({
640
- name: Name,
641
- docstring: DocString,
642
- entityType: EntityType,
643
- columns: OrderedColumns.map(function (_a) {
644
- var Name = _a.Name, Type = _a.Type, DocString = _a.DocString, CslType = _a.CslType;
645
- return ({
646
- name: Name,
647
- type: CslType,
648
- docstring: DocString,
649
- });
650
- }),
651
- });
652
- }),
653
- functions: Object.keys(Functions)
654
- .map(function (key) { return Functions[key]; })
655
- .map(function (_a) {
656
- var Name = _a.Name, Body = _a.Body, DocString = _a.DocString, InputParameters = _a.InputParameters;
657
- return ({
658
- name: Name,
659
- body: Body,
660
- docstring: DocString,
661
- inputParameters: InputParameters.map(function (inputParam) { return ({
662
- name: inputParam.Name,
663
- type: inputParam.Type,
664
- cslType: inputParam.CslType,
665
- cslDefaultValue: inputParam.CslDefaultValue,
666
- columns: inputParam.Columns
667
- ? inputParam.Columns.map(function (col) { return ({
668
- name: col.Name,
669
- type: col.Type,
670
- cslType: col.CslType,
671
- }); })
672
- : inputParam.Columns
673
- }); }),
674
- });
675
- }),
676
- });
677
- });
678
- var result = {
679
- clusterType: 'Engine',
680
- cluster: {
681
- connectionString: clusterConnectionString,
682
- databases: databases,
683
- },
684
- database: databases.filter(function (db) { return db.name === databaseInContextName; })[0],
685
- };
686
- return Promise.resolve(result);
687
- };
688
- KustoLanguageService.prototype.getSchema = function () {
689
- return Promise.resolve(this._schema);
690
- };
691
- KustoLanguageService.prototype.getCommandInContext = function (document, cursorOffset) {
692
- return this.isIntellisenseV2()
693
- ? this.getCommandInContextV2(document, cursorOffset)
694
- : this.getCommandInContextV1(document, cursorOffset);
695
- };
696
- KustoLanguageService.prototype.getCommandAndLocationInContext = function (document, cursorOffset) {
697
- // We are going to remove v1 intellisense. no use to keep parity.
698
- if (!document || !this.isIntellisenseV2()) {
699
- return Promise.resolve(null);
700
- }
701
- var script = this.parseDocumentV2(document);
702
- var block = this.getCurrentCommandV2(script, cursorOffset);
703
- if (!block) {
704
- return Promise.resolve(null);
705
- }
706
- var start = document.positionAt(block.Start);
707
- var end = document.positionAt(block.End);
708
- var location = ls.Location.create(document.uri, ls.Range.create(start, end));
709
- var text = block.Text;
710
- return Promise.resolve({
711
- text: text,
712
- location: location,
713
- });
714
- };
715
- KustoLanguageService.prototype.getCommandInContextV1 = function (document, cursorOffset) {
716
- this.parseDocumentV1(document, k.ParseMode.CommandTokensOnly);
717
- var command = this.getCurrentCommand(document, cursorOffset);
718
- if (!command) {
719
- return Promise.resolve(null);
720
- }
721
- return Promise.resolve(command.Text);
722
- };
723
- KustoLanguageService.prototype.getCommandInContextV2 = function (document, cursorOffset) {
724
- if (!document) {
725
- return Promise.resolve(null);
726
- }
727
- var script = this.parseDocumentV2(document);
728
- var block = this.getCurrentCommandV2(script, cursorOffset);
729
- if (!block) {
730
- return Promise.resolve(null);
731
- }
732
- // TODO: do we need to do tricks like V1 is doing in this.getCurrentCommand?
733
- return Promise.resolve(block.Text);
734
- };
735
- /**
736
- * Retrun an array of commands in document. each command contains the range and text.
737
- */
738
- KustoLanguageService.prototype.getCommandsInDocument = function (document) {
739
- if (!document) {
740
- return Promise.resolve([]);
741
- }
742
- return this.isIntellisenseV2()
743
- ? this.getCommandsInDocumentV2(document)
744
- : this.getCommandsInDocumentV1(document);
745
- };
746
- KustoLanguageService.prototype.getCommandsInDocumentV1 = function (document) {
747
- this.parseDocumentV1(document, k.ParseMode.CommandTokensOnly);
748
- var commands = this.toArray(this._parser.Results);
749
- return Promise.resolve(commands.map(function (_a) {
750
- var AbsoluteStart = _a.AbsoluteStart, AbsoluteEnd = _a.AbsoluteEnd, Text = _a.Text;
751
- return ({
752
- absoluteStart: AbsoluteStart,
753
- absoluteEnd: AbsoluteEnd,
754
- text: Text,
755
- });
756
- }));
757
- };
758
- KustoLanguageService.prototype.getFormattedCommandsInDocumentV2 = function (document, rangeStart, rangeEnd) {
759
- var script = this.parseDocumentV2(document);
760
- var commands = this.toArray(script.Blocks).filter(function (command) {
761
- if (!command.Text || command.Text.trim() == '')
762
- return false;
763
- if (rangeStart == null || rangeEnd == null)
764
- return true;
765
- // calculate command end position without \r\n.
766
- var commandEnd = command.End;
767
- var commandText = command.Text;
768
- for (var i = commandText.length - 1; i >= 0; i--) {
769
- if (commandText[i] != '\r' && commandText[i] != '\n') {
770
- break;
771
- }
772
- else {
773
- commandEnd--;
774
- }
775
- }
776
- if (command.Start > rangeStart && command.Start < rangeEnd)
777
- return true;
778
- if (commandEnd > rangeStart && commandEnd < rangeEnd)
779
- return true;
780
- if (command.Start <= rangeStart && commandEnd >= rangeEnd)
781
- return true;
782
- });
783
- if (commands.length === 0) {
784
- return { formattedCommands: [] };
785
- }
786
- var formattedCommands = commands.map(function (command) {
787
- var formatter = Kusto.Language.Editor.FormattingOptions.Default
788
- .WithIndentationSize(4)
789
- .WithInsertMissingTokens(false)
790
- .WithPipeOperatorStyle(Kusto.Language.Editor.PlacementStyle.Smart)
791
- .WithSemicolonStyle(Kusto.Language.Editor.PlacementStyle.None)
792
- .WithBrackettingStyle(k2.BrackettingStyle.Diagonal);
793
- if (rangeStart == null || rangeEnd == null || (rangeStart === command.Start && rangeEnd === command.End)) {
794
- var result = command.Service.GetFormattedText(formatter);
795
- return result.Text;
796
- }
797
- return command.Service.GetFormattedText(formatter).Text;
798
- });
799
- var originalRange = this.createRange(document, commands[0].Start, commands[commands.length - 1].End);
800
- return { formattedCommands: formattedCommands, originalRange: originalRange };
801
- };
802
- KustoLanguageService.prototype.getCommandsInDocumentV2 = function (document) {
803
- var script = this.parseDocumentV2(document);
804
- var commands = this.toArray(script.Blocks).filter(function (command) { return command.Text.trim() != ''; });
805
- return Promise.resolve(commands.map(function (_a) {
806
- var Start = _a.Start, End = _a.End, Text = _a.Text;
807
- return ({ absoluteStart: Start, absoluteEnd: End, text: Text });
808
- }));
809
- };
810
- KustoLanguageService.prototype.getClientDirective = function (text) {
811
- var outParam = { v: null };
812
- var isClientDirective = k.CslCommandParser.IsClientDirective(text, outParam);
813
- return Promise.resolve({
814
- isClientDirective: isClientDirective,
815
- directiveWithoutLeadingComments: outParam.v,
816
- });
817
- };
818
- KustoLanguageService.prototype.getAdminCommand = function (text) {
819
- var outParam = { v: null };
820
- var isAdminCommand = k.CslCommandParser.IsAdminCommand$1(text, outParam);
821
- return Promise.resolve({
822
- isAdminCommand: isAdminCommand,
823
- adminCommandWithoutLeadingComments: outParam.v,
824
- });
825
- };
826
- KustoLanguageService.prototype.findDefinition = function (document, position) {
827
- if (!document || !this.isIntellisenseV2()) {
828
- return Promise.resolve([]);
829
- }
830
- var script = this.parseDocumentV2(document);
831
- var cursorOffset = document.offsetAt(position);
832
- var currentBlock = this.getCurrentCommandV2(script, cursorOffset);
833
- if (!currentBlock) {
834
- return Promise.resolve([]);
835
- }
836
- var relatedInfo = currentBlock.Service.GetRelatedElements(document.offsetAt(position));
837
- var relatedElements = this.toArray(relatedInfo.Elements);
838
- var definition = relatedElements[0];
839
- if (!definition) {
840
- return Promise.resolve([]);
841
- }
842
- var start = document.positionAt(definition.Start);
843
- var end = document.positionAt(definition.End);
844
- var range = ls.Range.create(start, end);
845
- var location = ls.Location.create(document.uri, range);
846
- return Promise.resolve([location]);
847
- };
848
- KustoLanguageService.prototype.findReferences = function (document, position) {
849
- if (!document || !this.isIntellisenseV2()) {
850
- return Promise.resolve([]);
851
- }
852
- var script = this.parseDocumentV2(document);
853
- var cursorOffset = document.offsetAt(position);
854
- var currentBlock = this.getCurrentCommandV2(script, cursorOffset);
855
- if (!currentBlock) {
856
- return Promise.resolve([]);
857
- }
858
- var relatedInfo = currentBlock.Service.GetRelatedElements(document.offsetAt(position));
859
- var relatedElements = this.toArray(relatedInfo.Elements);
860
- if (!relatedElements || relatedElements.length == 0) {
861
- return Promise.resolve([]);
862
- }
863
- var references = relatedElements.map(function (relatedElement) {
864
- var start = document.positionAt(relatedElement.Start);
865
- var end = document.positionAt(relatedElement.End);
866
- var range = ls.Range.create(start, end);
867
- var location = ls.Location.create(document.uri, range);
868
- return location;
869
- });
870
- return Promise.resolve(references);
871
- };
872
- KustoLanguageService.prototype.getQueryParams = function (document, cursorOffset) {
873
- if (!document || !this.isIntellisenseV2()) {
874
- return Promise.resolve([]);
875
- }
876
- var script = this.parseDocumentV2(document);
877
- var parsedAndAnalyzed = this.parseAndAnalyze(document, cursorOffset);
878
- var queryParamStatements = this.toArray(parsedAndAnalyzed.Syntax.GetDescendants(Kusto.Language.Syntax.QueryParametersStatement));
879
- if (!queryParamStatements || queryParamStatements.length == 0) {
880
- return Promise.resolve([]);
881
- }
882
- var queryParams = [];
883
- queryParamStatements.forEach(function (paramStatement) {
884
- paramStatement.WalkElements(function (el) {
885
- return el.ReferencedSymbol && el.ReferencedSymbol.Type
886
- ? queryParams.push({ name: el.ReferencedSymbol.Name, type: el.ReferencedSymbol.Type.Name })
887
- : undefined;
888
- });
889
- });
890
- return Promise.resolve(queryParams);
891
- };
892
- KustoLanguageService.prototype.getRenderInfo = function (document, cursorOffset) {
893
- var _this = this;
894
- var parsedAndAnalyzed = this.parseAndAnalyze(document, cursorOffset);
895
- if (!parsedAndAnalyzed) {
896
- return Promise.resolve(undefined);
897
- }
898
- var renderStatements = this.toArray(parsedAndAnalyzed.Syntax.GetDescendants(Kusto.Language.Syntax.RenderOperator));
899
- if (!renderStatements || renderStatements.length === 0) {
900
- return Promise.resolve(undefined);
901
- }
902
- // assuming a single render statement
903
- var renderStatement = renderStatements[0];
904
- // Start and end relative to block start.
905
- var startOffset = renderStatement.TextStart;
906
- var endOffset = renderStatement.End;
907
- var visualization = renderStatement.ChartType.Text;
908
- var withClause = renderStatement.WithClause;
909
- if (!withClause) {
910
- var info = {
911
- options: {
912
- visualization: visualization,
913
- },
914
- location: { startOffset: startOffset, endOffset: endOffset },
915
- };
916
- return Promise.resolve(info);
917
- }
918
- var properties = this.toArray(withClause.Properties);
919
- var props = properties.reduce(function (prev, property) {
920
- var name = property.Element$1.Name.SimpleName;
921
- switch (name) {
922
- case 'xcolumn':
923
- var value = property.Element$1.Expression.ReferencedSymbol.Name;
924
- prev[name] = value;
925
- break;
926
- case 'ycolumns':
927
- case 'anomalycolumns':
928
- var nameNodes = _this.toArray(property.Element$1.Expression.Names);
929
- var values = nameNodes.map(function (nameNode) { return nameNode.Element$1.SimpleName; });
930
- prev[name] = values;
931
- break;
932
- case 'ymin':
933
- case 'ymax':
934
- var numericVal = parseFloat(property.Element$1.Expression.ConstantValue);
935
- prev[name] = numericVal;
936
- break;
937
- case 'title':
938
- case 'xtitle':
939
- case 'ytitle':
940
- case 'visualization':
941
- case 'series':
942
- var strVal = property.Element$1.Expression.ConstantValue;
943
- prev[name] = strVal;
944
- break;
945
- case 'xaxis':
946
- case 'yaxis':
947
- var scale = property.Element$1.Expression.ConstantValue;
948
- prev[name] = scale;
949
- break;
950
- case 'legend':
951
- var legend = property.Element$1.Expression.ConstantValue;
952
- prev[name] = legend;
953
- break;
954
- case 'ySplit':
955
- var split = property.Element$1.Expression.ConstantValue;
956
- prev[name] = split;
957
- break;
958
- case 'accumulate':
959
- var accumulate = property.Element$1.Expression.ConstantValue;
960
- prev[name] = accumulate;
961
- break;
962
- case 'kind':
963
- var val = property.Element$1.Expression.ConstantValue;
964
- prev[name] = val;
965
- break;
966
- default:
967
- assertNever(name);
968
- }
969
- return prev;
970
- }, {});
971
- var renderOptions = __assign({ visualization: visualization }, props);
972
- var renderInfo = {
973
- options: renderOptions,
974
- location: { startOffset: startOffset, endOffset: endOffset },
975
- };
976
- return Promise.resolve(renderInfo);
977
- };
978
- KustoLanguageService.prototype.getReferencedGlobalParams = function (document, cursorOffset) {
979
- if (!document || !this.isIntellisenseV2()) {
980
- return Promise.resolve([]);
981
- }
982
- var script = this.parseDocumentV2(document);
983
- var currentBlock = this.getCurrentCommandV2(script, cursorOffset);
984
- if (!currentBlock) {
985
- return Promise.resolve([]);
986
- }
987
- var text = currentBlock.Text;
988
- var parsedAndAnalyzed = Kusto.Language.KustoCode.ParseAndAnalyze(text, this._kustoJsSchemaV2);
989
- // We take the ambient parameters
990
- var ambientParameters = this.toArray(this._kustoJsSchemaV2.Parameters);
991
- // We take all referenced symbols in the query
992
- var referencedSymbols = this.toArray(parsedAndAnalyzed.Syntax.GetDescendants(Kusto.Language.Syntax.Expression))
993
- .filter(function (epression) { return epression.ReferencedSymbol !== null; })
994
- .map(function (x) { return x.ReferencedSymbol; });
995
- // The Intersection between them is the ambient parameters that are used in the query.
996
- // Note: Ideally we would use Set here (or at least array.Include), but were' compiling down to es2015.
997
- var intersection = referencedSymbols.filter(function (referencedSymbol) {
998
- return ambientParameters.filter(function (ambientParameter) { return ambientParameter === referencedSymbol; }).length > 0;
999
- });
1000
- var result = intersection.map(function (param) { return ({ name: param.Name, type: param.Type.Name }); });
1001
- return Promise.resolve(result);
1002
- };
1003
- KustoLanguageService.prototype.getGlobalParams = function (document) {
1004
- if (!this.isIntellisenseV2()) {
1005
- return Promise.resolve([]);
1006
- }
1007
- var params = this.toArray(this._kustoJsSchemaV2.Parameters);
1008
- var result = params.map(function (param) { return ({ name: param.Name, type: param.Type.Name }); });
1009
- return Promise.resolve(result);
1010
- };
1011
- KustoLanguageService.prototype.doRename = function (document, position, newName) {
1012
- var _a;
1013
- if (!document || !this.isIntellisenseV2()) {
1014
- return Promise.resolve(undefined);
1015
- }
1016
- var script = this.parseDocumentV2(document);
1017
- var cursorOffset = document.offsetAt(position);
1018
- var currentBLock = this.getCurrentCommandV2(script, cursorOffset);
1019
- if (!currentBLock) {
1020
- return Promise.resolve(undefined);
1021
- }
1022
- var relatedInfo = currentBLock.Service.GetRelatedElements(document.offsetAt(position));
1023
- var relatedElements = this.toArray(relatedInfo.Elements);
1024
- var declarations = relatedElements.filter(function (e) { return e.Kind == k2.RelatedElementKind.Declaration; });
1025
- // A declaration must be one of the elements
1026
- if (!declarations || declarations.length == 0) {
1027
- return Promise.resolve(undefined);
1028
- }
1029
- var edits = relatedElements.map(function (edit) {
1030
- var start = document.positionAt(edit.Start);
1031
- var end = document.positionAt(edit.End);
1032
- var range = ls.Range.create(start, end);
1033
- return ls.TextEdit.replace(range, newName);
1034
- });
1035
- // create a workspace edit
1036
- var workspaceEdit = { changes: (_a = {}, _a[document.uri] = edits, _a) };
1037
- return Promise.resolve(workspaceEdit);
1038
- };
1039
- KustoLanguageService.prototype.doHover = function (document, position) {
1040
- if (!document || !this.isIntellisenseV2()) {
1041
- return Promise.resolve(undefined);
1042
- }
1043
- var script = this.parseDocumentV2(document);
1044
- var cursorOffset = document.offsetAt(position);
1045
- var currentBLock = this.getCurrentCommandV2(script, cursorOffset);
1046
- if (!currentBLock) {
1047
- return Promise.resolve(undefined);
1048
- }
1049
- var isSupported = currentBLock.Service.IsFeatureSupported(k2.CodeServiceFeatures.QuickInfo, cursorOffset);
1050
- if (!isSupported) {
1051
- return Promise.resolve(undefined);
1052
- }
1053
- var quickInfo = currentBLock.Service.GetQuickInfo(cursorOffset);
1054
- if (!quickInfo || !quickInfo.Text) {
1055
- return Promise.resolve(undefined);
1056
- }
1057
- // Instead of just an empty line between the first line (the signature) and the second line (the description)
1058
- // add an horizontal line (* * * in markdown) between them.
1059
- return Promise.resolve({ contents: quickInfo.Text.replace('\n\n', '\n* * *\n') });
1060
- };
1061
- Object.defineProperty(KustoLanguageService, "dummySchema", {
1062
- //#region dummy schema for manual testing
1063
- get: function () {
1064
- var database = {
1065
- majorVersion: 0,
1066
- minorVersion: 0,
1067
- name: 'Kuskus',
1068
- tables: [
1069
- {
1070
- name: 'KustoLogs',
1071
- columns: [
1072
- {
1073
- name: 'Source',
1074
- type: 'string',
1075
- },
1076
- {
1077
- name: 'Timestamp',
1078
- type: 'datetime',
1079
- },
1080
- {
1081
- name: 'Directory',
1082
- type: 'string',
1083
- },
1084
- ],
1085
- docstring: 'A dummy description to test that docstring shows as expected when hovering over a table',
1086
- },
1087
- ],
1088
- functions: [
1089
- {
1090
- name: 'HowBig',
1091
- inputParameters: [
1092
- {
1093
- name: 'T',
1094
- columns: [
1095
- {
1096
- name: 'Timestamp',
1097
- type: 'System.DateTime',
1098
- cslType: 'datetime',
1099
- },
1100
- ],
1101
- },
1102
- ],
1103
- docstring: 'A dummy description to test that docstring shows as expected when hovering over a function',
1104
- body: "{\r\n union \r\n (T | count | project V='Volume', Metric = strcat(Count/1e9, ' Billion records')),\r\n (T | summarize FirstRecord=min(Timestamp)| project V='Volume', Metric = strcat(toint((now()-FirstRecord)/1d), ' Days of data (from: ', format_datetime(FirstRecord, 'yyyy-MM-dd'),')')),\r\n (T | where Timestamp > ago(1h) | count | project V='Velocity', Metric = strcat(Count/1e6, ' Million records / hour')),\r\n (T | summarize Latency=now()-max(Timestamp) | project V='Velocity', Metric = strcat(Latency / 1sec, ' seconds latency')),\r\n (T | take 1 | project V='Variety', Metric=tostring(pack_all()))\r\n | order by V \r\n}",
1105
- },
1106
- {
1107
- name: 'FindCIDPast24h',
1108
- inputParameters: [
1109
- {
1110
- name: 'clientActivityId',
1111
- type: 'System.String',
1112
- cslType: 'string',
1113
- },
1114
- ],
1115
- body: '{ KustoLogs | where Timestamp > now(-1d) | where ClientActivityId == clientActivityId} ',
1116
- },
1117
- ],
1118
- };
1119
- var languageServiceSchema = {
1120
- clusterType: 'Engine',
1121
- cluster: {
1122
- connectionString: 'https://kuskus.kusto.windows.net;fed=true',
1123
- databases: [database],
1124
- },
1125
- database: database,
1126
- };
1127
- return languageServiceSchema;
1128
- },
1129
- enumerable: false,
1130
- configurable: true
1131
- });
1132
- //#endregion
1133
- KustoLanguageService.convertToEntityDataType = function (kustoType) { };
1134
- /**
1135
- * We do not want to expose Bridge.Net generated schema, so we expose a cleaner javascript schema.
1136
- * Here it gets converted to the bridge.Net schema
1137
- * @param schema Language Service schema
1138
- */
1139
- KustoLanguageService.convertToKustoJsSchema = function (schema) {
1140
- switch (schema.clusterType) {
1141
- case 'Engine':
1142
- var currentDatabaseName_1 = schema.database ? schema.database.name : undefined;
1143
- var kCluster = new k.KustoIntelliSenseClusterEntity();
1144
- var kDatabaseInContext_1 = undefined;
1145
- kCluster.ConnectionString = schema.cluster.connectionString;
1146
- var databases_1 = [];
1147
- schema.cluster.databases.forEach(function (database) {
1148
- var kDatabase = new k.KustoIntelliSenseDatabaseEntity();
1149
- kDatabase.Name = database.name;
1150
- var tables = [];
1151
- database.tables.forEach(function (table) {
1152
- var kTable = new k.KustoIntelliSenseTableEntity();
1153
- kTable.Name = table.name;
1154
- var cols = [];
1155
- table.columns.forEach(function (column) {
1156
- var kColumn = new k.KustoIntelliSenseColumnEntity();
1157
- kColumn.Name = column.name;
1158
- kColumn.TypeCode = k.EntityDataType[getEntityDataTypeFromCslType(column.type)];
1159
- cols.push(kColumn);
1160
- });
1161
- kTable.Columns = new Bridge.ArrayEnumerable(cols);
1162
- tables.push(kTable);
1163
- });
1164
- var functions = [];
1165
- database.functions.forEach(function (fn) {
1166
- var kFunction = new k.KustoIntelliSenseFunctionEntity();
1167
- (kFunction.Name = fn.name),
1168
- (kFunction.CallName = s.getCallName(fn)),
1169
- (kFunction.Expression = s.getExpression(fn)),
1170
- functions.push(kFunction);
1171
- });
1172
- kDatabase.Tables = new Bridge.ArrayEnumerable(tables);
1173
- kDatabase.Functions = new Bridge.ArrayEnumerable(functions);
1174
- databases_1.push(kDatabase);
1175
- if (database.name == currentDatabaseName_1) {
1176
- kDatabaseInContext_1 = kDatabase;
1177
- }
1178
- });
1179
- kCluster.Databases = new Bridge.ArrayEnumerable(databases_1);
1180
- var kSchema = new k.KustoIntelliSenseQuerySchema(kCluster, kDatabaseInContext_1);
1181
- return kSchema;
1182
- case 'ClusterManager':
1183
- var accounts = schema.accounts.map(function (account) {
1184
- var kAccount = new k.KustoIntelliSenseAccountEntity();
1185
- kAccount.Name = account;
1186
- return kAccount;
1187
- });
1188
- var services = schema.services.map(function (service) {
1189
- var kService = new k.KustoIntelliSenseServiceEntity();
1190
- kService.Name = service;
1191
- return kService;
1192
- });
1193
- var connectionString = schema.connectionString;
1194
- var result = {
1195
- accounts: accounts,
1196
- services: services,
1197
- connectionString: connectionString,
1198
- };
1199
- return result;
1200
- case 'DataManagement':
1201
- return undefined;
1202
- default:
1203
- return assertNever(schema);
1204
- }
1205
- };
1206
- /**
1207
- * Returns something like '(x: string, y: datetime)'
1208
- * @param params scalar parameters
1209
- */
1210
- KustoLanguageService.scalarParametersToSignature = function (params) {
1211
- var signatureWithoutParens = params.map(function (param) { return param.name + ": " + param.cslType; }).join(', ');
1212
- return "(" + signatureWithoutParens + ")";
1213
- };
1214
- /**
1215
- * Returns something like '(x: string, T: (y: int))'
1216
- * @param params input parameters (tabular or scalar)
1217
- */
1218
- KustoLanguageService.inputParameterToSignature = function (params) {
1219
- var _this = this;
1220
- var signatureWithoutParens = params
1221
- .map(function (param) {
1222
- if (param.columns) {
1223
- var tableSignature = _this.scalarParametersToSignature(param.columns);
1224
- return param.name + ": " + tableSignature;
1225
- }
1226
- else {
1227
- return param.name + ": " + param.cslType;
1228
- }
1229
- })
1230
- .join(', ');
1231
- return "(" + signatureWithoutParens + ")";
1232
- };
1233
- /**
1234
- * converts a function definition to a let statement.
1235
- * @param fn function
1236
- */
1237
- KustoLanguageService.toLetStatement = function (fn) {
1238
- var signature = this.inputParameterToSignature(fn.inputParameters);
1239
- return "let " + fn.name + " = " + signature + " " + fn.body;
1240
- };
1241
- KustoLanguageService.createColumnSymbol = function (col) {
1242
- return new sym.ColumnSymbol(col.name, sym.ScalarTypes.GetSymbol(getCslTypeNameFromClrType(col.type)), col.docstring);
1243
- };
1244
- KustoLanguageService.createParameterSymbol = function (param) {
1245
- var paramSymbol = Kusto.Language.Symbols.ScalarTypes.GetSymbol(getCslTypeNameFromClrType(param.type));
1246
- return new sym.ParameterSymbol(param.name, paramSymbol, null);
1247
- };
1248
- KustoLanguageService.createParameter = function (param) {
1249
- if (!param.columns) {
1250
- var paramSymbol = Kusto.Language.Symbols.ScalarTypes.GetSymbol(getCslTypeNameFromClrType(param.type));
1251
- var expression = void 0;
1252
- if (param.cslDefaultValue && typeof param.cslDefaultValue === "string") {
1253
- var parser = parsing.QueryGrammar.From(Kusto.Language.GlobalState.Default).ConstantExpression;
1254
- expression = parsing.SyntaxParsers.ParseFirst({ prototype: parser }, parser, param.cslDefaultValue);
1255
- }
1256
- return new sym.Parameter.$ctor3(param.name, paramSymbol, null, null, null, false, null, 1, 1, expression, null);
1257
- }
1258
- if (param.columns.length == 0) {
1259
- return new sym.Parameter.ctor(param.name, sym.ParameterTypeKind.Tabular, sym.ArgumentKind.Expression, null, null, false, null, 1, 1, null, null);
1260
- }
1261
- var argumentType = new sym.TableSymbol.ctor(param.columns.map(function (col) { return KustoLanguageService.createColumnSymbol(col); }));
1262
- return new sym.Parameter.$ctor2(param.name, argumentType);
1263
- };
1264
- KustoLanguageService.convertToDatabaseSymbol = function (db, globalState, addFunctions) {
1265
- var createFunctionSymbol = function (fn) {
1266
- var parameters = fn.inputParameters.map(function (param) {
1267
- return KustoLanguageService.createParameter(param);
1268
- });
1269
- // TODO: handle outputColumns (right now it doesn't seem to be implemented for any function).
1270
- return new sym.FunctionSymbol.$ctor16(fn.name, fn.body, KustoLanguageService.toBridgeList(parameters), fn.docstring);
1271
- };
1272
- var createTableSymbol = function (tbl) {
1273
- var columnSymbols = tbl.columns.map(function (col) { return KustoLanguageService.createColumnSymbol(col); });
1274
- var symbol = new sym.TableSymbol.$ctor3(tbl.name, columnSymbols);
1275
- symbol.Description = tbl.docstring;
1276
- switch (tbl.entityType) {
1277
- case 'MaterializedViewTable':
1278
- symbol = symbol.WithIsMaterializedView(true);
1279
- case "ExternalTable":
1280
- symbol = symbol.WithIsExternal(true);
1281
- default:
1282
- }
1283
- return symbol;
1284
- };
1285
- var createDatabaseSymbol = function (db) {
1286
- var tableSymbols = db.tables ? db.tables.map(function (tbl) { return createTableSymbol(tbl); }) : [];
1287
- var functionSymbols = db.functions ? db.functions.map(function (fun) { return createFunctionSymbol(fun); }) : [];
1288
- return new sym.DatabaseSymbol.ctor(db.name, tableSymbols.concat(functionSymbols));
1289
- };
1290
- var databaseSymbol = createDatabaseSymbol(db);
1291
- return databaseSymbol;
1292
- };
1293
- KustoLanguageService.prototype.convertToKustoJsSchemaV2 = function (schema) {
1294
- var cached = this._schemaCache[schema.cluster.connectionString];
1295
- // create a cache entry for the cluster if non yet exists.
1296
- if (!cached) {
1297
- this._schemaCache[schema.cluster.connectionString] = {};
1298
- cached = this._schemaCache[schema.cluster.connectionString];
1299
- }
1300
- // Remove deleted databases from cache
1301
- var schemaDbLookup = schema.cluster.databases.reduce(function (prev, curr) { return (prev[curr.name] = curr); }, {});
1302
- Object.keys(cached).map(function (dbName) {
1303
- if (!schemaDbLookup[dbName]) {
1304
- delete cached.dbName;
1305
- }
1306
- });
1307
- var globalState = GlobalState.Default;
1308
- var currentDatabaseName = schema.database ? schema.database.name : undefined;
1309
- var databaseInContext = undefined;
1310
- // Update out-of-data databses to cache
1311
- var databases = schema.cluster.databases.map(function (db) {
1312
- var shouldIncludeFunctions = db.name === currentDatabaseName;
1313
- var cachedDb = cached[db.name];
1314
- // This is an older version than we have, or we need to parse functions.
1315
- if (!cachedDb ||
1316
- cachedDb.database.majorVersion < db.majorVersion ||
1317
- (shouldIncludeFunctions && !cachedDb.includesFunctions)) {
1318
- // only add functions for the database in context (it's very time consuming)
1319
- var databaseSymbol_1 = KustoLanguageService.convertToDatabaseSymbol(db, globalState, shouldIncludeFunctions);
1320
- cached[db.name] = { database: db, symbol: databaseSymbol_1, includesFunctions: shouldIncludeFunctions };
1321
- }
1322
- var databaseSymbol = cached[db.name].symbol;
1323
- if (db.name === currentDatabaseName) {
1324
- databaseInContext = databaseSymbol;
1325
- }
1326
- return databaseSymbol;
1327
- });
1328
- // Replace new URL due to polyfill issue in IE
1329
- // const hostname = new URL(schema.cluster.connectionString.split(';')[0]).hostname;
1330
- var hostname = schema.cluster.connectionString.match(/(.*\/\/)?([^\/;]*)/)[2];
1331
- var clusterName = hostname.split('.')[0];
1332
- var clusterSymbol = new sym.ClusterSymbol.ctor(clusterName, databases);
1333
- globalState = globalState.WithCluster(clusterSymbol);
1334
- if (databaseInContext) {
1335
- globalState = globalState.WithDatabase(databaseInContext);
1336
- }
1337
- // Inject gloabl parameters to global scope.
1338
- if (schema.globalParameters) {
1339
- var parameters = schema.globalParameters.map(function (param) {
1340
- return KustoLanguageService.createParameterSymbol(param);
1341
- });
1342
- globalState = globalState.WithParameters(KustoLanguageService.toBridgeList(parameters));
1343
- }
1344
- return globalState;
1345
- };
1346
- KustoLanguageService.prototype.getClassificationsFromParseResult = function (offset) {
1347
- var _this = this;
1348
- if (offset === void 0) { offset = 0; }
1349
- var classifications = this.toArray(this._parser.Results)
1350
- .map(function (command) { return _this.toArray(command.Tokens); })
1351
- .reduce(function (prev, curr) { return prev.concat(curr); }, [])
1352
- .map(function (cslCommandToken) {
1353
- var range = new k2.ClassifiedRange(_this.tokenKindToClassificationKind(cslCommandToken.TokenKind), cslCommandToken.AbsoluteStart + offset, cslCommandToken.Length);
1354
- return range;
1355
- });
1356
- return classifications;
1357
- };
1358
- /**
1359
- * trim trailing newlines from range
1360
- */
1361
- KustoLanguageService.trimTrailingNewlineFromRange = function (textInRange, rangeStartOffset, document, range) {
1362
- var currentIndex = textInRange.length - 1;
1363
- while (textInRange[currentIndex] === '\r' || textInRange[currentIndex] === '\n') {
1364
- --currentIndex;
1365
- }
1366
- var newEndOffset = rangeStartOffset + currentIndex + 1;
1367
- var newEndPosition = document.positionAt(newEndOffset);
1368
- var newRange = ls.Range.create(range.start, newEndPosition);
1369
- return newRange;
1370
- };
1371
- /**
1372
- * Maps numbers to strings, such that if a>b numerically, f(a)>f(b) lexicograhically.
1373
- * 1 -> "a", 26 -> "z", 27 -> "za", 28 -> "zb", 52 -> "zz", 53 ->"zza"
1374
- * @param order - The number to be converted to a sorting-string. order should start at 1.
1375
- * @returns A string repenting the order.
1376
- */
1377
- KustoLanguageService.prototype.getSortText = function (order) {
1378
- if (order <= 0) {
1379
- throw new RangeError("order should be a number >= 1. instead got " + order);
1380
- }
1381
- var sortText = '';
1382
- var numCharacters = 26; // "z" - "a" + 1;
1383
- var div = Math.floor(order / numCharacters);
1384
- for (var i = 0; i < div; ++i) {
1385
- sortText += 'z';
1386
- }
1387
- var reminder = order % numCharacters;
1388
- if (reminder > 0) {
1389
- sortText += String.fromCharCode(96 + reminder);
1390
- }
1391
- return sortText;
1392
- };
1393
- /**
1394
- * ParseTextV1 parses the given text with the given parse mode.
1395
- * Additionally - it will make sure not to provide rules provider for non-engine clusters
1396
- * since the only rules provider parse can handle is the engine's. It will try to look for function
1397
- * definitions to colorize and will throw since they're not there.
1398
- * @param text
1399
- * @param parseMode
1400
- */
1401
- KustoLanguageService.prototype.parseTextV1 = function (text, parseMode) {
1402
- this._parser.Parse(this._schema.clusterType === 'Engine' ? this._rulesProvider : null, text, parseMode);
1403
- };
1404
- KustoLanguageService.prototype.parseDocumentV1 = function (document, parseMode) {
1405
- // already parsed a later version, or better parse mode for this uri
1406
- if (this._parsePropertiesV1 &&
1407
- !this._parsePropertiesV1.isParseNeeded(document, this._rulesProvider, parseMode)) {
1408
- return;
1409
- }
1410
- this.parseTextV1(document.getText(), parseMode);
1411
- this._parsePropertiesV1 = new ParseProperties(document.version, document.uri, this._rulesProvider, parseMode);
1412
- };
1413
- KustoLanguageService.prototype.parseDocumentV2 = function (document) {
1414
- if (this._parsePropertiesV2 && !this._parsePropertiesV2.isParseNeeded(document, this._rulesProvider)) {
1415
- return this._script;
1416
- }
1417
- if (!this._script) {
1418
- this._script = k2.CodeScript.From$1(document.getText(), this._kustoJsSchemaV2);
1419
- }
1420
- else {
1421
- this._script = this._script.WithText(document.getText());
1422
- }
1423
- this._parsePropertiesV2 = new ParseProperties(document.version, document.uri);
1424
- return this._script;
1425
- };
1426
- /**
1427
- * Return the CslCommand that wraps the caret location, or undefined if caret is outside any command
1428
- * @param document the document to extract the current command from
1429
- * @param caretAbsolutePosition absolute caret position
1430
- */
1431
- KustoLanguageService.prototype.getCurrentCommand = function (document, caretAbsolutePosition) {
1432
- var commands = this.toArray(this._parser.Results);
1433
- var command = commands.filter(function (command) { return command.AbsoluteStart <= caretAbsolutePosition && command.AbsoluteEnd >= caretAbsolutePosition; })[0];
1434
- // There is an edge case when cursor appears at the end of the command
1435
- // which is not yet considered to be part of the parsed command (therefore: +1 for the AbsoluteEdit property)
1436
- if (!command) {
1437
- command = commands.filter(function (command) {
1438
- return command.AbsoluteStart <= caretAbsolutePosition && command.AbsoluteEnd + 1 >= caretAbsolutePosition;
1439
- })[0];
1440
- // If we have 2 newlines in the end of the text the cursor is _probably_ at the end of the text
1441
- // which this means that we're not actually standing on any command. Thus return null.
1442
- if (!command || command.Text.endsWith('\r\n\r\n')) {
1443
- return null;
1444
- }
1445
- }
1446
- return command;
1447
- };
1448
- KustoLanguageService.prototype.getCurrentCommandV2 = function (script, offset) {
1449
- var block = script.GetBlockAtPosition(offset);
1450
- return block;
1451
- };
1452
- KustoLanguageService.prototype.getTextToInsert = function (rule, option) {
1453
- var beforeApplyInfo = rule.GetBeforeApplyInfo(option.Value);
1454
- var afterApplyInfo = rule.GetAfterApplyInfo(option.Value);
1455
- // this is the basic text to be insterted,
1456
- // but we still need to figure out where the cursor will end up after completion is applied.
1457
- var insertText = beforeApplyInfo.Text || '' + option.Value + afterApplyInfo.Text || '';
1458
- var insertTextFormat = ls.InsertTextFormat.PlainText;
1459
- var snippetFinalTabStop = '$0';
1460
- if (afterApplyInfo.OffsetToken && afterApplyInfo.OffsetPosition) {
1461
- var tokenOffset = insertText.indexOf(afterApplyInfo.OffsetToken);
1462
- if (tokenOffset >= 0) {
1463
- insertText = this.insertToString(insertText, snippetFinalTabStop, tokenOffset - insertText.length + afterApplyInfo.OffsetPosition);
1464
- insertTextFormat = ls.InsertTextFormat.Snippet;
1465
- }
1466
- }
1467
- else if (afterApplyInfo.OffsetPosition) {
1468
- // We only handle negative offsets
1469
- insertText = this.insertToString(insertText, snippetFinalTabStop, afterApplyInfo.OffsetPosition);
1470
- insertTextFormat = ls.InsertTextFormat.Snippet;
1471
- }
1472
- return { insertText: insertText, insertTextFormat: insertTextFormat };
1473
- };
1474
- /**
1475
- * create a new string with stringToInsert inserted at offsetFromEnd in originalString.
1476
- * @param originalString string to insert to
1477
- * @param stringToInsert string to insert
1478
- * @param offsetFromEnd a negative number that will represent offset to the left. 0 means simple concat
1479
- */
1480
- KustoLanguageService.prototype.insertToString = function (originalString, stringToInsert, offsetFromEnd) {
1481
- var index = originalString.length + offsetFromEnd;
1482
- if (offsetFromEnd >= 0 || index < 0) {
1483
- return originalString; // Cannot insert before or after the string
1484
- }
1485
- var before = originalString.substring(0, index);
1486
- var after = originalString.substring(index);
1487
- return before + stringToInsert + after;
1488
- };
1489
- KustoLanguageService.prototype.getCommandWithoutLastWord = function (text) {
1490
- var lastWordRegex = XRegExp('[\\w_]*$', 's');
1491
- return text.replace(lastWordRegex, '');
1492
- };
1493
- KustoLanguageService.prototype.createRulesProvider = function (schema, clusterType) {
1494
- var queryParameters = new (List(String))();
1495
- var availableClusters = new (List(String))();
1496
- this._parser = new k.CslCommandParser();
1497
- if (clusterType == 'Engine') {
1498
- var engineSchema = schema;
1499
- this._rulesProvider =
1500
- this._languageSettings && this._languageSettings.includeControlCommands
1501
- ? new k.CslIntelliSenseRulesProvider.$ctor1(engineSchema.Cluster, engineSchema, queryParameters, availableClusters, null, true, true)
1502
- : new k.CslQueryIntelliSenseRulesProvider.$ctor1(engineSchema.Cluster, engineSchema, queryParameters, availableClusters, null, null, null);
1503
- return;
1504
- }
1505
- if (clusterType === 'DataManagement') {
1506
- this._rulesProvider = new k.DataManagerIntelliSenseRulesProvider(null);
1507
- return;
1508
- }
1509
- // This is a cluster manger
1510
- var _a = schema, accounts = _a.accounts, services = _a.services, connectionString = _a.connectionString;
1511
- new k.KustoIntelliSenseAccountEntity();
1512
- new k.KustoIntelliSenseServiceEntity();
1513
- this._rulesProvider = new k.ClusterManagerIntelliSenseRulesProvider.$ctor1(new Bridge.ArrayEnumerable(accounts), new Bridge.ArrayEnumerable(services), connectionString);
1514
- };
1515
- KustoLanguageService.prototype.kustoKindToLsKind = function (kustoKind) {
1516
- var res = this._kustoKindtolsKind[kustoKind];
1517
- return res ? res : ls.CompletionItemKind.Variable;
1518
- };
1519
- KustoLanguageService.prototype.kustoKindToLsKindV2 = function (kustoKind) {
1520
- var res = this._kustoKindToLsKindV2[kustoKind];
1521
- return res ? res : ls.CompletionItemKind.Variable;
1522
- };
1523
- KustoLanguageService.prototype.createRange = function (document, start, end) {
1524
- return ls.Range.create(document.positionAt(start), document.positionAt(end));
1525
- };
1526
- KustoLanguageService.prototype.toArray = function (bridgeList) {
1527
- return Bridge.toArray(bridgeList);
1528
- };
1529
- KustoLanguageService.toBridgeList = function (array) {
1530
- // copied from bridge.js from the implementation of Enumerable.prototype.toList
1531
- return new (System.Collections.Generic.List$1(System.Object).$ctor1)(array);
1532
- };
1533
- KustoLanguageService.prototype.tokenKindToClassificationKind = function (token) {
1534
- var conversion = this._tokenKindToClassificationKind[token];
1535
- return conversion || k2.ClassificationKind.PlainText;
1536
- };
1537
- KustoLanguageService.prototype.parseAndAnalyze = function (document, cursorOffset) {
1538
- if (!document || !this.isIntellisenseV2()) {
1539
- return undefined;
1540
- }
1541
- var script = this.parseDocumentV2(document);
1542
- var currentBlock = this.getCurrentCommandV2(script, cursorOffset);
1543
- if (!currentBlock) {
1544
- return undefined;
1545
- }
1546
- var text = currentBlock.Text;
1547
- var parsedAndAnalyzed = Kusto.Language.KustoCode.ParseAndAnalyze(text, this._kustoJsSchemaV2);
1548
- return parsedAndAnalyzed;
1549
- };
1550
- return KustoLanguageService;
1551
- }());
1552
- var languageService = new KustoLanguageService(KustoLanguageService.dummySchema, {
1553
- includeControlCommands: true,
1554
- useIntellisenseV2: true,
1555
- useSemanticColorization: true,
1556
- });
1557
- /**
1558
- * Obtain an instance of the kusto language service.
1559
- */
1560
- export function getKustoLanguageService() {
1561
- return languageService;
1562
- }
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ /// <reference path="../../node_modules/@kusto/language-service/Kusto.JavaScript.Client.d.ts" />
13
+ /// <reference path="../../node_modules/@kusto/language-service-next/Kusto.Language.Bridge.d.ts" />
14
+ /// <reference path="../typings/refs.d.ts" />
15
+ import * as s from './schema';
16
+ // polyfill string endsWith
17
+ if (!String.prototype.endsWith) {
18
+ String.prototype.endsWith = function (search, this_len) {
19
+ if (this_len === undefined || this_len > this.length) {
20
+ this_len = this.length;
21
+ }
22
+ return this.substring(this_len - search.length, this_len) === search;
23
+ };
24
+ }
25
+ // If we're running in a web worker - which doesn't share global context with the main thread -
26
+ // we need to manually load dependencies that are not explicit- meaning our non-module dependencies
27
+ // generated by Bridge.Net
28
+ if (typeof document == 'undefined') {
29
+ // monaco will run the worker from vs/base/worker so the relative path needs to be from there (hence going up 2 dirs)
30
+ importScripts('../../language/kusto/bridge.min.js');
31
+ importScripts('../../language/kusto/kusto.javascript.client.min.js');
32
+ importScripts('../../language/kusto/Kusto.Language.Bridge.min.js');
33
+ }
34
+ import * as ls from './../_deps/vscode-languageserver-types/main';
35
+ import * as XRegExp from './../_deps/xregexp/xregexp-all';
36
+ var k = Kusto.Data.IntelliSense;
37
+ var parsing = Kusto.Language.Parsing;
38
+ var k2 = Kusto.Language.Editor;
39
+ var sym = Kusto.Language.Symbols;
40
+ var GlobalState = Kusto.Language.GlobalState;
41
+ import { getCslTypeNameFromClrType, getEntityDataTypeFromCslType } from './schema';
42
+ var List = System.Collections.Generic.List$1;
43
+ function assertNever(x) {
44
+ throw new Error('Unexpected object: ' + x);
45
+ }
46
+ var ParseProperties = /** @class */ (function () {
47
+ function ParseProperties(version, uri, rulesProvider, parseMode) {
48
+ this.version = version;
49
+ this.uri = uri;
50
+ this.rulesProvider = rulesProvider;
51
+ this.parseMode = parseMode;
52
+ }
53
+ ParseProperties.prototype.isParseNeeded = function (document, rulesProvider, parseMode) {
54
+ if (document.uri === this.uri &&
55
+ (!rulesProvider || rulesProvider === this.rulesProvider) &&
56
+ document.version <= this.version &&
57
+ parseMode &&
58
+ parseMode <= this.parseMode) {
59
+ return false;
60
+ }
61
+ return true;
62
+ };
63
+ return ParseProperties;
64
+ }());
65
+ export var TokenKind;
66
+ (function (TokenKind) {
67
+ TokenKind[TokenKind["TableToken"] = 2] = "TableToken";
68
+ TokenKind[TokenKind["TableColumnToken"] = 4] = "TableColumnToken";
69
+ TokenKind[TokenKind["OperatorToken"] = 8] = "OperatorToken";
70
+ TokenKind[TokenKind["SubOperatorToken"] = 16] = "SubOperatorToken";
71
+ TokenKind[TokenKind["CalculatedColumnToken"] = 32] = "CalculatedColumnToken";
72
+ TokenKind[TokenKind["StringLiteralToken"] = 64] = "StringLiteralToken";
73
+ TokenKind[TokenKind["FunctionNameToken"] = 128] = "FunctionNameToken";
74
+ TokenKind[TokenKind["UnknownToken"] = 256] = "UnknownToken";
75
+ TokenKind[TokenKind["CommentToken"] = 512] = "CommentToken";
76
+ TokenKind[TokenKind["PlainTextToken"] = 1024] = "PlainTextToken";
77
+ TokenKind[TokenKind["DataTypeToken"] = 2048] = "DataTypeToken";
78
+ TokenKind[TokenKind["ControlCommandToken"] = 4096] = "ControlCommandToken";
79
+ TokenKind[TokenKind["CommandPartToken"] = 8192] = "CommandPartToken";
80
+ TokenKind[TokenKind["QueryParametersToken"] = 16384] = "QueryParametersToken";
81
+ TokenKind[TokenKind["CslCommandToken"] = 32768] = "CslCommandToken";
82
+ TokenKind[TokenKind["LetVariablesToken"] = 65536] = "LetVariablesToken";
83
+ TokenKind[TokenKind["PluginToken"] = 131072] = "PluginToken";
84
+ TokenKind[TokenKind["BracketRangeToken"] = 262144] = "BracketRangeToken";
85
+ TokenKind[TokenKind["ClientDirectiveToken"] = 524288] = "ClientDirectiveToken";
86
+ })(TokenKind || (TokenKind = {}));
87
+ /**
88
+ * convert the bridge.net object to a plain javascript object that only contains data.
89
+ * @param k2Classifications @kusto/language-service-next bridge.net object
90
+ */
91
+ function toClassifiedRange(k2Classifications) {
92
+ return k2Classifications.map(function (classification) { return ({
93
+ start: classification.Start,
94
+ end: classification.End,
95
+ length: classification.Length,
96
+ kind: classification.Kind,
97
+ }); });
98
+ }
99
+ /**
100
+ * Kusto Language service translates the kusto object model (transpiled from C# by Bridge.Net)
101
+ * to the vscode language server types, which are used by vscode lanugage extensions.
102
+ * This should make things easier in the future to provide a vscode extension based on this translation layer.
103
+ *
104
+ * Further translations, if needed, to support specific editors (Atom, sublime, Etc)
105
+ * should be done on top of this API, since it is (at least meant to be) a standard that is supported by multiple editors.
106
+ *
107
+ * Note1: Currenlty monaco isn't using this object model so further translation will be necessary on calling modules.
108
+ *
109
+ * Note2: This file is responsible for interacting with the kusto object model and exposing Microsoft language service types.
110
+ * An exception to that rule is tokenization (and syntax highlighting which depends on it) -
111
+ * since it's not currently part of the Microosft language service protocol. Thus tokenize() _does_ 'leak' kusto types to the callers.
112
+ */
113
+ var KustoLanguageService = /** @class */ (function () {
114
+ function KustoLanguageService(schema, languageSettings) {
115
+ var _a, _b, _c, _d;
116
+ var _this = this;
117
+ this._toOptionKind = (_a = {},
118
+ _a[k2.CompletionKind.AggregateFunction] = k.OptionKind.FunctionAggregation,
119
+ _a[k2.CompletionKind.BuiltInFunction] = k.OptionKind.FunctionServerSide,
120
+ _a[k2.CompletionKind.Cluster] = k.OptionKind.Database,
121
+ _a[k2.CompletionKind.Column] = k.OptionKind.Column,
122
+ _a[k2.CompletionKind.CommandPrefix] = k.OptionKind.None,
123
+ _a[k2.CompletionKind.Database] = k.OptionKind.Database,
124
+ _a[k2.CompletionKind.DatabaseFunction] = k.OptionKind.FunctionServerSide,
125
+ _a[k2.CompletionKind.Example] = k.OptionKind.None,
126
+ _a[k2.CompletionKind.Identifier] = k.OptionKind.None,
127
+ _a[k2.CompletionKind.Keyword] = k.OptionKind.None,
128
+ _a[k2.CompletionKind.LocalFunction] = k.OptionKind.FunctionLocal,
129
+ _a[k2.CompletionKind.MaterialiedView] = k.OptionKind.MaterializedView,
130
+ _a[k2.CompletionKind.Parameter] = k.OptionKind.Parameter,
131
+ _a[k2.CompletionKind.Punctuation] = k.OptionKind.None,
132
+ _a[k2.CompletionKind.QueryPrefix] = k.OptionKind.Operator,
133
+ _a[k2.CompletionKind.RenderChart] = k.OptionKind.Operator,
134
+ _a[k2.CompletionKind.ScalarInfix] = k.OptionKind.None,
135
+ _a[k2.CompletionKind.ScalarPrefix] = k.OptionKind.None,
136
+ _a[k2.CompletionKind.ScalarType] = k.OptionKind.DataType,
137
+ _a[k2.CompletionKind.Syntax] = k.OptionKind.None,
138
+ _a[k2.CompletionKind.Table] = k.OptionKind.Table,
139
+ _a[k2.CompletionKind.TabularPrefix] = k.OptionKind.None,
140
+ _a[k2.CompletionKind.TabularSuffix] = k.OptionKind.None,
141
+ _a[k2.CompletionKind.Unknown] = k.OptionKind.None,
142
+ _a[k2.CompletionKind.Variable] = k.OptionKind.Parameter,
143
+ _a[k2.CompletionKind.Option] = k.OptionKind.Option,
144
+ _a);
145
+ this.disabledCompletionItemsV2 = {
146
+ // render charts
147
+ ladderchart: k2.CompletionKind.RenderChart,
148
+ pivotchart: k2.CompletionKind.RenderChart,
149
+ timeline: k2.CompletionKind.RenderChart,
150
+ timepivot: k2.CompletionKind.RenderChart,
151
+ '3Dchart': k2.CompletionKind.RenderChart,
152
+ list: k2.CompletionKind.RenderChart,
153
+ };
154
+ this.isIntellisenseV2 = function () {
155
+ return _this._languageSettings.useIntellisenseV2 && _this._schema && _this._schema.clusterType === 'Engine';
156
+ };
157
+ this.disabledCompletionItemsV1 = {
158
+ capacity: k.OptionKind.Policy,
159
+ callout: k.OptionKind.Policy,
160
+ encoding: k.OptionKind.Policy,
161
+ batching: k.OptionKind.Policy,
162
+ querythrottling: k.OptionKind.Policy,
163
+ merge: k.OptionKind.Policy,
164
+ querylimit: k.OptionKind.Policy,
165
+ rowstore: k.OptionKind.Policy,
166
+ streamingingestion: k.OptionKind.Policy,
167
+ restricted_view_access: k.OptionKind.Policy,
168
+ sharding: k.OptionKind.Policy,
169
+ 'restricted-viewers': k.OptionKind.Policy,
170
+ attach: k.OptionKind.Command,
171
+ purge: k.OptionKind.Command,
172
+ };
173
+ this._kustoKindtolsKind = (_b = {},
174
+ _b[k.OptionKind.None] = ls.CompletionItemKind.Interface,
175
+ _b[k.OptionKind.Operator] = ls.CompletionItemKind.Method,
176
+ _b[k.OptionKind.Command] = ls.CompletionItemKind.Method,
177
+ _b[k.OptionKind.Service] = ls.CompletionItemKind.Class,
178
+ _b[k.OptionKind.Policy] = ls.CompletionItemKind.Reference,
179
+ _b[k.OptionKind.Database] = ls.CompletionItemKind.Class,
180
+ _b[k.OptionKind.Table] = ls.CompletionItemKind.Class,
181
+ _b[k.OptionKind.DataType] = ls.CompletionItemKind.Class,
182
+ _b[k.OptionKind.Literal] = ls.CompletionItemKind.Property,
183
+ _b[k.OptionKind.Parameter] = ls.CompletionItemKind.Variable,
184
+ _b[k.OptionKind.IngestionMapping] = ls.CompletionItemKind.Variable,
185
+ _b[k.OptionKind.ExpressionFunction] = ls.CompletionItemKind.Variable,
186
+ _b[k.OptionKind.Option] = ls.CompletionItemKind.Interface,
187
+ _b[k.OptionKind.OptionKind] = ls.CompletionItemKind.Interface,
188
+ _b[k.OptionKind.OptionRender] = ls.CompletionItemKind.Interface,
189
+ _b[k.OptionKind.Column] = ls.CompletionItemKind.Function,
190
+ _b[k.OptionKind.ColumnString] = ls.CompletionItemKind.Field,
191
+ _b[k.OptionKind.ColumnNumeric] = ls.CompletionItemKind.Field,
192
+ _b[k.OptionKind.ColumnDateTime] = ls.CompletionItemKind.Field,
193
+ _b[k.OptionKind.ColumnTimespan] = ls.CompletionItemKind.Field,
194
+ _b[k.OptionKind.FunctionServerSide] = ls.CompletionItemKind.Field,
195
+ _b[k.OptionKind.FunctionAggregation] = ls.CompletionItemKind.Field,
196
+ _b[k.OptionKind.FunctionFilter] = ls.CompletionItemKind.Field,
197
+ _b[k.OptionKind.FunctionScalar] = ls.CompletionItemKind.Field,
198
+ _b[k.OptionKind.ClientDirective] = ls.CompletionItemKind.Enum,
199
+ _b);
200
+ this._kustoKindToLsKindV2 = (_c = {},
201
+ _c[k2.CompletionKind.AggregateFunction] = ls.CompletionItemKind.Field,
202
+ _c[k2.CompletionKind.BuiltInFunction] = ls.CompletionItemKind.Field,
203
+ _c[k2.CompletionKind.Cluster] = ls.CompletionItemKind.Class,
204
+ _c[k2.CompletionKind.Column] = ls.CompletionItemKind.Function,
205
+ _c[k2.CompletionKind.CommandPrefix] = ls.CompletionItemKind.Field,
206
+ _c[k2.CompletionKind.Database] = ls.CompletionItemKind.Class,
207
+ _c[k2.CompletionKind.DatabaseFunction] = ls.CompletionItemKind.Field,
208
+ _c[k2.CompletionKind.Example] = ls.CompletionItemKind.Text,
209
+ _c[k2.CompletionKind.Identifier] = ls.CompletionItemKind.Method,
210
+ _c[k2.CompletionKind.Keyword] = ls.CompletionItemKind.Method,
211
+ _c[k2.CompletionKind.LocalFunction] = ls.CompletionItemKind.Field,
212
+ _c[k2.CompletionKind.MaterialiedView] = ls.CompletionItemKind.Class,
213
+ _c[k2.CompletionKind.Parameter] = ls.CompletionItemKind.Variable,
214
+ _c[k2.CompletionKind.Punctuation] = ls.CompletionItemKind.Interface,
215
+ _c[k2.CompletionKind.QueryPrefix] = ls.CompletionItemKind.Function,
216
+ _c[k2.CompletionKind.RenderChart] = ls.CompletionItemKind.Method,
217
+ _c[k2.CompletionKind.ScalarInfix] = ls.CompletionItemKind.Field,
218
+ _c[k2.CompletionKind.ScalarPrefix] = ls.CompletionItemKind.Field,
219
+ _c[k2.CompletionKind.ScalarType] = ls.CompletionItemKind.TypeParameter,
220
+ _c[k2.CompletionKind.Syntax] = ls.CompletionItemKind.Method,
221
+ _c[k2.CompletionKind.Table] = ls.CompletionItemKind.Class,
222
+ _c[k2.CompletionKind.TabularPrefix] = ls.CompletionItemKind.Field,
223
+ // datatable, externaldata
224
+ _c[k2.CompletionKind.TabularSuffix] = ls.CompletionItemKind.Field,
225
+ _c[k2.CompletionKind.Unknown] = ls.CompletionItemKind.Interface,
226
+ _c[k2.CompletionKind.Variable] = ls.CompletionItemKind.Variable,
227
+ _c[k2.CompletionKind.Option] = ls.CompletionItemKind.Text,
228
+ _c);
229
+ this._tokenKindToClassificationKind = (_d = {},
230
+ _d[TokenKind.TableToken] = k2.ClassificationKind.Table,
231
+ _d[TokenKind.TableColumnToken] = k2.ClassificationKind.Column,
232
+ _d[TokenKind.OperatorToken] = k2.ClassificationKind.QueryOperator,
233
+ _d[TokenKind.SubOperatorToken] = k2.ClassificationKind.Function,
234
+ _d[TokenKind.CalculatedColumnToken] = k2.ClassificationKind.Column,
235
+ _d[TokenKind.StringLiteralToken] = k2.ClassificationKind.Literal,
236
+ _d[TokenKind.FunctionNameToken] = k2.ClassificationKind.Function,
237
+ _d[TokenKind.UnknownToken] = k2.ClassificationKind.PlainText,
238
+ _d[TokenKind.CommentToken] = k2.ClassificationKind.Comment,
239
+ _d[TokenKind.PlainTextToken] = k2.ClassificationKind.PlainText,
240
+ _d[TokenKind.DataTypeToken] = k2.ClassificationKind.Type,
241
+ _d[TokenKind.ControlCommandToken] = k2.ClassificationKind.PlainText,
242
+ _d[TokenKind.CommandPartToken] = k2.ClassificationKind.PlainText,
243
+ _d[TokenKind.QueryParametersToken] = k2.ClassificationKind.QueryParameter,
244
+ _d[TokenKind.CslCommandToken] = k2.ClassificationKind.Keyword,
245
+ _d[TokenKind.LetVariablesToken] = k2.ClassificationKind.Identifier,
246
+ _d[TokenKind.PluginToken] = k2.ClassificationKind.Function,
247
+ _d[TokenKind.BracketRangeToken] = k2.ClassificationKind.Keyword,
248
+ _d[TokenKind.ClientDirectiveToken] = k2.ClassificationKind.Keyword,
249
+ _d);
250
+ this._schemaCache = {};
251
+ this._kustoJsSchema = KustoLanguageService.convertToKustoJsSchema(schema);
252
+ this._kustoJsSchemaV2 = this.convertToKustoJsSchemaV2(schema);
253
+ this._schema = schema;
254
+ this.configure(languageSettings);
255
+ this._newlineAppendPipePolicy = new Kusto.Data.IntelliSense.ApplyPolicy();
256
+ this._newlineAppendPipePolicy.Text = '\n| ';
257
+ }
258
+ KustoLanguageService.prototype.configure = function (languageSettings) {
259
+ this._languageSettings = languageSettings;
260
+ // Since we're still reverting to V1 intellisense for control commands, we need to update the rules provider
261
+ // (which is a notion of V1 intellisense).
262
+ this.createRulesProvider(this._kustoJsSchema, this._schema.clusterType);
263
+ };
264
+ KustoLanguageService.prototype.doComplete = function (document, position) {
265
+ return this.isIntellisenseV2() ? this.doCompleteV2(document, position) : this.doCompleteV1(document, position);
266
+ };
267
+ KustoLanguageService.prototype.doCompleteV2 = function (document, position) {
268
+ var _this = this;
269
+ if (!document) {
270
+ return Promise.resolve(ls.CompletionList.create([]));
271
+ }
272
+ var script = this.parseDocumentV2(document);
273
+ var cursorOffset = document.offsetAt(position);
274
+ var currentcommand = this.getCurrentCommandV2(script, cursorOffset);
275
+ var completionItems = currentcommand.Service.GetCompletionItems(cursorOffset);
276
+ var disabledItems = this.disabledCompletionItemsV2;
277
+ if (this._languageSettings.disabledCompletionItems) {
278
+ this._languageSettings.disabledCompletionItems.map(function (item) {
279
+ // logic will treat unknown as a '*' wildcard, meaning that if the key is in the object
280
+ // the completion item will be suppressed.
281
+ disabledItems[item] = k2.CompletionKind.Unknown;
282
+ });
283
+ }
284
+ var items = this.toArray(completionItems.Items)
285
+ .filter(function (item) {
286
+ return !(item &&
287
+ item.MatchText &&
288
+ disabledItems[item.MatchText] !== undefined &&
289
+ (disabledItems[item.MatchText] === k2.CompletionKind.Unknown ||
290
+ disabledItems[item.MatchText] === item.Kind));
291
+ })
292
+ .map(function (kItem, i) {
293
+ var v1CompletionOption = new k.CompletionOption(_this._toOptionKind[kItem.Kind] || k.OptionKind.None, kItem.DisplayText);
294
+ var helpTopic = _this.getTopic(v1CompletionOption);
295
+ // If we have AfterText it means that the cursor should no be placed at end of suggested text.
296
+ // In that case we switch to snippet format and represent the point where the cursor should be as
297
+ // as '\$0'
298
+ var _a = kItem.AfterText && kItem.AfterText.length > 0
299
+ ? {
300
+ textToInsert: kItem.EditText + '$0' + kItem.AfterText,
301
+ format: ls.InsertTextFormat.Snippet,
302
+ }
303
+ : {
304
+ textToInsert: kItem.EditText,
305
+ format: ls.InsertTextFormat.PlainText,
306
+ }, textToInsert = _a.textToInsert, format = _a.format;
307
+ var lsItem = ls.CompletionItem.create(kItem.DisplayText);
308
+ var startPosition = document.positionAt(completionItems.EditStart);
309
+ var endPosition = document.positionAt(completionItems.EditStart + completionItems.EditLength);
310
+ lsItem.textEdit = ls.TextEdit.replace(ls.Range.create(startPosition, endPosition), textToInsert);
311
+ lsItem.sortText = _this.getSortText(i + 1);
312
+ lsItem.kind = _this.kustoKindToLsKindV2(kItem.Kind);
313
+ lsItem.insertTextFormat = format;
314
+ lsItem.detail = helpTopic ? helpTopic.ShortDescription : undefined;
315
+ lsItem.documentation = helpTopic
316
+ ? { value: helpTopic.LongDescription, kind: ls.MarkupKind.Markdown }
317
+ : undefined;
318
+ return lsItem;
319
+ });
320
+ return Promise.resolve(ls.CompletionList.create(items));
321
+ };
322
+ /**
323
+ * when trying to get a topic we need the funtion name (abs, tolower, ETC).
324
+ * The problem is that the 'Value' string also contains the arguments (e.g abs(nubmer)), which means that we are
325
+ * not able to correlate the option with its documentation.
326
+ * This piece of code tries to strip this hwne getting topic.
327
+ * @param completionOption the Completion option
328
+ */
329
+ KustoLanguageService.prototype.getTopic = function (completionOption) {
330
+ if (completionOption.Kind == k.OptionKind.FunctionScalar ||
331
+ completionOption.Kind == k.OptionKind.FunctionAggregation) {
332
+ // from a value like 'abs(number)' remove the '(number)' so that only 'abs' will remain
333
+ var indexOfParen = completionOption.Value.indexOf('(');
334
+ if (indexOfParen >= 0) {
335
+ completionOption = new k.CompletionOption(completionOption.Kind, completionOption.Value.substring(0, indexOfParen));
336
+ }
337
+ }
338
+ return k.CslDocumentation.Instance.GetTopic(completionOption);
339
+ };
340
+ KustoLanguageService.prototype.doCompleteV1 = function (document, position) {
341
+ var _this = this;
342
+ // TODO: fix typing in CslCommandParser to allow rulesProvider to be query only.
343
+ var caretAbsolutePosition = document.offsetAt(position);
344
+ // find out what's the current command to only parse this one.
345
+ this.parseDocumentV1(document, k.ParseMode.CommandTokensOnly);
346
+ var currentCommand = this.getCurrentCommand(document, caretAbsolutePosition);
347
+ var commandTextUntilCursor = '';
348
+ if (currentCommand) {
349
+ var commandStartOffset = currentCommand.AbsoluteStart;
350
+ this.parseTextV1(currentCommand.Text, k.ParseMode.TokenizeAllText);
351
+ var caretRelativePosition = caretAbsolutePosition - currentCommand.AbsoluteStart;
352
+ commandTextUntilCursor = currentCommand.Text.substring(currentCommand.CslExpressionStartPosition, caretRelativePosition);
353
+ }
354
+ var commandTextWithoutLastWord = this.getCommandWithoutLastWord(commandTextUntilCursor);
355
+ var context = this._rulesProvider.AnalyzeCommand$1(commandTextUntilCursor, currentCommand).Context;
356
+ var result = { v: null };
357
+ this._rulesProvider.TryMatchAnyRule(commandTextWithoutLastWord, result);
358
+ var rule = result.v;
359
+ if (rule) {
360
+ var completionOptions = this.toArray(rule.GetCompletionOptions(context));
361
+ // TODO once AppendPipePolicy becomes a public static member of ApplyPolicy in our c# code, and bridge.Net transplies this,
362
+ // remove the 'as any' part..
363
+ // Also = DefaultApplyPolicy is internal in c# code, so not exposed in d.ts, so we cast it to any.
364
+ if (this._languageSettings.newlineAfterPipe &&
365
+ rule.DefaultAfterApplyPolicy === Kusto.Data.IntelliSense.ApplyPolicy.AppendPipePolicy) {
366
+ rule.DefaultAfterApplyPolicy = this._newlineAppendPipePolicy;
367
+ }
368
+ var options = completionOptions
369
+ .filter(function (option) {
370
+ return !(option && option.Value && _this.disabledCompletionItemsV1[option.Value] === option.Kind);
371
+ })
372
+ .map(function (option, ordinal) {
373
+ var _a = _this.getTextToInsert(rule, option), insertText = _a.insertText, insertTextFormat = _a.insertTextFormat;
374
+ var helpTopic = k.CslDocumentation.Instance.GetTopic(option);
375
+ var item = ls.CompletionItem.create(option.Value);
376
+ item.kind = _this.kustoKindToLsKind(option.Kind);
377
+ item.insertText = insertText;
378
+ item.insertTextFormat = insertTextFormat;
379
+ item.sortText = _this.getSortText(ordinal + 1);
380
+ item.detail = helpTopic ? helpTopic.ShortDescription : undefined;
381
+ item.documentation = helpTopic
382
+ ? { value: helpTopic.LongDescription, kind: ls.MarkupKind.Markdown }
383
+ : undefined;
384
+ return item;
385
+ });
386
+ return Promise.resolve(ls.CompletionList.create(options));
387
+ }
388
+ return Promise.resolve(ls.CompletionList.create([]));
389
+ };
390
+ KustoLanguageService.prototype.doRangeFormat = function (document, range) {
391
+ if (!document) {
392
+ return Promise.resolve([]);
393
+ }
394
+ var rangeStartOffset = document.offsetAt(range.start);
395
+ var rangeEndOffset = document.offsetAt(range.end);
396
+ var commands = this.getFormattedCommandsInDocumentV2(document, rangeStartOffset, rangeEndOffset);
397
+ if (!commands.originalRange || commands.formattedCommands.length === 0) {
398
+ return Promise.resolve([]);
399
+ }
400
+ return Promise.resolve([ls.TextEdit.replace(commands.originalRange, commands.formattedCommands.join(''))]);
401
+ };
402
+ KustoLanguageService.prototype.doDocumentFormat = function (document) {
403
+ if (!document) {
404
+ return Promise.resolve([]);
405
+ }
406
+ var startPos = document.positionAt(0);
407
+ var endPos = document.positionAt(document.getText().length);
408
+ var fullDocRange = ls.Range.create(startPos, endPos);
409
+ var formattedDoc = this.getFormattedCommandsInDocumentV2(document).formattedCommands.join('');
410
+ return Promise.resolve([ls.TextEdit.replace(fullDocRange, formattedDoc)]);
411
+ };
412
+ // Method is not triggered, instead doRangeFormat is invoked with the range of the caret's line.
413
+ KustoLanguageService.prototype.doCurrentCommandFormat = function (document, caretPosition) {
414
+ var offset = document.offsetAt(caretPosition);
415
+ var range = this.createRange(document, offset - 1, offset + 1);
416
+ return this.doRangeFormat(document, range);
417
+ };
418
+ KustoLanguageService.prototype.doFolding = function (document) {
419
+ if (!document) {
420
+ return Promise.resolve([]);
421
+ }
422
+ return this.getCommandsInDocument(document).then(function (commands) {
423
+ return commands.map(function (command) {
424
+ // don't count the last empty line as part of the folded range (consider linux, mac, pc newlines)
425
+ if (command.text.endsWith('\r\n')) {
426
+ command.absoluteEnd -= 2;
427
+ }
428
+ else if (command.text.endsWith('\r') || command.text.endsWith('\n')) {
429
+ --command.absoluteEnd;
430
+ }
431
+ var startPosition = document.positionAt(command.absoluteStart);
432
+ var endPosition = document.positionAt(command.absoluteEnd);
433
+ return {
434
+ startLine: startPosition.line,
435
+ startCharacter: startPosition.character,
436
+ endLine: endPosition.line,
437
+ endCharacter: endPosition.character,
438
+ };
439
+ });
440
+ });
441
+ };
442
+ KustoLanguageService.prototype.doValidation = function (document, changeIntervals) {
443
+ var _this = this;
444
+ // didn't implement validation for v1.
445
+ if (!document || !this.isIntellisenseV2()) {
446
+ return Promise.resolve([]);
447
+ }
448
+ var script = this.parseDocumentV2(document);
449
+ var blocks = this.toArray(script.Blocks);
450
+ if (changeIntervals.length > 0) {
451
+ blocks = this.getAffectedBlocks(blocks, changeIntervals);
452
+ }
453
+ var diagnostics = blocks
454
+ .map(function (block) {
455
+ var diagnostics = _this.toArray(block.Service.GetDiagnostics());
456
+ if (diagnostics) {
457
+ return diagnostics;
458
+ }
459
+ return [];
460
+ })
461
+ .reduce(function (prev, curr) { return prev.concat(curr); }, []);
462
+ var lsDiagnostics = this.toLsDiagnostics(diagnostics, document);
463
+ return Promise.resolve(lsDiagnostics);
464
+ };
465
+ KustoLanguageService.prototype.toLsDiagnostics = function (diagnostics, document) {
466
+ return diagnostics
467
+ .filter(function (diag) { return diag.HasLocation; })
468
+ .map(function (diag) {
469
+ var start = document.positionAt(diag.Start);
470
+ var end = document.positionAt(diag.Start + diag.Length);
471
+ var range = ls.Range.create(start, end);
472
+ return ls.Diagnostic.create(range, diag.Message, ls.DiagnosticSeverity.Error);
473
+ });
474
+ };
475
+ /**
476
+ * Colorize one or more kusto blocks (a.k.a commands), or just the entire document.
477
+ * Supports multi-cursor editing (colorizes blocks on multiple changes).
478
+ * @param document The document to colorize
479
+ * @param changeIntervals an array containing 0 or more changed intervals. if the array is empty - just colorize the entire row.
480
+ * if the array contains a single change - just color the kusto blocks that wraps this change. If multiple changes are provided,
481
+ * colorize all blocks that intersect these changes.
482
+ * The code will try to only parse once if this is the same command.
483
+ */
484
+ KustoLanguageService.prototype.doColorization = function (document, changeIntervals) {
485
+ var _this = this;
486
+ if (!document || !this._languageSettings.useSemanticColorization) {
487
+ return Promise.resolve([]);
488
+ }
489
+ // V1 intellisense
490
+ if (!this.isIntellisenseV2()) {
491
+ // Handle specific ranges changes (and not the whole doc)
492
+ if (changeIntervals.length > 0) {
493
+ this.parseDocumentV1(document, k.ParseMode.CommandTokensOnly);
494
+ var affectedCommands = this.toArray(this._parser.Results).filter(function (command) {
495
+ // a command is affected if it intersects at least on of changed ranges.
496
+ return command // command can be null. we're filtering all nulls in the array.
497
+ ? changeIntervals.some(function (_a) {
498
+ var changeStart = _a.start, changeEnd = _a.end;
499
+ // both intervals intersect if either the start or the end of interval A is inside interval B.
500
+ // If we deleted something at the end of a command, the interval will not intersect the current command.
501
+ // so we also want consider affected commands commands the end where the interval begins.
502
+ // hence the + 1.
503
+ return (command.AbsoluteStart >= changeStart && command.AbsoluteStart <= changeEnd) ||
504
+ (changeStart >= command.AbsoluteStart && changeStart <= command.AbsoluteEnd + 1);
505
+ })
506
+ : false;
507
+ });
508
+ // We're not on any command so don't return any classifications.
509
+ // this can happen if we're at the and of the file and deleting empty rows (for example).
510
+ if (!affectedCommands || affectedCommands.length === 0) {
511
+ return Promise.resolve([
512
+ {
513
+ classifications: [],
514
+ absoluteStart: changeIntervals[0].start,
515
+ absoluteEnd: changeIntervals[0].end,
516
+ },
517
+ ]);
518
+ }
519
+ var results = affectedCommands.map(function (command) {
520
+ _this.parseTextV1(command.Text, k.ParseMode.TokenizeAllText);
521
+ var k2Classifications = _this.getClassificationsFromParseResult(command.AbsoluteStart);
522
+ var classifications = toClassifiedRange(k2Classifications);
523
+ return {
524
+ classifications: classifications,
525
+ absoluteStart: command.AbsoluteStart,
526
+ absoluteEnd: command.AbsoluteEnd,
527
+ };
528
+ });
529
+ return Promise.resolve(results);
530
+ }
531
+ // Entire document requested
532
+ this.parseDocumentV1(document, k.ParseMode.TokenizeAllText);
533
+ var classifications_1 = this.getClassificationsFromParseResult();
534
+ return Promise.resolve([
535
+ {
536
+ classifications: toClassifiedRange(classifications_1),
537
+ absoluteStart: 0,
538
+ absoluteEnd: document.getText().length,
539
+ },
540
+ ]);
541
+ }
542
+ // V2 intellisense
543
+ var script = this.parseDocumentV2(document);
544
+ if (changeIntervals.length > 0) {
545
+ var blocks_1 = this.toArray(script.Blocks);
546
+ var affectedBlocks = this.getAffectedBlocks(blocks_1, changeIntervals);
547
+ var result = affectedBlocks.map(function (block) { return ({
548
+ classifications: toClassifiedRange(_this.toArray(block.Service.GetClassifications(block.Start, block.End).Classifications)),
549
+ absoluteStart: block.Start,
550
+ absoluteEnd: block.End,
551
+ }); });
552
+ return Promise.resolve(result);
553
+ }
554
+ // Entire document requested
555
+ var blocks = this.toArray(script.Blocks);
556
+ var classifications = blocks
557
+ .map(function (block) {
558
+ return _this.toArray(block.Service.GetClassifications(block.Start, block.Length).Classifications);
559
+ })
560
+ .reduce(function (prev, curr) { return prev.concat(curr); }, []);
561
+ return Promise.resolve([
562
+ {
563
+ classifications: toClassifiedRange(classifications),
564
+ absoluteStart: 0,
565
+ absoluteEnd: document.getText().length,
566
+ },
567
+ ]);
568
+ };
569
+ KustoLanguageService.prototype.getAffectedBlocks = function (blocks, changeIntervals) {
570
+ return blocks.filter(function (block) {
571
+ // a command is affected if it intersects at least on of changed ranges.
572
+ return block // command can be null. we're filtering all nulls in the array.
573
+ ? changeIntervals.some(function (_a) {
574
+ var changeStart = _a.start, changeEnd = _a.end;
575
+ // both intervals intersect if either the start or the end of interval A is inside interval B.
576
+ return (block.Start >= changeStart && block.Start <= changeEnd) ||
577
+ (changeStart >= block.Start && changeStart <= block.End + 1);
578
+ })
579
+ : false;
580
+ });
581
+ };
582
+ KustoLanguageService.prototype.setSchema = function (schema) {
583
+ var _this = this;
584
+ this._schema = schema;
585
+ if (this._languageSettings.useIntellisenseV2) {
586
+ var kustoJsSchemaV2 = schema && schema.clusterType === 'Engine' ? this.convertToKustoJsSchemaV2(schema) : null;
587
+ this._kustoJsSchemaV2 = kustoJsSchemaV2;
588
+ this._script = undefined;
589
+ this._parsePropertiesV2 = undefined;
590
+ }
591
+ // since V2 doesn't support control commands, we're initializing V1 intellisense for both cases and we'll going to use V1 intellisense for contorl commands.
592
+ return new Promise(function (resolve, reject) {
593
+ var kustoJsSchema = schema ? KustoLanguageService.convertToKustoJsSchema(schema) : undefined;
594
+ _this._kustoJsSchema = kustoJsSchema;
595
+ _this.createRulesProvider(kustoJsSchema, schema.clusterType);
596
+ resolve(undefined);
597
+ });
598
+ };
599
+ KustoLanguageService.prototype.setParameters = function (parameters) {
600
+ if (!this._languageSettings.useIntellisenseV2 || this._schema.clusterType !== 'Engine') {
601
+ throw new Error('setParameters requires intellisense V2 and Engine cluster');
602
+ }
603
+ this._schema.globalParameters = parameters;
604
+ var symbols = parameters.map(function (param) { return KustoLanguageService.createParameterSymbol(param); });
605
+ this._kustoJsSchemaV2 = this._kustoJsSchemaV2.WithParameters(KustoLanguageService.toBridgeList(symbols));
606
+ return Promise.resolve(undefined);
607
+ };
608
+ /**
609
+ * A combination of normalizeSchema and setSchema
610
+ * @param schema schema json as received from .show schema as json
611
+ * @param clusterConnectionString cluster connection string
612
+ * @param databaseInContextName name of database in context
613
+ */
614
+ KustoLanguageService.prototype.setSchemaFromShowSchema = function (schema, clusterConnectionString, databaseInContextName, globalParameters) {
615
+ var _this = this;
616
+ return this.normalizeSchema(schema, clusterConnectionString, databaseInContextName).then(function (normalized) {
617
+ return _this.setSchema(__assign(__assign({}, normalized), { globalParameters: globalParameters }));
618
+ });
619
+ };
620
+ /**
621
+ * Converts the result of .show schema as json to a normalized schema used by kusto lagnuage service.
622
+ * @param schema result of show schema
623
+ * @param clusterConnectionString cluster connection string`
624
+ * @param databaseInContextName database in context name
625
+ */
626
+ KustoLanguageService.prototype.normalizeSchema = function (schema, clusterConnectionString, databaseInContextName) {
627
+ var databases = Object.keys(schema.Databases)
628
+ .map(function (key) { return schema.Databases[key]; })
629
+ .map(function (_a) {
630
+ var Name = _a.Name, Tables = _a.Tables, Functions = _a.Functions, MinorVersion = _a.MinorVersion, MajorVersion = _a.MajorVersion;
631
+ return ({
632
+ name: Name,
633
+ minorVersion: MinorVersion,
634
+ majorVersion: MajorVersion,
635
+ tables: Object.keys(Tables)
636
+ .map(function (key) { return Tables[key]; })
637
+ .map(function (_a) {
638
+ var Name = _a.Name, OrderedColumns = _a.OrderedColumns, DocString = _a.DocString, EntityType = _a.EntityType;
639
+ return ({
640
+ name: Name,
641
+ docstring: DocString,
642
+ entityType: EntityType,
643
+ columns: OrderedColumns.map(function (_a) {
644
+ var Name = _a.Name, Type = _a.Type, DocString = _a.DocString, CslType = _a.CslType;
645
+ return ({
646
+ name: Name,
647
+ type: CslType,
648
+ docstring: DocString,
649
+ });
650
+ }),
651
+ });
652
+ }),
653
+ functions: Object.keys(Functions)
654
+ .map(function (key) { return Functions[key]; })
655
+ .map(function (_a) {
656
+ var Name = _a.Name, Body = _a.Body, DocString = _a.DocString, InputParameters = _a.InputParameters;
657
+ return ({
658
+ name: Name,
659
+ body: Body,
660
+ docstring: DocString,
661
+ inputParameters: InputParameters.map(function (inputParam) { return ({
662
+ name: inputParam.Name,
663
+ type: inputParam.Type,
664
+ cslType: inputParam.CslType,
665
+ cslDefaultValue: inputParam.CslDefaultValue,
666
+ columns: inputParam.Columns
667
+ ? inputParam.Columns.map(function (col) { return ({
668
+ name: col.Name,
669
+ type: col.Type,
670
+ cslType: col.CslType,
671
+ }); })
672
+ : inputParam.Columns
673
+ }); }),
674
+ });
675
+ }),
676
+ });
677
+ });
678
+ var result = {
679
+ clusterType: 'Engine',
680
+ cluster: {
681
+ connectionString: clusterConnectionString,
682
+ databases: databases,
683
+ },
684
+ database: databases.filter(function (db) { return db.name === databaseInContextName; })[0],
685
+ };
686
+ return Promise.resolve(result);
687
+ };
688
+ KustoLanguageService.prototype.getSchema = function () {
689
+ return Promise.resolve(this._schema);
690
+ };
691
+ KustoLanguageService.prototype.getCommandInContext = function (document, cursorOffset) {
692
+ return this.isIntellisenseV2()
693
+ ? this.getCommandInContextV2(document, cursorOffset)
694
+ : this.getCommandInContextV1(document, cursorOffset);
695
+ };
696
+ KustoLanguageService.prototype.getCommandAndLocationInContext = function (document, cursorOffset) {
697
+ // We are going to remove v1 intellisense. no use to keep parity.
698
+ if (!document || !this.isIntellisenseV2()) {
699
+ return Promise.resolve(null);
700
+ }
701
+ var script = this.parseDocumentV2(document);
702
+ var block = this.getCurrentCommandV2(script, cursorOffset);
703
+ if (!block) {
704
+ return Promise.resolve(null);
705
+ }
706
+ var start = document.positionAt(block.Start);
707
+ var end = document.positionAt(block.End);
708
+ var location = ls.Location.create(document.uri, ls.Range.create(start, end));
709
+ var text = block.Text;
710
+ return Promise.resolve({
711
+ text: text,
712
+ location: location,
713
+ });
714
+ };
715
+ KustoLanguageService.prototype.getCommandInContextV1 = function (document, cursorOffset) {
716
+ this.parseDocumentV1(document, k.ParseMode.CommandTokensOnly);
717
+ var command = this.getCurrentCommand(document, cursorOffset);
718
+ if (!command) {
719
+ return Promise.resolve(null);
720
+ }
721
+ return Promise.resolve(command.Text);
722
+ };
723
+ KustoLanguageService.prototype.getCommandInContextV2 = function (document, cursorOffset) {
724
+ if (!document) {
725
+ return Promise.resolve(null);
726
+ }
727
+ var script = this.parseDocumentV2(document);
728
+ var block = this.getCurrentCommandV2(script, cursorOffset);
729
+ if (!block) {
730
+ return Promise.resolve(null);
731
+ }
732
+ // TODO: do we need to do tricks like V1 is doing in this.getCurrentCommand?
733
+ return Promise.resolve(block.Text);
734
+ };
735
+ /**
736
+ * Retrun an array of commands in document. each command contains the range and text.
737
+ */
738
+ KustoLanguageService.prototype.getCommandsInDocument = function (document) {
739
+ if (!document) {
740
+ return Promise.resolve([]);
741
+ }
742
+ return this.isIntellisenseV2()
743
+ ? this.getCommandsInDocumentV2(document)
744
+ : this.getCommandsInDocumentV1(document);
745
+ };
746
+ KustoLanguageService.prototype.getCommandsInDocumentV1 = function (document) {
747
+ this.parseDocumentV1(document, k.ParseMode.CommandTokensOnly);
748
+ var commands = this.toArray(this._parser.Results);
749
+ return Promise.resolve(commands.map(function (_a) {
750
+ var AbsoluteStart = _a.AbsoluteStart, AbsoluteEnd = _a.AbsoluteEnd, Text = _a.Text;
751
+ return ({
752
+ absoluteStart: AbsoluteStart,
753
+ absoluteEnd: AbsoluteEnd,
754
+ text: Text,
755
+ });
756
+ }));
757
+ };
758
+ KustoLanguageService.prototype.toPlacementStyle = function (formatterPlacementStyle) {
759
+ if (!formatterPlacementStyle) {
760
+ return undefined;
761
+ }
762
+ switch (formatterPlacementStyle) {
763
+ case 'None': return k2.PlacementStyle.None;
764
+ case 'NewLine': return k2.PlacementStyle.NewLine;
765
+ case 'Smart': return k2.PlacementStyle.Smart;
766
+ default: throw new Error('Unknown PlacementStyle');
767
+ }
768
+ };
769
+ KustoLanguageService.prototype.getFormattedCommandsInDocumentV2 = function (document, rangeStart, rangeEnd) {
770
+ var _this = this;
771
+ var script = this.parseDocumentV2(document);
772
+ var commands = this.toArray(script.Blocks).filter(function (command) {
773
+ if (!command.Text || command.Text.trim() == '')
774
+ return false;
775
+ if (rangeStart == null || rangeEnd == null)
776
+ return true;
777
+ // calculate command end position without \r\n.
778
+ var commandEnd = command.End;
779
+ var commandText = command.Text;
780
+ for (var i = commandText.length - 1; i >= 0; i--) {
781
+ if (commandText[i] != '\r' && commandText[i] != '\n') {
782
+ break;
783
+ }
784
+ else {
785
+ commandEnd--;
786
+ }
787
+ }
788
+ if (command.Start > rangeStart && command.Start < rangeEnd)
789
+ return true;
790
+ if (commandEnd > rangeStart && commandEnd < rangeEnd)
791
+ return true;
792
+ if (command.Start <= rangeStart && commandEnd >= rangeEnd)
793
+ return true;
794
+ });
795
+ if (commands.length === 0) {
796
+ return { formattedCommands: [] };
797
+ }
798
+ var formattedCommands = commands.map(function (command) {
799
+ var _a, _b;
800
+ var formatterOptions = _this._languageSettings.formatter;
801
+ var formatter = Kusto.Language.Editor.FormattingOptions.Default
802
+ .WithIndentationSize((_a = formatterOptions === null || formatterOptions === void 0 ? void 0 : formatterOptions.indentationSize) !== null && _a !== void 0 ? _a : 4)
803
+ .WithInsertMissingTokens(false)
804
+ .WithPipeOperatorStyle((_b = _this.toPlacementStyle(formatterOptions === null || formatterOptions === void 0 ? void 0 : formatterOptions.pipeOperatorStyle)) !== null && _b !== void 0 ? _b : k2.PlacementStyle.Smart)
805
+ .WithSemicolonStyle(Kusto.Language.Editor.PlacementStyle.None)
806
+ .WithBrackettingStyle(k2.BrackettingStyle.Diagonal);
807
+ if (rangeStart == null || rangeEnd == null || (rangeStart === command.Start && rangeEnd === command.End)) {
808
+ var result = command.Service.GetFormattedText(formatter);
809
+ return result.Text;
810
+ }
811
+ return command.Service.GetFormattedText(formatter).Text;
812
+ });
813
+ var originalRange = this.createRange(document, commands[0].Start, commands[commands.length - 1].End);
814
+ return { formattedCommands: formattedCommands, originalRange: originalRange };
815
+ };
816
+ KustoLanguageService.prototype.getCommandsInDocumentV2 = function (document) {
817
+ var script = this.parseDocumentV2(document);
818
+ var commands = this.toArray(script.Blocks).filter(function (command) { return command.Text.trim() != ''; });
819
+ return Promise.resolve(commands.map(function (_a) {
820
+ var Start = _a.Start, End = _a.End, Text = _a.Text;
821
+ return ({ absoluteStart: Start, absoluteEnd: End, text: Text });
822
+ }));
823
+ };
824
+ KustoLanguageService.prototype.getClientDirective = function (text) {
825
+ var outParam = { v: null };
826
+ var isClientDirective = k.CslCommandParser.IsClientDirective(text, outParam);
827
+ return Promise.resolve({
828
+ isClientDirective: isClientDirective,
829
+ directiveWithoutLeadingComments: outParam.v,
830
+ });
831
+ };
832
+ KustoLanguageService.prototype.getAdminCommand = function (text) {
833
+ var outParam = { v: null };
834
+ var isAdminCommand = k.CslCommandParser.IsAdminCommand$1(text, outParam);
835
+ return Promise.resolve({
836
+ isAdminCommand: isAdminCommand,
837
+ adminCommandWithoutLeadingComments: outParam.v,
838
+ });
839
+ };
840
+ KustoLanguageService.prototype.findDefinition = function (document, position) {
841
+ if (!document || !this.isIntellisenseV2()) {
842
+ return Promise.resolve([]);
843
+ }
844
+ var script = this.parseDocumentV2(document);
845
+ var cursorOffset = document.offsetAt(position);
846
+ var currentBlock = this.getCurrentCommandV2(script, cursorOffset);
847
+ if (!currentBlock) {
848
+ return Promise.resolve([]);
849
+ }
850
+ var relatedInfo = currentBlock.Service.GetRelatedElements(document.offsetAt(position));
851
+ var relatedElements = this.toArray(relatedInfo.Elements);
852
+ var definition = relatedElements[0];
853
+ if (!definition) {
854
+ return Promise.resolve([]);
855
+ }
856
+ var start = document.positionAt(definition.Start);
857
+ var end = document.positionAt(definition.End);
858
+ var range = ls.Range.create(start, end);
859
+ var location = ls.Location.create(document.uri, range);
860
+ return Promise.resolve([location]);
861
+ };
862
+ KustoLanguageService.prototype.findReferences = function (document, position) {
863
+ if (!document || !this.isIntellisenseV2()) {
864
+ return Promise.resolve([]);
865
+ }
866
+ var script = this.parseDocumentV2(document);
867
+ var cursorOffset = document.offsetAt(position);
868
+ var currentBlock = this.getCurrentCommandV2(script, cursorOffset);
869
+ if (!currentBlock) {
870
+ return Promise.resolve([]);
871
+ }
872
+ var relatedInfo = currentBlock.Service.GetRelatedElements(document.offsetAt(position));
873
+ var relatedElements = this.toArray(relatedInfo.Elements);
874
+ if (!relatedElements || relatedElements.length == 0) {
875
+ return Promise.resolve([]);
876
+ }
877
+ var references = relatedElements.map(function (relatedElement) {
878
+ var start = document.positionAt(relatedElement.Start);
879
+ var end = document.positionAt(relatedElement.End);
880
+ var range = ls.Range.create(start, end);
881
+ var location = ls.Location.create(document.uri, range);
882
+ return location;
883
+ });
884
+ return Promise.resolve(references);
885
+ };
886
+ KustoLanguageService.prototype.getQueryParams = function (document, cursorOffset) {
887
+ if (!document || !this.isIntellisenseV2()) {
888
+ return Promise.resolve([]);
889
+ }
890
+ var script = this.parseDocumentV2(document);
891
+ var parsedAndAnalyzed = this.parseAndAnalyze(document, cursorOffset);
892
+ var queryParamStatements = this.toArray(parsedAndAnalyzed.Syntax.GetDescendants(Kusto.Language.Syntax.QueryParametersStatement));
893
+ if (!queryParamStatements || queryParamStatements.length == 0) {
894
+ return Promise.resolve([]);
895
+ }
896
+ var queryParams = [];
897
+ queryParamStatements.forEach(function (paramStatement) {
898
+ paramStatement.WalkElements(function (el) {
899
+ return el.ReferencedSymbol && el.ReferencedSymbol.Type
900
+ ? queryParams.push({ name: el.ReferencedSymbol.Name, type: el.ReferencedSymbol.Type.Name })
901
+ : undefined;
902
+ });
903
+ });
904
+ return Promise.resolve(queryParams);
905
+ };
906
+ KustoLanguageService.prototype.getRenderInfo = function (document, cursorOffset) {
907
+ var _this = this;
908
+ var parsedAndAnalyzed = this.parseAndAnalyze(document, cursorOffset);
909
+ if (!parsedAndAnalyzed) {
910
+ return Promise.resolve(undefined);
911
+ }
912
+ var renderStatements = this.toArray(parsedAndAnalyzed.Syntax.GetDescendants(Kusto.Language.Syntax.RenderOperator));
913
+ if (!renderStatements || renderStatements.length === 0) {
914
+ return Promise.resolve(undefined);
915
+ }
916
+ // assuming a single render statement
917
+ var renderStatement = renderStatements[0];
918
+ // Start and end relative to block start.
919
+ var startOffset = renderStatement.TextStart;
920
+ var endOffset = renderStatement.End;
921
+ var visualization = renderStatement.ChartType.Text;
922
+ var withClause = renderStatement.WithClause;
923
+ if (!withClause) {
924
+ var info = {
925
+ options: {
926
+ visualization: visualization,
927
+ },
928
+ location: { startOffset: startOffset, endOffset: endOffset },
929
+ };
930
+ return Promise.resolve(info);
931
+ }
932
+ var properties = this.toArray(withClause.Properties);
933
+ var props = properties.reduce(function (prev, property) {
934
+ var name = property.Element$1.Name.SimpleName;
935
+ switch (name) {
936
+ case 'xcolumn':
937
+ var value = property.Element$1.Expression.ReferencedSymbol.Name;
938
+ prev[name] = value;
939
+ break;
940
+ case 'ycolumns':
941
+ case 'anomalycolumns':
942
+ var nameNodes = _this.toArray(property.Element$1.Expression.Names);
943
+ var values = nameNodes.map(function (nameNode) { return nameNode.Element$1.SimpleName; });
944
+ prev[name] = values;
945
+ break;
946
+ case 'ymin':
947
+ case 'ymax':
948
+ var numericVal = parseFloat(property.Element$1.Expression.ConstantValue);
949
+ prev[name] = numericVal;
950
+ break;
951
+ case 'title':
952
+ case 'xtitle':
953
+ case 'ytitle':
954
+ case 'visualization':
955
+ case 'series':
956
+ var strVal = property.Element$1.Expression.ConstantValue;
957
+ prev[name] = strVal;
958
+ break;
959
+ case 'xaxis':
960
+ case 'yaxis':
961
+ var scale = property.Element$1.Expression.ConstantValue;
962
+ prev[name] = scale;
963
+ break;
964
+ case 'legend':
965
+ var legend = property.Element$1.Expression.ConstantValue;
966
+ prev[name] = legend;
967
+ break;
968
+ case 'ySplit':
969
+ var split = property.Element$1.Expression.ConstantValue;
970
+ prev[name] = split;
971
+ break;
972
+ case 'accumulate':
973
+ var accumulate = property.Element$1.Expression.ConstantValue;
974
+ prev[name] = accumulate;
975
+ break;
976
+ case 'kind':
977
+ var val = property.Element$1.Expression.ConstantValue;
978
+ prev[name] = val;
979
+ break;
980
+ default:
981
+ assertNever(name);
982
+ }
983
+ return prev;
984
+ }, {});
985
+ var renderOptions = __assign({ visualization: visualization }, props);
986
+ var renderInfo = {
987
+ options: renderOptions,
988
+ location: { startOffset: startOffset, endOffset: endOffset },
989
+ };
990
+ return Promise.resolve(renderInfo);
991
+ };
992
+ KustoLanguageService.prototype.getReferencedGlobalParams = function (document, cursorOffset) {
993
+ if (!document || !this.isIntellisenseV2()) {
994
+ return Promise.resolve([]);
995
+ }
996
+ var script = this.parseDocumentV2(document);
997
+ var currentBlock = this.getCurrentCommandV2(script, cursorOffset);
998
+ if (!currentBlock) {
999
+ return Promise.resolve([]);
1000
+ }
1001
+ var text = currentBlock.Text;
1002
+ var parsedAndAnalyzed = Kusto.Language.KustoCode.ParseAndAnalyze(text, this._kustoJsSchemaV2);
1003
+ // We take the ambient parameters
1004
+ var ambientParameters = this.toArray(this._kustoJsSchemaV2.Parameters);
1005
+ // We take all referenced symbols in the query
1006
+ var referencedSymbols = this.toArray(parsedAndAnalyzed.Syntax.GetDescendants(Kusto.Language.Syntax.Expression))
1007
+ .filter(function (epression) { return epression.ReferencedSymbol !== null; })
1008
+ .map(function (x) { return x.ReferencedSymbol; });
1009
+ // The Intersection between them is the ambient parameters that are used in the query.
1010
+ // Note: Ideally we would use Set here (or at least array.Include), but were' compiling down to es2015.
1011
+ var intersection = referencedSymbols.filter(function (referencedSymbol) {
1012
+ return ambientParameters.filter(function (ambientParameter) { return ambientParameter === referencedSymbol; }).length > 0;
1013
+ });
1014
+ var result = intersection.map(function (param) { return ({ name: param.Name, type: param.Type.Name }); });
1015
+ return Promise.resolve(result);
1016
+ };
1017
+ KustoLanguageService.prototype.getGlobalParams = function (document) {
1018
+ if (!this.isIntellisenseV2()) {
1019
+ return Promise.resolve([]);
1020
+ }
1021
+ var params = this.toArray(this._kustoJsSchemaV2.Parameters);
1022
+ var result = params.map(function (param) { return ({ name: param.Name, type: param.Type.Name }); });
1023
+ return Promise.resolve(result);
1024
+ };
1025
+ KustoLanguageService.prototype.doRename = function (document, position, newName) {
1026
+ var _a;
1027
+ if (!document || !this.isIntellisenseV2()) {
1028
+ return Promise.resolve(undefined);
1029
+ }
1030
+ var script = this.parseDocumentV2(document);
1031
+ var cursorOffset = document.offsetAt(position);
1032
+ var currentBLock = this.getCurrentCommandV2(script, cursorOffset);
1033
+ if (!currentBLock) {
1034
+ return Promise.resolve(undefined);
1035
+ }
1036
+ var relatedInfo = currentBLock.Service.GetRelatedElements(document.offsetAt(position));
1037
+ var relatedElements = this.toArray(relatedInfo.Elements);
1038
+ var declarations = relatedElements.filter(function (e) { return e.Kind == k2.RelatedElementKind.Declaration; });
1039
+ // A declaration must be one of the elements
1040
+ if (!declarations || declarations.length == 0) {
1041
+ return Promise.resolve(undefined);
1042
+ }
1043
+ var edits = relatedElements.map(function (edit) {
1044
+ var start = document.positionAt(edit.Start);
1045
+ var end = document.positionAt(edit.End);
1046
+ var range = ls.Range.create(start, end);
1047
+ return ls.TextEdit.replace(range, newName);
1048
+ });
1049
+ // create a workspace edit
1050
+ var workspaceEdit = { changes: (_a = {}, _a[document.uri] = edits, _a) };
1051
+ return Promise.resolve(workspaceEdit);
1052
+ };
1053
+ KustoLanguageService.prototype.doHover = function (document, position) {
1054
+ if (!document || !this.isIntellisenseV2()) {
1055
+ return Promise.resolve(undefined);
1056
+ }
1057
+ var script = this.parseDocumentV2(document);
1058
+ var cursorOffset = document.offsetAt(position);
1059
+ var currentBLock = this.getCurrentCommandV2(script, cursorOffset);
1060
+ if (!currentBLock) {
1061
+ return Promise.resolve(undefined);
1062
+ }
1063
+ var isSupported = currentBLock.Service.IsFeatureSupported(k2.CodeServiceFeatures.QuickInfo, cursorOffset);
1064
+ if (!isSupported) {
1065
+ return Promise.resolve(undefined);
1066
+ }
1067
+ var quickInfo = currentBLock.Service.GetQuickInfo(cursorOffset);
1068
+ if (!quickInfo || !quickInfo.Items) {
1069
+ return Promise.resolve(undefined);
1070
+ }
1071
+ var items = this.toArray(quickInfo.Items);
1072
+ if (!items) {
1073
+ return Promise.resolve(undefined);
1074
+ }
1075
+ // Errors are already shown in getDiagnostics. we don't want them in doHover.
1076
+ items = items.filter(function (item) { return item.Kind !== k2.QuickInfoKind.Error; });
1077
+ var itemsText = items.map(function (item) { return item.Text.replace('\n\n', '\n* * *\n'); });
1078
+ // separate items by horizontal line.
1079
+ var text = itemsText.join("\n* * *\n");
1080
+ // Instead of just an empty line between the first line (the signature) and the second line (the description)
1081
+ // add an horizontal line (* * * in markdown) between them.
1082
+ return Promise.resolve({ contents: text });
1083
+ };
1084
+ Object.defineProperty(KustoLanguageService, "dummySchema", {
1085
+ //#region dummy schema for manual testing
1086
+ get: function () {
1087
+ var database = {
1088
+ majorVersion: 0,
1089
+ minorVersion: 0,
1090
+ name: 'Kuskus',
1091
+ tables: [
1092
+ {
1093
+ name: 'KustoLogs',
1094
+ columns: [
1095
+ {
1096
+ name: 'Source',
1097
+ type: 'string',
1098
+ },
1099
+ {
1100
+ name: 'Timestamp',
1101
+ type: 'datetime',
1102
+ },
1103
+ {
1104
+ name: 'Directory',
1105
+ type: 'string',
1106
+ },
1107
+ ],
1108
+ docstring: 'A dummy description to test that docstring shows as expected when hovering over a table',
1109
+ },
1110
+ ],
1111
+ functions: [
1112
+ {
1113
+ name: 'HowBig',
1114
+ inputParameters: [
1115
+ {
1116
+ name: 'T',
1117
+ columns: [
1118
+ {
1119
+ name: 'Timestamp',
1120
+ type: 'System.DateTime',
1121
+ cslType: 'datetime',
1122
+ },
1123
+ ],
1124
+ },
1125
+ ],
1126
+ docstring: 'A dummy description to test that docstring shows as expected when hovering over a function',
1127
+ body: "{\r\n union \r\n (T | count | project V='Volume', Metric = strcat(Count/1e9, ' Billion records')),\r\n (T | summarize FirstRecord=min(Timestamp)| project V='Volume', Metric = strcat(toint((now()-FirstRecord)/1d), ' Days of data (from: ', format_datetime(FirstRecord, 'yyyy-MM-dd'),')')),\r\n (T | where Timestamp > ago(1h) | count | project V='Velocity', Metric = strcat(Count/1e6, ' Million records / hour')),\r\n (T | summarize Latency=now()-max(Timestamp) | project V='Velocity', Metric = strcat(Latency / 1sec, ' seconds latency')),\r\n (T | take 1 | project V='Variety', Metric=tostring(pack_all()))\r\n | order by V \r\n}",
1128
+ },
1129
+ {
1130
+ name: 'FindCIDPast24h',
1131
+ inputParameters: [
1132
+ {
1133
+ name: 'clientActivityId',
1134
+ type: 'System.String',
1135
+ cslType: 'string',
1136
+ },
1137
+ ],
1138
+ body: '{ KustoLogs | where Timestamp > now(-1d) | where ClientActivityId == clientActivityId} ',
1139
+ },
1140
+ ],
1141
+ };
1142
+ var languageServiceSchema = {
1143
+ clusterType: 'Engine',
1144
+ cluster: {
1145
+ connectionString: 'https://kuskus.kusto.windows.net;fed=true',
1146
+ databases: [database],
1147
+ },
1148
+ database: database,
1149
+ };
1150
+ return languageServiceSchema;
1151
+ },
1152
+ enumerable: false,
1153
+ configurable: true
1154
+ });
1155
+ //#endregion
1156
+ KustoLanguageService.convertToEntityDataType = function (kustoType) { };
1157
+ /**
1158
+ * We do not want to expose Bridge.Net generated schema, so we expose a cleaner javascript schema.
1159
+ * Here it gets converted to the bridge.Net schema
1160
+ * @param schema Language Service schema
1161
+ */
1162
+ KustoLanguageService.convertToKustoJsSchema = function (schema) {
1163
+ switch (schema.clusterType) {
1164
+ case 'Engine':
1165
+ var currentDatabaseName_1 = schema.database ? schema.database.name : undefined;
1166
+ var kCluster = new k.KustoIntelliSenseClusterEntity();
1167
+ var kDatabaseInContext_1 = undefined;
1168
+ kCluster.ConnectionString = schema.cluster.connectionString;
1169
+ var databases_1 = [];
1170
+ schema.cluster.databases.forEach(function (database) {
1171
+ var kDatabase = new k.KustoIntelliSenseDatabaseEntity();
1172
+ kDatabase.Name = database.name;
1173
+ var tables = [];
1174
+ database.tables.forEach(function (table) {
1175
+ var kTable = new k.KustoIntelliSenseTableEntity();
1176
+ kTable.Name = table.name;
1177
+ var cols = [];
1178
+ table.columns.forEach(function (column) {
1179
+ var kColumn = new k.KustoIntelliSenseColumnEntity();
1180
+ kColumn.Name = column.name;
1181
+ kColumn.TypeCode = k.EntityDataType[getEntityDataTypeFromCslType(column.type)];
1182
+ cols.push(kColumn);
1183
+ });
1184
+ kTable.Columns = new Bridge.ArrayEnumerable(cols);
1185
+ tables.push(kTable);
1186
+ });
1187
+ var functions = [];
1188
+ database.functions.forEach(function (fn) {
1189
+ var kFunction = new k.KustoIntelliSenseFunctionEntity();
1190
+ (kFunction.Name = fn.name),
1191
+ (kFunction.CallName = s.getCallName(fn)),
1192
+ (kFunction.Expression = s.getExpression(fn)),
1193
+ functions.push(kFunction);
1194
+ });
1195
+ kDatabase.Tables = new Bridge.ArrayEnumerable(tables);
1196
+ kDatabase.Functions = new Bridge.ArrayEnumerable(functions);
1197
+ databases_1.push(kDatabase);
1198
+ if (database.name == currentDatabaseName_1) {
1199
+ kDatabaseInContext_1 = kDatabase;
1200
+ }
1201
+ });
1202
+ kCluster.Databases = new Bridge.ArrayEnumerable(databases_1);
1203
+ var kSchema = new k.KustoIntelliSenseQuerySchema(kCluster, kDatabaseInContext_1);
1204
+ return kSchema;
1205
+ case 'ClusterManager':
1206
+ var accounts = schema.accounts.map(function (account) {
1207
+ var kAccount = new k.KustoIntelliSenseAccountEntity();
1208
+ kAccount.Name = account;
1209
+ return kAccount;
1210
+ });
1211
+ var services = schema.services.map(function (service) {
1212
+ var kService = new k.KustoIntelliSenseServiceEntity();
1213
+ kService.Name = service;
1214
+ return kService;
1215
+ });
1216
+ var connectionString = schema.connectionString;
1217
+ var result = {
1218
+ accounts: accounts,
1219
+ services: services,
1220
+ connectionString: connectionString,
1221
+ };
1222
+ return result;
1223
+ case 'DataManagement':
1224
+ return undefined;
1225
+ default:
1226
+ return assertNever(schema);
1227
+ }
1228
+ };
1229
+ /**
1230
+ * Returns something like '(x: string, y: datetime)'
1231
+ * @param params scalar parameters
1232
+ */
1233
+ KustoLanguageService.scalarParametersToSignature = function (params) {
1234
+ var signatureWithoutParens = params.map(function (param) { return param.name + ": " + param.cslType; }).join(', ');
1235
+ return "(" + signatureWithoutParens + ")";
1236
+ };
1237
+ /**
1238
+ * Returns something like '(x: string, T: (y: int))'
1239
+ * @param params input parameters (tabular or scalar)
1240
+ */
1241
+ KustoLanguageService.inputParameterToSignature = function (params) {
1242
+ var _this = this;
1243
+ var signatureWithoutParens = params
1244
+ .map(function (param) {
1245
+ if (param.columns) {
1246
+ var tableSignature = _this.scalarParametersToSignature(param.columns);
1247
+ return param.name + ": " + tableSignature;
1248
+ }
1249
+ else {
1250
+ return param.name + ": " + param.cslType;
1251
+ }
1252
+ })
1253
+ .join(', ');
1254
+ return "(" + signatureWithoutParens + ")";
1255
+ };
1256
+ /**
1257
+ * converts a function definition to a let statement.
1258
+ * @param fn function
1259
+ */
1260
+ KustoLanguageService.toLetStatement = function (fn) {
1261
+ var signature = this.inputParameterToSignature(fn.inputParameters);
1262
+ return "let " + fn.name + " = " + signature + " " + fn.body;
1263
+ };
1264
+ KustoLanguageService.createColumnSymbol = function (col) {
1265
+ return new sym.ColumnSymbol(col.name, sym.ScalarTypes.GetSymbol(getCslTypeNameFromClrType(col.type)), col.docstring);
1266
+ };
1267
+ KustoLanguageService.createParameterSymbol = function (param) {
1268
+ var paramSymbol = Kusto.Language.Symbols.ScalarTypes.GetSymbol(getCslTypeNameFromClrType(param.type));
1269
+ return new sym.ParameterSymbol(param.name, paramSymbol, null);
1270
+ };
1271
+ KustoLanguageService.createParameter = function (param) {
1272
+ if (!param.columns) {
1273
+ var paramSymbol = Kusto.Language.Symbols.ScalarTypes.GetSymbol(getCslTypeNameFromClrType(param.type));
1274
+ var expression = void 0;
1275
+ if (param.cslDefaultValue && typeof param.cslDefaultValue === "string") {
1276
+ var parser = parsing.QueryGrammar.From(Kusto.Language.GlobalState.Default).ConstantExpression;
1277
+ expression = parsing.SyntaxParsers.ParseFirst({ prototype: parser }, parser, param.cslDefaultValue);
1278
+ }
1279
+ return new sym.Parameter.$ctor3(param.name, paramSymbol, null, null, null, false, null, 1, 1, expression, null);
1280
+ }
1281
+ if (param.columns.length == 0) {
1282
+ return new sym.Parameter.ctor(param.name, sym.ParameterTypeKind.Tabular, sym.ArgumentKind.Expression, null, null, false, null, 1, 1, null, null);
1283
+ }
1284
+ var argumentType = new sym.TableSymbol.ctor(param.columns.map(function (col) { return KustoLanguageService.createColumnSymbol(col); }));
1285
+ return new sym.Parameter.$ctor2(param.name, argumentType);
1286
+ };
1287
+ KustoLanguageService.convertToDatabaseSymbol = function (db, globalState, addFunctions) {
1288
+ var createFunctionSymbol = function (fn) {
1289
+ var parameters = fn.inputParameters.map(function (param) {
1290
+ return KustoLanguageService.createParameter(param);
1291
+ });
1292
+ // TODO: handle outputColumns (right now it doesn't seem to be implemented for any function).
1293
+ return new sym.FunctionSymbol.$ctor16(fn.name, fn.body, KustoLanguageService.toBridgeList(parameters), fn.docstring);
1294
+ };
1295
+ var createTableSymbol = function (tbl) {
1296
+ var columnSymbols = tbl.columns.map(function (col) { return KustoLanguageService.createColumnSymbol(col); });
1297
+ var symbol = new sym.TableSymbol.$ctor3(tbl.name, columnSymbols);
1298
+ symbol.Description = tbl.docstring;
1299
+ switch (tbl.entityType) {
1300
+ case 'MaterializedViewTable':
1301
+ symbol = symbol.WithIsMaterializedView(true);
1302
+ case "ExternalTable":
1303
+ symbol = symbol.WithIsExternal(true);
1304
+ default:
1305
+ }
1306
+ return symbol;
1307
+ };
1308
+ var createDatabaseSymbol = function (db) {
1309
+ var tableSymbols = db.tables ? db.tables.map(function (tbl) { return createTableSymbol(tbl); }) : [];
1310
+ var functionSymbols = db.functions ? db.functions.map(function (fun) { return createFunctionSymbol(fun); }) : [];
1311
+ return new sym.DatabaseSymbol.ctor(db.name, tableSymbols.concat(functionSymbols));
1312
+ };
1313
+ var databaseSymbol = createDatabaseSymbol(db);
1314
+ return databaseSymbol;
1315
+ };
1316
+ KustoLanguageService.prototype.convertToKustoJsSchemaV2 = function (schema) {
1317
+ var cached = this._schemaCache[schema.cluster.connectionString];
1318
+ // create a cache entry for the cluster if non yet exists.
1319
+ if (!cached) {
1320
+ this._schemaCache[schema.cluster.connectionString] = {};
1321
+ cached = this._schemaCache[schema.cluster.connectionString];
1322
+ }
1323
+ // Remove deleted databases from cache
1324
+ var schemaDbLookup = schema.cluster.databases.reduce(function (prev, curr) { return (prev[curr.name] = curr); }, {});
1325
+ Object.keys(cached).map(function (dbName) {
1326
+ if (!schemaDbLookup[dbName]) {
1327
+ delete cached.dbName;
1328
+ }
1329
+ });
1330
+ var globalState = GlobalState.Default;
1331
+ var currentDatabaseName = schema.database ? schema.database.name : undefined;
1332
+ var databaseInContext = undefined;
1333
+ // Update out-of-data databses to cache
1334
+ var databases = schema.cluster.databases.map(function (db) {
1335
+ var shouldIncludeFunctions = db.name === currentDatabaseName;
1336
+ var cachedDb = cached[db.name];
1337
+ // This is an older version than we have, or we need to parse functions.
1338
+ if (!cachedDb ||
1339
+ cachedDb.database.majorVersion < db.majorVersion ||
1340
+ (shouldIncludeFunctions && !cachedDb.includesFunctions)) {
1341
+ // only add functions for the database in context (it's very time consuming)
1342
+ var databaseSymbol_1 = KustoLanguageService.convertToDatabaseSymbol(db, globalState, shouldIncludeFunctions);
1343
+ cached[db.name] = { database: db, symbol: databaseSymbol_1, includesFunctions: shouldIncludeFunctions };
1344
+ }
1345
+ var databaseSymbol = cached[db.name].symbol;
1346
+ if (db.name === currentDatabaseName) {
1347
+ databaseInContext = databaseSymbol;
1348
+ }
1349
+ return databaseSymbol;
1350
+ });
1351
+ // Replace new URL due to polyfill issue in IE
1352
+ // const hostname = new URL(schema.cluster.connectionString.split(';')[0]).hostname;
1353
+ var hostname = schema.cluster.connectionString.match(/(.*\/\/)?([^\/;]*)/)[2];
1354
+ var clusterName = hostname.split('.')[0];
1355
+ var clusterSymbol = new sym.ClusterSymbol.ctor(clusterName, databases);
1356
+ globalState = globalState.WithCluster(clusterSymbol);
1357
+ if (databaseInContext) {
1358
+ globalState = globalState.WithDatabase(databaseInContext);
1359
+ }
1360
+ // Inject gloabl parameters to global scope.
1361
+ if (schema.globalParameters) {
1362
+ var parameters = schema.globalParameters.map(function (param) {
1363
+ return KustoLanguageService.createParameterSymbol(param);
1364
+ });
1365
+ globalState = globalState.WithParameters(KustoLanguageService.toBridgeList(parameters));
1366
+ }
1367
+ return globalState;
1368
+ };
1369
+ KustoLanguageService.prototype.getClassificationsFromParseResult = function (offset) {
1370
+ var _this = this;
1371
+ if (offset === void 0) { offset = 0; }
1372
+ var classifications = this.toArray(this._parser.Results)
1373
+ .map(function (command) { return _this.toArray(command.Tokens); })
1374
+ .reduce(function (prev, curr) { return prev.concat(curr); }, [])
1375
+ .map(function (cslCommandToken) {
1376
+ var range = new k2.ClassifiedRange(_this.tokenKindToClassificationKind(cslCommandToken.TokenKind), cslCommandToken.AbsoluteStart + offset, cslCommandToken.Length);
1377
+ return range;
1378
+ });
1379
+ return classifications;
1380
+ };
1381
+ /**
1382
+ * trim trailing newlines from range
1383
+ */
1384
+ KustoLanguageService.trimTrailingNewlineFromRange = function (textInRange, rangeStartOffset, document, range) {
1385
+ var currentIndex = textInRange.length - 1;
1386
+ while (textInRange[currentIndex] === '\r' || textInRange[currentIndex] === '\n') {
1387
+ --currentIndex;
1388
+ }
1389
+ var newEndOffset = rangeStartOffset + currentIndex + 1;
1390
+ var newEndPosition = document.positionAt(newEndOffset);
1391
+ var newRange = ls.Range.create(range.start, newEndPosition);
1392
+ return newRange;
1393
+ };
1394
+ /**
1395
+ * Maps numbers to strings, such that if a>b numerically, f(a)>f(b) lexicograhically.
1396
+ * 1 -> "a", 26 -> "z", 27 -> "za", 28 -> "zb", 52 -> "zz", 53 ->"zza"
1397
+ * @param order - The number to be converted to a sorting-string. order should start at 1.
1398
+ * @returns A string repenting the order.
1399
+ */
1400
+ KustoLanguageService.prototype.getSortText = function (order) {
1401
+ if (order <= 0) {
1402
+ throw new RangeError("order should be a number >= 1. instead got " + order);
1403
+ }
1404
+ var sortText = '';
1405
+ var numCharacters = 26; // "z" - "a" + 1;
1406
+ var div = Math.floor(order / numCharacters);
1407
+ for (var i = 0; i < div; ++i) {
1408
+ sortText += 'z';
1409
+ }
1410
+ var reminder = order % numCharacters;
1411
+ if (reminder > 0) {
1412
+ sortText += String.fromCharCode(96 + reminder);
1413
+ }
1414
+ return sortText;
1415
+ };
1416
+ /**
1417
+ * ParseTextV1 parses the given text with the given parse mode.
1418
+ * Additionally - it will make sure not to provide rules provider for non-engine clusters
1419
+ * since the only rules provider parse can handle is the engine's. It will try to look for function
1420
+ * definitions to colorize and will throw since they're not there.
1421
+ * @param text
1422
+ * @param parseMode
1423
+ */
1424
+ KustoLanguageService.prototype.parseTextV1 = function (text, parseMode) {
1425
+ this._parser.Parse(this._schema.clusterType === 'Engine' ? this._rulesProvider : null, text, parseMode);
1426
+ };
1427
+ KustoLanguageService.prototype.parseDocumentV1 = function (document, parseMode) {
1428
+ // already parsed a later version, or better parse mode for this uri
1429
+ if (this._parsePropertiesV1 &&
1430
+ !this._parsePropertiesV1.isParseNeeded(document, this._rulesProvider, parseMode)) {
1431
+ return;
1432
+ }
1433
+ this.parseTextV1(document.getText(), parseMode);
1434
+ this._parsePropertiesV1 = new ParseProperties(document.version, document.uri, this._rulesProvider, parseMode);
1435
+ };
1436
+ KustoLanguageService.prototype.parseDocumentV2 = function (document) {
1437
+ if (this._parsePropertiesV2 && !this._parsePropertiesV2.isParseNeeded(document, this._rulesProvider)) {
1438
+ return this._script;
1439
+ }
1440
+ if (!this._script) {
1441
+ this._script = k2.CodeScript.From$1(document.getText(), this._kustoJsSchemaV2);
1442
+ }
1443
+ else {
1444
+ this._script = this._script.WithText(document.getText());
1445
+ }
1446
+ this._parsePropertiesV2 = new ParseProperties(document.version, document.uri);
1447
+ return this._script;
1448
+ };
1449
+ /**
1450
+ * Return the CslCommand that wraps the caret location, or undefined if caret is outside any command
1451
+ * @param document the document to extract the current command from
1452
+ * @param caretAbsolutePosition absolute caret position
1453
+ */
1454
+ KustoLanguageService.prototype.getCurrentCommand = function (document, caretAbsolutePosition) {
1455
+ var commands = this.toArray(this._parser.Results);
1456
+ var command = commands.filter(function (command) { return command.AbsoluteStart <= caretAbsolutePosition && command.AbsoluteEnd >= caretAbsolutePosition; })[0];
1457
+ // There is an edge case when cursor appears at the end of the command
1458
+ // which is not yet considered to be part of the parsed command (therefore: +1 for the AbsoluteEdit property)
1459
+ if (!command) {
1460
+ command = commands.filter(function (command) {
1461
+ return command.AbsoluteStart <= caretAbsolutePosition && command.AbsoluteEnd + 1 >= caretAbsolutePosition;
1462
+ })[0];
1463
+ // If we have 2 newlines in the end of the text the cursor is _probably_ at the end of the text
1464
+ // which this means that we're not actually standing on any command. Thus return null.
1465
+ if (!command || command.Text.endsWith('\r\n\r\n')) {
1466
+ return null;
1467
+ }
1468
+ }
1469
+ return command;
1470
+ };
1471
+ KustoLanguageService.prototype.getCurrentCommandV2 = function (script, offset) {
1472
+ var block = script.GetBlockAtPosition(offset);
1473
+ return block;
1474
+ };
1475
+ KustoLanguageService.prototype.getTextToInsert = function (rule, option) {
1476
+ var beforeApplyInfo = rule.GetBeforeApplyInfo(option.Value);
1477
+ var afterApplyInfo = rule.GetAfterApplyInfo(option.Value);
1478
+ // this is the basic text to be insterted,
1479
+ // but we still need to figure out where the cursor will end up after completion is applied.
1480
+ var insertText = beforeApplyInfo.Text || '' + option.Value + afterApplyInfo.Text || '';
1481
+ var insertTextFormat = ls.InsertTextFormat.PlainText;
1482
+ var snippetFinalTabStop = '$0';
1483
+ if (afterApplyInfo.OffsetToken && afterApplyInfo.OffsetPosition) {
1484
+ var tokenOffset = insertText.indexOf(afterApplyInfo.OffsetToken);
1485
+ if (tokenOffset >= 0) {
1486
+ insertText = this.insertToString(insertText, snippetFinalTabStop, tokenOffset - insertText.length + afterApplyInfo.OffsetPosition);
1487
+ insertTextFormat = ls.InsertTextFormat.Snippet;
1488
+ }
1489
+ }
1490
+ else if (afterApplyInfo.OffsetPosition) {
1491
+ // We only handle negative offsets
1492
+ insertText = this.insertToString(insertText, snippetFinalTabStop, afterApplyInfo.OffsetPosition);
1493
+ insertTextFormat = ls.InsertTextFormat.Snippet;
1494
+ }
1495
+ return { insertText: insertText, insertTextFormat: insertTextFormat };
1496
+ };
1497
+ /**
1498
+ * create a new string with stringToInsert inserted at offsetFromEnd in originalString.
1499
+ * @param originalString string to insert to
1500
+ * @param stringToInsert string to insert
1501
+ * @param offsetFromEnd a negative number that will represent offset to the left. 0 means simple concat
1502
+ */
1503
+ KustoLanguageService.prototype.insertToString = function (originalString, stringToInsert, offsetFromEnd) {
1504
+ var index = originalString.length + offsetFromEnd;
1505
+ if (offsetFromEnd >= 0 || index < 0) {
1506
+ return originalString; // Cannot insert before or after the string
1507
+ }
1508
+ var before = originalString.substring(0, index);
1509
+ var after = originalString.substring(index);
1510
+ return before + stringToInsert + after;
1511
+ };
1512
+ KustoLanguageService.prototype.getCommandWithoutLastWord = function (text) {
1513
+ var lastWordRegex = XRegExp('[\\w_]*$', 's');
1514
+ return text.replace(lastWordRegex, '');
1515
+ };
1516
+ KustoLanguageService.prototype.createRulesProvider = function (schema, clusterType) {
1517
+ var queryParameters = new (List(String))();
1518
+ var availableClusters = new (List(String))();
1519
+ this._parser = new k.CslCommandParser();
1520
+ if (clusterType == 'Engine') {
1521
+ var engineSchema = schema;
1522
+ this._rulesProvider =
1523
+ this._languageSettings && this._languageSettings.includeControlCommands
1524
+ ? new k.CslIntelliSenseRulesProvider.$ctor1(engineSchema.Cluster, engineSchema, queryParameters, availableClusters, null, true, true)
1525
+ : new k.CslQueryIntelliSenseRulesProvider.$ctor1(engineSchema.Cluster, engineSchema, queryParameters, availableClusters, null, null, null);
1526
+ return;
1527
+ }
1528
+ if (clusterType === 'DataManagement') {
1529
+ this._rulesProvider = new k.DataManagerIntelliSenseRulesProvider(null);
1530
+ return;
1531
+ }
1532
+ // This is a cluster manger
1533
+ var _a = schema, accounts = _a.accounts, services = _a.services, connectionString = _a.connectionString;
1534
+ new k.KustoIntelliSenseAccountEntity();
1535
+ new k.KustoIntelliSenseServiceEntity();
1536
+ this._rulesProvider = new k.ClusterManagerIntelliSenseRulesProvider.$ctor1(new Bridge.ArrayEnumerable(accounts), new Bridge.ArrayEnumerable(services), connectionString);
1537
+ };
1538
+ KustoLanguageService.prototype.kustoKindToLsKind = function (kustoKind) {
1539
+ var res = this._kustoKindtolsKind[kustoKind];
1540
+ return res ? res : ls.CompletionItemKind.Variable;
1541
+ };
1542
+ KustoLanguageService.prototype.kustoKindToLsKindV2 = function (kustoKind) {
1543
+ var res = this._kustoKindToLsKindV2[kustoKind];
1544
+ return res ? res : ls.CompletionItemKind.Variable;
1545
+ };
1546
+ KustoLanguageService.prototype.createRange = function (document, start, end) {
1547
+ return ls.Range.create(document.positionAt(start), document.positionAt(end));
1548
+ };
1549
+ KustoLanguageService.prototype.toArray = function (bridgeList) {
1550
+ return Bridge.toArray(bridgeList);
1551
+ };
1552
+ KustoLanguageService.toBridgeList = function (array) {
1553
+ // copied from bridge.js from the implementation of Enumerable.prototype.toList
1554
+ return new (System.Collections.Generic.List$1(System.Object).$ctor1)(array);
1555
+ };
1556
+ KustoLanguageService.prototype.tokenKindToClassificationKind = function (token) {
1557
+ var conversion = this._tokenKindToClassificationKind[token];
1558
+ return conversion || k2.ClassificationKind.PlainText;
1559
+ };
1560
+ KustoLanguageService.prototype.parseAndAnalyze = function (document, cursorOffset) {
1561
+ if (!document || !this.isIntellisenseV2()) {
1562
+ return undefined;
1563
+ }
1564
+ var script = this.parseDocumentV2(document);
1565
+ var currentBlock = this.getCurrentCommandV2(script, cursorOffset);
1566
+ if (!currentBlock) {
1567
+ return undefined;
1568
+ }
1569
+ var text = currentBlock.Text;
1570
+ var parsedAndAnalyzed = Kusto.Language.KustoCode.ParseAndAnalyze(text, this._kustoJsSchemaV2);
1571
+ return parsedAndAnalyzed;
1572
+ };
1573
+ return KustoLanguageService;
1574
+ }());
1575
+ var languageService = new KustoLanguageService(KustoLanguageService.dummySchema, {
1576
+ includeControlCommands: true,
1577
+ useIntellisenseV2: true,
1578
+ useSemanticColorization: true,
1579
+ });
1580
+ /**
1581
+ * Obtain an instance of the kusto language service.
1582
+ */
1583
+ export function getKustoLanguageService() {
1584
+ return languageService;
1585
+ }