diff --git a/README.md b/README.md index b1adcd6..fd6fb37 100644 --- a/README.md +++ b/README.md @@ -278,7 +278,7 @@ To use `helmfmt` as a pre-commit hook, add the following to your `.pre-commit-co ```yaml repos: - repo: https://github.com/digitalstudium/helmfmt - rev: v0.4.3 + rev: v0.4.4 hooks: - id: helmfmt ``` diff --git a/VERSION b/VERSION index 17b2ccd..6f2743d 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.4.3 +0.4.4 diff --git a/format.go b/format.go index 0db7432..2a36fe4 100644 --- a/format.go +++ b/format.go @@ -32,7 +32,7 @@ var ( commentEndRe = regexp.MustCompile(`\*\/(?:}}|\s-}})`) // Паттерны для определения типов токенов - varRe = regexp.MustCompile(`^\s*\$\w+\s*:=`) + varRe = regexp.MustCompile(`^\s*\$\w+\s*:?=`) controlRe = regexp.MustCompile(`^\s*(if|range|with|define|block)\b`) elseRe = regexp.MustCompile(`^\s*else\b`) endRe = regexp.MustCompile(`^\s*end\b`) @@ -63,34 +63,34 @@ func validateTemplateSyntax(src string) error { // Главная функция выравнивания func formatIndentation(src string, config *Config, filePath string) string { - lines := strings.Split(src, "\n") - depth := 0 - - for i := 0; i < len(lines); i++ { - trimmed := strings.TrimSpace(lines[i]) - if trimmed == "" { - continue - } - - // standalone comment block => indent with current depth, don't attach to next token - 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") - } - i = cEnd - continue - } - } - - commentStart := i - - keyword, _, endLine, kind, found := getTokenAtLineStartSkippingLeadingComments(lines, i, config) - if !found { - continue - } + lines := strings.Split(src, "\n") + depth := 0 + + for i := 0; i < len(lines); i++ { + trimmed := strings.TrimSpace(lines[i]) + if trimmed == "" { + continue + } + + // standalone comment block => indent with current depth, don't attach to next token + 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") + } + i = cEnd + continue + } + } + + commentStart := i + + keyword, _, endLine, kind, found := getTokenAtLineStartSkippingLeadingComments(lines, i, config) + if !found { + continue + } // Check if we should skip indenting this simple function if kind == tokSimple { diff --git a/templates_test/templates/var_assignment.yaml b/templates_test/templates/var_assignment.yaml new file mode 100644 index 0000000..da84941 --- /dev/null +++ b/templates_test/templates/var_assignment.yaml @@ -0,0 +1,8 @@ +{{- $value := "" }} +{{- if .Values.option1 }} +{{- $value = .Values.option1.value }} +{{- else if .Values.option2 }} +{{- $value = "some-value" }} +{{- else }} +{{- fail "Either option1.value must be set or option2 must be true" }} +{{- end }} diff --git a/templates_test/templates_expected/var_assignment.yaml b/templates_test/templates_expected/var_assignment.yaml new file mode 100644 index 0000000..070d168 --- /dev/null +++ b/templates_test/templates_expected/var_assignment.yaml @@ -0,0 +1,8 @@ +{{- $value := "" }} +{{- if .Values.option1 }} + {{- $value = .Values.option1.value }} +{{- else if .Values.option2 }} + {{- $value = "some-value" }} +{{- else }} + {{- fail "Either option1.value must be set or option2 must be true" }} +{{- end }} diff --git a/templates_test/var_assignment.yaml b/templates_test/var_assignment.yaml new file mode 100644 index 0000000..c9aaea8 --- /dev/null +++ b/templates_test/var_assignment.yaml @@ -0,0 +1,3 @@ +name: "Variable assignment indentation test" +input_file: "templates/var_assignment.yaml" +expected_file: "templates_expected/var_assignment.yaml"