Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(serving): support scoped cross-references #5530

Merged
merged 2 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions kythe/go/serving/xrefs/xrefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -892,20 +892,23 @@ readLoop:
case xrefs.IsDefKind(req.DefinitionKind, grp.Kind, cr.Incomplete):
filtered := filter.FilterGroup(grp)
reply.Total.Definitions += int64(len(grp.Anchor))
reply.Total.Definitions += int64(countRefs(grp.GetScopedReference()))
reply.Filtered.Definitions += int64(filtered)
if wantMoreCrossRefs {
stats.addAnchors(&crs.Definition, grp)
}
case xrefs.IsDeclKind(req.DeclarationKind, grp.Kind, cr.Incomplete):
filtered := filter.FilterGroup(grp)
reply.Total.Declarations += int64(len(grp.Anchor))
reply.Total.Declarations += int64(countRefs(grp.GetScopedReference()))
reply.Filtered.Declarations += int64(filtered)
if wantMoreCrossRefs {
stats.addAnchors(&crs.Declaration, grp)
}
case xrefs.IsRefKind(req.ReferenceKind, grp.Kind):
filtered := filter.FilterGroup(grp)
reply.Total.References += int64(len(grp.Anchor))
reply.Total.References += int64(countRefs(grp.GetScopedReference()))
reply.Filtered.References += int64(filtered)
if wantMoreCrossRefs {
stats.addAnchors(&crs.Reference, grp)
Expand Down Expand Up @@ -1236,6 +1239,14 @@ func addMergeNode(mergeMap map[string]string, allTickets []string, rootNode, mer
return allTickets
}

func countRefs(rs []*srvpb.PagedCrossReferences_ScopedReference) int {
var n int
for _, ref := range rs {
n += len(ref.GetReference())
}
return n
}

func nodeKind(n *srvpb.Node) string {
if n == nil {
return ""
Expand Down Expand Up @@ -1370,6 +1381,10 @@ func (s *refStats) addRelatedNodes(crs *xpb.CrossReferencesReply_CrossReferenceS
func (s *refStats) addAnchors(to *[]*xpb.CrossReferencesReply_RelatedAnchor, grp *srvpb.PagedCrossReferences_Group) bool {
kind := edges.Canonical(grp.Kind)
as := grp.Anchor
for _, refs := range grp.GetScopedReference() {
// TODO(schroederc): make scopes available in API
as = append(as, refs.GetReference()...)
}
fileInfos := makeFileInfoMap(grp.FileInfo)

if s.total == s.max {
Expand Down
14 changes: 14 additions & 0 deletions kythe/go/serving/xrefs/xrefs_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,8 @@ func (f *corpusPathFilter) FilterGroup(grp *srvpb.PagedCrossReferences_Group) (f
var n int
grp.Anchor, n = f.filterAnchors(grp.GetAnchor())
filtered += n
grp.ScopedReference, n = f.filterReferences(grp.GetScopedReference())
filtered += n
grp.RelatedNode, n = f.filterRelatedNodes(grp.GetRelatedNode())
filtered += n
grp.Caller, n = f.filterCallers(grp.GetCaller())
Expand All @@ -428,6 +430,18 @@ func (f *corpusPathFilter) filterAnchors(as []*srvpb.ExpandedAnchor) ([]*srvpb.E
return as[:j], len(as) - j
}

func (f *corpusPathFilter) filterReferences(rs []*srvpb.PagedCrossReferences_ScopedReference) ([]*srvpb.PagedCrossReferences_ScopedReference, int) {
var j int
for i, c := range rs {
if !f.AllowExpandedAnchor(c.GetScope()) {
continue
}
rs[j] = rs[i]
j++
}
return rs[:j], len(rs) - j
}

func (f *corpusPathFilter) filterCallers(cs []*srvpb.PagedCrossReferences_Caller) ([]*srvpb.PagedCrossReferences_Caller, int) {
var j int
for i, c := range cs {
Expand Down
52 changes: 27 additions & 25 deletions kythe/go/serving/xrefs/xrefs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,35 +359,37 @@ var (
Group: []*srvpb.PagedCrossReferences_Group{{
BuildConfig: "testConfig",
Kind: "%/kythe/edge/defines/binding",
Anchor: []*srvpb.ExpandedAnchor{{
Ticket: "kythe://c?lang=otpl?path=/a/path#27-33",
BuildConfiguration: "testConfig",
ScopedReference: []*srvpb.PagedCrossReferences_ScopedReference{{
Reference: []*srvpb.ExpandedAnchor{{
Ticket: "kythe://c?lang=otpl?path=/a/path#27-33",
BuildConfiguration: "testConfig",

Span: &cpb.Span{
Start: &cpb.Point{
ByteOffset: 27,
LineNumber: 2,
ColumnOffset: 10,
},
End: &cpb.Point{
ByteOffset: 33,
LineNumber: 3,
ColumnOffset: 5,
Span: &cpb.Span{
Start: &cpb.Point{
ByteOffset: 27,
LineNumber: 2,
ColumnOffset: 10,
},
End: &cpb.Point{
ByteOffset: 33,
LineNumber: 3,
ColumnOffset: 5,
},
},
},

SnippetSpan: &cpb.Span{
Start: &cpb.Point{
ByteOffset: 17,
LineNumber: 2,
},
End: &cpb.Point{
ByteOffset: 27,
LineNumber: 2,
ColumnOffset: 10,
SnippetSpan: &cpb.Span{
Start: &cpb.Point{
ByteOffset: 17,
LineNumber: 2,
},
End: &cpb.Point{
ByteOffset: 27,
LineNumber: 2,
ColumnOffset: 10,
},
},
},
Snippet: "here and ",
Snippet: "here and ",
}},
}},
}},
PageIndex: []*srvpb.PagedCrossReferences_PageIndex{{
Expand Down
15 changes: 15 additions & 0 deletions kythe/proto/serving.proto
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,20 @@ message PagedCrossReferences {
int32 ordinal = 2;
}

// References to the source node with an associated semantic scope.
message ScopedReference {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like an interesting change! Will this require any schema changes for the indexers to generate the scope data?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really. It requires the childof edges that are already part of the callsite relation. A couple of indexers already emit this data: #4187 and 0a3afc9

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool. 🥰

// The definition anchor covering the semantic scope.
ExpandedAnchor scope = 1;
// The semantic ticket for the scope.
string semantic_scope = 2;

// MarkedSource for the scope.
kythe.proto.common.MarkedSource marked_source = 3;

// Specific locations within the scope that reference the source node.
repeated ExpandedAnchor reference = 4;
}

// Caller of the source node with all associated callsites within the caller.
message Caller {
// The anchor covering the caller.
Expand All @@ -301,6 +315,7 @@ message PagedCrossReferences {
repeated ExpandedAnchor anchor = 2;
repeated RelatedNode related_node = 3;
repeated Caller caller = 4;
repeated ScopedReference scoped_reference = 7;

// List of files referenced in the group
repeated FileInfo file_info = 6;
Expand Down