Skip to content

Commit be78ef4

Browse files
committed
fix: squeeze consecutive blank lines in generated output
Execute template into a buffer and remove runs of 3+ newlines, replacing them with double newlines to reduce unnecessary whitespace in generated files.
1 parent daa240a commit be78ef4

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

internal/sqlc/generator.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
package sqlc
22

33
import (
4+
"bytes"
45
"fmt"
56
"os"
67
"path/filepath"
8+
"regexp"
79
"strings"
810

911
"github.com/go-openapi/inflect"
1012
"github.com/sqlc-contrib/sqlc-gen-queries/internal/sqlc/template"
1113
)
1214

15+
// blank matches two or more consecutive blank lines.
16+
var blank = regexp.MustCompile(`\n{3,}`)
17+
1318
func init() {
1419
inflect.AddSingular("quota", "quota")
1520
inflect.AddPlural("quota", "quotas")
@@ -182,8 +187,14 @@ func (x *Generator) Generate() error {
182187
Table: &table,
183188
Queries: queries,
184189
}
185-
// Execute template
186-
if err := template.Execute(file, ctx); err != nil {
190+
// Execute template into buffer, then squeeze blank lines
191+
var buffer bytes.Buffer
192+
if err := template.Execute(&buffer, ctx); err != nil {
193+
return err
194+
}
195+
196+
data := blank.ReplaceAll(buffer.Bytes(), []byte("\n\n"))
197+
if _, err := file.Write(data); err != nil {
187198
return err
188199
}
189200
}

0 commit comments

Comments
 (0)