Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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++ {
Expand Down
3 changes: 3 additions & 0 deletions templates_test/comment_yaml_indentation.yaml
Original file line number Diff line number Diff line change
@@ -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"
8 changes: 8 additions & 0 deletions templates_test/templates/comment_yaml_indentation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{- if .Values.enabled }}
{{/*
Example values:
- name: istio-system
podSecurity:
enforce: baseline
*/}}
{{- end }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{- if .Values.enabled }}
{{/*
Example values:
- name: istio-system
podSecurity:
enforce: baseline
*/}}
{{- end }}
Loading