88)
99
1010func FormatNodeWithResolvedAlias (n ast.Node , anchorNodeMap map [string ]ast.Node ) string {
11- tk := n . GetToken ( )
11+ tk := getFirstToken ( n )
1212 if tk == nil {
1313 return ""
1414 }
@@ -18,7 +18,7 @@ func FormatNodeWithResolvedAlias(n ast.Node, anchorNodeMap map[string]ast.Node)
1818}
1919
2020func FormatNode (n ast.Node ) string {
21- tk := n . GetToken ( )
21+ tk := getFirstToken ( n )
2222 if tk == nil {
2323 return ""
2424 }
@@ -29,7 +29,7 @@ func FormatFile(file *ast.File) string {
2929 if len (file .Docs ) == 0 {
3030 return ""
3131 }
32- tk := file .Docs [0 ]. GetToken ( )
32+ tk := getFirstToken ( file .Docs [0 ])
3333 if tk == nil {
3434 return ""
3535 }
@@ -131,6 +131,58 @@ func hasComment(n ast.Node) bool {
131131 return false
132132}
133133
134+ func getFirstToken (n ast.Node ) * token.Token {
135+ if n == nil {
136+ return nil
137+ }
138+ switch nn := n .(type ) {
139+ case * ast.DocumentNode :
140+ if nn .Start != nil {
141+ return nn .Start
142+ }
143+ return getFirstToken (nn .Body )
144+ case * ast.NullNode :
145+ return nn .Token
146+ case * ast.BoolNode :
147+ return nn .Token
148+ case * ast.IntegerNode :
149+ return nn .Token
150+ case * ast.FloatNode :
151+ return nn .Token
152+ case * ast.StringNode :
153+ return nn .Token
154+ case * ast.InfinityNode :
155+ return nn .Token
156+ case * ast.NanNode :
157+ return nn .Token
158+ case * ast.LiteralNode :
159+ return nn .Start
160+ case * ast.DirectiveNode :
161+ return nn .Start
162+ case * ast.TagNode :
163+ return nn .Start
164+ case * ast.MappingNode :
165+ if nn .IsFlowStyle {
166+ return nn .Start
167+ }
168+ if len (nn .Values ) == 0 {
169+ return nn .Start
170+ }
171+ return getFirstToken (nn .Values [0 ].Key )
172+ case * ast.MappingKeyNode :
173+ return nn .Start
174+ case * ast.MergeKeyNode :
175+ return nn .Token
176+ case * ast.SequenceNode :
177+ return nn .Start
178+ case * ast.AnchorNode :
179+ return nn .Start
180+ case * ast.AliasNode :
181+ return nn .Start
182+ }
183+ return nil
184+ }
185+
134186type Formatter struct {
135187 existsComment bool
136188 tokenToOriginMap map [* token.Token ]string
@@ -155,17 +207,66 @@ func newFormatter(tk *token.Token, existsComment bool) *Formatter {
155207 tokenToOriginMap [tk ] = origin
156208 origin = ""
157209 }
158-
159210 return & Formatter {
160211 existsComment : existsComment ,
161212 tokenToOriginMap : tokenToOriginMap ,
162213 }
163214}
164215
216+ func getIndentNumByFirstLineToken (tk * token.Token ) int {
217+ defaultIndent := tk .Position .Column - 1
218+
219+ // key: value
220+ // ^
221+ // next
222+ if tk .Type == token .SequenceEntryType {
223+ // If the current token is the sequence entry.
224+ // the indent is calculated from the column value of the current token.
225+ return defaultIndent
226+ }
227+
228+ // key: value
229+ // ^
230+ // next
231+ if tk .Next != nil && tk .Next .Type == token .MappingValueType {
232+ // If the current token is the key in the mapping-value,
233+ // the indent is calculated from the column value of the current token.
234+ return defaultIndent
235+ }
236+
237+ if tk .Prev == nil {
238+ return defaultIndent
239+ }
240+ prev := tk .Prev
241+
242+ // key: value
243+ // ^
244+ // prev
245+ if prev .Type == token .MappingValueType {
246+ // If the current token is the value in the mapping-value,
247+ // the indent is calculated from the column value of the key two steps back.
248+ if prev .Prev == nil {
249+ return defaultIndent
250+ }
251+ return prev .Prev .Position .Column - 1
252+ }
253+
254+ // - value
255+ // ^
256+ // prev
257+ if prev .Type == token .SequenceEntryType {
258+ // If the value is not a mapping-value and the previous token was a sequence entry,
259+ // the indent is calculated using the column value of the sequence entry token.
260+ return prev .Position .Column - 1
261+ }
262+
263+ return defaultIndent
264+ }
265+
165266func (f * Formatter ) format (n ast.Node ) string {
166267 return f .trimSpacePrefix (
167268 f .trimIndentSpace (
168- n . GetToken (). Position . IndentNum ,
269+ getIndentNumByFirstLineToken ( getFirstToken ( n )) ,
169270 f .trimNewLineCharPrefix (f .formatNode (n )),
170271 ),
171272 )
@@ -306,7 +407,8 @@ func (f *Formatter) formatAnchor(n *ast.AnchorNode) string {
306407
307408func (f * Formatter ) formatAlias (n * ast.AliasNode ) string {
308409 if f .anchorNodeMap != nil {
309- node := f .anchorNodeMap [n .Value .GetToken ().Value ]
410+ anchorName := n .Value .GetToken ().Value
411+ node := f .anchorNodeMap [anchorName ]
310412 if node != nil {
311413 formatted := f .formatNode (node )
312414 // If formatted text contains newline characters, indentation needs to be considered.
0 commit comments