Skip to content

Commit e5f33a6

Browse files
committed
feat: improve block indexer
1 parent a72fc4b commit e5f33a6

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

internal/twig/parser.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package twig
22

33
import (
4+
"bytes"
45
"strings"
56

67
tree_sitter "github.com/tree-sitter/go-tree-sitter"
@@ -52,10 +53,20 @@ func ParseTwig(filePath string, node *tree_sitter.Node, content []byte) (*TwigFi
5253
Blocks: make(map[string]TwigBlock),
5354
}
5455

56+
if !bytes.Contains(content, []byte("{%")) {
57+
return file, nil
58+
}
59+
5560
// Find all blocks recursively
56-
findBlocks(node, content, file)
61+
if bytes.Contains(content, []byte("block")) {
62+
findBlocks(node, content, file)
63+
}
5764

5865
// Find extends tag
66+
if !bytes.Contains(content, []byte("extends")) && !bytes.Contains(content, []byte("sw_extends")) {
67+
return file, nil
68+
}
69+
5970
var cursor = node.Walk()
6071
defer cursor.Close()
6172

@@ -65,14 +76,28 @@ func ParseTwig(filePath string, node *tree_sitter.Node, content []byte) (*TwigFi
6576

6677
if node.Kind() == "tag" {
6778
// Check if this is an extends tag by examining the tag text
68-
tagText := string(node.Utf8Text(content))
6979
isExtendsTag := false
80+
tagName := ""
81+
for i := 0; i < int(node.NamedChildCount()); i++ {
82+
child := node.NamedChild(uint(i))
83+
if child.Kind() == "name" {
84+
tagName = string(child.Utf8Text(content))
85+
break
86+
}
87+
}
7088

7189
// Check if the tag contains "extends" or "sw_extends"
72-
if strings.Contains(tagText, "extends") || strings.Contains(tagText, "sw_extends") {
90+
if tagName == "extends" || tagName == "sw_extends" {
7391
isExtendsTag = true
7492
}
7593

94+
if !isExtendsTag && tagName == "" {
95+
tagText := string(node.Utf8Text(content))
96+
if strings.Contains(tagText, "extends") || strings.Contains(tagText, "sw_extends") {
97+
isExtendsTag = true
98+
}
99+
}
100+
76101
// If it's an extends tag, look for the string parameter
77102
if isExtendsTag {
78103
for i := 0; i < int(node.NamedChildCount()); i++ {

0 commit comments

Comments
 (0)