From 9fffda5df59fcbe5989cf65e3b014c48d9ca2660 Mon Sep 17 00:00:00 2001 From: kennylong Date: Mon, 12 Sep 2022 21:32:59 +0800 Subject: [PATCH] fix: line break format issue after refactor code (#44) --- formatters/basic/features.go | 2 +- formatters/basic/formatter_test.go | 20 ++++++++++++++++++++ internal/hotfix/retain_line_break.go | 18 +++++++++++------- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/formatters/basic/features.go b/formatters/basic/features.go index 780b895..252591b 100644 --- a/formatters/basic/features.go +++ b/formatters/basic/features.go @@ -54,7 +54,7 @@ func ConfigureFeaturesFromConfig(config *Config) yamlfmt.FeatureList { if config.LineEnding == yamlfmt.LineBreakStyleCRLF { linebreakStr = "\r\n" } - featLineBreak := hotfix.MakeFeatureRetainLineBreak(linebreakStr, config.Indent) + featLineBreak := hotfix.MakeFeatureRetainLineBreak(linebreakStr) features = append(features, featLineBreak) } return features diff --git a/formatters/basic/formatter_test.go b/formatters/basic/formatter_test.go index e5d66ed..8c1c010 100644 --- a/formatters/basic/formatter_test.go +++ b/formatters/basic/formatter_test.go @@ -173,6 +173,26 @@ shell: | # hello, world # bye echo "hello, world" +`, + }, + { + desc: "multi level nested literal string", + input: `a: 1 +x: + y: + shell: | + #!/usr/bin/env bash + + # bye + echo "hello, world"`, + expect: `a: 1 +x: + y: + shell: | + #!/usr/bin/env bash + + # bye + echo "hello, world" `, }, } diff --git a/internal/hotfix/retain_line_break.go b/internal/hotfix/retain_line_break.go index 49fd9f7..14ed738 100644 --- a/internal/hotfix/retain_line_break.go +++ b/internal/hotfix/retain_line_break.go @@ -31,22 +31,26 @@ type paddinger struct { strings.Builder } -func (p *paddinger) adjust(size int) { +func (p *paddinger) adjust(txt string) { + var indentSize int + for i := 0; i < len(txt) && txt[i] == ' '; i++ { // yaml only allows space to indent. + indentSize++ + } // Grows if the given size is larger than us and always return the max padding. - for diff := size - p.Len(); diff > 0; diff-- { + for diff := indentSize - p.Len(); diff > 0; diff-- { p.WriteByte(' ') } } -func MakeFeatureRetainLineBreak(linebreakStr string, indent int) yamlfmt.Feature { +func MakeFeatureRetainLineBreak(linebreakStr string) yamlfmt.Feature { return yamlfmt.Feature{ Name: "Retain Line Breaks", - BeforeAction: replaceLineBreakFeature(linebreakStr, indent), + BeforeAction: replaceLineBreakFeature(linebreakStr), AfterAction: restoreLineBreakFeature(linebreakStr), } } -func replaceLineBreakFeature(newlineStr string, indentSize int) yamlfmt.FeatureFunc { +func replaceLineBreakFeature(newlineStr string) yamlfmt.FeatureFunc { return func(content []byte) ([]byte, error) { var buf bytes.Buffer reader := bytes.NewReader(content) @@ -54,7 +58,7 @@ func replaceLineBreakFeature(newlineStr string, indentSize int) yamlfmt.FeatureF var padding paddinger for scanner.Scan() { txt := scanner.Text() - padding.adjust(indentSize) + padding.adjust(txt) if strings.TrimSpace(txt) == "" { // line break or empty space line. buf.WriteString(padding.String()) // prepend some padding incase literal multiline strings. buf.WriteString(lineBreakPlaceholder) @@ -62,7 +66,7 @@ func replaceLineBreakFeature(newlineStr string, indentSize int) yamlfmt.FeatureF continue } buf.WriteString(txt) - buf.WriteByte('\n') + buf.WriteString(newlineStr) } return buf.Bytes(), scanner.Err() }