forked from zzet/gortex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcss.go
More file actions
173 lines (151 loc) · 4.89 KB
/
Copy pathcss.go
File metadata and controls
173 lines (151 loc) · 4.89 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
package languages
import (
"strings"
"github.com/zzet/gortex/internal/graph"
"github.com/zzet/gortex/internal/parser"
sitter "github.com/zzet/gortex/internal/parser/tsitter"
"github.com/zzet/gortex/internal/parser/tsitter/css"
)
// qCssAll is a single tree-sitter query alternating over every pattern
// the CSS extractor needs — @import rules, class selectors, id
// selectors, and custom-property (`--name`) declarations. One cursor
// walk per file replaces the three EachMatch passes plus the separate
// recursive declaration walk the previous design made. Capture names
// are disjoint across patterns so the dispatch in Extract branches on
// which one is set.
const qCssAll = `
[
(import_statement) @import.def
(class_selector
(class_name) @class.name) @class.def
(id_selector
(id_name) @id.name) @id.def
(declaration
(property_name) @prop.name) @prop.def
]
`
// CSSExtractor extracts CSS files into graph nodes and edges. A single
// precompiled alternation query drives one cursor walk per file.
type CSSExtractor struct {
lang *sitter.Language
qAll *parser.PreparedQuery
}
func NewCSSExtractor() *CSSExtractor {
lang := css.GetLanguage()
return &CSSExtractor{
lang: lang,
qAll: parser.MustPreparedQuery(qCssAll, lang),
}
}
func (e *CSSExtractor) Language() string { return "css" }
func (e *CSSExtractor) Extensions() []string { return []string{".css"} }
func (e *CSSExtractor) Extract(filePath string, src []byte) (*parser.ExtractionResult, error) {
tree, err := parser.ParseFile(src, e.lang)
if err != nil {
return nil, err
}
defer tree.Close()
root := tree.RootNode()
result := &parser.ExtractionResult{}
fileNode := &graph.Node{
ID: filePath, Kind: graph.KindFile, Name: filePath,
FilePath: filePath, StartLine: 1, EndLine: int(root.EndPoint().Row) + 1,
Language: "css",
}
fileID := fileNode.ID
result.Nodes = append(result.Nodes, fileNode)
seen := make(map[string]bool)
parser.EachMatch(e.qAll, root, src, func(m parser.QueryResult) {
switch {
case m.Captures["import.def"] != nil:
def := m.Captures["import.def"]
// Extract the path from @import url("...") or @import "...".
importPath := extractCSSImportPath(def.Text)
if importPath == "" {
return
}
result.Edges = append(result.Edges, &graph.Edge{
From: fileID,
To: "unresolved::import::" + importPath,
Kind: graph.EdgeImports,
FilePath: filePath,
Line: def.StartLine + 1,
})
case m.Captures["class.def"] != nil:
name := m.Captures["class.name"].Text
def := m.Captures["class.def"]
id := filePath + "::." + name
if seen[id] {
return
}
seen[id] = true
result.Nodes = append(result.Nodes, &graph.Node{
ID: id, Kind: graph.KindType, Name: "." + name,
FilePath: filePath, StartLine: def.StartLine + 1, EndLine: def.EndLine + 1,
Language: "css",
})
result.Edges = append(result.Edges, &graph.Edge{
From: fileID, To: id, Kind: graph.EdgeDefines,
FilePath: filePath, Line: def.StartLine + 1,
})
case m.Captures["id.def"] != nil:
name := m.Captures["id.name"].Text
def := m.Captures["id.def"]
id := filePath + "::#" + name
if seen[id] {
return
}
seen[id] = true
result.Nodes = append(result.Nodes, &graph.Node{
ID: id, Kind: graph.KindVariable, Name: "#" + name,
FilePath: filePath, StartLine: def.StartLine + 1, EndLine: def.EndLine + 1,
Language: "css",
})
result.Edges = append(result.Edges, &graph.Edge{
From: fileID, To: id, Kind: graph.EdgeDefines,
FilePath: filePath, Line: def.StartLine + 1,
})
case m.Captures["prop.def"] != nil:
// CSS custom properties — declarations whose property
// name starts with "--".
name := m.Captures["prop.name"].Text
if !strings.HasPrefix(name, "--") {
return
}
def := m.Captures["prop.def"]
id := filePath + "::" + name
if seen[id] {
return
}
seen[id] = true
result.Nodes = append(result.Nodes, &graph.Node{
ID: id, Kind: graph.KindVariable, Name: name,
FilePath: filePath, StartLine: def.StartLine + 1, EndLine: def.EndLine + 1,
Language: "css", Meta: map[string]any{
"custom_property": true,
},
})
result.Edges = append(result.Edges, &graph.Edge{
From: fileID, To: id, Kind: graph.EdgeDefines,
FilePath: filePath, Line: def.StartLine + 1,
})
}
})
return result, nil
}
// extractCSSImportPath extracts the path from an @import statement.
// Handles: @import url("path"); @import url('path'); @import "path"; @import 'path';
func extractCSSImportPath(text string) string {
text = strings.TrimPrefix(text, "@import")
text = strings.TrimSpace(text)
text = strings.TrimSuffix(text, ";")
text = strings.TrimSpace(text)
// Handle url("...") or url('...')
if strings.HasPrefix(text, "url(") {
text = strings.TrimPrefix(text, "url(")
text = strings.TrimSuffix(text, ")")
text = strings.TrimSpace(text)
}
text = strings.Trim(text, `"'`)
return text
}