-
-
Notifications
You must be signed in to change notification settings - Fork 238
/
types.go
87 lines (80 loc) · 2.44 KB
/
types.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
package visualize
import (
"context"
"fmt"
"html"
"io"
"strconv"
"strings"
"github.com/a-h/templ"
"github.com/a-h/templ/parser/v2"
)
func HTML(templFileName string, templContents, goContents string, sourceMap *parser.SourceMap) templ.Component {
tl := templLines{contents: string(templContents), sourceMap: sourceMap}
gl := goLines{contents: string(goContents), sourceMap: sourceMap}
return combine(templFileName, tl, gl)
}
type templLines struct {
contents string
sourceMap *parser.SourceMap
}
func (tl templLines) Render(ctx context.Context, w io.Writer) (err error) {
templLines := strings.Split(tl.contents, "\n")
for lineIndex, line := range templLines {
if _, err = w.Write([]byte("<span>" + strconv.Itoa(lineIndex) + " </span>\n")); err != nil {
return
}
for colIndex, c := range line {
if tgt, ok := tl.sourceMap.TargetPositionFromSource(uint32(lineIndex), uint32(colIndex)); ok {
sourceID := fmt.Sprintf("src_%d_%d", lineIndex, colIndex)
targetID := fmt.Sprintf("tgt_%d_%d", tgt.Line, tgt.Col)
if err := mappedCharacter(string(c), sourceID, targetID).Render(ctx, w); err != nil {
return err
}
} else {
s := html.EscapeString(string(c))
s = strings.ReplaceAll(s, "\t", " ")
s = strings.ReplaceAll(s, " ", " ")
if _, err := w.Write([]byte(s)); err != nil {
return err
}
}
}
if _, err = w.Write([]byte("\n<br/>\n")); err != nil {
return
}
}
return nil
}
type goLines struct {
contents string
sourceMap *parser.SourceMap
}
func (gl goLines) Render(ctx context.Context, w io.Writer) (err error) {
templLines := strings.Split(gl.contents, "\n")
for lineIndex, line := range templLines {
if _, err = w.Write([]byte("<span>" + strconv.Itoa(lineIndex) + " </span>\n")); err != nil {
return
}
for colIndex, c := range line {
if src, ok := gl.sourceMap.SourcePositionFromTarget(uint32(lineIndex), uint32(colIndex)); ok {
sourceID := fmt.Sprintf("src_%d_%d", src.Line, src.Col)
targetID := fmt.Sprintf("tgt_%d_%d", lineIndex, colIndex)
if err := mappedCharacter(string(c), sourceID, targetID).Render(ctx, w); err != nil {
return err
}
} else {
s := html.EscapeString(string(c))
s = strings.ReplaceAll(s, "\t", " ")
s = strings.ReplaceAll(s, " ", " ")
if _, err := w.Write([]byte(s)); err != nil {
return err
}
}
}
if _, err = w.Write([]byte("\n<br/>\n")); err != nil {
return
}
}
return nil
}