Repository navigation
Expand file tree
/
Copy pathassembly.go
More file actions
160 lines (155 loc) · 5.35 KB
/
Copy pathassembly.go
File metadata and controls
160 lines (155 loc) · 5.35 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
package codegraph
import (
"context"
"fmt"
"path"
"github.com/compforge/codegraph/internal/analysis"
)
// graphAssembler owns public object registration and publication budgets.
// +spec=Only selected entities are materialized; every relation has published endpoints.
// +link=component://docs/language-pipeline.md
type graphAssembler struct {
index *analysis.Index
files map[string]analysis.Facts
opts Options
tags *TagMatcher
ids map[analysis.Ref]string
nodes map[string]Node
relations map[string]Relation
evidence, references int
stage string
}
func newGraphAssembler(index *analysis.Index, selected selection, opts Options, tags *TagMatcher) *graphAssembler {
a := &graphAssembler{stage: "publication", index: index, files: index.Files, opts: opts, tags: tags, ids: map[analysis.Ref]string{}, nodes: map[string]Node{}, relations: map[string]Relation{}}
for ref := range selected {
e := index.Entities[ref]
switch {
case ref.IsDocument():
a.ids[ref] = DocumentID(ref.Path)
case ref.IsDeclaration():
a.ids[ref] = declarationID(ref.Path, NodeKind(e.Kind), e.QualifiedName, e.Location.Start)
default:
a.ids[ref] = "node:" + identity(ref.SyntheticKey())
}
}
return a
}
func (a *graphAssembler) addNode(n Node) error {
if !n.Kind.IsPublic() {
return fmt.Errorf("cannot publish internal node kind %q", n.Kind)
}
if _, exists := a.nodes[n.ID]; !exists && len(a.nodes) >= a.opts.MaxNodes {
return &BuildBudgetError{Stage: a.stage, Resource: "MaxNodes", Used: len(a.nodes), Adding: 1, Limit: a.opts.MaxNodes}
}
a.nodes[n.ID] = n
return nil
}
func (a *graphAssembler) addRelation(r Relation) error {
if !r.Kind.IsPublic() {
return fmt.Errorf("cannot publish internal relation kind %q", r.Kind)
}
if _, ok := a.nodes[r.Source]; !ok {
return nil
}
if _, ok := a.nodes[r.Target]; !ok {
return nil
}
if r.ID == "" {
r.ID = identity(r.Source, r.Target, r.Kind, r.Location.Path, r.Location.StartByte, r.Location.EndByte)
}
if _, exists := a.relations[r.ID]; exists {
return nil
}
if len(a.relations) >= a.opts.MaxRelations {
return &BuildBudgetError{Stage: a.stage, Resource: "MaxRelations", Used: len(a.relations), Adding: 1, Limit: a.opts.MaxRelations}
}
if len(r.Evidence) > a.opts.MaxEvidence-a.evidence {
return &BuildBudgetError{Stage: a.stage, Resource: "MaxEvidence", Used: a.evidence, Adding: len(r.Evidence), Limit: a.opts.MaxEvidence}
}
a.relations[r.ID] = r
a.evidence += len(r.Evidence)
return nil
}
func (a *graphAssembler) relation(e analysis.Edge) Relation {
loc := location(a.files[e.Path], e.Span)
r := Relation{ID: identity(a.ids[e.Source], a.ids[e.Target], e.Kind, loc.Path, loc.StartByte, loc.EndByte), Source: a.ids[e.Source], Target: a.ids[e.Target], Kind: RelationKind(e.Kind), Confidence: Confidence(e.Confidence), Location: loc}
for _, proof := range e.Evidence {
evidence := Evidence{Basis: proof.Basis, Confidence: Confidence(proof.Confidence)}
if proof.Location != nil {
evidence.Location = locationPtr(a.files[proof.Location.Path], proof.Location.Span)
}
r.Evidence = append(r.Evidence, evidence)
}
return r
}
func (a *graphAssembler) Assemble(ctx context.Context, selected selection, failures map[string]Diagnostic) error {
for ref := range selected {
e := a.index.Entities[ref]
if err := ctx.Err(); err != nil {
return err
}
id := a.ids[ref]
n := Node{ID: id, Kind: NodeKind(e.Kind), Entrypoint: e.Entrypoint, Name: e.Name, QualifiedName: e.QualifiedName, Language: e.Language}
if e.Binding != nil {
b := ModuleBinding(*e.Binding)
n.Binding = &b
}
if ref.IsDocument() {
n.Gitlink = a.files[ref.Path].Gitlink
if n.Language == "" && n.Gitlink == "" {
n.Language = Language(ref.Path)
}
n.Manifest = projectManifest(a.files[ref.Path])
}
if e.Location != nil {
f := a.files[e.Location.Path]
n.Location = locationPtr(f, e.Location.Span)
n.Documentation = projectDocumentation(f, e.Documentation)
for _, m := range e.Comments {
n.Markers = append(n.Markers, Marker{Kind: MarkerKind(m.Kind), Text: m.Text, Location: location(f, m.Span)})
}
}
if e.SignatureLocation != nil {
f := a.files[e.SignatureLocation.Path]
span := e.SignatureLocation.Span
n.Signature = string(f.Source[span.Start:span.End])
n.SignatureLocation = locationPtr(f, span)
}
if e.NameLocation != nil {
n.NameLocation = locationPtr(a.files[e.NameLocation.Path], e.NameLocation.Span)
}
if err := a.addNode(n); err != nil {
return err
}
}
for _, d := range failures {
if d.Code == "parse_error" && permits(a.opts.Admission, DocumentNodeKind) {
loc := d.Location
if err := a.addNode(Node{ID: DocumentID(loc.Path), Kind: DocumentNodeKind, Name: path.Base(loc.Path), Language: Language(loc.Path), Location: &loc}); err != nil {
return err
}
}
}
for _, e := range a.index.Edges {
if err := ctx.Err(); err != nil {
return err
}
// Decorator occurrences own the public decorates relation, as in the full graph.
if e.Kind == Decorates {
continue
}
if _, ok := selected[e.Source]; !ok {
continue
}
if _, ok := selected[e.Target]; !ok {
continue
}
if err := a.addRelation(a.relation(e)); err != nil {
return err
}
}
if err := a.publishDocumentStructure(ctx, failures); err != nil {
return err
}
return a.publishReferences(ctx)
}