From a354ae574e7d5ba88f2dd3293fbe588f46352af0 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Wed, 7 Dec 2022 08:28:40 -0800 Subject: [PATCH] preserve comment spacing in autofmt --- ci/release/changelogs/next.md | 3 +++ d2format/format.go | 2 +- d2format/format_test.go | 17 +++++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index fcaa580c4..5ea2ca695 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -5,6 +5,9 @@ #### Improvements 🧹 +- Fmt now preserves leading comment spacing. + [#400](https://github.com/terrastruct/d2/issues/400) + #### Bugfixes ⛑️ - Fixed crash when sequence diagrams had no messages. diff --git a/d2format/format.go b/d2format/format.go index 754e3302a..a45d55f63 100644 --- a/d2format/format.go +++ b/d2format/format.go @@ -81,7 +81,7 @@ func (p *printer) comment(c *d2ast.Comment) { lines := strings.Split(c.Value, "\n") for i, line := range lines { p.sb.WriteString("#") - if line != "" && !strings.HasPrefix(line, " ") { + if line != "" { p.sb.WriteByte(' ') } p.sb.WriteString(line) diff --git a/d2format/format_test.go b/d2format/format_test.go index 4bfea1519..883229068 100644 --- a/d2format/format_test.go +++ b/d2format/format_test.go @@ -592,6 +592,23 @@ hi # Fraud is the homage that force pays to reason. in: `x: {} `, exp: `x +`, + }, + { + name: "leading_space_comments", + in: `# foo +# foobar +# baz +x -> y +#foo +y +`, + exp: `# foo +# foobar +# baz +x -> y +# foo +y `, }, }