Skip to content

Commit ae09e3b

Browse files
scheme: Add is_definition relationship (#88)
1 parent b8dda7d commit ae09e3b

File tree

21 files changed

+2958
-2130
lines changed

21 files changed

+2958
-2130
lines changed

.gitattributes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ bindings/rust/src/scip.rs linguist-generated=true
44
bindings/haskell/src/Proto/**.hs linguist-generated=true
55
docs/scip.md linguist-generated=true
66
yarn.lock linguist-generated=true
7+
cmd/tests/reprolang/src/grammar.json linguist-generated=true
8+
cmd/tests/reprolang/src/node-types.json linguist-generated=true
9+
cmd/tests/reprolang/src/parser.c linguist-generated=true

bindings/go/scip/convert.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,15 @@ func (g *graph) emitRelationship(relationship *Relationship, rangeID, documentID
289289
return []int{relationshipIDs.ReferenceResult}
290290
}
291291

292+
if relationship.IsDefinition {
293+
g.emitEdge("item", reader.Edge{
294+
OutV: relationshipIDs.DefinitionResult,
295+
InVs: []int{rangeID},
296+
Document: documentID,
297+
})
298+
return []int{relationshipIDs.DefinitionResult}
299+
}
300+
292301
return nil
293302
}
294303

bindings/go/scip/scip.pb.go

Lines changed: 210 additions & 184 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindings/haskell/src/Proto/Scip.hs

Lines changed: 713 additions & 645 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindings/haskell/src/Proto/Scip_Fields.hs

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindings/rust/src/generated/scip.rs

Lines changed: 768 additions & 713 deletions
Large diffs are not rendered by default.

bindings/typescript/scip.ts

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/tests/reprolang/bindings/go/repro/ast.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,21 @@ import (
99
"github.com/sourcegraph/scip/bindings/go/scip"
1010
)
1111

12-
type definitionStatement struct {
13-
docstring string
14-
name *identifier
12+
type relationships struct {
1513
implementsRelation *identifier
1614
referencesRelation *identifier
1715
typeDefinesRelation *identifier
16+
definedByRelation *identifier
17+
}
18+
19+
type definitionStatement struct {
20+
docstring string
21+
name *identifier
22+
relations relationships
1823
}
1924

20-
func (s *definitionStatement) relationIdentifiers() []*identifier {
21-
return []*identifier{s.implementsRelation, s.referencesRelation, s.typeDefinesRelation}
25+
func (r *relationships) identifiers() []*identifier {
26+
return []*identifier{r.implementsRelation, r.referencesRelation, r.typeDefinesRelation, r.definedByRelation}
2227
}
2328

2429
type referenceStatement struct {
@@ -53,6 +58,11 @@ func newIdentifier(s *reproSourceFile, n *sitter.Node) *identifier {
5358
}
5459
}
5560

61+
type relationshipsStatement struct {
62+
name *identifier
63+
relations relationships
64+
}
65+
5666
func NewRangePositionFromNode(node *sitter.Node) *scip.Range {
5767
return &scip.Range{
5868
Start: scip.Position{
@@ -73,6 +83,7 @@ func (i *identifier) resolveSymbol(localScope *reproScope, context *reproContext
7383
}
7484
symbol, ok := scope.names[i.value]
7585
if !ok {
86+
fmt.Printf("scope.names = %v\n", scope.names)
7687
symbol = "local ERROR_UNRESOLVED_SYMBOL"
7788
}
7889
i.symbol = symbol

cmd/tests/reprolang/bindings/go/repro/namer.go

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,63 +9,83 @@ import (
99
// enterGlobalDefinitions inserts the names of the global symbols that are defined in this
1010
// dependency into the provided global scope.
1111
func (d *reproDependency) enterGlobalDefinitions(context *reproContext) {
12+
enter := func(file *reproSourceFile, name *identifier) {
13+
if name.isLocalSymbol() {
14+
return
15+
}
16+
symbol := newGlobalSymbol(d.Package, file, name)
17+
parsedSymbol, err := scip.ParseSymbol(symbol)
18+
if err != nil {
19+
return
20+
}
21+
newName := newGlobalName(context.pkg, parsedSymbol)
22+
context.globalScope.names[newName] = symbol
23+
}
1224
for _, file := range d.Sources {
1325
for _, definition := range file.definitions {
14-
if definition.name.isLocalSymbol() {
15-
continue
16-
}
17-
symbol := newGlobalSymbol(d.Package, file, definition)
18-
parsedSymbol, err := scip.ParseSymbol(symbol)
19-
if err != nil {
20-
continue
21-
}
22-
name := newGlobalName(context.pkg, parsedSymbol)
23-
context.globalScope.names[name] = symbol
26+
enter(file, definition.name)
27+
}
28+
for _, relationship := range file.relationships {
29+
enter(file, relationship.name)
2430
}
2531
}
2632
}
2733

2834
// enterDefinitions inserts the names of the definitions into the appropriate scope (local symbols go into the local scope).
2935
func (s *reproSourceFile) enterDefinitions(context *reproContext) {
30-
for _, def := range s.definitions {
36+
enter := func(name *identifier, defName *identifier) {
3137
scope := context.globalScope
32-
if def.name.isLocalSymbol() {
38+
if name.isLocalSymbol() {
3339
scope = s.localScope
3440
}
3541
var symbol string
36-
if def.name.isLocalSymbol() {
37-
symbol = fmt.Sprintf("local %s", def.name.value[len("local"):])
42+
if name.isLocalSymbol() {
43+
symbol = fmt.Sprintf("local %s", defName.value[len("local"):])
3844
} else {
39-
symbol = newGlobalSymbol(context.pkg, s, def)
45+
symbol = newGlobalSymbol(context.pkg, s, defName)
46+
}
47+
name.symbol = symbol
48+
scope.names[name.value] = symbol
49+
}
50+
for _, def := range s.definitions {
51+
enter(def.name, def.name)
52+
}
53+
for _, rel := range s.relationships {
54+
if rel.relations.definedByRelation != nil {
55+
enter(rel.name, rel.relations.definedByRelation)
4056
}
41-
def.name.symbol = symbol
42-
scope.names[def.name.value] = symbol
4357
}
4458
}
4559

4660
// resolveReferences updates the .symbol field for all names of reference identifiers.
4761
func (s *reproSourceFile) resolveReferences(context *reproContext) {
48-
for _, def := range s.definitions {
49-
for _, ident := range def.relationIdentifiers() {
62+
resolveIdents := func(rel relationships) {
63+
for _, ident := range rel.identifiers() {
5064
if ident == nil {
5165
continue
5266
}
5367
ident.resolveSymbol(s.localScope, context)
5468
}
5569
}
70+
for _, def := range s.definitions {
71+
resolveIdents(def.relations)
72+
}
73+
for _, rel := range s.relationships {
74+
resolveIdents(rel.relations)
75+
}
5676
for _, ref := range s.references {
5777
ref.name.resolveSymbol(s.localScope, context)
5878
}
5979
}
6080

6181
// newGlobalSymbol returns an SCIP symbol for the given definition.
62-
func newGlobalSymbol(pkg *scip.Package, document *reproSourceFile, definition *definitionStatement) string {
82+
func newGlobalSymbol(pkg *scip.Package, document *reproSourceFile, name *identifier) string {
6383
return fmt.Sprintf(
6484
"reprolang repro_manager %v %v %v/%v",
6585
pkg.Name,
6686
pkg.Version,
6787
document.Source.RelativePath,
68-
definition.name.value,
88+
name.value,
6989
)
7090
}
7191

cmd/tests/reprolang/bindings/go/repro/parser.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,39 @@ func (s *reproSourceFile) loadStatements() {
2727
continue
2828
}
2929
switch child.Type() {
30-
case "definition_statement":
30+
case "relationships_statement", "definition_statement":
3131
docstring := ""
3232
docstringNode := child.ChildByFieldName("docstring")
3333
if docstringNode != nil {
3434
docstring = s.nodeText(docstringNode)[len("# doctring:"):]
3535
}
36-
statement := &definitionStatement{
37-
docstring: docstring,
38-
name: newIdentifier(s, child.ChildByFieldName("name")),
39-
}
36+
name := newIdentifier(s, child.ChildByFieldName("name"))
37+
relations := relationships{}
4038
for i := uint32(0); i < child.NamedChildCount(); i++ {
4139
relation := child.NamedChild(int(i))
4240
switch relation.Type() {
4341
case "implementation_relation":
44-
statement.implementsRelation = newIdentifier(s, relation.ChildByFieldName("name"))
42+
relations.implementsRelation = newIdentifier(s, relation.ChildByFieldName("name"))
4543
case "type_definition_relation":
46-
statement.typeDefinesRelation = newIdentifier(s, relation.ChildByFieldName("name"))
44+
relations.typeDefinesRelation = newIdentifier(s, relation.ChildByFieldName("name"))
4745
case "references_relation":
48-
statement.referencesRelation = newIdentifier(s, relation.ChildByFieldName("name"))
46+
relations.referencesRelation = newIdentifier(s, relation.ChildByFieldName("name"))
47+
case "defined_by_relation":
48+
relations.definedByRelation = newIdentifier(s, relation.ChildByFieldName("name"))
4949
}
5050
}
51-
s.definitions = append(s.definitions, statement)
51+
if child.Type() == "definition_statement" {
52+
s.definitions = append(s.definitions, &definitionStatement{
53+
docstring: docstring,
54+
name: name,
55+
relations: relations,
56+
})
57+
} else {
58+
s.relationships = append(s.relationships, &relationshipsStatement{
59+
name: name,
60+
relations: relations,
61+
})
62+
}
5263
case "reference_statement":
5364
s.references = append(s.references, &referenceStatement{
5465
name: newIdentifier(s, child.ChildByFieldName("name")),

0 commit comments

Comments
 (0)