@@ -7,6 +7,16 @@ import (
7
7
"github.com/goccy/go-yaml/token"
8
8
)
9
9
10
+ func FormatNodeWithResolvedAlias (n ast.Node , anchorNodeMap map [string ]ast.Node ) string {
11
+ tk := n .GetToken ()
12
+ if tk == nil {
13
+ return ""
14
+ }
15
+ formatter := newFormatter (tk , hasComment (n ))
16
+ formatter .anchorNodeMap = anchorNodeMap
17
+ return formatter .format (n )
18
+ }
19
+
10
20
func FormatNode (n ast.Node ) string {
11
21
tk := n .GetToken ()
12
22
if tk == nil {
@@ -124,6 +134,7 @@ func hasComment(n ast.Node) bool {
124
134
type Formatter struct {
125
135
existsComment bool
126
136
tokenToOriginMap map [* token.Token ]string
137
+ anchorNodeMap map [string ]ast.Node
127
138
}
128
139
129
140
func newFormatter (tk * token.Token , existsComment bool ) * Formatter {
@@ -294,6 +305,19 @@ func (f *Formatter) formatAnchor(n *ast.AnchorNode) string {
294
305
}
295
306
296
307
func (f * Formatter ) formatAlias (n * ast.AliasNode ) string {
308
+ if f .anchorNodeMap != nil {
309
+ node := f .anchorNodeMap [n .Value .GetToken ().Value ]
310
+ if node != nil {
311
+ formatted := f .formatNode (node )
312
+ // If formatted text contains newline characters, indentation needs to be considered.
313
+ if strings .Contains (formatted , "\n " ) {
314
+ // If the first character is not a newline, the first line should be output without indentation.
315
+ isIgnoredFirstLine := ! strings .HasPrefix (formatted , "\n " )
316
+ formatted = f .addIndentSpace (n .GetToken ().Position .IndentNum , formatted , isIgnoredFirstLine )
317
+ }
318
+ return formatted
319
+ }
320
+ }
297
321
return f .origin (n .Start ) + f .formatNode (n .Value )
298
322
}
299
323
@@ -385,7 +409,7 @@ func (f *Formatter) trimIndentSpace(trimIndentNum int, v string) string {
385
409
}
386
410
lines := strings .Split (normalizeNewLineChars (v ), "\n " )
387
411
out := make ([]string , 0 , len (lines ))
388
- for _ , line := range strings . Split ( v , " \n " ) {
412
+ for _ , line := range lines {
389
413
var cnt int
390
414
out = append (out , strings .TrimLeftFunc (line , func (r rune ) bool {
391
415
cnt ++
@@ -395,6 +419,23 @@ func (f *Formatter) trimIndentSpace(trimIndentNum int, v string) string {
395
419
return strings .Join (out , "\n " )
396
420
}
397
421
422
+ func (f * Formatter ) addIndentSpace (indentNum int , v string , isIgnoredFirstLine bool ) string {
423
+ if indentNum == 0 {
424
+ return v
425
+ }
426
+ indent := strings .Repeat (" " , indentNum )
427
+ lines := strings .Split (normalizeNewLineChars (v ), "\n " )
428
+ out := make ([]string , 0 , len (lines ))
429
+ for idx , line := range lines {
430
+ if line == "" || (isIgnoredFirstLine && idx == 0 ) {
431
+ out = append (out , line )
432
+ continue
433
+ }
434
+ out = append (out , indent + line )
435
+ }
436
+ return strings .Join (out , "\n " )
437
+ }
438
+
398
439
// normalizeNewLineChars normalize CRLF and CR to LF.
399
440
func normalizeNewLineChars (v string ) string {
400
441
return strings .ReplaceAll (strings .ReplaceAll (v , "\r \n " , "\n " ), "\r " , "\n " )
0 commit comments