Skip to content

Commit

Permalink
feat(go_indexer): add definition links to MarkedSource (#5194)
Browse files Browse the repository at this point in the history
  • Loading branch information
schroederc committed Jan 25, 2022
1 parent 2440083 commit 8694856
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 8 deletions.
1 change: 1 addition & 0 deletions kythe/go/indexer/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ go_library(
],
deps = [
"//kythe/go/extractors/govname",
"//kythe/go/util/kytheuri",
"//kythe/go/util/metadata",
"//kythe/go/util/ptypes",
"//kythe/go/util/schema/edges",
Expand Down
2 changes: 1 addition & 1 deletion kythe/go/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,6 @@ func Resolve(unit *apb.CompilationUnit, f Fetcher, opts *ResolveOptions) (*Packa
Error: func(err error) { pi.Errors = append(pi.Errors, err) },
}
pi.Package, _ = c.Check(pi.Name, pi.FileSet, pi.Files, pi.Info)
pi.PackageVName[pi.Package] = unit.VName

// Fill in the mapping from packages to vnames.
for ip, vname := range imap {
Expand All @@ -436,6 +435,7 @@ func Resolve(unit *apb.CompilationUnit, f Fetcher, opts *ResolveOptions) (*Packa
pi.VName = proto.Clone(unit.VName).(*spb.VName)
pi.VName.Language = govname.Language
pi.VName.Signature = "package"
pi.PackageVName[pi.Package] = pi.VName

return pi, nil
}
Expand Down
19 changes: 13 additions & 6 deletions kythe/go/indexer/markedsource.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"log"
"strings"

"kythe.io/kythe/go/util/kytheuri"
"kythe.io/kythe/go/util/schema/facts"

"github.com/golang/protobuf/proto"
Expand All @@ -37,6 +38,7 @@ func (pi *PackageInfo) MarkedSource(obj types.Object) *cpb.MarkedSource {
Child: []*cpb.MarkedSource{{
Kind: cpb.MarkedSource_IDENTIFIER,
PreText: objectName(obj),
Link: []*cpb.Link{{Definition: []string{kytheuri.ToString(pi.ObjectVName(obj))}}},
}},
}

Expand Down Expand Up @@ -94,6 +96,7 @@ func (pi *PackageInfo) MarkedSource(obj types.Object) *cpb.MarkedSource {
Child: []*cpb.MarkedSource{{
Kind: cpb.MarkedSource_TYPE,
PreText: typeName(recv.Type()) + typeArgs(recv.Type()),
Link: []*cpb.Link{{Definition: []string{kytheuri.ToString(pi.ObjectVName(recv))}}},
}},
})
firstParam = 1
Expand Down Expand Up @@ -210,23 +213,27 @@ func typeArgs(typ types.Type) string {
// are no appropriate qualifiers.
func (pi *PackageInfo) typeContext(obj types.Object) []*cpb.MarkedSource {
var ms []*cpb.MarkedSource
addID := func(s string) {
ms = append(ms, &cpb.MarkedSource{
addID := func(s string, v *spb.VName) {
id := &cpb.MarkedSource{
Kind: cpb.MarkedSource_IDENTIFIER,
PreText: s,
})
}
if v != nil {
id.Link = []*cpb.Link{{Definition: []string{kytheuri.ToString(v)}}}
}
ms = append(ms, id)
}
for cur := pi.owner[obj]; cur != nil; cur = pi.owner[cur] {
if t, ok := cur.(interface {
Name() string
}); ok {
addID(t.Name())
addID(t.Name(), pi.ObjectVName(cur))
} else {
addID(typeName(cur.Type()))
addID(typeName(cur.Type()), pi.ObjectVName(cur))
}
}
if pkg := obj.Pkg(); pkg != nil {
addID(pi.importPath(pkg))
addID(pi.importPath(pkg), pi.PackageVName[pkg])
}
for i, j := 0, len(ms)-1; i < j; {
ms[i], ms[j] = ms[j], ms[i]
Expand Down
5 changes: 4 additions & 1 deletion kythe/go/indexer/testdata/code/funcdecl.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Package funcdecl tests code facts for a function declaration.
//- @funcdecl defines/binding Pkg
package funcdecl

//- @Positive defines/binding Pos
Expand All @@ -20,6 +21,7 @@ package funcdecl
//- XCtx child.1 XFunc
//- XPkg.kind "IDENTIFIER"
//- XPkg.pre_text "funcdecl"
//- XPkg link Pkg
//- XFunc.kind "IDENTIFIER"
//- XFunc.pre_text "Positive"
//- XId.kind "IDENTIFIER"
Expand Down Expand Up @@ -53,9 +55,10 @@ package funcdecl
//- PCContext.kind "CONTEXT"
//- PCContext child.0 PCPkg
//- PCPkg.pre_text "funcdecl"
//- PCPkg link Pkg
//- PCIdent.kind "IDENTIFIER"
//- PCIdent.pre_text "Positive"
//-
//- PCIdent link Pos
func Positive(x int) bool {
return x > 0
}
Expand Down
5 changes: 5 additions & 0 deletions kythe/go/indexer/testdata/code/interface.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Package iface tests code facts for an interface type.
//- @iface defines/binding Pkg
package iface

//- @Thinger defines/binding Thinger
Expand All @@ -10,6 +11,7 @@ package iface
//- TName child.1 TIdent
//- TIdent.kind "IDENTIFIER"
//- TIdent.pre_text "Thinger"
//- TIdent link Thinger
type Thinger interface {
//- @Thing defines/binding Thing
//- Thing code MCode
Expand Down Expand Up @@ -38,9 +40,12 @@ type Thinger interface {
//- MContext.kind "CONTEXT"
//- MContext.post_child_text "."
//- MContext child.0 MPkg
//- MPkg link Pkg
//- MContext child.1 MOwner
//- MOwner link Thinger
//- MPkg.pre_text "test/iface"
//- MOwner.pre_text "Thinger"
//- MIdent.pre_text "Thing"
//- MIdent link Thing
Thing()
}
1 change: 1 addition & 0 deletions kythe/go/indexer/testdata/code/pkgvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ package pkgvar
//-
//- VIdent.kind "IDENTIFIER"
//- VIdent.pre_text "V"
//- VIdent link Var
var V int

0 comments on commit 8694856

Please sign in to comment.