forked from zzet/gortex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsharp_dotnet.go
More file actions
66 lines (60 loc) · 1.77 KB
/
Copy pathcsharp_dotnet.go
File metadata and controls
66 lines (60 loc) · 1.77 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
package languages
import (
"regexp"
"sort"
"strings"
"github.com/zzet/gortex/internal/graph"
"github.com/zzet/gortex/internal/parser"
)
// diRegistrationRe matches a .NET dependency-injection registration —
// services.AddScoped<IFoo, Foo>(), AddSingleton<IBar>(),
// AddTransient<…>(), AddHostedService<…>().
var diRegistrationRe = regexp.MustCompile(
`\.Add(Scoped|Singleton|Transient|HostedService)\s*<\s*([\w.]+)\s*(?:,\s*([\w.]+)\s*)?>`)
// comInteropRe matches a COM / native-interop marker attribute.
var comInteropRe = regexp.MustCompile(
`\[\s*(?:ComImport|ComVisible|Guid|DllImport|InterfaceType|ClassInterface|ComSourceInterfaces)\b`)
// detectDotNetSurfaces stamps the C# file node with the two .NET
// surfaces a tree-sitter symbol walk does not otherwise expose:
// dependency-injection registrations (Meta["di_registrations"]) and a
// COM / native-interop flag (Meta["com_interop"]). Both are core to
// understanding a .NET service's wiring and unmanaged boundary.
func detectDotNetSurfaces(src []byte, result *parser.ExtractionResult) {
var file *graph.Node
for _, n := range result.Nodes {
if n.Kind == graph.KindFile {
file = n
break
}
}
if file == nil {
return
}
text := string(src)
var regs []string
seen := map[string]bool{}
for _, m := range diRegistrationRe.FindAllStringSubmatch(text, -1) {
entry := strings.ToLower(m[1]) + " " + m[2]
if m[3] != "" {
entry += " -> " + m[3]
}
if !seen[entry] {
seen[entry] = true
regs = append(regs, entry)
}
}
hasCOM := comInteropRe.MatchString(text)
if len(regs) == 0 && !hasCOM {
return
}
if file.Meta == nil {
file.Meta = map[string]any{}
}
if len(regs) > 0 {
sort.Strings(regs)
file.Meta["di_registrations"] = regs
}
if hasCOM {
file.Meta["com_interop"] = true
}
}