-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender_xml.go
More file actions
162 lines (144 loc) · 4.26 KB
/
render_xml.go
File metadata and controls
162 lines (144 loc) · 4.26 KB
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
package repomap
import (
"fmt"
"strings"
)
// FormatXML formats ranked files as a structured XML document.
// maxTokens controls the output size (estimated as len(text)/4).
// Returns empty string if no files have symbols.
func FormatXML(files []RankedFile, maxTokens int) string {
totalFiles, totalSymbols := countTotals(files)
if totalFiles == 0 {
return ""
}
if maxTokens > 0 {
files = BudgetFiles(files, maxTokens)
}
var b strings.Builder
b.WriteString(`<?xml version="1.0" encoding="UTF-8"?>`)
b.WriteByte('\n')
fmt.Fprintf(&b, "<repomap files=\"%d\" symbols=\"%d\">\n", totalFiles, totalSymbols)
if graph := xmlDependencyGraph(files); graph != "" {
fmt.Fprint(&b, graph)
}
for _, f := range files {
if f.DetailLevel < 0 && maxTokens > 0 {
continue
}
xmlFileBlock(&b, f)
}
b.WriteString("</repomap>\n")
return b.String()
}
// xmlEscape returns s with XML-special characters escaped.
func xmlEscape(s string) string {
s = strings.ReplaceAll(s, "&", "&")
s = strings.ReplaceAll(s, "<", "<")
s = strings.ReplaceAll(s, ">", ">")
s = strings.ReplaceAll(s, "\"", """)
s = strings.ReplaceAll(s, "'", "'")
return s
}
// xmlDependencyGraph builds the <dependencies> section for Go packages.
func xmlDependencyGraph(files []RankedFile) string {
deps, _ := buildPackageDeps(files)
if deps == nil {
return ""
}
var b strings.Builder
b.WriteString(" <dependencies>\n")
for _, pkg := range sortedPkgs(deps) {
fmt.Fprintf(&b, " <pkg name=\"%s\">%s</pkg>\n",
xmlEscape(pkg), xmlEscape(strings.Join(deps[pkg], ", ")))
}
b.WriteString(" </dependencies>\n")
return b.String()
}
// xmlFileBlock appends the XML block for one file.
func xmlFileBlock(b *strings.Builder, f RankedFile) {
attrs := fmt.Sprintf(`path="%s" lang="%s"`, xmlEscape(f.Path), xmlEscape(f.Language))
if f.Score > 0 {
attrs += fmt.Sprintf(` score="%d"`, f.Score)
}
if f.Tag != "" {
attrs += fmt.Sprintf(` tag="%s"`, xmlEscape(f.Tag))
}
if f.DetailLevel >= 0 {
attrs += fmt.Sprintf(` detail="%d"`, f.DetailLevel)
}
if f.Package != "" {
attrs += fmt.Sprintf(` pkg="%s"`, xmlEscape(f.Package))
}
if f.ParseMethod != "" {
attrs += fmt.Sprintf(` parsed="%s"`, xmlEscape(f.ParseMethod))
}
if len(f.Boundaries) > 0 {
attrs += fmt.Sprintf(` boundaries="%s"`, xmlEscape(strings.Join(f.Boundaries, ",")))
}
// Diagnostic attrs — minImportedBy=1 emits raw counts (XML is machine-read).
// Tag/ParseMethod attrs above already cover the full values; skip diagnostics
// that would duplicate them.
var diagAttrs []string
for _, d := range fileDiagnostics(f, 1) {
switch d.Attr {
case `tag="entry"`, `parsed="regex"`:
continue // emitted as top-level attrs above
}
diagAttrs = append(diagAttrs, d.Attr)
}
openAttrs := attrs
if len(diagAttrs) > 0 {
openAttrs = attrs + " " + strings.Join(diagAttrs, " ")
}
if len(f.Symbols) == 0 {
fmt.Fprintf(b, " <file %s/>\n", openAttrs)
return
}
fmt.Fprintf(b, " <file %s>\n", openAttrs)
xmlSymbols(b, f.Symbols)
b.WriteString(" </file>\n")
}
// xmlSymbols appends <symbols> with each symbol as a child element.
func xmlSymbols(b *strings.Builder, syms []Symbol) {
b.WriteString(" <symbols>\n")
for _, s := range syms {
attr := fmt.Sprintf(`name="%s" kind="%s"`, xmlEscape(s.Name), xmlEscape(s.Kind))
if s.Exported {
attr += ` exported="true"`
}
if s.Line > 0 {
attr += fmt.Sprintf(` line="%d"`, s.Line)
}
if span := s.LineSpan(); span > 0 {
attr += fmt.Sprintf(` span="%d"`, span)
}
if s.Receiver != "" {
attr += fmt.Sprintf(` receiver="%s"`, xmlEscape(s.Receiver))
}
switch s.Kind {
case "function", "method":
if s.ParamCount > 0 {
attr += fmt.Sprintf(` params="%d"`, s.ParamCount)
}
if s.ResultCount > 0 {
attr += fmt.Sprintf(` results="%d"`, s.ResultCount)
}
case "interface":
if s.ParamCount > 0 {
attr += fmt.Sprintf(` methods="%d"`, s.ParamCount)
}
}
if len(s.Implements) > 0 {
attr += fmt.Sprintf(` implements="%s"`, xmlEscape(strings.Join(s.Implements, ",")))
}
if s.Doc != "" {
attr += fmt.Sprintf(` doc="%s"`, xmlEscape(s.Doc))
}
if s.Signature != "" {
fmt.Fprintf(b, " <sym %s>%s</sym>\n", attr, xmlEscape(s.Signature))
} else {
fmt.Fprintf(b, " <sym %s/>\n", attr)
}
}
b.WriteString(" </symbols>\n")
}