@@ -31,6 +31,7 @@ import { stringifyObject, StringifySettings } from '../utils/json';
3131import { isDefined , isString } from '../utils/objects' ;
3232import * as nls from 'vscode-nls' ;
3333import { setKubernetesParserOption } from '../parser/isKubernetes' ;
34+ import { isMapContainsEmptyPair } from '../utils/astUtils' ;
3435import { indexOf } from '../utils/astUtils' ;
3536
3637const localize = nls . loadMessageBundle ( ) ;
@@ -242,28 +243,34 @@ export class YamlCompletion {
242243 }
243244 }
244245 } else if ( node . value === null ) {
245- if ( isPair ( parent ) && parent . key === node ) {
246- node = parent ;
247- } else if (
248- isPair ( parent ) &&
249- lineContent . trim ( ) . length === 0 &&
250- textBuffer . getLineContent ( position . line - 1 ) . indexOf ( ':' ) > 0 &&
251- textBuffer . getLineContent ( position . line - 1 ) . indexOf ( '-' ) < 0
252- ) {
253- const map = this . createTempObjNode ( currentWord , node , currentDoc ) ;
254-
255- const parentParent = currentDoc . getParent ( parent ) ;
256- if ( parentParent && ( isMap ( parentParent ) || isSeq ( parentParent ) ) ) {
257- parentParent . set ( parent . key , map ) ;
246+ if ( isPair ( parent ) ) {
247+ if ( parent . key === node ) {
248+ node = parent ;
258249 } else {
259- currentDoc . internalDocument . set ( parent . key , map ) ;
260- }
261- currentProperty = ( map as YAMLMap ) . items [ 0 ] ;
262- node = map ;
263- } else if ( lineContent . trim ( ) . length === 0 ) {
264- const parentParent = currentDoc . getParent ( parent ) ;
265- if ( parentParent ) {
266- node = parentParent ;
250+ if ( isNode ( parent . key ) && parent . key . range ) {
251+ const parentParent = currentDoc . getParent ( parent ) ;
252+ if ( foundByClosest && parentParent && isMap ( parentParent ) && isMapContainsEmptyPair ( parentParent ) ) {
253+ node = parentParent ;
254+ } else {
255+ const parentPosition = document . positionAt ( parent . key . range [ 0 ] ) ;
256+ //if cursor has bigger indentation that parent key, then we need to complete new empty object
257+ if ( position . character > parentPosition . character && position . line !== parentPosition . line ) {
258+ const map = this . createTempObjNode ( currentWord , node , currentDoc ) ;
259+
260+ if ( parentParent && ( isMap ( parentParent ) || isSeq ( parentParent ) ) ) {
261+ parentParent . set ( parent . key , map ) ;
262+ } else {
263+ currentDoc . internalDocument . set ( parent . key , map ) ;
264+ }
265+ currentProperty = ( map as YAMLMap ) . items [ 0 ] ;
266+ node = map ;
267+ } else if ( parentPosition . character === position . character ) {
268+ if ( parentParent ) {
269+ node = parentParent ;
270+ }
271+ }
272+ }
273+ }
267274 }
268275 } else if ( isSeq ( parent ) ) {
269276 if ( lineContent . charAt ( position . character - 1 ) !== '-' ) {
@@ -373,7 +380,12 @@ export class YamlCompletion {
373380 const schemaProperties = schema . schema . properties ;
374381 if ( schemaProperties ) {
375382 const maxProperties = schema . schema . maxProperties ;
376- if ( maxProperties === undefined || node . items === undefined || node . items . length < maxProperties ) {
383+ if (
384+ maxProperties === undefined ||
385+ node . items === undefined ||
386+ node . items . length < maxProperties ||
387+ isMapContainsEmptyPair ( node )
388+ ) {
377389 for ( const key in schemaProperties ) {
378390 if ( Object . prototype . hasOwnProperty . call ( schemaProperties , key ) ) {
379391 const propertySchema = schemaProperties [ key ] ;
0 commit comments