Skip to content

Commit

Permalink
feat(serving): support scoped cross-references (#5530)
Browse files Browse the repository at this point in the history
This change only adds support to the ScopedReferences in the serving
data, and the actual server will unwrap them into unscoped references. A
future change will support ScopedReferences in the CrossReferences API.
  • Loading branch information
schroederc committed Mar 9, 2023
1 parent ff8bbd1 commit 2387903
Show file tree
Hide file tree
Showing 5 changed files with 414 additions and 250 deletions.
15 changes: 15 additions & 0 deletions kythe/go/serving/xrefs/xrefs.go
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
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
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
Expand Up @@ -276,6 +276,20 @@ message PagedCrossReferences {
int32 ordinal = 2;
}

// References to the source node with an associated semantic scope.
message ScopedReference {
// 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

0 comments on commit 2387903

Please sign in to comment.