-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathadapter.go
More file actions
181 lines (162 loc) · 4.85 KB
/
adapter.go
File metadata and controls
181 lines (162 loc) · 4.85 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
package gotemplate
import (
"fmt"
"path/filepath"
"strings"
pkgParser "github.com/doITmagic/rag-code-mcp/pkg/parser"
)
// ConvertToSymbols converts parsed GoTemplate results to parser.Symbol entries
// with structural relations (dependency for {{ template }}, inheritance-like for {{ block }}).
func ConvertToSymbols(templates []GoTemplate) []pkgParser.Symbol {
var symbols []pkgParser.Symbol
for _, tpl := range templates {
baseName := filepath.Base(tpl.FilePath)
nameNoExt := strings.TrimSuffix(baseName, filepath.Ext(baseName))
// If template has {{ define }} blocks, create a symbol per define.
if len(tpl.Defines) > 0 {
for _, def := range tpl.Defines {
sym := buildDefineSymbol(tpl, def)
symbols = append(symbols, sym)
}
}
// Always create a file-level symbol for the whole template.
sym := buildFileSymbol(tpl, nameNoExt)
symbols = append(symbols, sym)
}
return symbols
}
// buildFileSymbol creates a file-level symbol representing the entire template.
func buildFileSymbol(tpl GoTemplate, nameNoExt string) pkgParser.Symbol {
// Build signature summary
var sigParts []string
sigParts = append(sigParts, "go_template")
if len(tpl.Defines) > 0 {
names := make([]string, len(tpl.Defines))
for i, d := range tpl.Defines {
names[i] = d.Name
}
sigParts = append(sigParts, fmt.Sprintf("defines: %s", strings.Join(names, ", ")))
}
if len(tpl.TemplateIncludes) > 0 {
names := make([]string, len(tpl.TemplateIncludes))
for i, t := range tpl.TemplateIncludes {
names[i] = t.Name
}
sigParts = append(sigParts, fmt.Sprintf("includes: %s", strings.Join(names, ", ")))
}
if len(tpl.Blocks) > 0 {
names := make([]string, len(tpl.Blocks))
for i, b := range tpl.Blocks {
names[i] = b.Name
}
sigParts = append(sigParts, fmt.Sprintf("blocks: %s", strings.Join(names, ", ")))
}
// Build docstring
var docParts []string
if len(tpl.Variables) > 0 {
docParts = append(docParts, fmt.Sprintf("Variables: %s", strings.Join(tpl.Variables, ", ")))
}
if len(tpl.CustomFuncs) > 0 {
docParts = append(docParts, fmt.Sprintf("Custom funcs: %s", strings.Join(tpl.CustomFuncs, ", ")))
}
if len(tpl.Ranges) > 0 {
vars := make([]string, len(tpl.Ranges))
for i, r := range tpl.Ranges {
vars[i] = r.Variable
}
docParts = append(docParts, fmt.Sprintf("Iterates: %s", strings.Join(vars, ", ")))
}
// Build relations: template includes → dependency
var relations []pkgParser.Relation
for _, inc := range tpl.TemplateIncludes {
relations = append(relations, pkgParser.Relation{
TargetName: inc.Name,
Type: pkgParser.RelDependency,
})
}
endLine := tpl.TotalLines
if endLine < 1 {
endLine = 1
}
return pkgParser.Symbol{
Name: nameNoExt,
Type: pkgParser.Type,
FilePath: tpl.FilePath,
Language: "html",
StartLine: 1,
EndLine: endLine,
Signature: strings.Join(sigParts, " | "),
Docstring: strings.Join(docParts, " | "),
IsPublic: true,
Relations: relations,
Metadata: map[string]any{
"template_type": "go_template",
"defines": extractDefineNames(tpl),
"includes": extractIncludeNames(tpl),
"blocks": extractBlockNames(tpl),
"variables": tpl.Variables,
"custom_funcs": tpl.CustomFuncs,
"ranges": extractRangeVars(tpl),
},
}
}
// buildDefineSymbol creates a symbol for a specific {{ define "name" }} block.
func buildDefineSymbol(tpl GoTemplate, def DefineDirective) pkgParser.Symbol {
endLine := def.EndLine
if endLine < def.Line {
endLine = def.Line
}
// Relations: any {{ template "x" }} inside this define are dependencies
var relations []pkgParser.Relation
for _, inc := range tpl.TemplateIncludes {
if inc.Line >= def.Line && inc.Line <= endLine {
relations = append(relations, pkgParser.Relation{
TargetName: inc.Name,
Type: pkgParser.RelDependency,
})
}
}
return pkgParser.Symbol{
Name: def.Name,
Type: pkgParser.Type,
FilePath: tpl.FilePath,
Language: "html",
StartLine: def.Line,
EndLine: endLine,
Signature: fmt.Sprintf(`go_template | {{ define "%s" }}`, def.Name),
IsPublic: true,
Relations: relations,
Metadata: map[string]any{
"template_type": "go_template_define",
"define_name": def.Name,
},
}
}
func extractDefineNames(tpl GoTemplate) []string {
names := make([]string, len(tpl.Defines))
for i, d := range tpl.Defines {
names[i] = d.Name
}
return names
}
func extractIncludeNames(tpl GoTemplate) []string {
names := make([]string, len(tpl.TemplateIncludes))
for i, t := range tpl.TemplateIncludes {
names[i] = t.Name
}
return names
}
func extractBlockNames(tpl GoTemplate) []string {
names := make([]string, len(tpl.Blocks))
for i, b := range tpl.Blocks {
names[i] = b.Name
}
return names
}
func extractRangeVars(tpl GoTemplate) []string {
vars := make([]string, len(tpl.Ranges))
for i, r := range tpl.Ranges {
vars[i] = r.Variable
}
return vars
}