-
-
Notifications
You must be signed in to change notification settings - Fork 284
/
rangewriter.go
164 lines (139 loc) · 3.27 KB
/
rangewriter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package generator
import (
"io"
"strconv"
"strings"
"unicode/utf8"
"github.com/a-h/templ/parser/v2"
)
func NewRangeWriter(w io.Writer) *RangeWriter {
return &RangeWriter{
w: w,
literalWriter: prodLiteralWriter{},
}
}
type RangeWriter struct {
Current parser.Position
inLiteral bool
w io.Writer
// Extract strings.
literalWriter literalWriter
}
type literalWriter interface {
writeLiteral(inLiteral bool, s string) string
closeLiteral(indent int) string
literals() string
}
type watchLiteralWriter struct {
index int
builder *strings.Builder
}
func (w *watchLiteralWriter) closeLiteral(indent int) string {
w.index++
w.builder.WriteString("\n")
return ""
}
func (w *watchLiteralWriter) writeLiteral(inLiteral bool, s string) string {
w.builder.WriteString(s)
if inLiteral {
return ""
}
return "templ_7745c5c3_Err = templ.WriteWatchModeString(templ_7745c5c3_Buffer, " + strconv.Itoa(w.index+1) + ")\n"
}
func (w *watchLiteralWriter) literals() string {
return w.builder.String()
}
type prodLiteralWriter struct{}
func (prodLiteralWriter) closeLiteral(indent int) string {
return "\")\n"
}
func (prodLiteralWriter) writeLiteral(inLiteral bool, s string) string {
if inLiteral {
return s
}
return `_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("` + s
}
func (prodLiteralWriter) literals() string {
return ""
}
func (rw *RangeWriter) closeLiteral(indent int) (r parser.Range, err error) {
rw.inLiteral = false
if _, err := rw.write(rw.literalWriter.closeLiteral(indent)); err != nil {
return r, err
}
err = rw.writeErrorHandler(indent)
return
}
func (rw *RangeWriter) WriteIndent(level int, s string) (r parser.Range, err error) {
if rw.inLiteral {
if _, err = rw.closeLiteral(level); err != nil {
return
}
}
_, err = rw.write(strings.Repeat("\t", level))
if err != nil {
return
}
return rw.write(s)
}
func (rw *RangeWriter) WriteStringLiteral(level int, s string) (r parser.Range, err error) {
if !rw.inLiteral {
_, err = rw.write(strings.Repeat("\t", level))
if err != nil {
return
}
}
if _, err := rw.write(rw.literalWriter.writeLiteral(rw.inLiteral, s)); err != nil {
return r, err
}
rw.inLiteral = true
return
}
func (rw *RangeWriter) Write(s string) (r parser.Range, err error) {
if rw.inLiteral {
if _, err = rw.closeLiteral(0); err != nil {
return
}
}
return rw.write(s)
}
func (rw *RangeWriter) write(s string) (r parser.Range, err error) {
r.From = parser.Position{
Index: rw.Current.Index,
Line: rw.Current.Line,
Col: rw.Current.Col,
}
utf8Bytes := make([]byte, 4)
for _, c := range s {
rlen := utf8.EncodeRune(utf8Bytes, c)
rw.Current.Col += uint32(rlen)
if c == '\n' {
rw.Current.Line++
rw.Current.Col = 0
}
_, err = rw.w.Write(utf8Bytes[:rlen])
rw.Current.Index += int64(rlen)
if err != nil {
return r, err
}
}
r.To = rw.Current
return r, err
}
func (rw *RangeWriter) writeErrorHandler(indentLevel int) (err error) {
_, err = rw.WriteIndent(indentLevel, "if templ_7745c5c3_Err != nil {\n")
if err != nil {
return err
}
indentLevel++
_, err = rw.WriteIndent(indentLevel, "return templ_7745c5c3_Err\n")
if err != nil {
return err
}
indentLevel--
_, err = rw.WriteIndent(indentLevel, "}\n")
if err != nil {
return err
}
return err
}