Fixes #2365: [json] JSON could complete identifier with quotes
This commit is contained in:
parent
706413e537
commit
5785cf462e
|
@ -107,10 +107,10 @@ export class JSONCompletion {
|
|||
let isLast = properties.length === 0 || offset >= properties[properties.length - 1].start;
|
||||
if (schema) {
|
||||
// property proposals with schema
|
||||
this.getPropertySuggestions(schema, doc, node, currentKey, addValue, isLast, collector);
|
||||
} else if (node.parent) {
|
||||
this.getPropertySuggestions(schema, doc, node, addValue, isLast, collector);
|
||||
} else {
|
||||
// property proposals without schema
|
||||
this.getSchemaLessPropertySuggestions(doc, node, collector);
|
||||
this.getSchemaLessPropertySuggestions(doc, node, currentKey, currentWord, isLast, collector);
|
||||
}
|
||||
|
||||
let location = node.getNodeLocation();
|
||||
|
@ -135,6 +135,7 @@ export class JSONCompletion {
|
|||
// value proposals without schema
|
||||
this.getSchemaLessValueSuggestions(doc, node, offset, document, collector);
|
||||
}
|
||||
|
||||
if (!node) {
|
||||
this.contributions.forEach((contribution) => {
|
||||
let collectPromise = contribution.collectDefaultSuggestions(textDocumentPosition.uri, collector);
|
||||
|
@ -162,7 +163,7 @@ export class JSONCompletion {
|
|||
});
|
||||
}
|
||||
|
||||
private getPropertySuggestions(schema: SchemaService.ResolvedSchema, doc: Parser.JSONDocument, node: Parser.ASTNode, currentWord: string, addValue: boolean, isLast: boolean, collector: ISuggestionsCollector): void {
|
||||
private getPropertySuggestions(schema: SchemaService.ResolvedSchema, doc: Parser.JSONDocument, node: Parser.ASTNode, addValue: boolean, isLast: boolean, collector: ISuggestionsCollector): void {
|
||||
let matchingSchemas: Parser.IApplicableSchema[] = [];
|
||||
doc.validate(schema.schema, matchingSchemas, node.start);
|
||||
|
||||
|
@ -179,13 +180,14 @@ export class JSONCompletion {
|
|||
});
|
||||
}
|
||||
|
||||
private getSchemaLessPropertySuggestions(doc: Parser.JSONDocument, node: Parser.ASTNode, collector: ISuggestionsCollector): void {
|
||||
private getSchemaLessPropertySuggestions(doc: Parser.JSONDocument, node: Parser.ASTNode, currentKey: string, currentWord: string, isLast: boolean, collector: ISuggestionsCollector): void {
|
||||
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: '' });
|
||||
});
|
||||
};
|
||||
if (node.parent) {
|
||||
if (node.parent.type === 'property') {
|
||||
// if the object is a property value, check the tree for other objects that hang under a property of the same name
|
||||
let parentKey = (<Parser.PropertyASTNode>node.parent).key.value;
|
||||
|
@ -204,6 +206,10 @@ 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: '' });
|
||||
}
|
||||
}
|
||||
|
||||
private getSchemaLessValueSuggestions(doc: Parser.JSONDocument, node: Parser.ASTNode, offset: number, document: ITextDocument, collector: ISuggestionsCollector): void {
|
||||
let collectSuggestionsForValues = (value: Parser.ASTNode) => {
|
||||
|
@ -404,6 +410,7 @@ export class JSONCompletion {
|
|||
}
|
||||
result += ': ';
|
||||
|
||||
if (propertySchema) {
|
||||
let defaultVal = propertySchema.default;
|
||||
if (typeof defaultVal !== 'undefined') {
|
||||
result = result + this.getSnippetForValue(defaultVal);
|
||||
|
@ -433,6 +440,9 @@ export class JSONCompletion {
|
|||
return result;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
result += '{{0}}';
|
||||
}
|
||||
if (!isLast) {
|
||||
result += ',';
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue