-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.go
More file actions
530 lines (478 loc) · 15.6 KB
/
render.go
File metadata and controls
530 lines (478 loc) · 15.6 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
package repomap
import (
"fmt"
"slices"
"strings"
)
// FormatMap formats ranked files into a token-budgeted text representation.
// maxTokens controls the output size (estimated as len(text)/4).
// Returns empty string if no files have symbols.
// When verbose is true, shows all symbols without summarization.
// When detail is true, shows signatures for funcs/methods and fields for structs.
func FormatMap(files []RankedFile, maxTokens int, verbose, detail bool) string {
totalFiles, totalSymbols := countTotals(files)
if totalFiles == 0 {
return ""
}
var b strings.Builder
fmt.Fprint(&b, buildHeader(files, totalFiles, totalSymbols))
if verbose {
for _, f := range files {
if len(f.Symbols) == 0 {
fmt.Fprint(&b, formatFileHeaderOnly(f))
continue
}
if detail {
fmt.Fprint(&b, formatFileBlockDetail(f))
} else {
fmt.Fprint(&b, formatFileBlockVerbose(f))
}
}
return b.String()
}
// Budget mode: assign detail levels, then render.
files = BudgetFiles(files, maxTokens)
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.formatDetail())
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()
}
// FormatMapCompact formats ranked files into the lean orientation mode:
// path + exported symbol names only, NO signatures, NO godoc, NO struct fields.
// Budget is applied using compactCost so more files fit vs. the enriched default.
// Returns empty string if no files have symbols.
func FormatMapCompact(files []RankedFile, maxTokens int) string {
totalFiles, totalSymbols := countTotals(files)
if totalFiles == 0 {
return ""
}
var b strings.Builder
fmt.Fprint(&b, buildHeader(files, totalFiles, totalSymbols))
// Budget mode using compact cost — more files fit vs. enriched default.
files = BudgetFilesCompact(files, maxTokens)
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
}
switch f.DetailLevel {
case 0:
fmt.Fprint(&b, formatFileHeaderOnly(f))
case 1:
fmt.Fprint(&b, formatFileBlockSummary(f))
default:
fmt.Fprint(&b, formatFileBlockLean(f))
}
shownFiles++
}
if len(headerOnly) > 0 {
fmt.Fprint(&b, formatCollapsedPaths(headerOnly))
}
if shownFiles < totalFiles {
omitted := totalFiles - shownFiles
fmt.Fprintf(&b, "(%d files omitted — increase -t)\n", omitted)
}
return b.String()
}
// formatFileBlockLean renders the lean orientation block for -f compact:
// path + exported symbol names only — no signatures, no godoc, no struct fields.
// Symbols are grouped as comma-separated names on a single indented line per category
// (using existing orderedGroups machinery) to keep the output scannable.
func formatFileBlockLean(f RankedFile) string {
var b strings.Builder
fmt.Fprint(&b, formatFileLine(f))
for _, g := range orderedGroups(f.Path, f.Symbols) {
names := make([]string, 0, len(g.syms))
for _, s := range g.syms {
if s.Exported {
names = append(names, s.Name)
}
}
slices.Sort(names)
if len(names) > 0 {
fmt.Fprintf(&b, " %s: %s\n", g.label, strings.Join(names, ", "))
}
}
fmt.Fprint(&b, "\n")
return b.String()
}
// countTotals returns the total number of files and the total symbol count.
func countTotals(files []RankedFile) (int, int) {
nSymbols := 0
for _, f := range files {
nSymbols += len(f.Symbols)
}
return len(files), nSymbols
}
// buildHeader returns the shared header block (title + dependency graph) used
// by all format modes.
func buildHeader(files []RankedFile, totalFiles, totalSymbols int) string {
var b strings.Builder
fmt.Fprintf(&b, "## Repository Map (%d files, %d symbols)\n\n", totalFiles, totalSymbols)
if graph := formatDependencyGraph(files); graph != "" {
fmt.Fprint(&b, graph)
fmt.Fprint(&b, "\n")
}
return b.String()
}
// formatFileBlockVerbose returns a verbose block showing all symbols without summarization.
func formatFileBlockVerbose(f RankedFile) string {
var b strings.Builder
fmt.Fprint(&b, formatFileLineDetail(f))
for _, g := range orderedGroups(f.Path, f.Symbols) {
names := make([]string, 0, len(g.syms))
for _, s := range g.syms {
names = append(names, symDisplayName(s))
}
slices.Sort(names)
fmt.Fprintf(&b, " %s: %s\n", g.label, strings.Join(names, ", "))
}
fmt.Fprint(&b, "\n")
return b.String()
}
// formatFileBlockSummary returns a summary block showing category counts only.
func formatFileBlockSummary(f RankedFile) string {
var b strings.Builder
fmt.Fprint(&b, formatFileLine(f))
groups := summarizeSymbols(f)
counts := make([]string, 0, len(groups))
for _, g := range groups {
counts = append(counts, fmt.Sprintf("%d %s", g.count, g.label))
}
if len(counts) > 0 {
fmt.Fprintf(&b, " %s\n", strings.Join(counts, ", "))
}
fmt.Fprint(&b, "\n")
return b.String()
}
// formatFileBlockDetail returns a detailed block showing signatures and struct fields.
func formatFileBlockDetail(f RankedFile) string {
var b strings.Builder
fmt.Fprint(&b, formatFileLineDetail(f))
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)
}
}
}
fmt.Fprint(&b, "\n")
return b.String()
}
// fileDiagnostic is one active diagnostic signal on a file. Each renderer
// chooses whether to emit Label (text) or Attr (XML).
type fileDiagnostic struct {
Label string // text form: "imported by 4", "untested"
Attr string // xml form: `imported-by="4"`, `untested="true"`
}
// fileDiagnostics returns the active signals for a file. minImportedBy is the
// threshold below which ImportedBy is suppressed (text uses 2 to hide
// single-importer noise; XML uses 1 to preserve raw counts).
func fileDiagnostics(f RankedFile, minImportedBy int) []fileDiagnostic {
var out []fileDiagnostic
if f.Tag == "entry" {
out = append(out, fileDiagnostic{Label: "entry", Attr: `tag="entry"`})
}
if f.ImportedBy >= minImportedBy && f.ImportedBy > 0 {
out = append(out, fileDiagnostic{
Label: fmt.Sprintf("imported by %d", f.ImportedBy),
Attr: fmt.Sprintf(`imported-by="%d"`, f.ImportedBy),
})
}
if f.DependsOn > 0 {
out = append(out, fileDiagnostic{
Label: fmt.Sprintf("imports: %d", f.DependsOn),
Attr: fmt.Sprintf(`imports="%d"`, f.DependsOn),
})
}
if f.Untested {
out = append(out, fileDiagnostic{Label: "untested", Attr: `untested="true"`})
}
if f.ParseMethod == "regex" {
out = append(out, fileDiagnostic{Label: "inferred", Attr: `parsed="regex"`})
}
return out
}
// formatFileLine returns the header line for a file block (path + tag/badge annotations).
// Does NOT include boundary labels — use formatFileLineVerbose for detail/verbose modes.
func formatFileLine(f RankedFile) string {
diags := fileDiagnostics(f, 2)
if len(diags) == 0 {
return f.Path + "\n"
}
labels := make([]string, len(diags))
for i, d := range diags {
labels[i] = d.Label
}
return fmt.Sprintf("%s [%s]\n", f.Path, strings.Join(labels, ", "))
}
// formatFileLineDetail returns the header line for detail/verbose file blocks,
// including boundary labels when present. Compact mode uses formatFileLine instead.
func formatFileLineDetail(f RankedFile) string {
diags := fileDiagnostics(f, 2)
labels := make([]string, 0, len(diags)+len(f.Boundaries))
for _, d := range diags {
labels = append(labels, d.Label)
}
labels = append(labels, f.Boundaries...)
if len(labels) == 0 {
return f.Path + "\n"
}
return fmt.Sprintf("%s [%s]\n", f.Path, strings.Join(labels, ", "))
}
// formatFileBlockCompact returns the compact block with struct fields for top-ranked types.
func formatFileBlockCompact(f RankedFile, topTypes map[string]bool) string {
var b strings.Builder
fmt.Fprint(&b, formatFileLine(f))
groups := summarizeSymbols(f)
for _, g := range groups {
fmt.Fprintf(&b, " %s: %s\n", g.label, g.summary)
}
for _, s := range f.Symbols {
if s.Signature == "" || s.Signature == "{}" {
continue
}
if (s.Kind == "struct" || s.Kind == "interface") && topTypes[s.Name] {
fmt.Fprintf(&b, " %s %s%s\n", s.Name, s.Signature, implementsTag(s))
}
}
fmt.Fprint(&b, "\n")
return b.String()
}
// docTag returns a parenthetical tag annotating whether doc-line extraction
// is available for this file's language. Go and PHP are fully supported;
// all other languages omit doc lines, and their file header carries "[doc: n/a]".
func docTag(f RankedFile) string {
switch f.Language {
case "go", "php":
return ""
default:
return " [doc: n/a]"
}
}
// formatFileLineDefault returns the header line for the enriched default block,
// appending [doc: n/a] for non-Go files where doc extraction is unavailable.
func formatFileLineDefault(f RankedFile) string {
tag := docTag(f)
if tag == "" {
return formatFileLine(f)
}
// Insert docTag before the trailing newline.
base := formatFileLine(f)
// base ends with "\n"; trim it, append tag, restore newline.
return strings.TrimSuffix(base, "\n") + tag + "\n"
}
// formatFileBlockDefault renders the enriched default block for an LLM consumer:
// exported symbol names + signatures + godoc first line + exported struct fields.
// Symbols are ordered by renderKindWeight descending, then alphabetically for ties.
// Unexported symbols are omitted.
func formatFileBlockDefault(f RankedFile) string {
var b strings.Builder
fmt.Fprint(&b, formatFileLineDefault(f))
// Sort symbols: high renderKindWeight first, then alphabetically within the same weight.
syms := make([]Symbol, len(f.Symbols))
copy(syms, f.Symbols)
slices.SortStableFunc(syms, func(a, b Symbol) int {
wa, wb := renderKindWeight(a.Kind, a.Exported), renderKindWeight(b.Kind, b.Exported)
if wa != wb {
return wb - wa // descending by weight
}
return strings.Compare(a.Name, b.Name)
})
isPHP := f.Language == "php"
for _, s := range syms {
if !s.Exported {
continue
}
var line string
switch {
case isPHP && s.Signature != "":
// PHP signatures are self-contained (include keyword + name + params).
// Rendering " <signature>" avoids double-printing keyword or name.
line = fmt.Sprintf(" %s%s", s.Signature, annotationTag(s))
case s.Kind == "method" && s.Receiver != "":
line = fmt.Sprintf(" func (%s) %s%s%s", s.Receiver, s.Name, s.Signature, annotationTag(s))
case s.Signature != "" && s.Signature != "{}":
line = fmt.Sprintf(" %s %s%s%s", kindKeyword(s.Kind), s.Name, s.Signature, annotationTag(s))
default:
line = fmt.Sprintf(" %s %s%s", kindKeyword(s.Kind), s.Name, annotationTag(s))
}
fmt.Fprintln(&b, line)
if s.Doc != "" {
fmt.Fprintf(&b, " // %s\n", s.Doc)
}
}
fmt.Fprint(&b, "\n")
return b.String()
}
// kindKeyword maps a symbol kind to its Go syntax keyword for display.
// Unknown kinds fall back to the raw kind string so output is never empty.
func kindKeyword(kind string) string {
switch kind {
case "function", "fn":
return "func"
case "struct", "class":
return "type"
case "interface":
return "type"
case "type":
return "type"
case "method":
return "func"
case "constant", "const":
return "const"
case "variable", "var", "static":
return "var"
default:
return kind
}
}
// formatFileHeaderOnly returns a minimal block for files with no exported symbols.
func formatFileHeaderOnly(f RankedFile) string {
var b strings.Builder
fmt.Fprint(&b, formatFileLine(f))
if f.Package != "" {
fmt.Fprintf(&b, " (package %s)\n", f.Package)
}
fmt.Fprint(&b, "\n")
return b.String()
}
// formatDetail renders the file at its assigned DetailLevel.
// DetailLevel 2 and 3 both dispatch to formatFileBlockDefault (enriched default).
// DetailLevel 3 aliasing DetailLevel 2 is intentional for v0.7.0;
// distinction deferred to v0.8.0 (e.g. unexported fields at level 3).
func (f RankedFile) formatDetail() string {
switch f.DetailLevel {
case 0:
return formatFileHeaderOnly(f)
case 1:
return formatFileBlockSummary(f)
case 2, 3:
return formatFileBlockDefault(f)
default:
return ""
}
}
// Symbol-level diagnostic thresholds. A symbol exceeding any of these gets
// an annotation in the rendered output so an LLM can spot the smell quickly.
const (
sizeThreshold = 50 // line span → [NL]
paramThreshold = 4 // function/method params > this → [Np]
resultThreshold = 2 // function/method returns > this → [Nr]
methodsThreshold = 5 // interface methods > this → [Nm]
)
// sizeTag returns " [NL]" for symbols exceeding the size threshold, or "".
// Kept for existing tests and single-signal callers.
func sizeTag(s Symbol) string {
if span := s.LineSpan(); span >= sizeThreshold {
return fmt.Sprintf(" [%dL]", span)
}
return ""
}
// annotationTag returns a single bracketed suffix combining all symbol-level
// diagnostic signals (size + signature smells), e.g. " [185L, 5p, 3r]".
// Empty string if no signal crosses its threshold.
func annotationTag(s Symbol) string {
var parts []string
if span := s.LineSpan(); span >= sizeThreshold {
parts = append(parts, fmt.Sprintf("%dL", span))
}
switch s.Kind {
case "function", "method":
if s.ParamCount > paramThreshold {
parts = append(parts, fmt.Sprintf("%dp", s.ParamCount))
}
if s.ResultCount > resultThreshold {
parts = append(parts, fmt.Sprintf("%dr", s.ResultCount))
}
case "interface":
if s.ParamCount > methodsThreshold {
parts = append(parts, fmt.Sprintf("%dm", s.ParamCount))
}
}
if len(parts) == 0 {
return ""
}
return " [" + strings.Join(parts, ", ") + "]"
}
// symDisplayName returns the display name for a symbol, with diagnostic tags.
func symDisplayName(s Symbol) string {
name := s.Name
if s.Kind == "method" && s.Receiver != "" {
name = s.Receiver + "." + s.Name
}
return name + annotationTag(s) + implementsTag(s)
}
// implementsTag returns " [impl: A, B]" for structs that satisfy interfaces,
// or "" if the symbol has no detected implementations.
func implementsTag(s Symbol) string {
if len(s.Implements) == 0 {
return ""
}
return " [impl: " + strings.Join(s.Implements, ", ") + "]"
}
// collapsedPreviewLimit is the max number of paths shown before truncation.
const collapsedPreviewLimit = 5
// formatCollapsedPaths renders header-only files as a single compact line.
// Shows: (+N more: a.go, b.go, c.go, ...)
func formatCollapsedPaths(paths []string) string {
if len(paths) == 0 {
return ""
}
slices.Sort(paths)
preview := paths
truncated := false
if len(preview) > collapsedPreviewLimit {
preview = preview[:collapsedPreviewLimit]
truncated = true
}
body := strings.Join(preview, ", ")
if truncated {
body += ", ..."
}
return fmt.Sprintf("(+%d more: %s)\n", len(paths), body)
}