-
Notifications
You must be signed in to change notification settings - Fork 269
/
yamlCompletion.ts
585 lines (527 loc) · 19.2 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
/*---------------------------------------------------------------------------------------------
* 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 Parser = require('../parser/jsonParser');
import Json = require('jsonc-parser');
import SchemaService = require('./jsonSchemaService');
import { JSONSchema } from '../jsonSchema';
import { JSONWorkerContribution, CompletionsCollector } from '../jsonContributions';
import { PromiseConstructor, Thenable } from 'vscode-json-languageservice';
import { CompletionItem, CompletionItemKind, CompletionList, TextDocument, Position, Range, TextEdit, InsertTextFormat } from 'vscode-languageserver-types';
import * as nls from 'vscode-nls';
import { matchOffsetToDocument } from '../utils/arrUtils';
const localize = nls.loadMessageBundle();
export class YAMLCompletion {
private schemaService: SchemaService.IJSONSchemaService;
private contributions: JSONWorkerContribution[];
private promise: PromiseConstructor;
private customTags: Array<String>;
constructor(schemaService: SchemaService.IJSONSchemaService, contributions: JSONWorkerContribution[] = [], promiseConstructor?: PromiseConstructor) {
this.schemaService = schemaService;
this.contributions = contributions;
this.promise = promiseConstructor || Promise;
this.customTags = [];
}
public configure(customTags: Array<String>){
this.customTags = customTags;
}
public doResolve(item: CompletionItem): Thenable<CompletionItem> {
for (let i = this.contributions.length - 1; i >= 0; i--) {
if (this.contributions[i].resolveCompletion) {
let resolver = this.contributions[i].resolveCompletion(item);
if (resolver) {
return resolver;
}
}
}
return this.promise.resolve(item);
}
public doComplete(document: TextDocument, position: Position, doc): Thenable<CompletionList> {
let result: CompletionList = {
items: [],
isIncomplete: false
};
let offset = document.offsetAt(position);
if(document.getText()[offset] === ":"){
return Promise.resolve(result);
}
let 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);
}
let currentWord = this.getCurrentWord(document, offset);
let overwriteRange = null;
if(node && node.type === 'null'){
let nodeStartPos = document.positionAt(node.start);
nodeStartPos.character += 1;
let nodeEndPos = document.positionAt(node.end);
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.start), document.positionAt(node.end));
} else {
let overwriteStart = offset - currentWord.length;
if (overwriteStart > 0 && document.getText()[overwriteStart - 1] === '"') {
overwriteStart--;
}
overwriteRange = Range.create(document.positionAt(overwriteStart), position);
}
let proposed: { [key: string]: CompletionItem } = {};
let collector: CompletionsCollector = {
add: (suggestion: CompletionItem) => {
let existing = proposed[suggestion.label];
if (!existing) {
proposed[suggestion.label] = suggestion;
if (overwriteRange) {
suggestion.textEdit = TextEdit.replace(overwriteRange, suggestion.insertText);
}
result.items.push(suggestion);
} else if (!existing.documentation) {
existing.documentation = suggestion.documentation;
}
},
setAsIncomplete: () => {
result.isIncomplete = true;
},
error: (message: string) => {
console.error(message);
},
log: (message: string) => {
console.log(message);
},
getNumberOfProposals: () => {
return result.items.length;
}
};
return this.schemaService.getSchemaForResource(document.uri).then((schema) => {
if(!schema){
return Promise.resolve(result);
}
let newSchema = schema;
if (schema.schema && schema.schema.schemaSequence && schema.schema.schemaSequence[currentDocIndex]) {
newSchema = new SchemaService.ResolvedSchema(schema.schema.schemaSequence[currentDocIndex]);
}
let collectionPromises: Thenable<any>[] = [];
let addValue = true;
let currentKey = '';
let currentProperty: Parser.PropertyASTNode = null;
if (node) {
if (node.type === 'string') {
let stringNode = <Parser.StringASTNode>node;
if (stringNode.isKey) {
addValue = !(node.parent && ((<Parser.PropertyASTNode>node.parent).value));
currentProperty = node.parent ? <Parser.PropertyASTNode>node.parent : null;
currentKey = document.getText().substring(node.start + 1, node.end - 1);
if (node.parent) {
node = node.parent.parent;
}
}
}
}
// proposals for properties
if (node && node.type === 'object') {
// don't suggest properties that are already present
let properties = (<Parser.ObjectASTNode>node).properties;
properties.forEach(p => {
if (!currentProperty || currentProperty !== p) {
proposed[p.key.value] = CompletionItem.create('__');
}
});
let separatorAfter = '';
if (addValue) {
separatorAfter = this.evaluateSeparatorAfter(document, document.offsetAt(overwriteRange.end));
}
if (newSchema) {
// property proposals with schema
this.getPropertyCompletions(newSchema, currentDoc, node, addValue, collector, separatorAfter);
}
let location = node.getPath();
this.contributions.forEach((contribution) => {
let collectPromise = contribution.collectPropertyCompletions(document.uri, location, currentWord, addValue, false, collector);
if (collectPromise) {
collectionPromises.push(collectPromise);
}
});
if ((!schema && currentWord.length > 0 && document.getText().charAt(offset - currentWord.length - 1) !== '"')) {
collector.add({
kind: CompletionItemKind.Property,
label: this.getLabelForValue(currentWord),
insertText: this.getInsertTextForProperty(currentWord, null, false, separatorAfter),
insertTextFormat: InsertTextFormat.Snippet,
documentation: ''
});
}
}
// proposals for values
let types: { [type: string]: boolean } = {};
if (newSchema) {
this.getValueCompletions(newSchema, currentDoc, node, offset, document, collector, types);
}
if (this.contributions.length > 0) {
this.getContributedValueCompletions(currentDoc, node, offset, document, collector, collectionPromises);
}
if (this.customTags.length > 0) {
this.getCustomTagValueCompletions(collector);
}
return this.promise.all(collectionPromises).then(() => {
return result;
});
});
}
private getPropertyCompletions(schema: SchemaService.ResolvedSchema, doc, node: Parser.ASTNode, addValue: boolean, collector: CompletionsCollector, separatorAfter: string): void {
let matchingSchemas = doc.getMatchingSchemas(schema.schema);
matchingSchemas.forEach((s) => {
if (s.node === node && !s.inverted) {
let schemaProperties = s.schema.properties;
if (schemaProperties) {
Object.keys(schemaProperties).forEach((key: string) => {
let propertySchema = schemaProperties[key];
if (!propertySchema.deprecationMessage && !propertySchema["doNotSuggest"]) {
collector.add({
kind: CompletionItemKind.Property,
label: key,
insertText: this.getInsertTextForProperty(key, propertySchema, addValue, separatorAfter),
insertTextFormat: InsertTextFormat.Snippet,
documentation: propertySchema.description || ''
});
}
});
}
}
});
}
private getValueCompletions(schema: SchemaService.ResolvedSchema, doc, node: Parser.ASTNode, offset: number, document: TextDocument, collector: CompletionsCollector, types: { [type: string]: boolean } ): void {
let offsetForSeparator = offset;
let parentKey: string = null;
let valueNode: Parser.ASTNode = null;
if (node && (node.type === 'string' || node.type === 'number' || node.type === 'boolean')) {
offsetForSeparator = node.end;
valueNode = node;
node = node.parent;
}
if(node && node.type === 'null'){
let 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(let prop in nodeParent["properties"]){
let currNode = nodeParent["properties"][prop];
if(currNode.key && currNode.key.location === node.location){
node = currNode;
}
}
}
}
if (!node) {
this.addSchemaValueCompletions(schema.schema, collector, types, "");
return;
}
if ((node.type === 'property') && offset > (<Parser.PropertyASTNode>node).colonOffset) {
let propertyNode = <Parser.PropertyASTNode>node;
let valueNode = propertyNode.value;
if (valueNode && offset > valueNode.end) {
return; // we are past the value node
}
parentKey = propertyNode.key.value;
node = node.parent;
}
let separatorAfter = this.evaluateSeparatorAfter(document, offsetForSeparator);
if (node && (parentKey !== null || node.type === 'array')) {
let matchingSchemas = doc.getMatchingSchemas(schema.schema);
matchingSchemas.forEach(s => {
if (s.node === node && !s.inverted && s.schema) {
if (s.schema.items) {
if (Array.isArray(s.schema.items)) {
let index = this.findItemAtOffset(node, document, offset);
if (index < s.schema.items.length) {
this.addSchemaValueCompletions(s.schema.items[index], collector, types, separatorAfter);
}
} else {
this.addSchemaValueCompletions(s.schema.items, collector, types, separatorAfter);
}
}
if (s.schema.properties) {
let propertySchema = s.schema.properties[parentKey];
if (propertySchema) {
this.addSchemaValueCompletions(propertySchema, collector, types, separatorAfter);
}
}
}
});
}
if(node){
if (types['boolean']) {
this.addBooleanValueCompletion(true, collector, separatorAfter);
this.addBooleanValueCompletion(false, collector, separatorAfter);
}
if (types['null']) {
this.addNullValueCompletion(collector, separatorAfter);
}
}
}
private getContributedValueCompletions(doc: Parser.JSONDocument, node: Parser.ASTNode, offset: number, document: TextDocument, collector: CompletionsCollector, collectionPromises: Thenable<any>[]) {
if (!node) {
this.contributions.forEach((contribution) => {
let collectPromise = contribution.collectDefaultCompletions(document.uri, collector);
if (collectPromise) {
collectionPromises.push(collectPromise);
}
});
} else {
if (node.type === 'string' || node.type === 'number' || node.type === 'boolean' || node.type === 'null') {
node = node.parent;
}
if ((node.type === 'property') && offset > (<Parser.PropertyASTNode>node).colonOffset) {
let parentKey = (<Parser.PropertyASTNode>node).key.value;
let valueNode = (<Parser.PropertyASTNode>node).value;
if (!valueNode || offset <= valueNode.end) {
let location = node.parent.getPath();
this.contributions.forEach((contribution) => {
let collectPromise = contribution.collectValueCompletions(document.uri, location, parentKey, collector);
if (collectPromise) {
collectionPromises.push(collectPromise);
}
});
}
}
}
}
private getCustomTagValueCompletions(collector: CompletionsCollector) {
this.customTags.forEach((customTagItem) => {
let tagItemSplit = customTagItem.split(" ");
if(tagItemSplit && tagItemSplit[0]){
this.addCustomTagValueCompletion(collector, " ", tagItemSplit[0]);
}
});
}
private addSchemaValueCompletions(schema: JSONSchema, collector: CompletionsCollector, types: { [type: string]: boolean }, separatorAfter: string): void {
this.addDefaultValueCompletions(schema, collector, separatorAfter);
this.addEnumValueCompletions(schema, collector, separatorAfter);
this.collectTypes(schema, types);
if (Array.isArray(schema.allOf)) {
schema.allOf.forEach(s => this.addSchemaValueCompletions(s, collector, types, separatorAfter));
}
if (Array.isArray(schema.anyOf)) {
schema.anyOf.forEach(s => this.addSchemaValueCompletions(s, collector, types, separatorAfter));
}
if (Array.isArray(schema.oneOf)) {
schema.oneOf.forEach(s => this.addSchemaValueCompletions(s, collector, types, separatorAfter));
}
}
private addDefaultValueCompletions(schema: JSONSchema, collector: CompletionsCollector, separatorAfter: string, arrayDepth = 0): void {
let hasProposals = false;
if (schema.default) {
let type = schema.type;
let value = schema.default;
for (let i = arrayDepth; i > 0; i--) {
value = [value];
type = 'array';
}
collector.add({
kind: this.getSuggestionKind(type),
label: this.getLabelForValue(value),
insertText: this.getInsertTextForValue(value, separatorAfter),
insertTextFormat: InsertTextFormat.Snippet,
detail: localize('json.suggest.default', 'Default value'),
});
hasProposals = true;
}
if (!hasProposals && schema.items && !Array.isArray(schema.items)) {
this.addDefaultValueCompletions(schema.items, collector, separatorAfter, arrayDepth + 1);
}
}
private addEnumValueCompletions(schema: JSONSchema, collector: CompletionsCollector, separatorAfter: string): void {
if (Array.isArray(schema.enum)) {
for (let i = 0, length = schema.enum.length; i < length; i++) {
let enm = schema.enum[i];
let documentation = schema.description;
if (schema.enumDescriptions && i < schema.enumDescriptions.length) {
documentation = schema.enumDescriptions[i];
}
collector.add({
kind: this.getSuggestionKind(schema.type),
label: this.getLabelForValue(enm),
insertText: this.getInsertTextForValue(enm, separatorAfter),
insertTextFormat: InsertTextFormat.Snippet,
documentation
});
}
}
}
private collectTypes(schema: JSONSchema, types: { [type: string]: boolean }) {
let type = schema.type;
if (Array.isArray(type)) {
type.forEach(t => types[t] = true);
} else {
types[type] = true;
}
}
private addBooleanValueCompletion(value: boolean, collector: CompletionsCollector, separatorAfter: string): void {
collector.add({
kind: this.getSuggestionKind('boolean'),
label: value ? 'true' : 'false',
insertText: this.getInsertTextForValue(value, separatorAfter),
insertTextFormat: InsertTextFormat.Snippet,
documentation: ''
});
}
private addNullValueCompletion(collector: CompletionsCollector, separatorAfter: string): void {
collector.add({
kind: this.getSuggestionKind('null'),
label: 'null',
insertText: 'null' + separatorAfter,
insertTextFormat: InsertTextFormat.Snippet,
documentation: ''
});
}
private addCustomTagValueCompletion(collector: CompletionsCollector, separatorAfter: string, label: string): void {
collector.add({
kind: this.getSuggestionKind('string'),
label: label,
insertText: label + separatorAfter,
insertTextFormat: InsertTextFormat.Snippet,
documentation: ''
});
}
private getLabelForValue(value: any): string {
let label = typeof value === "string" ? value : JSON.stringify(value);
if (label.length > 57) {
return label.substr(0, 57).trim() + '...';
}
return label;
}
private getSuggestionKind(type: any): CompletionItemKind {
if (Array.isArray(type)) {
let 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 getCurrentWord(document: TextDocument, offset: number) {
var i = offset - 1;
var text = document.getText();
while (i >= 0 && ' \t\n\r\v":{[,]}'.indexOf(text.charAt(i)) === -1) {
i--;
}
return text.substring(i + 1, offset);
}
private findItemAtOffset(node: Parser.ASTNode, document: TextDocument, offset: number) {
let scanner = Json.createScanner(document.getText(), true);
let children = node.getChildNodes();
for (let i = children.length - 1; i >= 0; i--) {
let child = children[i];
if (offset > child.end) {
scanner.setPosition(child.end);
let token = scanner.scan();
if (token === Json.SyntaxKind.CommaToken && offset >= scanner.getTokenOffset() + scanner.getTokenLength()) {
return i + 1;
}
return i;
} else if (offset >= child.start) {
return i;
}
}
return 0;
}
private isInComment(document: TextDocument, start: number, offset: number) {
let scanner = Json.createScanner(document.getText(), false);
scanner.setPosition(start);
let token = scanner.scan();
while (token !== Json.SyntaxKind.EOF && (scanner.getTokenOffset() + scanner.getTokenLength() < offset)) {
token = scanner.scan();
}
return (token === Json.SyntaxKind.LineCommentTrivia || token === Json.SyntaxKind.BlockCommentTrivia) && scanner.getTokenOffset() <= offset;
}
private getInsertTextForPlainText(text: string): string {
return text.replace(/[\\\$\}]/g, '\\$&'); // escape $, \ and }
}
private getInsertTextForValue(value: any, separatorAfter: string): string {
var text = value;
if (text === '{}') {
return '{\n\t$1\n}' + separatorAfter;
} else if (text === '[]') {
return '[\n\t$1\n]' + separatorAfter;
}
return this.getInsertTextForPlainText(text + separatorAfter);
}
private getInsertTextForProperty(key: string, propertySchema: JSONSchema, addValue: boolean, separatorAfter: string): string {
let propertyText = this.getInsertTextForValue(key, '');
// if (!addValue) {
// return propertyText;
// }
let resultText = propertyText + ':';
let value;
let nValueProposals = 0;
if (propertySchema) {
if (nValueProposals === 0) {
var type = Array.isArray(propertySchema.type) ? propertySchema.type[0] : propertySchema.type;
if (!type) {
if (propertySchema.properties) {
type = 'object';
} else if (propertySchema.items) {
type = 'array';
}
}
switch (type) {
case 'boolean':
value = ' $1';
break;
case 'string':
value = ' $1';
break;
case 'object':
value = '\n\t';
break;
case 'array':
value = '\n\t- ';
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;
}
private evaluateSeparatorAfter(document: TextDocument, offset: number) {
let scanner = Json.createScanner(document.getText(), true);
scanner.setPosition(offset);
let token = scanner.scan();
switch (token) {
case Json.SyntaxKind.CommaToken:
case Json.SyntaxKind.CloseBraceToken:
case Json.SyntaxKind.CloseBracketToken:
case Json.SyntaxKind.EOF:
return '';
default:
return '';
}
}
}