-
-
Notifications
You must be signed in to change notification settings - Fork 273
/
rangewriter.go
111 lines (101 loc) · 2.12 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
package generator
import (
"io"
"strings"
"github.com/a-h/templ/parser/v2"
)
func NewRangeWriter(w io.Writer) *RangeWriter {
return &RangeWriter{
w: w,
}
}
type RangeWriter struct {
Current parser.Position
inLiteral bool
w io.Writer
}
func (rw *RangeWriter) closeLiteral(indent int) (r parser.Range, err error) {
rw.inLiteral = false
_, err = rw.write("\")\n")
if err != nil {
return
}
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.WriteIndent(level, `_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("`); err != nil {
return
}
}
_, err = rw.write(s)
if err != nil {
return
}
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,
}
var n int
for _, c := range s {
rw.Current.Col++
if c == '\n' {
rw.Current.Line++
rw.Current.Col = 0
}
n, err = io.WriteString(rw.w, string(c))
rw.Current.Index += int64(n)
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
}