|
| 1 | +package workflow |
| 2 | + |
| 3 | +import "strings" |
| 4 | + |
| 5 | +// yamlBlockScalarState tracks whether the scanner is currently inside a YAML |
| 6 | +// literal (|) or folded (>) block scalar. It is used by appendYAMLLine to |
| 7 | +// decide whether a line's trailing whitespace may be safely trimmed. |
| 8 | +// |
| 9 | +// The zero value is ready to use. Create a new instance per YAML stream and |
| 10 | +// pass it to every appendYAMLLine call in that stream. |
| 11 | +type yamlBlockScalarState struct { |
| 12 | + pending bool // saw a block-scalar header; waiting for the first content line |
| 13 | + active bool // inside a block scalar's payload |
| 14 | + headerIndent int // leading-space count of the block-scalar header line |
| 15 | + bodyIndent int // leading-space count of the first payload line |
| 16 | +} |
| 17 | + |
| 18 | +// update advances the block-scalar state machine for sourceLine (the raw line |
| 19 | +// as it appears in the source YAML, before any prefix or indentation adjustment |
| 20 | +// is applied). It returns true when sourceLine is part of a block scalar's |
| 21 | +// payload and its content must NOT be right-trimmed. |
| 22 | +func (s *yamlBlockScalarState) update(sourceLine string) bool { |
| 23 | + trimmed := strings.TrimRight(sourceLine, " \t") |
| 24 | + |
| 25 | + if s.pending || s.active { |
| 26 | + if trimmed == "" { |
| 27 | + // Blank lines keep the state alive without advancing it: |
| 28 | + // inside an active scalar they are payload; inside a pending |
| 29 | + // scalar they simply delay the first content line. |
| 30 | + return s.active |
| 31 | + } |
| 32 | + lineIndent := countLeadingSpaces(sourceLine) |
| 33 | + if s.pending { |
| 34 | + if lineIndent <= s.headerIndent { |
| 35 | + // No indented content followed the header; leave the scalar. |
| 36 | + s.pending = false |
| 37 | + // Fall through to structural handling below. |
| 38 | + } else { |
| 39 | + s.bodyIndent = lineIndent |
| 40 | + s.active = true |
| 41 | + s.pending = false |
| 42 | + return true // first payload line |
| 43 | + } |
| 44 | + } |
| 45 | + if s.active { |
| 46 | + if lineIndent < s.bodyIndent { |
| 47 | + // Outdented non-blank line: we have left the block scalar. |
| 48 | + s.active = false |
| 49 | + // Fall through to structural handling below. |
| 50 | + } else { |
| 51 | + return true // still inside the block scalar |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Structural line: detect whether it introduces a new block scalar. |
| 57 | + if trimmed != "" { |
| 58 | + if headerIndent, ok := blockScalarHeaderIndentForLine(trimmed); ok { |
| 59 | + s.pending = true |
| 60 | + s.headerIndent = headerIndent |
| 61 | + } |
| 62 | + } |
| 63 | + return false |
| 64 | +} |
| 65 | + |
| 66 | +// appendYAMLLine writes content to b with the given prefix. |
| 67 | +// |
| 68 | +// - Blank content (empty or whitespace-only) is always written as a bare |
| 69 | +// newline with no prefix, regardless of isBlockScalarContent, to avoid |
| 70 | +// emitting lines that contain only spaces (yamllint trailing-spaces). |
| 71 | +// - When isBlockScalarContent is true, non-blank content is written verbatim |
| 72 | +// so that trailing whitespace that is semantically significant inside a |
| 73 | +// shell script or template literal is preserved. |
| 74 | +// - When isBlockScalarContent is false, trailing whitespace is trimmed before |
| 75 | +// writing so that yamllint trailing-spaces warnings are suppressed. |
| 76 | +// |
| 77 | +// Callers obtain isBlockScalarContent by calling yamlBlockScalarState.update |
| 78 | +// with the original source line. When the source indentation is adjusted before |
| 79 | +// writing (e.g. a 2-space prefix is stripped in writeStepsSection), callers |
| 80 | +// must still pass the original source line to update so that indent-based |
| 81 | +// entry/exit detection remains accurate, and then pass the adjusted content |
| 82 | +// here. |
| 83 | +func appendYAMLLine(b *strings.Builder, prefix, content string, isBlockScalarContent bool) { |
| 84 | + // Always emit blank lines as bare newlines to avoid trailing-space violations. |
| 85 | + if strings.TrimRight(content, " \t") == "" { |
| 86 | + b.WriteByte('\n') |
| 87 | + return |
| 88 | + } |
| 89 | + if isBlockScalarContent { |
| 90 | + b.WriteString(prefix) |
| 91 | + b.WriteString(content) |
| 92 | + b.WriteByte('\n') |
| 93 | + return |
| 94 | + } |
| 95 | + b.WriteString(prefix) |
| 96 | + b.WriteString(strings.TrimRight(content, " \t")) |
| 97 | + b.WriteByte('\n') |
| 98 | +} |
0 commit comments