-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalls_render.go
More file actions
219 lines (197 loc) · 6.57 KB
/
Copy pathcalls_render.go
File metadata and controls
219 lines (197 loc) · 6.57 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package repomap
import (
"fmt"
"slices"
"strings"
)
// formatCallersInline formats callers for a symbol as a compact inline string.
// Example: "callers: ../hook/hook.go:18, ../search/scorer.go:55 (3 total)"
func formatCallersInline(locs []Location, total int) string {
if len(locs) == 0 {
return ""
}
parts := make([]string, len(locs))
for i, loc := range locs {
parts[i] = fmt.Sprintf("%s:%d", loc.File, loc.Line)
}
slices.Sort(parts)
body := strings.Join(parts, ", ")
if total > len(locs) {
return fmt.Sprintf("callers: %s (%d total)", body, total)
}
return fmt.Sprintf("callers: %s", body)
}
// formatFileBlockVerboseWithCallers is like formatFileBlockVerbose but annotates
// each symbol group line with caller counts from the callers map.
func formatFileBlockVerboseWithCallers(f RankedFile, callers SymbolCallers, limit int, explain bool) string {
var b strings.Builder
fmt.Fprint(&b, formatFileLineDetail(f, explain))
for _, g := range orderedGroups(f.Path, f.Symbols) {
names := make([]string, 0, len(g.syms))
for _, s := range g.syms {
name := symDisplayName(s)
if locs := callers.CallersForSymbol(f.Path, s); len(locs) > 0 {
name += fmt.Sprintf(" [callers: %d]", len(locs))
}
names = append(names, name)
}
slices.Sort(names)
fmt.Fprintf(&b, " %s: %s\n", g.label, strings.Join(names, ", "))
}
fmt.Fprint(&b, "\n")
return b.String()
}
// formatFileBlockDetailWithCallers extends formatFileBlockDetail to show callers.
func formatFileBlockDetailWithCallers(f RankedFile, callers SymbolCallers, limit int, explain bool) string {
var b strings.Builder
fmt.Fprint(&b, formatFileLineDetail(f, explain))
for _, g := range orderedGroups(f.Path, f.Symbols) {
slices.SortFunc(g.syms, func(a, b Symbol) int {
return strings.Compare(a.Name, b.Name)
})
fmt.Fprintf(&b, " %s:\n", g.label)
for _, s := range g.syms {
var line string
switch {
case g.key == "methods" && s.Receiver != "":
if s.Signature != "" {
line = fmt.Sprintf("%s.%s%s%s", s.Receiver, s.Name, s.Signature, annotationTag(s))
} else {
line = fmt.Sprintf("%s.%s%s", s.Receiver, s.Name, annotationTag(s))
}
case (g.key == "types" || g.key == "interfaces") && s.Signature != "":
line = fmt.Sprintf("%s %s%s%s", s.Name, s.Signature, annotationTag(s), implementsTag(s))
case g.key == "funcs" && s.Signature != "":
line = fmt.Sprintf("%s%s%s", s.Name, s.Signature, annotationTag(s))
default:
line = s.Name + annotationTag(s)
}
fmt.Fprintf(&b, " %s\n", line)
if s.Doc != "" {
fmt.Fprintf(&b, " // %s\n", s.Doc)
}
if locs := callers.CallersForSymbol(f.Path, s); len(locs) > 0 {
fmt.Fprintf(&b, " %s\n", formatCallersInline(locs, len(locs)))
}
}
}
fmt.Fprint(&b, "\n")
return b.String()
}
// formatFileBlockCompactWithCallers extends compact output to show per-group caller counts.
// For each symbol group, if any symbol in the group has callers, it appends
// a callers annotation to the group header.
func formatFileBlockCompactWithCallers(f RankedFile, topTypes map[string]bool, callers SymbolCallers, explain bool) string {
var b strings.Builder
fmt.Fprint(&b, formatFileLine(f, explain))
// Use orderedGroups (which carries the symbols) to compute per-group caller counts,
// but still emit the summary strings from summarizeSymbols for display consistency.
summaryGroups := summarizeSymbols(f)
categorizedGroups := orderedGroups(f.Path, f.Symbols)
// Build a label → caller-count map from the categorized groups.
callersByLabel := make(map[string]int, len(categorizedGroups))
for _, cg := range categorizedGroups {
total := 0
for _, s := range cg.syms {
if locs := callers.CallersForSymbol(f.Path, s); len(locs) > 0 {
total += len(locs)
}
}
if total > 0 {
callersByLabel[cg.label] = total
}
}
for _, g := range summaryGroups {
line := fmt.Sprintf(" %s: %s", g.label, g.summary)
if n := callersByLabel[g.label]; n > 0 {
line += fmt.Sprintf(" [%d callers]", n)
}
fmt.Fprintln(&b, line)
}
for _, s := range f.Symbols {
if s.Signature == "" || s.Signature == "{}" {
continue
}
if (s.Kind == "struct" || s.Kind == "interface") && topTypes[s.Name] {
locs := callers.CallersForSymbol(f.Path, s)
suffix := ""
if len(locs) > 0 {
suffix = fmt.Sprintf(" [callers: %d]", len(locs))
}
fmt.Fprintf(&b, " %s %s%s%s\n", s.Name, s.Signature, implementsTag(s), suffix)
}
}
fmt.Fprint(&b, "\n")
return b.String()
}
// FormatMapWithCallers formats the ranked files like FormatMap but injects caller
// information from the callers map into the output.
// cfg may be nil — nil means no file-level detail overrides.
func FormatMapWithCallers(files []RankedFile, maxTokens int, verbose, detail bool, callers SymbolCallers, limit int, cfg *BlocklistConfig, explain bool) string {
files = cloneRanked(files)
totalFiles, totalSymbols := countTotals(files)
if totalFiles == 0 {
return ""
}
var b strings.Builder
fmt.Fprint(&b, buildHeader("calls", files, totalFiles, totalSymbols))
if verbose {
for _, f := range files {
if len(f.Symbols) == 0 {
fmt.Fprint(&b, formatFileHeaderOnly(f, explain))
continue
}
if detail {
fmt.Fprint(&b, formatFileBlockDetailWithCallers(f, callers, limit, explain))
} else {
fmt.Fprint(&b, formatFileBlockVerboseWithCallers(f, callers, limit, explain))
}
}
return b.String()
}
// Budget mode.
files = BudgetFiles(files, maxTokens, cfg)
var headerOnly []string
shownFiles := 0
for _, f := range files {
if f.DetailLevel < 0 {
continue
}
if f.DetailLevel == 0 && len(f.Symbols) == 0 && f.Tag == "" {
headerOnly = append(headerOnly, f.Path)
shownFiles++
continue
}
fmt.Fprint(&b, f.formatDetailWithCallers(callers, limit, explain))
shownFiles++
}
if len(headerOnly) > 0 {
fmt.Fprint(&b, formatCollapsedPaths(headerOnly))
}
if shownFiles < totalFiles {
omitted := totalFiles - shownFiles
fmt.Fprintf(&b, "(%d files omitted — increase -t or use -f compact)\n", omitted)
}
return b.String()
}
// formatDetailWithCallers renders the file at its assigned DetailLevel, injecting callers.
func (f RankedFile) formatDetailWithCallers(callers SymbolCallers, limit int, explain bool) string {
switch f.DetailLevel {
case 0:
return formatFileHeaderOnly(f, explain)
case 1:
return formatFileBlockSummary(f, explain)
case 2:
return formatFileBlockCompactWithCallers(f, nil, callers, explain)
case 3:
top := make(map[string]bool)
for _, s := range f.Symbols {
if s.HasFields() {
top[s.Name] = true
}
}
return formatFileBlockCompactWithCallers(f, top, callers, explain)
default:
return ""
}
}