Merge pull request #402 from alixander/preserve-comment-space

fmt: Preserve leading comment spacing
This commit is contained in:
Alexander Wang 2022-12-13 10:09:20 -08:00 committed by GitHub
commit bf139735bf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 1 deletions

View file

@ -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.

View file

@ -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)

View file

@ -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
`,
},
}