@@ -172,7 +172,7 @@ export class JSONCompletion {
172172 if ( schemaProperties ) {
173173 Object . keys ( schemaProperties ) . forEach ( ( key : string ) => {
174174 let propertySchema = schemaProperties [ key ] ;
175- collector . add ( { kind : CompletionItemKind . Property , label : key , insertText : this . getSnippetForProperty ( key , propertySchema , addValue , isLast ) , documentation : propertySchema . description || '' } ) ;
175+ collector . add ( { kind : CompletionItemKind . Property , label : key , insertText : this . getTextForProperty ( key , propertySchema , addValue , isLast ) , documentation : propertySchema . description || '' } ) ;
176176 } ) ;
177177 }
178178 }
@@ -183,7 +183,7 @@ export class JSONCompletion {
183183 let collectSuggestionsForSimilarObject = ( obj : Parser . ObjectASTNode ) => {
184184 obj . properties . forEach ( ( p ) => {
185185 let key = p . key . value ;
186- collector . add ( { kind : CompletionItemKind . Property , label : key , insertText : this . getSnippetForSimilarProperty ( key , p . value ) , documentation : '' } ) ;
186+ collector . add ( { kind : CompletionItemKind . Property , label : key , insertText : this . getTextForSimilarProperty ( key , p . value ) , documentation : '' } ) ;
187187 } ) ;
188188 } ;
189189 if ( node . parent ) {
@@ -206,14 +206,14 @@ export class JSONCompletion {
206206 }
207207 }
208208 if ( ! currentKey && currentWord . length > 0 ) {
209- collector . add ( { kind : CompletionItemKind . Property , label : JSON . stringify ( currentWord ) , insertText : this . getSnippetForProperty ( currentWord , null , true , isLast ) , documentation : '' } ) ;
209+ collector . add ( { kind : CompletionItemKind . Property , label : this . getLabelForValue ( currentWord ) , insertText : this . getTextForProperty ( currentWord , null , true , isLast ) , documentation : '' } ) ;
210210 }
211211 }
212212
213213 private getSchemaLessValueSuggestions ( doc : Parser . JSONDocument , node : Parser . ASTNode , offset : number , document : ITextDocument , collector : ISuggestionsCollector ) : void {
214214 let collectSuggestionsForValues = ( value : Parser . ASTNode ) => {
215215 if ( ! value . contains ( offset ) ) {
216- let content = this . getMatchingSnippet ( value , document ) ;
216+ let content = this . getTextForMatchingNode ( value , document ) ;
217217 collector . add ( { kind : this . getSuggestionKind ( value . type ) , label : content , insertText : content , documentation : '' } ) ;
218218 }
219219 if ( value . type === 'boolean' ) {
@@ -347,6 +347,16 @@ export class JSONCompletion {
347347 detail : nls . localize ( 'json.suggest.default' , 'Default value' ) ,
348348 } ) ;
349349 }
350+ if ( Array . isArray ( schema . defaultSnippets ) ) {
351+ schema . defaultSnippets . forEach ( s => {
352+ collector . add ( {
353+ kind : CompletionItemKind . Snippet ,
354+ label : this . getLabelForSnippetValue ( s . body ) ,
355+ insertText : this . getTextForSnippetValue ( s . body )
356+ } ) ;
357+ } ) ;
358+ }
359+
350360 if ( Array . isArray ( schema . allOf ) ) {
351361 schema . allOf . forEach ( ( s ) => this . addDefaultSuggestion ( s , collector ) ) ;
352362 }
@@ -360,19 +370,33 @@ export class JSONCompletion {
360370
361371 private getLabelForValue ( value : any ) : string {
362372 let label = JSON . stringify ( value ) ;
363- label = label . replace ( '{{' , '' ) . replace ( '}}' , '' ) ;
373+ if ( label . length > 57 ) {
374+ return label . substr ( 0 , 57 ) . trim ( ) + '...' ;
375+ }
376+ return label ;
377+ }
378+
379+ private getLabelForSnippetValue ( value : any ) : string {
380+ let label = JSON . stringify ( value ) ;
381+ label = label . replace ( / \{ \{ | \} \} / g, '' ) ;
364382 if ( label . length > 57 ) {
365383 return label . substr ( 0 , 57 ) . trim ( ) + '...' ;
366384 }
367385 return label ;
368386 }
369387
370388 private getTextForValue ( value : any ) : string {
389+ var text = JSON . stringify ( value , null , '\t' ) ;
390+ text = text . replace ( / [ \\ \{ \} ] / g, '\\$&' ) ;
391+ return text ;
392+ }
393+
394+ private getTextForSnippetValue ( value : any ) : string {
371395 return JSON . stringify ( value , null , '\t' ) ;
372396 }
373397
374- private getSnippetForValue ( value : any ) : string {
375- let snippet = JSON . stringify ( value , null , '\t' ) ;
398+ private getTextForEnumValue ( value : any ) : string {
399+ let snippet = this . getTextForValue ( value ) ;
376400 switch ( typeof value ) {
377401 case 'object' :
378402 if ( value === null ) {
@@ -405,7 +429,7 @@ export class JSONCompletion {
405429 }
406430
407431
408- private getMatchingSnippet ( node : Parser . ASTNode , document : ITextDocument ) : string {
432+ private getTextForMatchingNode ( node : Parser . ASTNode , document : ITextDocument ) : string {
409433 switch ( node . type ) {
410434 case 'array' :
411435 return '[]' ;
@@ -417,9 +441,9 @@ export class JSONCompletion {
417441 }
418442 }
419443
420- private getSnippetForProperty ( key : string , propertySchema : JsonSchema . IJSONSchema , addValue : boolean , isLast : boolean ) : string {
444+ private getTextForProperty ( key : string , propertySchema : JsonSchema . IJSONSchema , addValue : boolean , isLast : boolean ) : string {
421445
422- let result = '"' + key + '"' ;
446+ let result = this . getTextForValue ( key ) ;
423447 if ( ! addValue ) {
424448 return result ;
425449 }
@@ -428,9 +452,9 @@ export class JSONCompletion {
428452 if ( propertySchema ) {
429453 let defaultVal = propertySchema . default ;
430454 if ( typeof defaultVal !== 'undefined' ) {
431- result = result + this . getSnippetForValue ( defaultVal ) ;
455+ result = result + this . getTextForEnumValue ( defaultVal ) ;
432456 } else if ( propertySchema . enum && propertySchema . enum . length > 0 ) {
433- result = result + this . getSnippetForValue ( propertySchema . enum [ 0 ] ) ;
457+ result = result + this . getTextForEnumValue ( propertySchema . enum [ 0 ] ) ;
434458 } else {
435459 var type = Array . isArray ( propertySchema . type ) ? propertySchema . type [ 0 ] : propertySchema . type ;
436460 switch ( type ) {
@@ -465,8 +489,8 @@ export class JSONCompletion {
465489 return result ;
466490 }
467491
468- private getSnippetForSimilarProperty ( key : string , templateValue : Parser . ASTNode ) : string {
469- return '"' + key + '"' ;
492+ private getTextForSimilarProperty ( key : string , templateValue : Parser . ASTNode ) : string {
493+ return this . getTextForValue ( key ) ;
470494 }
471495
472496 private getCurrentWord ( document : ITextDocument , offset : number ) {
0 commit comments