Skip to content

Commit

Permalink
Improve newline handling
Browse files Browse the repository at this point in the history
  • Loading branch information
fbbdev committed May 19, 2024
1 parent 6d42438 commit dc1dd61
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
4 changes: 2 additions & 2 deletions v3/internal/generator/render/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,15 +296,15 @@ func (m *module) PostponedCreates() []string {
continue
}

builder.WriteString("\n \"")
builder.WriteString(newline + " \"")
template.JSEscape(&builder, []byte(field.JsonName))
builder.WriteString("\": ")
builder.WriteString(createField)
builder.WriteRune(',')
}

if len(info.Fields) > 0 {
builder.WriteRune('\n')
builder.WriteString(newline)
}
builder.WriteString("})")

Expand Down
5 changes: 2 additions & 3 deletions v3/internal/generator/render/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ func hasdoc(group *ast.CommentGroup) bool {
}

// TODO: this is horrible, make it more efficient?
text := group.Text()
return text != "" && text != "\n"
return strings.ContainsFunc(group.Text(), func(r rune) bool { return !unicode.IsSpace(r) })
}

var commentTerminator = []byte("*/")
Expand All @@ -36,7 +35,7 @@ var commentTerminator = []byte("*/")
// with the given indentation.
func jsdoc(comment string, indent string) string {
var builder strings.Builder
prefix := []byte("\n" + indent + " * ")
prefix := []byte(newline + indent + " * ")

scanner := bufio.NewScanner(bytes.NewReader([]byte(comment)))
for scanner.Scan() {
Expand Down
14 changes: 14 additions & 0 deletions v3/internal/generator/render/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package render

import (
"embed"
"strings"
"text/template"
)

Expand All @@ -23,3 +24,16 @@ var tmplModels = map[tmplLanguage]*template.Template{
}

var tmplIndex = template.Must(template.New("index.tmpl").Funcs(tmplFunctions).ParseFS(templates, "templates/index.tmpl"))

var newline string

func init() {
var builder strings.Builder

err := template.Must(template.New("newline.tmpl").ParseFS(templates, "templates/newline.tmpl")).Execute(&builder, nil)
if err != nil {
panic(err)
}

newline = builder.String()
}
6 changes: 6 additions & 0 deletions v3/internal/generator/render/templates/newline.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{{/* This template should render to a single newline (either LF or CRLF). */}}
{{/*
Git might be configured to rewrite LF newlines to CRLF
in templates, test cases and data, especially on Windows.
Having a newline template enables detection of the current newline mode.
*/ -}}

0 comments on commit dc1dd61

Please sign in to comment.