-
Notifications
You must be signed in to change notification settings - Fork 7
feat(html): add Go template parser with structural analysis #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cc2d09e
feat(html): add Go template parser with structural analysis
e65d513
fix: uninstall now correctly parses V2 registry to delete per-workspa…
758d184
fix(gotemplate): address PR #47 review + Go↔Template AST linking
ff45478
fix(lint): check filepath.WalkDir return value (errcheck)
41e3ca1
fix(test): skip updater tests on GitHub API 403 rate limit
b97131e
fix(gotemplate): correct else-if stack handling and review fixes
b2c2cf1
fix: address PR #47 review comments
doITmagic e3b14bd
fix: remove unused reVariable regex to pass golangci-lint
doITmagic 17302e7
fix: address final 2 PR #47 review comments
doITmagic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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 }}). | ||
|
doITmagic marked this conversation as resolved.
Outdated
|
||
| 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 | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.