@@ -95,11 +95,15 @@ export class SingleYAMLDocument extends JSONDocument {
9595 return this . internalDocument . warnings . map ( YAMLErrorToYamlDocDiagnostics ) ;
9696 }
9797
98- getNodeFromPosition ( positionOffset : number , textBuffer : TextBuffer ) : [ YamlNode | undefined , boolean ] {
98+ getNodeFromPosition (
99+ positionOffset : number ,
100+ textBuffer : TextBuffer ,
101+ configuredIndentation ?: number
102+ ) : [ YamlNode | undefined , boolean ] {
99103 const position = textBuffer . getPosition ( positionOffset ) ;
100104 const lineContent = textBuffer . getLineContent ( position . line ) ;
101105 if ( lineContent . trim ( ) . length === 0 ) {
102- return [ this . findClosestNode ( positionOffset , textBuffer ) , true ] ;
106+ return [ this . findClosestNode ( positionOffset , textBuffer , configuredIndentation ) , true ] ;
103107 }
104108
105109 let closestNode : Node ;
@@ -122,7 +126,7 @@ export class SingleYAMLDocument extends JSONDocument {
122126 return [ closestNode , false ] ;
123127 }
124128
125- findClosestNode ( offset : number , textBuffer : TextBuffer ) : YamlNode {
129+ findClosestNode ( offset : number , textBuffer : TextBuffer , configuredIndentation ?: number ) : YamlNode {
126130 let offsetDiff = this . internalDocument . range [ 2 ] ;
127131 let maxOffset = this . internalDocument . range [ 0 ] ;
128132 let closestNode : YamlNode ;
@@ -151,34 +155,57 @@ export class SingleYAMLDocument extends JSONDocument {
151155 }
152156
153157 if ( indentation === position . character ) {
154- closestNode = this . getProperParentByIndentation ( indentation , closestNode , textBuffer ) ;
158+ closestNode = this . getProperParentByIndentation ( indentation , closestNode , textBuffer , '' , configuredIndentation ) ;
155159 }
156160
157161 return closestNode ;
158162 }
159163
160- private getProperParentByIndentation ( indentation : number , node : YamlNode , textBuffer : TextBuffer ) : YamlNode {
164+ private getProperParentByIndentation (
165+ indentation : number ,
166+ node : YamlNode ,
167+ textBuffer : TextBuffer ,
168+ currentLine : string ,
169+ configuredIndentation : number ,
170+ rootParent ?: YamlNode
171+ ) : YamlNode {
161172 if ( ! node ) {
162173 return this . internalDocument . contents as Node ;
163174 }
175+ configuredIndentation = ! configuredIndentation ? 2 : configuredIndentation ;
164176 if ( isNode ( node ) && node . range ) {
165177 const position = textBuffer . getPosition ( node . range [ 0 ] ) ;
178+ const lineContent = textBuffer . getLineContent ( position . line ) ;
179+ currentLine = currentLine === '' ? lineContent . trim ( ) : currentLine ;
180+ if ( currentLine . startsWith ( '-' ) && indentation === configuredIndentation && currentLine === lineContent . trim ( ) ) {
181+ position . character += indentation ;
182+ }
166183 if ( position . character > indentation && position . character > 0 ) {
167184 const parent = this . getParent ( node ) ;
168185 if ( parent ) {
169- return this . getProperParentByIndentation ( indentation , parent , textBuffer ) ;
186+ return this . getProperParentByIndentation (
187+ indentation ,
188+ parent ,
189+ textBuffer ,
190+ currentLine ,
191+ configuredIndentation ,
192+ rootParent
193+ ) ;
170194 }
171195 } else if ( position . character < indentation ) {
172196 const parent = this . getParent ( node ) ;
173197 if ( isPair ( parent ) && isNode ( parent . value ) ) {
174198 return parent . value ;
199+ } else if ( isPair ( rootParent ) && isNode ( rootParent . value ) ) {
200+ return rootParent . value ;
175201 }
176202 } else {
177203 return node ;
178204 }
179205 } else if ( isPair ( node ) ) {
206+ rootParent = node ;
180207 const parent = this . getParent ( node ) ;
181- return this . getProperParentByIndentation ( indentation , parent , textBuffer ) ;
208+ return this . getProperParentByIndentation ( indentation , parent , textBuffer , currentLine , configuredIndentation , rootParent ) ;
182209 }
183210 return node ;
184211 }
0 commit comments