fill Pretty-prints fodder. The crowded and separateToken params control whether single whitespace characters are added to keep tokens from joining together in the output. The intuition of crowded is that the caller passes true for crowded if the last thing printed would crowd whatever we're printing
(fodder ast.Fodder, crowded bool, separateToken bool, final bool)
| 51 | // If crowded is false and separateToken is false then no space is printed |
| 52 | // after or before the fodder, even if the last fodder was an interstitial. |
| 53 | func (u *unparser) fodderFill(fodder ast.Fodder, crowded bool, separateToken bool, final bool) { |
| 54 | var lastIndent int |
| 55 | for i, fod := range fodder { |
| 56 | skipTrailing := final && (i == (len(fodder) - 1)) |
| 57 | switch fod.Kind { |
| 58 | case ast.FodderParagraph: |
| 59 | for i, l := range fod.Comment { |
| 60 | // Do not indent empty lines (note: first line is never empty). |
| 61 | if len(l) > 0 { |
| 62 | // First line is already indented by previous fod. |
| 63 | if i > 0 { |
| 64 | for i := 0; i < lastIndent; i++ { |
| 65 | u.write(" ") |
| 66 | } |
| 67 | } |
| 68 | u.write(l) |
| 69 | } |
| 70 | u.write("\n") |
| 71 | } |
| 72 | if !skipTrailing { |
| 73 | for i := 0; i < fod.Blanks; i++ { |
| 74 | u.write("\n") |
| 75 | } |
| 76 | for i := 0; i < fod.Indent; i++ { |
| 77 | u.write(" ") |
| 78 | } |
| 79 | } |
| 80 | lastIndent = fod.Indent |
| 81 | crowded = false |
| 82 | |
| 83 | case ast.FodderLineEnd: |
| 84 | if len(fod.Comment) > 0 { |
| 85 | u.write(" ") |
| 86 | u.write(fod.Comment[0]) |
| 87 | } |
| 88 | u.write("\n") |
| 89 | if !skipTrailing { |
| 90 | for i := 0; i < fod.Blanks; i++ { |
| 91 | u.write("\n") |
| 92 | } |
| 93 | for i := 0; i < fod.Indent; i++ { |
| 94 | u.write(" ") |
| 95 | } |
| 96 | } |
| 97 | lastIndent = fod.Indent |
| 98 | crowded = false |
| 99 | |
| 100 | case ast.FodderInterstitial: |
| 101 | if crowded { |
| 102 | u.write(" ") |
| 103 | } |
| 104 | u.write(fod.Comment[0]) |
| 105 | crowded = true |
| 106 | } |
| 107 | } |
| 108 | if separateToken && crowded { |
| 109 | u.write(" ") |
| 110 | } |