From 8efb097b8ff6e78921d1f71e7b536236cd6a63b1 Mon Sep 17 00:00:00 2001 From: DanielHabenicht Date: Fri, 31 Jul 2026 15:35:03 +0200 Subject: [PATCH] Preserve internal indentation of YAML in comment blocks Standalone block comments were reindented by trimming each line's leading whitespace and prepending the current depth indent, which flattened any relative indentation of the comment body (e.g. YAML examples). Now the opening line is reindented and the remaining lines are shifted by the same delta, preserving their relative structure. Fixes #17 --- format.go | 34 +++++++++++++++++-- templates_test/comment_yaml_indentation.yaml | 3 ++ .../templates/comment_yaml_indentation.yaml | 8 +++++ .../comment_yaml_indentation.yaml | 8 +++++ 4 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 templates_test/comment_yaml_indentation.yaml create mode 100644 templates_test/templates/comment_yaml_indentation.yaml create mode 100644 templates_test/templates_expected/comment_yaml_indentation.yaml diff --git a/format.go b/format.go index 2a36fe4..14703df 100644 --- a/format.go +++ b/format.go @@ -76,9 +76,14 @@ func formatIndentation(src string, config *Config, filePath string) string { if commentOpenRe.MatchString(lines[i]) { cEnd, remainder, ok := skipLeadingBlockComment(lines, i) if ok && strings.TrimSpace(remainder) == "" { - indent := strings.Repeat(" ", depth*config.IndentSize) - for j := i; j <= cEnd && j < len(lines); j++ { - lines[j] = indent + strings.TrimLeft(lines[j], " \t") + newIndent := depth * config.IndentSize + // Reindent the opening line and shift the remaining lines by the same + // amount, preserving any relative indentation of the comment body + // (e.g. YAML examples inside the comment). + delta := newIndent - leadingWhitespace(lines[i]) + lines[i] = strings.Repeat(" ", newIndent) + strings.TrimLeft(lines[i], " \t") + for j := i + 1; j <= cEnd && j < len(lines); j++ { + lines[j] = shiftIndent(lines[j], delta) } i = cEnd continue @@ -229,6 +234,29 @@ func parseTokenFromLine(lines []string, lineIdx int, line string, config *Config return "", lineIdx, lineIdx, tokNone, false } +// leadingWhitespace returns the number of leading space/tab characters. +func leadingWhitespace(line string) int { + n := 0 + for n < len(line) && (line[n] == ' ' || line[n] == '\t') { + n++ + } + return n +} + +// shiftIndent adjusts a line's leading indentation by delta columns (clamped at +// zero) while keeping the rest of the line verbatim. Blank lines are untouched. +func shiftIndent(line string, delta int) string { + if strings.TrimSpace(line) == "" { + return line + } + cur := leadingWhitespace(line) + width := cur + delta + if width < 0 { + width = 0 + } + return strings.Repeat(" ", width) + line[cur:] +} + // Пропуск блочного комментария func skipLeadingBlockComment(lines []string, start int) (endLine int, remainder string, ok bool) { for i := start; i < len(lines); i++ { diff --git a/templates_test/comment_yaml_indentation.yaml b/templates_test/comment_yaml_indentation.yaml new file mode 100644 index 0000000..8f221b5 --- /dev/null +++ b/templates_test/comment_yaml_indentation.yaml @@ -0,0 +1,3 @@ +name: "Comment block preserves internal YAML indentation (issue #17)" +input_file: "templates/comment_yaml_indentation.yaml" +expected_file: "templates_expected/comment_yaml_indentation.yaml" diff --git a/templates_test/templates/comment_yaml_indentation.yaml b/templates_test/templates/comment_yaml_indentation.yaml new file mode 100644 index 0000000..a8985c6 --- /dev/null +++ b/templates_test/templates/comment_yaml_indentation.yaml @@ -0,0 +1,8 @@ +{{- if .Values.enabled }} +{{/* +Example values: +- name: istio-system + podSecurity: + enforce: baseline +*/}} +{{- end }} diff --git a/templates_test/templates_expected/comment_yaml_indentation.yaml b/templates_test/templates_expected/comment_yaml_indentation.yaml new file mode 100644 index 0000000..8308567 --- /dev/null +++ b/templates_test/templates_expected/comment_yaml_indentation.yaml @@ -0,0 +1,8 @@ +{{- if .Values.enabled }} + {{/* + Example values: + - name: istio-system + podSecurity: + enforce: baseline + */}} +{{- end }}