Fixes #3494: [snippets] [debt] don't allow snippet syntax in default values

This commit is contained in:
Martin Aeschlimann
2016-02-29 22:29:09 +01:00
parent c676ceb733
commit f0c6ccccce
19 changed files with 227 additions and 135 deletions
+38 -14
View File
@@ -172,7 +172,7 @@ export class JSONCompletion {
if (schemaProperties) {
Object.keys(schemaProperties).forEach((key: string) => {
let propertySchema = schemaProperties[key];
collector.add({ kind: CompletionItemKind.Property, label: key, insertText: this.getSnippetForProperty(key, propertySchema, addValue, isLast), documentation: propertySchema.description || '' });
collector.add({ kind: CompletionItemKind.Property, label: key, insertText: this.getTextForProperty(key, propertySchema, addValue, isLast), documentation: propertySchema.description || '' });
});
}
}
@@ -183,7 +183,7 @@ export class JSONCompletion {
let collectSuggestionsForSimilarObject = (obj: Parser.ObjectASTNode) => {
obj.properties.forEach((p) => {
let key = p.key.value;
collector.add({ kind: CompletionItemKind.Property, label: key, insertText: this.getSnippetForSimilarProperty(key, p.value), documentation: '' });
collector.add({ kind: CompletionItemKind.Property, label: key, insertText: this.getTextForSimilarProperty(key, p.value), documentation: '' });
});
};
if (node.parent) {
@@ -206,14 +206,14 @@ export class JSONCompletion {
}
}
if (!currentKey && currentWord.length > 0) {
collector.add({ kind: CompletionItemKind.Property, label: JSON.stringify(currentWord), insertText: this.getSnippetForProperty(currentWord, null, true, isLast), documentation: '' });
collector.add({ kind: CompletionItemKind.Property, label: this.getLabelForValue(currentWord), insertText: this.getTextForProperty(currentWord, null, true, isLast), documentation: '' });
}
}
private getSchemaLessValueSuggestions(doc: Parser.JSONDocument, node: Parser.ASTNode, offset: number, document: ITextDocument, collector: ISuggestionsCollector): void {
let collectSuggestionsForValues = (value: Parser.ASTNode) => {
if (!value.contains(offset)) {
let content = this.getMatchingSnippet(value, document);
let content = this.getTextForMatchingNode(value, document);
collector.add({ kind: this.getSuggestionKind(value.type), label: content, insertText: content, documentation: '' });
}
if (value.type === 'boolean') {
@@ -347,6 +347,16 @@ export class JSONCompletion {
detail: nls.localize('json.suggest.default', 'Default value'),
});
}
if (Array.isArray(schema.defaultSnippets)) {
schema.defaultSnippets.forEach(s => {
collector.add({
kind: CompletionItemKind.Snippet,
label: this.getLabelForSnippetValue(s.body),
insertText: this.getTextForSnippetValue(s.body)
});
});
}
if (Array.isArray(schema.allOf)) {
schema.allOf.forEach((s) => this.addDefaultSuggestion(s, collector));
}
@@ -360,7 +370,15 @@ export class JSONCompletion {
private getLabelForValue(value: any): string {
let label = JSON.stringify(value);
label = label.replace('{{', '').replace('}}', '');
if (label.length > 57) {
return label.substr(0, 57).trim() + '...';
}
return label;
}
private getLabelForSnippetValue(value: any): string {
let label = JSON.stringify(value);
label = label.replace(/\{\{|\}\}/g, '');
if (label.length > 57) {
return label.substr(0, 57).trim() + '...';
}
@@ -368,11 +386,17 @@ export class JSONCompletion {
}
private getTextForValue(value: any): string {
var text = JSON.stringify(value, null, '\t');
text = text.replace(/[\\\{\}]/g, '\\$&');
return text;
}
private getTextForSnippetValue(value: any): string {
return JSON.stringify(value, null, '\t');
}
private getSnippetForValue(value: any): string {
let snippet = JSON.stringify(value, null, '\t');
private getTextForEnumValue(value: any): string {
let snippet = this.getTextForValue(value);
switch (typeof value) {
case 'object':
if (value === null) {
@@ -405,7 +429,7 @@ export class JSONCompletion {
}
private getMatchingSnippet(node: Parser.ASTNode, document: ITextDocument): string {
private getTextForMatchingNode(node: Parser.ASTNode, document: ITextDocument): string {
switch (node.type) {
case 'array':
return '[]';
@@ -417,9 +441,9 @@ export class JSONCompletion {
}
}
private getSnippetForProperty(key: string, propertySchema: JsonSchema.IJSONSchema, addValue: boolean, isLast: boolean): string {
private getTextForProperty(key: string, propertySchema: JsonSchema.IJSONSchema, addValue: boolean, isLast: boolean): string {
let result = '"' + key + '"';
let result = this.getTextForValue(key);
if (!addValue) {
return result;
}
@@ -428,9 +452,9 @@ export class JSONCompletion {
if (propertySchema) {
let defaultVal = propertySchema.default;
if (typeof defaultVal !== 'undefined') {
result = result + this.getSnippetForValue(defaultVal);
result = result + this.getTextForEnumValue(defaultVal);
} else if (propertySchema.enum && propertySchema.enum.length > 0) {
result = result + this.getSnippetForValue(propertySchema.enum[0]);
result = result + this.getTextForEnumValue(propertySchema.enum[0]);
} else {
var type = Array.isArray(propertySchema.type) ? propertySchema.type[0] : propertySchema.type;
switch (type) {
@@ -465,8 +489,8 @@ export class JSONCompletion {
return result;
}
private getSnippetForSimilarProperty(key: string, templateValue: Parser.ASTNode): string {
return '"' + key + '"';
private getTextForSimilarProperty(key: string, templateValue: Parser.ASTNode): string {
return this.getTextForValue(key);
}
private getCurrentWord(document: ITextDocument, offset: number) {