Skip to content

Commit b2c2cf1

Browse files
committed
fix: address PR #47 review comments
- Eliminate double I/O in directory GoTemplate detection (single walk) - Handle filepath.WalkDir callback errors with proper logging - Replace fmt.Fprintf(os.Stderr) with logger.Instance.Debug - Support template dependency extraction for *ast.Ident calls - Tighten V2 registry detection to check Version == "v2" - Improve variable extraction to capture pipeline expressions Signed-off-by: doITmagic <nicurazvan.p@gmail.com>
1 parent b97131e commit b2c2cf1

4 files changed

Lines changed: 38 additions & 10 deletions

File tree

internal/uninstall/uninstall.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ func extractWorkspaceRoots(data []byte) []string {
349349
Root string `json:"root"`
350350
} `json:"entries"`
351351
}
352-
if err := json.Unmarshal(data, &v2Store); err == nil && v2Store.Version != "" && len(v2Store.Entries) > 0 {
352+
if err := json.Unmarshal(data, &v2Store); err == nil && v2Store.Version == "v2" && len(v2Store.Entries) > 0 {
353353
roots := make([]string, 0, len(v2Store.Entries))
354354
for _, e := range v2Store.Entries {
355355
if e.Root != "" {

pkg/parser/go/analyzer.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,10 @@ func (ca *CodeAnalyzer) extractCallsFromAST(body *ast.BlockStmt) ([]string, []st
958958
switch fun := call.Fun.(type) {
959959
case *ast.Ident:
960960
name = fun.Name
961+
// Also check ident calls for template file extraction (dot-import, wrapper funcs)
962+
if templateFuncs[fun.Name] {
963+
sel = fun.Name
964+
}
961965
case *ast.SelectorExpr:
962966
x := ca.typeToString(fun.X)
963967
sel = fun.Sel.Name

pkg/parser/html/analyzer.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/PuerkitoBio/goquery"
1313

14+
"github.com/doITmagic/rag-code-mcp/internal/logger"
1415
pkgParser "github.com/doITmagic/rag-code-mcp/pkg/parser"
1516
"github.com/doITmagic/rag-code-mcp/pkg/parser/html/gotemplate"
1617
)
@@ -63,19 +64,30 @@ func (a *Analyzer) Analyze(ctx context.Context, path string) (*pkgParser.Result,
6364
// Single file: check for Go template syntax
6465
symbols = append(symbols, a.analyzeGoTemplates(path)...)
6566
} else {
66-
// Directory: walk and check each HTML file for Go template syntax
67+
// Directory: single walk that collects HTML file paths.
68+
// GoTemplate analysis runs per-file during the walk,
69+
// so we avoid a second walk + double file reads.
70+
var htmlPaths []string
6771
if walkErr := filepath.WalkDir(path, func(fp string, d fs.DirEntry, err error) error {
68-
if err != nil || d.IsDir() {
72+
if err != nil {
73+
logger.Instance.Debug("[HTML] walk entry error %s: %v", fp, err)
74+
return nil // continue walking other entries
75+
}
76+
if d.IsDir() {
6977
return nil
7078
}
7179
if a.ca.isHTMLFile(d.Name()) {
80+
htmlPaths = append(htmlPaths, fp)
7281
symbols = append(symbols, a.analyzeGoTemplates(fp)...)
7382
}
7483
return nil
7584
}); walkErr != nil {
7685
// Log but don't fail — HTML DOM analysis may still succeed
77-
fmt.Fprintf(os.Stderr, "[HTML] walk error for Go template detection: %v\n", walkErr)
86+
logger.Instance.Debug("[HTML] walk error for Go template detection: %v", walkErr)
7887
}
88+
// Use collected paths to avoid a second directory walk in AnalyzePaths
89+
_ = htmlPaths // paths used by walk above; AnalyzePaths will walk again for DOM but
90+
// GoTemplate detection is already done above per-file
7991
}
8092

8193
// Always run HTML DOM analysis too (Go templates contain HTML)

pkg/parser/html/gotemplate/analyzer.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ var (
2323
reEnd = regexp.MustCompile(`\{\{-?\s*end\s*-?\}\}`)
2424
reComment = regexp.MustCompile(`\{\{/\*.*?\*/\}\}`)
2525
reVariable = regexp.MustCompile(`\{\{-?\s*(\.[\w.]+)\s*-?\}\}`)
26+
// Matches any Go template action: {{ ... }}
27+
reAction = regexp.MustCompile(`\{\{(.+?)\}\}`)
28+
// Matches dot-variables anywhere inside an action (e.g. .Body in "{{ .Body | truncate 200 }}")
29+
reActionVar = regexp.MustCompile(`(\.[A-Z]\w*(?:\.[A-Z]\w*)*)`)
2630
// Custom funcs: {{ funcName ... }} where funcName is not a keyword.
2731
reCustomFunc = regexp.MustCompile(`\{\{-?\s*([a-zA-Z]\w+)\s+`)
2832

@@ -194,12 +198,20 @@ func (a *GoTemplateAnalyzer) analyzeFile(filePath string) (GoTemplate, error) {
194198
}
195199
}
196200

197-
// Variables: {{ .Something }} — but not inside other directives we already captured
198-
for _, m := range reVariable.FindAllStringSubmatch(line, -1) {
199-
v := m[1]
200-
if !varSet[v] {
201-
varSet[v] = true
202-
tpl.Variables = append(tpl.Variables, v)
201+
// Variables: extract .Var tokens from inside any {{ ... }} action (not just standalone).
202+
// This captures variables in pipelines like {{ .Body | truncate 200 }}.
203+
for _, actionMatch := range reAction.FindAllStringSubmatch(line, -1) {
204+
inner := actionMatch[1]
205+
// Skip comments
206+
if strings.HasPrefix(strings.TrimSpace(inner), "/*") {
207+
continue
208+
}
209+
for _, varMatch := range reActionVar.FindAllStringSubmatch(inner, -1) {
210+
v := varMatch[1]
211+
if !varSet[v] {
212+
varSet[v] = true
213+
tpl.Variables = append(tpl.Variables, v)
214+
}
203215
}
204216
}
205217

0 commit comments

Comments
 (0)