From e75c630aff9bea05d5d43f0c584cdc50c0878576 Mon Sep 17 00:00:00 2001 From: Martin Aeschlimann Date: Wed, 27 Jan 2016 11:40:02 +0100 Subject: [PATCH] Fixes #2388 [json] Completing a JSON suggestion enters snippet mode --- extensions/json/server/src/jsonCompletion.ts | 22 ++++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/extensions/json/server/src/jsonCompletion.ts b/extensions/json/server/src/jsonCompletion.ts index 7fab87683c0..7eecb4c65c9 100644 --- a/extensions/json/server/src/jsonCompletion.ts +++ b/extensions/json/server/src/jsonCompletion.ts @@ -34,7 +34,7 @@ export class JSONCompletion { let offset = document.offsetAt(textDocumentPosition.position); let node = doc.getNodeFromOffsetEndInclusive(offset); - + let overwriteRange = null; let result: CompletionList = { items: [], @@ -53,7 +53,7 @@ export class JSONCompletion { if (overwriteRange) { suggestion.textEdit = TextEdit.replace(overwriteRange, suggestion.insertText); } - + result.items.push(suggestion); } }, @@ -67,7 +67,7 @@ export class JSONCompletion { return this.schemaService.getSchemaForResource(textDocumentPosition.uri, doc).then((schema) => { let collectionPromises: Thenable[] = []; - + let addValue = true; let currentKey = ''; let currentWord = ''; @@ -102,7 +102,7 @@ export class JSONCompletion { proposed[p.key.value] = true; } }); - + let isLast = properties.length === 0 || offset >= properties[properties.length - 1].start; if (schema) { // property proposals with schema @@ -111,7 +111,7 @@ export class JSONCompletion { // property proposals without schema this.getSchemaLessPropertySuggestions(doc, node, collector); } - + let location = node.getNodeLocation(); this.contributions.forEach((contribution) => { let collectPromise = contribution.collectPropertySuggestions(textDocumentPosition.uri, location, this.getCurrentWord(document, offset), addValue, isLast, collector); @@ -293,12 +293,12 @@ export class JSONCompletion { } private addBooleanSuggestion(value: boolean, collector: ISuggestionsCollector): void { - collector.add({ kind: this.getSuggestionKind('boolean'), label: value ? 'true' : 'false', insertText: this.getSnippetForValue(value), documentation: '' }); + collector.add({ kind: this.getSuggestionKind('boolean'), label: value ? 'true' : 'false', insertText: this.getTextForValue(value), documentation: '' }); } private addEnumSuggestion(schema: JsonSchema.IJSONSchema, collector: ISuggestionsCollector): void { if (Array.isArray(schema.enum)) { - schema.enum.forEach((enm) => collector.add({ kind: this.getSuggestionKind(schema.type), label: this.getLabelForValue(enm), insertText: this.getSnippetForValue(enm), documentation: '' })); + schema.enum.forEach((enm) => collector.add({ kind: this.getSuggestionKind(schema.type), label: this.getLabelForValue(enm), insertText: this.getTextForValue(enm), documentation: '' })); } else if (schema.type === 'boolean') { this.addBooleanSuggestion(true, collector); this.addBooleanSuggestion(false, collector); @@ -319,7 +319,7 @@ export class JSONCompletion { collector.add({ kind: this.getSuggestionKind(schema.type), label: this.getLabelForValue(schema.default), - insertText: this.getSnippetForValue(schema.default), + insertText: this.getTextForValue(schema.default), detail: nls.localize('json.suggest.default', 'Default value'), }); } @@ -343,6 +343,10 @@ export class JSONCompletion { return label; } + private getTextForValue(value: any): string { + return JSON.stringify(value, null, '\t'); + } + private getSnippetForValue(value: any): string { let snippet = JSON.stringify(value, null, '\t'); switch (typeof value) { @@ -435,7 +439,7 @@ export class JSONCompletion { private getSnippetForSimilarProperty(key: string, templateValue: Parser.ASTNode): string { return '"' + key + '"'; } - + private getCurrentWord(document: ITextDocument, offset: number) { var i = offset - 1; var text = document.getText();