-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdot.go
More file actions
83 lines (71 loc) · 2.05 KB
/
Copy pathdot.go
File metadata and controls
83 lines (71 loc) · 2.05 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
package dscope
import (
"fmt"
"io"
"strings"
)
const TheoryOfScopeVisualization = `
dscope visualization theory:
- Graphs represent effective dependency resolution semantics.
- Built-in dependencies are shown as built-ins, even when a user supplies an
ignored definition for the same type.
- Ignored definitions must not contribute labels or dependency edges.
`
func (scope Scope) ToDOT(w io.Writer) error {
if _, err := io.WriteString(w, "digraph dscope {\n"); err != nil {
return err
}
if _, err := io.WriteString(w, " rankdir=LR;\n"); err != nil {
return err
}
if _, err := io.WriteString(w, " node [shape=box, style=filled, fillcolor=lightblue];\n"); err != nil {
return err
}
nodes := make(map[_TypeID]struct{})
edges := make(map[[2]_TypeID]struct{})
nodeInfo := make(map[_TypeID]string)
for typ := range scope.AllTypes() {
id := getTypeID(typ)
if isAlwaysProvided(id) {
nodes[id] = struct{}{}
nodeInfo[id] = fmt.Sprintf("Type: %s\\nBuilt-in", typ.String())
}
}
for effectiveValue := range scope.values.IterValues() {
typeID := effectiveValue.typeInfo.TypeID
if isAlwaysProvided(typeID) {
continue
}
typeName := typeIDToType(typeID).String()
nodes[typeID] = struct{}{}
nodeInfo[typeID] = fmt.Sprintf(
"Type: %s\\nDefined By: %s",
typeName,
effectiveValue.typeInfo.DefType.String(),
)
for _, dependencyID := range effectiveValue.typeInfo.Dependencies {
if _, ok := scope.values.Load(dependencyID); ok || isAlwaysProvided(dependencyID) {
nodes[dependencyID] = struct{}{}
edges[[2]_TypeID{dependencyID, typeID}] = struct{}{}
}
}
}
for id := range nodes {
label := typeIDToType(id).String()
if info, ok := nodeInfo[id]; ok {
label = info
}
if _, err := fmt.Fprintf(w, " \"%d\" [label=\"%s\"];\n", id, strings.ReplaceAll(label, "\"", "\\\"")); err != nil {
return err
}
}
for edge := range edges {
if _, err := fmt.Fprintf(w, " \"%d\" -> \"%d\";\n", edge[0], edge[1]); err != nil {
return err
}
}
if _, err := io.WriteString(w, "}\n"); err != nil {
return err
}
return nil
}