-
Notifications
You must be signed in to change notification settings - Fork 264
/
yamlCompletion.ts
1102 lines (1029 loc) · 38.9 KB
/
yamlCompletion.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*---------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as Parser from '../parser/jsonParser07';
import { ASTNode, ObjectASTNode, PropertyASTNode } from '../jsonASTTypes';
import { parse as parseYAML } from '../parser/yamlParser07';
import { YAMLSchemaService } from './yamlSchemaService';
import { JSONSchema, JSONSchemaRef } from '../jsonSchema';
import { CompletionsCollector } from 'vscode-json-languageservice';
import { TextDocument } from 'vscode-languageserver-textdocument';
import {
CompletionItem,
CompletionItemKind,
CompletionList,
Position,
Range,
TextEdit,
InsertTextFormat,
} from 'vscode-languageserver-types';
import * as nls from 'vscode-nls';
import { getLineOffsets, filterInvalidCustomTags, matchOffsetToDocument } from '../utils/arrUtils';
import { LanguageSettings } from '../yamlLanguageService';
import { ResolvedSchema } from 'vscode-json-languageservice/lib/umd/services/jsonSchemaService';
import { JSONCompletion } from 'vscode-json-languageservice/lib/umd/services/jsonCompletion';
import { stringifyObject, StringifySettings } from '../utils/json';
import { guessIndentation } from '../utils/indentationGuesser';
import { TextBuffer } from '../utils/textBuffer';
import { setKubernetesParserOption } from '../parser/isKubernetes';
import { ClientCapabilities, MarkupContent } from 'vscode-languageserver';
import { Telemetry } from '../../languageserver/telemetry';
const localize = nls.loadMessageBundle();
const doubleQuotesEscapeRegExp = /[\\]+"/g;
export class YAMLCompletion extends JSONCompletion {
private schemaService: YAMLSchemaService;
private customTags: Array<string>;
private completion: boolean;
private indentation: string;
private configuredIndentation: string | undefined;
constructor(
schemaService: YAMLSchemaService,
clientCapabilities: ClientCapabilities = {},
private readonly telemetry: Telemetry
) {
super(schemaService, [], Promise, clientCapabilities);
this.schemaService = schemaService;
this.customTags = [];
this.completion = true;
}
public configure(languageSettings: LanguageSettings, customTags: Array<string>): void {
if (languageSettings) {
this.completion = languageSettings.completion;
}
this.customTags = customTags;
this.configuredIndentation = languageSettings.indentation;
}
public doComplete(document: TextDocument, position: Position, isKubernetes = false): Promise<CompletionList> {
const result: CompletionList = {
items: [],
isIncomplete: false,
};
if (!this.completion) {
return Promise.resolve(result);
}
const originalPosition = Position.create(position.line, position.character);
const completionFix = this.completionHelper(document, position);
const newText = completionFix.newText;
const doc = parseYAML(newText);
const textBuffer = new TextBuffer(document);
if (!this.configuredIndentation) {
const indent = guessIndentation(textBuffer, 2, true);
this.indentation = indent.insertSpaces ? ' '.repeat(indent.tabSize) : '\t';
} else {
this.indentation = this.configuredIndentation;
}
setKubernetesParserOption(doc.documents, isKubernetes);
const offset = document.offsetAt(position);
if (document.getText()[offset] === ':') {
return Promise.resolve(result);
}
const currentDoc = matchOffsetToDocument(offset, doc);
if (currentDoc === null) {
return Promise.resolve(result);
}
const currentDocIndex = doc.documents.indexOf(currentDoc);
let node = currentDoc.getNodeFromOffsetEndInclusive(offset);
// if (this.isInComment(document, node ? node.start : 0, offset)) {
// return Promise.resolve(result);
// }
const currentWord = super.getCurrentWord(document, offset);
let overwriteRange = null;
if (node && node.type === 'null') {
const nodeStartPos = document.positionAt(node.offset);
nodeStartPos.character += 1;
const nodeEndPos = document.positionAt(node.offset + node.length);
nodeEndPos.character += 1;
overwriteRange = Range.create(nodeStartPos, nodeEndPos);
} else if (node && (node.type === 'string' || node.type === 'number' || node.type === 'boolean')) {
overwriteRange = Range.create(document.positionAt(node.offset), document.positionAt(node.offset + node.length));
} else {
let overwriteStart = document.offsetAt(originalPosition) - currentWord.length;
if (overwriteStart > 0 && document.getText()[overwriteStart - 1] === '"') {
overwriteStart--;
}
overwriteRange = Range.create(document.positionAt(overwriteStart), originalPosition);
}
const proposed: { [key: string]: CompletionItem } = {};
const collector: CompletionsCollector = {
add: (suggestion: CompletionItem) => {
let label = suggestion.label;
const existing = proposed[label];
if (!existing) {
label = label.replace(/[\n]/g, '↵');
if (label.length > 60) {
const shortendedLabel = label.substr(0, 57).trim() + '...';
if (!proposed[shortendedLabel]) {
label = shortendedLabel;
}
}
if (overwriteRange && overwriteRange.start.line === overwriteRange.end.line) {
suggestion.textEdit = TextEdit.replace(overwriteRange, suggestion.insertText);
}
suggestion.label = label;
proposed[label] = suggestion;
result.items.push(suggestion);
} else if (!existing.documentation) {
existing.documentation = suggestion.documentation;
}
},
setAsIncomplete: () => {
result.isIncomplete = true;
},
error: (message: string) => {
console.error(message);
this.telemetry.sendError('yaml.completion.error', { error: message });
},
log: (message: string) => {
console.log(message);
},
getNumberOfProposals: () => {
return result.items.length;
},
};
if (this.customTags.length > 0) {
this.getCustomTagValueCompletions(collector);
}
currentDoc.currentDocIndex = currentDocIndex;
return this.schemaService.getSchemaForResource(document.uri, currentDoc).then((schema) => {
if (!schema) {
return Promise.resolve(result);
}
const newSchema = schema;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const collectionPromises: Promise<any>[] = [];
let addValue = true;
let currentProperty: PropertyASTNode = null;
if (node) {
if (node.type === 'string') {
const parent = node.parent;
if (parent && parent.type === 'property' && parent.keyNode === node) {
addValue = !parent.valueNode;
currentProperty = parent;
if (parent) {
node = parent.parent;
}
}
}
if (node.type === 'null') {
const parent = node.parent;
if (parent && parent.type === 'property' && parent.valueNode === node) {
addValue = !parent.valueNode;
currentProperty = parent;
if (parent) {
node = parent;
}
}
}
}
// proposals for properties
if (node && node.type === 'object') {
// don't suggest properties that are already present
const properties = (<ObjectASTNode>node).properties;
properties.forEach((p) => {
if (!currentProperty || currentProperty !== p) {
proposed[p.keyNode.value] = CompletionItem.create('__');
}
});
const separatorAfter = '';
if (newSchema) {
// property proposals with schema
this.getPropertyCompletions(
newSchema,
currentDoc,
node,
addValue,
separatorAfter,
collector,
textBuffer,
overwriteRange
);
}
if (!schema && currentWord.length > 0 && document.getText().charAt(offset - currentWord.length - 1) !== '"') {
collector.add({
kind: CompletionItemKind.Property,
label: currentWord,
insertText: this.getInsertTextForProperty(currentWord, null, false, separatorAfter),
insertTextFormat: InsertTextFormat.Snippet,
documentation: '',
});
}
}
// proposals for values
const types: { [type: string]: boolean } = {};
if (newSchema) {
this.getValueCompletions(newSchema, currentDoc, node, offset, document, collector, types);
}
return Promise.all(collectionPromises).then(() => {
return result;
});
});
}
private getPropertyCompletions(
schema: ResolvedSchema,
doc: Parser.JSONDocument,
node: ObjectASTNode,
addValue: boolean,
separatorAfter: string,
collector: CompletionsCollector,
textBuffer: TextBuffer,
overwriteRange: Range
): void {
const matchingSchemas = doc.getMatchingSchemas(schema.schema);
const existingKey = textBuffer.getText(overwriteRange);
const hasColumn = textBuffer.getLineContent(overwriteRange.start.line).indexOf(':') === -1;
matchingSchemas.forEach((s) => {
if (s.node === node && !s.inverted) {
this.collectDefaultSnippets(s.schema, separatorAfter, collector, {
newLineFirst: false,
indentFirstObject: false,
shouldIndentWithTab: false,
});
const schemaProperties = s.schema.properties;
if (schemaProperties) {
const maxProperties = s.schema.maxProperties;
if (maxProperties === undefined || node.properties === undefined || node.properties.length <= maxProperties) {
Object.keys(schemaProperties).forEach((key: string) => {
const propertySchema = schemaProperties[key];
if (typeof propertySchema === 'object' && !propertySchema.deprecationMessage && !propertySchema['doNotSuggest']) {
let identCompensation = '';
if (node.parent && node.parent.type === 'array' && node.properties.length <= 1) {
// because there is a slash '-' to prevent the properties generated to have the correct
// indent
const sourceText = textBuffer.getText();
const indexOfSlash = sourceText.lastIndexOf('-', node.offset - 1);
if (indexOfSlash >= 0) {
// add one space to compensate the '-'
identCompensation = ' ' + sourceText.slice(indexOfSlash + 1, node.offset);
}
}
let insertText = key;
if (!key.startsWith(existingKey) || hasColumn) {
insertText = this.getInsertTextForProperty(
key,
propertySchema,
addValue,
separatorAfter,
identCompensation + this.indentation
);
}
collector.add({
kind: CompletionItemKind.Property,
label: key,
insertText,
insertTextFormat: InsertTextFormat.Snippet,
documentation: super.fromMarkup(propertySchema.markdownDescription) || propertySchema.description || '',
});
}
});
}
}
// Error fix
// If this is a array of string/boolean/number
// test:
// - item1
// it will treated as a property key since `:` has been appended
if (node.type === 'object' && node.parent && node.parent.type === 'array' && s.schema.type !== 'object') {
this.addSchemaValueCompletions(s.schema, separatorAfter, collector, {});
}
}
if (node.parent && s.node === node.parent && node.type === 'object' && s.schema.defaultSnippets) {
// For some reason the first item in the array needs to be treated differently, otherwise
// the indentation will not be correct
if (node.properties.length === 1) {
this.collectDefaultSnippets(
s.schema,
separatorAfter,
collector,
{
newLineFirst: false,
indentFirstObject: false,
shouldIndentWithTab: true,
},
1
);
} else {
this.collectDefaultSnippets(
s.schema,
separatorAfter,
collector,
{
newLineFirst: false,
indentFirstObject: true,
shouldIndentWithTab: false,
},
1
);
}
}
});
}
private getValueCompletions(
schema: ResolvedSchema,
doc: Parser.JSONDocument,
node: ASTNode,
offset: number,
document: TextDocument,
collector: CompletionsCollector,
types: { [type: string]: boolean }
): void {
let parentKey: string = null;
if (node && (node.type === 'string' || node.type === 'number' || node.type === 'boolean')) {
node = node.parent;
}
if (node && node.type === 'null') {
const nodeParent = node.parent;
/*
* This is going to be an object for some reason and we need to find the property
* Its an issue with the null node
*/
if (nodeParent && nodeParent.type === 'object') {
for (const prop in nodeParent['properties']) {
const currNode = nodeParent['properties'][prop];
if (currNode.keyNode && currNode.keyNode.value === node.location) {
node = currNode;
}
}
}
}
if (!node) {
this.addSchemaValueCompletions(schema.schema, '', collector, types);
return;
}
if (node.type === 'property' && offset > (<PropertyASTNode>node).colonOffset) {
const valueNode = node.valueNode;
if (valueNode && offset > valueNode.offset + valueNode.length) {
return; // we are past the value node
}
parentKey = node.keyNode.value;
node = node.parent;
}
if (node && (parentKey !== null || node.type === 'array')) {
const separatorAfter = '';
const matchingSchemas = doc.getMatchingSchemas(schema.schema);
matchingSchemas.forEach((s) => {
if (s.node === node && !s.inverted && s.schema) {
if (s.schema.items) {
this.collectDefaultSnippets(s.schema, separatorAfter, collector, {
newLineFirst: false,
indentFirstObject: false,
shouldIndentWithTab: false,
});
if (Array.isArray(s.schema.items)) {
const index = super.findItemAtOffset(node, document, offset);
if (index < s.schema.items.length) {
this.addSchemaValueCompletions(s.schema.items[index], separatorAfter, collector, types);
}
} else if (typeof s.schema.items === 'object' && s.schema.items.type === 'object') {
collector.add({
kind: super.getSuggestionKind(s.schema.items.type),
label: '- (array item)',
documentation: `Create an item of an array${
s.schema.description === undefined ? '' : '(' + s.schema.description + ')'
}`,
insertText: `- ${this.getInsertTextForObject(s.schema.items, separatorAfter, ' ').insertText.trimLeft()}`,
insertTextFormat: InsertTextFormat.Snippet,
});
this.addSchemaValueCompletions(s.schema.items, separatorAfter, collector, types);
} else if (typeof s.schema.items === 'object' && s.schema.items.anyOf) {
s.schema.items.anyOf
.filter((i) => typeof i === 'object')
.forEach((i: JSONSchema, index) => {
const insertText = `- ${this.getInsertTextForObject(i, separatorAfter).insertText.trimLeft()}`;
//append insertText to documentation
const documentation = this.getDocumentationWithMarkdownText(
`Create an item of an array${s.schema.description === undefined ? '' : '(' + s.schema.description + ')'}`,
insertText
);
collector.add({
kind: super.getSuggestionKind(i.type),
label: '- (array item) ' + (index + 1),
documentation: documentation,
insertText: insertText,
insertTextFormat: InsertTextFormat.Snippet,
});
});
this.addSchemaValueCompletions(s.schema.items, separatorAfter, collector, types);
} else {
this.addSchemaValueCompletions(s.schema.items, separatorAfter, collector, types);
}
}
if (s.schema.properties) {
const propertySchema = s.schema.properties[parentKey];
if (propertySchema) {
this.addSchemaValueCompletions(propertySchema, separatorAfter, collector, types);
}
}
}
});
if (types['boolean']) {
this.addBooleanValueCompletion(true, separatorAfter, collector);
this.addBooleanValueCompletion(false, separatorAfter, collector);
}
if (types['null']) {
this.addNullValueCompletion(separatorAfter, collector);
}
}
}
private getCustomTagValueCompletions(collector: CompletionsCollector): void {
const validCustomTags = filterInvalidCustomTags(this.customTags);
validCustomTags.forEach((validTag) => {
// Valid custom tags are guarenteed to be strings
const label = validTag.split(' ')[0];
this.addCustomTagValueCompletion(collector, ' ', label);
});
}
private addSchemaValueCompletions(
schema: JSONSchemaRef,
separatorAfter: string,
collector: CompletionsCollector,
types: { [type: string]: boolean }
): void {
super.addSchemaValueCompletions(schema, separatorAfter, collector, types);
}
private addDefaultValueCompletions(
schema: JSONSchema,
separatorAfter: string,
collector: CompletionsCollector,
arrayDepth = 0
): void {
let hasProposals = false;
if (isDefined(schema.default)) {
let type = schema.type;
let value = schema.default;
for (let i = arrayDepth; i > 0; i--) {
value = [value];
type = 'array';
}
let label;
if (typeof value == 'object') {
label = 'Default value';
} else {
label = (value as unknown).toString().replace(doubleQuotesEscapeRegExp, '"');
}
collector.add({
kind: this.getSuggestionKind(type),
label,
insertText: this.getInsertTextForValue(value, separatorAfter, type),
insertTextFormat: InsertTextFormat.Snippet,
detail: localize('json.suggest.default', 'Default value'),
});
hasProposals = true;
}
if (Array.isArray(schema.examples)) {
schema.examples.forEach((example) => {
let type = schema.type;
let value = example;
for (let i = arrayDepth; i > 0; i--) {
value = [value];
type = 'array';
}
collector.add({
kind: this.getSuggestionKind(type),
label: value,
insertText: this.getInsertTextForValue(value, separatorAfter, type),
insertTextFormat: InsertTextFormat.Snippet,
});
hasProposals = true;
});
}
this.collectDefaultSnippets(schema, separatorAfter, collector, {
newLineFirst: true,
indentFirstObject: true,
shouldIndentWithTab: true,
});
if (!hasProposals && typeof schema.items === 'object' && !Array.isArray(schema.items)) {
this.addDefaultValueCompletions(schema.items, separatorAfter, collector, arrayDepth + 1);
}
}
private collectDefaultSnippets(
schema: JSONSchema,
separatorAfter: string,
collector: CompletionsCollector,
settings: StringifySettings,
arrayDepth = 0
): void {
if (Array.isArray(schema.defaultSnippets)) {
schema.defaultSnippets.forEach((s) => {
let type = schema.type;
let value = s.body;
let label = s.label;
let insertText: string;
let filterText: string;
if (isDefined(value)) {
const type = s.type || schema.type;
if (arrayDepth === 0 && type === 'array') {
// We know that a - isn't present yet so we need to add one
const fixedObj = {};
Object.keys(value).forEach((val, index) => {
if (index === 0 && !val.startsWith('-')) {
fixedObj[`- ${val}`] = value[val];
} else {
fixedObj[` ${val}`] = value[val];
}
});
value = fixedObj;
}
insertText = this.getInsertTextForSnippetValue(value, separatorAfter, settings);
label = label || this.getLabelForSnippetValue(value);
} else if (typeof s.bodyText === 'string') {
let prefix = '',
suffix = '',
indent = '';
for (let i = arrayDepth; i > 0; i--) {
prefix = prefix + indent + '[\n';
suffix = suffix + '\n' + indent + ']';
indent += this.indentation;
type = 'array';
}
insertText = prefix + indent + s.bodyText.split('\n').join('\n' + indent) + suffix + separatorAfter;
label = label || insertText;
filterText = insertText.replace(/[\n]/g, ''); // remove new lines
}
collector.add({
kind: s.suggestionKind || this.getSuggestionKind(type),
label,
documentation: super.fromMarkup(s.markdownDescription) || s.description,
insertText,
insertTextFormat: InsertTextFormat.Snippet,
filterText,
});
});
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private getInsertTextForSnippetValue(value: any, separatorAfter: string, settings: StringifySettings, depth?: number): string {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const replacer = (value: any): string | any => {
if (typeof value === 'string') {
if (value[0] === '^') {
return value.substr(1);
}
if (value === 'true' || value === 'false') {
return `"${value}"`;
}
}
return value;
};
return stringifyObject(value, '', replacer, settings, depth) + separatorAfter;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private getLabelForSnippetValue(value: any): string {
const label = JSON.stringify(value);
return label.replace(/\$\{\d+:([^}]+)\}|\$\d+/g, '$1');
}
private addCustomTagValueCompletion(collector: CompletionsCollector, separatorAfter: string, label: string): void {
collector.add({
kind: super.getSuggestionKind('string'),
label: label,
insertText: label + separatorAfter,
insertTextFormat: InsertTextFormat.Snippet,
documentation: '',
});
}
private addBooleanValueCompletion(value: boolean, separatorAfter: string, collector: CompletionsCollector): void {
collector.add({
kind: this.getSuggestionKind('boolean'),
label: value ? 'true' : 'false',
insertText: this.getInsertTextForValue(value, separatorAfter, 'boolean'),
insertTextFormat: InsertTextFormat.Snippet,
documentation: '',
});
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private getSuggestionKind(type: any): CompletionItemKind {
if (Array.isArray(type)) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const array = <any[]>type;
type = array.length > 0 ? array[0] : null;
}
if (!type) {
return CompletionItemKind.Value;
}
switch (type) {
case 'string':
return CompletionItemKind.Value;
case 'object':
return CompletionItemKind.Module;
case 'property':
return CompletionItemKind.Property;
default:
return CompletionItemKind.Value;
}
}
private addNullValueCompletion(separatorAfter: string, collector: CompletionsCollector): void {
collector.add({
kind: this.getSuggestionKind('null'),
label: 'null',
insertText: 'null' + separatorAfter,
insertTextFormat: InsertTextFormat.Snippet,
documentation: '',
});
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private getInsertTextForValue(value: any, separatorAfter: string, type: string | string[]): string {
if (value === null) {
value = 'null'; // replace type null with string 'null'
}
switch (typeof value) {
case 'object': {
const indent = this.indentation;
return this.getInsertTemplateForValue(value, indent, { index: 1 }, separatorAfter);
}
}
type = Array.isArray(type) ? type[0] : type;
if (type === 'string') {
value = convertToStringValue(value);
}
return this.getInsertTextForPlainText(value + separatorAfter);
}
private getInsertTemplateForValue(
value: unknown | [],
indent: string,
navOrder: { index: number },
separatorAfter: string
): string {
if (Array.isArray(value)) {
let insertText = '\n';
for (const arrValue of value) {
insertText += `${indent}- \${${navOrder.index++}:${arrValue}}\n`;
}
return insertText;
} else if (typeof value === 'object') {
let insertText = '\n';
for (const key in value) {
if (Object.prototype.hasOwnProperty.call(value, key)) {
const element = value[key];
insertText += `${indent}\${${navOrder.index++}:${key}}:`;
let valueTemplate;
if (typeof element === 'object') {
valueTemplate = `${this.getInsertTemplateForValue(element, indent + this.indentation, navOrder, separatorAfter)}`;
} else {
valueTemplate = ` \${${navOrder.index++}:${this.getInsertTextForPlainText(element + separatorAfter)}}\n`;
}
insertText += `${valueTemplate}`;
}
}
return insertText;
}
return this.getInsertTextForPlainText(value + separatorAfter);
}
private getInsertTextForPlainText(text: string): string {
return text.replace(/[\\$}]/g, '\\$&'); // escape $, \ and }
}
private getInsertTextForObject(
schema: JSONSchema,
separatorAfter: string,
indent = this.indentation,
insertIndex = 1
): InsertText {
let insertText = '';
if (!schema.properties) {
insertText = `${indent}$${insertIndex++}\n`;
return { insertText, insertIndex };
}
Object.keys(schema.properties).forEach((key: string) => {
const propertySchema = schema.properties[key] as JSONSchema;
let type = Array.isArray(propertySchema.type) ? propertySchema.type[0] : propertySchema.type;
if (!type) {
if (propertySchema.properties) {
type = 'object';
}
if (propertySchema.items) {
type = 'array';
}
}
if (schema.required && schema.required.indexOf(key) > -1) {
switch (type) {
case 'boolean':
case 'string':
case 'number':
case 'integer':
insertText += `${indent}${key}: $${insertIndex++}\n`;
break;
case 'array':
{
const arrayInsertResult = this.getInsertTextForArray(propertySchema.items, separatorAfter, insertIndex++);
const arrayInsertLines = arrayInsertResult.insertText.split('\n');
let arrayTemplate = arrayInsertResult.insertText;
if (arrayInsertLines.length > 1) {
for (let index = 1; index < arrayInsertLines.length; index++) {
const element = arrayInsertLines[index];
arrayInsertLines[index] = `${indent}${this.indentation} ${element.trimLeft()}`;
}
arrayTemplate = arrayInsertLines.join('\n');
}
insertIndex = arrayInsertResult.insertIndex;
insertText += `${indent}${key}:\n${indent}${this.indentation}- ${arrayTemplate}\n`;
}
break;
case 'object':
{
const objectInsertResult = this.getInsertTextForObject(
propertySchema,
separatorAfter,
`${indent}${this.indentation}`,
insertIndex++
);
insertIndex = objectInsertResult.insertIndex;
insertText += `${indent}${key}:\n${objectInsertResult.insertText}\n`;
}
break;
}
} else if (propertySchema.default !== undefined) {
switch (type) {
case 'boolean':
case 'number':
case 'integer':
insertText += `${indent}${key}: \${${insertIndex++}:${propertySchema.default}}\n`;
break;
case 'string':
insertText += `${indent}${key}: \${${insertIndex++}:${convertToStringValue(propertySchema.default)}}\n`;
break;
case 'array':
case 'object':
// TODO: support default value for array object
break;
}
}
});
if (insertText.trim().length === 0) {
insertText = `${indent}$${insertIndex++}\n`;
}
insertText = insertText.trimRight() + separatorAfter;
return { insertText, insertIndex };
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private getInsertTextForArray(schema: any, separatorAfter: string, insertIndex = 1): InsertText {
let insertText = '';
if (!schema) {
insertText = `$${insertIndex++}`;
return { insertText, insertIndex };
}
let type = Array.isArray(schema.type) ? schema.type[0] : schema.type;
if (!type) {
if (schema.properties) {
type = 'object';
}
if (schema.items) {
type = 'array';
}
}
switch (schema.type) {
case 'boolean':
insertText = `\${${insertIndex++}:false}`;
break;
case 'number':
case 'integer':
insertText = `\${${insertIndex++}:0}`;
break;
case 'string':
insertText = `\${${insertIndex++}:""}`;
break;
case 'object':
{
const objectInsertResult = this.getInsertTextForObject(schema, separatorAfter, `${this.indentation} `, insertIndex++);
insertText = objectInsertResult.insertText.trimLeft();
insertIndex = objectInsertResult.insertIndex;
}
break;
}
return { insertText, insertIndex };
}
private getInsertTextForProperty(
key: string,
propertySchema: JSONSchema,
addValue: boolean,
separatorAfter: string,
ident = this.indentation
): string {
const propertyText = this.getInsertTextForValue(key, '', 'string');
const resultText = propertyText + ':';
let value: string;
let nValueProposals = 0;
if (propertySchema) {
let type = Array.isArray(propertySchema.type) ? propertySchema.type[0] : propertySchema.type;
if (!type) {
if (propertySchema.properties) {
type = 'object';
} else if (propertySchema.items) {
type = 'array';
}
}
if (Array.isArray(propertySchema.defaultSnippets)) {
if (propertySchema.defaultSnippets.length === 1) {
const body = propertySchema.defaultSnippets[0].body;
if (isDefined(body)) {
value = this.getInsertTextForSnippetValue(
body,
'',
{
newLineFirst: true,
indentFirstObject: false,
shouldIndentWithTab: false,
},
1
);
// add space before default snippet value
if (!value.startsWith(' ') && !value.startsWith('\n')) {
value = ' ' + value;
}
}
}
nValueProposals += propertySchema.defaultSnippets.length;
}
if (propertySchema.enum) {
if (!value && propertySchema.enum.length === 1) {
value = ' ' + this.getInsertTextForGuessedValue(propertySchema.enum[0], '', type);
}
nValueProposals += propertySchema.enum.length;
}
if (isDefined(propertySchema.default)) {
if (!value) {
value = ' ' + this.getInsertTextForGuessedValue(propertySchema.default, '', type);
}
nValueProposals++;
}
if (Array.isArray(propertySchema.examples) && propertySchema.examples.length) {
if (!value) {
value = ' ' + this.getInsertTextForGuessedValue(propertySchema.examples[0], '', type);
}
nValueProposals += propertySchema.examples.length;
}
if (propertySchema.properties) {
return `${resultText}\n${this.getInsertTextForObject(propertySchema, separatorAfter, ident).insertText}`;
} else if (propertySchema.items) {
return `${resultText}\n${this.indentation}- ${
this.getInsertTextForArray(propertySchema.items, separatorAfter).insertText
}`;
}
if (nValueProposals === 0) {
switch (type) {
case 'boolean':
value = ' $1';
break;
case 'string':
value = ' $1';
break;
case 'object':
value = `\n${ident}`;
break;
case 'array':
value = `\n${ident}- `;
break;
case 'number':
case 'integer':
value = ' ${1:0}';
break;
case 'null':
value = ' ${1:null}';
break;
default:
return propertyText;
}
}
}
if (!value || nValueProposals > 1) {
value = ' $1';
}
return resultText + value + separatorAfter;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private getInsertTextForGuessedValue(value: any, separatorAfter: string, type: string): string {
switch (typeof value) {
case 'object':
if (value === null) {
return '${1:null}' + separatorAfter;
}
return this.getInsertTextForValue(value, separatorAfter, type);
case 'string': {
let snippetValue = JSON.stringify(value);
snippetValue = snippetValue.substr(1, snippetValue.length - 2); // remove quotes
snippetValue = this.getInsertTextForPlainText(snippetValue); // escape \ and }
if (type === 'string') {
snippetValue = convertToStringValue(snippetValue);
}
return '${1:' + snippetValue + '}' + separatorAfter;
}
case 'number':
case 'boolean':
return '${1:' + value + '}' + separatorAfter;
}
return this.getInsertTextForValue(value, separatorAfter, type);
}
private getLabelForValue(value: string): string {
if (value === null) {
return 'null'; // return string with 'null' value if schema contains null as possible value
}
return value;
}
/**
* Corrects simple syntax mistakes to load possible nodes even if a semicolon is missing
*/
private completionHelper(document: TextDocument, textDocumentPosition: Position): NewTextAndPosition {
// Get the string we are looking at via a substring
const linePos = textDocumentPosition.line;
const position = textDocumentPosition;
const lineOffset = getLineOffsets(document.getText());
const start = lineOffset[linePos]; // Start of where the autocompletion is happening
let end = 0; // End of where the autocompletion is happening
if (lineOffset[linePos + 1]) {
end = lineOffset[linePos + 1];
} else {
end = document.getText().length;
}
while (end - 1 >= 0 && this.is_EOL(document.getText().charCodeAt(end - 1))) {
end--;
}
const textLine = document.getText().substring(start, end);
// Check if document contains only white spaces and line delimiters