From bccea440168d24f4d235b5a72078fb78374e5511 Mon Sep 17 00:00:00 2001 From: Digital Studium Date: Fri, 20 Mar 2026 10:23:34 +0300 Subject: [PATCH 1/4] Add indentation for var assignment --- VERSION | 2 +- format.go | 58 +++++++++---------- templates_test/templates/var_assignment.yaml | 8 +++ .../templates_expected/var_assignment.yaml | 8 +++ 4 files changed, 46 insertions(+), 30 deletions(-) create mode 100644 templates_test/templates/var_assignment.yaml create mode 100644 templates_test/templates_expected/var_assignment.yaml 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..9fcfb7f --- /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 }} \ No newline at end of file diff --git a/templates_test/templates_expected/var_assignment.yaml b/templates_test/templates_expected/var_assignment.yaml new file mode 100644 index 0000000..3f5f7c9 --- /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 }} \ No newline at end of file From ab61f5814183ac08525898721f447fe1efbea604 Mon Sep 17 00:00:00 2001 From: Digital Studium Date: Fri, 20 Mar 2026 10:32:39 +0300 Subject: [PATCH 2/4] Add test --- templates_test/templates/var_assignment.yaml | 2 +- templates_test/templates_expected/var_assignment.yaml | 2 +- templates_test/var_assignment.yaml | 4 ++++ 3 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 templates_test/var_assignment.yaml diff --git a/templates_test/templates/var_assignment.yaml b/templates_test/templates/var_assignment.yaml index 9fcfb7f..da84941 100644 --- a/templates_test/templates/var_assignment.yaml +++ b/templates_test/templates/var_assignment.yaml @@ -5,4 +5,4 @@ {{- $value = "some-value" }} {{- else }} {{- fail "Either option1.value must be set or option2 must be true" }} -{{- end }} \ No newline at end of file +{{- end }} diff --git a/templates_test/templates_expected/var_assignment.yaml b/templates_test/templates_expected/var_assignment.yaml index 3f5f7c9..070d168 100644 --- a/templates_test/templates_expected/var_assignment.yaml +++ b/templates_test/templates_expected/var_assignment.yaml @@ -5,4 +5,4 @@ {{- $value = "some-value" }} {{- else }} {{- fail "Either option1.value must be set or option2 must be true" }} -{{- end }} \ No newline at end of file +{{- end }} diff --git a/templates_test/var_assignment.yaml b/templates_test/var_assignment.yaml new file mode 100644 index 0000000..3fffd48 --- /dev/null +++ b/templates_test/var_assignment.yaml @@ -0,0 +1,4 @@ +name: "Plain yaml" +input_file: "templates/var_assignment.yaml" +expected_file: "templates_expected/var_assignment.yaml" + From 7cb6e6700e6505f9ac5b019333d9c3b9d92706c0 Mon Sep 17 00:00:00 2001 From: Digital Studium Date: Fri, 20 Mar 2026 10:36:02 +0300 Subject: [PATCH 3/4] Rename test --- templates_test/var_assignment.yaml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/templates_test/var_assignment.yaml b/templates_test/var_assignment.yaml index 3fffd48..c9aaea8 100644 --- a/templates_test/var_assignment.yaml +++ b/templates_test/var_assignment.yaml @@ -1,4 +1,3 @@ -name: "Plain yaml" +name: "Variable assignment indentation test" input_file: "templates/var_assignment.yaml" expected_file: "templates_expected/var_assignment.yaml" - From 407fd3cc178898a7b15622bc8c98f2baf4dfda2e Mon Sep 17 00:00:00 2001 From: Digital Studium Date: Fri, 20 Mar 2026 10:37:42 +0300 Subject: [PATCH 4/4] Update version in readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 ```