-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcategorize.go
More file actions
177 lines (164 loc) · 4.5 KB
/
Copy pathcategorize.go
File metadata and controls
177 lines (164 loc) · 4.5 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
package repomap
import (
"path/filepath"
"strings"
)
// kindToCategory maps symbol kinds to their display category.
var kindToCategory = map[string]string{
// Go kinds
"struct": "types",
"type": "types",
"function": "funcs",
"fn": "funcs",
"method": "methods",
"constant": "consts",
"const": "consts",
"variable": "vars",
"static": "vars",
// Shared Go + PHP
"interface": "interfaces",
"enum": "enums",
// PHP-specific kinds
"class": "classes",
"trait": "types",
"case": "consts",
"property": "vars",
"namespace": "other",
}
// categoryOrder defines the display order for symbol categories.
var categoryOrder = []struct {
key string
label string
}{
{"tests", "tests"},
{"types", "types"},
{"interfaces", "interfaces"},
{"classes", "classes"},
{"enums", "enums"},
{"funcs", "funcs"},
{"methods", "methods"},
{"consts", "consts"},
{"vars", "vars"},
{"other", "other"},
}
// categorizedGroup holds a category key, display label, and its symbols.
type categorizedGroup struct {
key string
label string
syms []Symbol
}
// orderedGroups returns symbols grouped by category in display order,
// skipping empty categories.
func orderedGroups(path string, syms []Symbol) []categorizedGroup {
categorized := categorizeByKind(path, syms)
var groups []categorizedGroup
for _, item := range categoryOrder {
s := categorized[item.key]
if len(s) == 0 {
continue
}
groups = append(groups, categorizedGroup{key: item.key, label: item.label, syms: s})
}
return groups
}
func summarizeSymbols(f RankedFile) []symbolGroup {
categorized := categorizeByKind(f.Path, f.Symbols)
var groups []symbolGroup
for _, item := range categoryOrder {
syms := categorized[item.key]
if len(syms) == 0 {
continue
}
groups = append(groups, symbolGroup{
label: item.label,
summary: summarizeGroup(item.key, syms),
count: len(syms),
})
}
return groups
}
func symbolCategory(path string, s Symbol) string {
if isTestSymbol(path, s) {
return "tests"
}
if cat, ok := kindToCategory[s.Kind]; ok {
return cat
}
return "other"
}
// renderKindWeight returns the display-ordering priority for a symbol within a file.
// Higher weight = rendered first. Exported symbols always outrank unexported.
// Weights reflect architectural information density for LLM consumption.
//
// Go and PHP kinds are handled by the same switch — symmetric weights where
// concepts align (class=struct=10, enum=type=8, property=var=3).
func renderKindWeight(kind string, exported bool) int {
if !exported {
return 1
}
switch kind {
// Weight 10 — primary compositional units
case "struct", "interface", "class":
return 10
// Weight 9 — horizontal reuse (PHP trait)
case "trait":
return 9
// Weight 8 — type definitions
case "type", "enum":
return 8
// Weight 6 — free functions
case "function", "fn":
return 6
// Weight 5 — class-bound behaviour
case "method":
return 5
// Weight 4 — enum values (above const: they are the enum's reason to exist)
case "case":
return 4
// Weight 3 — named values and fields
case "constant", "const", "variable", "var", "static", "property":
return 3
// Weight 0 — namespace: header-only, position controlled by line order
case "namespace":
return 0
default:
return 2
}
}
// categorizeByKind groups symbols by their category key.
func categorizeByKind(path string, syms []Symbol) map[string][]Symbol {
m := make(map[string][]Symbol)
for _, s := range syms {
cat := symbolCategory(path, s)
m[cat] = append(m[cat], s)
}
return m
}
func isTestSymbol(path string, s Symbol) bool {
if !strings.HasSuffix(path, "_test.go") {
return false
}
return strings.HasPrefix(s.Name, "Test") || strings.HasPrefix(s.Name, "Benchmark") || strings.HasPrefix(s.Name, "Fuzz")
}
func isTestFile(path string) bool {
ext := filepath.Ext(path)
base := filepath.Base(path)
switch ext {
case ".go":
return strings.HasSuffix(path, "_test.go")
case ".py":
return strings.HasPrefix(base, "test_") || strings.HasSuffix(base, "_test.py")
case ".rs":
return strings.Contains(path, "/tests/") || strings.HasSuffix(base, "_test.rs")
case ".java":
return strings.HasSuffix(base, "Test.java") || strings.HasSuffix(base, "Tests.java")
case ".ts", ".tsx", ".js", ".jsx":
return strings.HasSuffix(base, ".test"+ext) || strings.HasSuffix(base, ".spec"+ext)
case ".rb":
return strings.HasSuffix(base, "_test.rb") || strings.HasSuffix(base, "_spec.rb")
case ".php":
return strings.HasSuffix(base, "Test.php")
default:
return false
}
}