Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
require requested range to match syntactic occurrence exactly
Browse files Browse the repository at this point in the history
  • Loading branch information
kritzcreek committed Jun 12, 2024
1 parent 3248ffa commit d605278
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 42 deletions.
29 changes: 20 additions & 9 deletions internal/codeintel/codenav/scip_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,33 @@ type IOccurrence interface {
GetRange() []int32
}

func findIntersectingOccurrences[Occurrence IOccurrence](occurrences []Occurrence, search scip.Range) []Occurrence {
n, _ := slices.BinarySearchFunc(occurrences, search.Start, func(occ Occurrence, p scip.Position) int {
func findOccurrencesWithEqualRange[Occurrence IOccurrence](occurrences []Occurrence, search scip.Range) []Occurrence {
n, found := slices.BinarySearchFunc(occurrences, search.Start, func(occ Occurrence, p scip.Position) int {
occRange := scip.NewRangeUnchecked(occ.GetRange())
return occRange.Start.Compare(p)
})
n = max(0, n-1)

result := make([]Occurrence, 0)
results := []Occurrence{}
if !found {
return results
}
// Binary search is not guaranteed to find the last or first index, so we need to check in both directions
for _, occurrence := range occurrences[n:] {
parsedRange := scip.NewRangeUnchecked(occurrence.GetRange())
if search.End.Compare(parsedRange.Start) < 0 {
if parsedRange.Compare(search) == 0 {
results = append(results, occurrence)
} else {
break
}
if search.Intersects(parsedRange) {
result = append(result, occurrence)
}
for i := n - 1; i >= 0; i-- {
occurrence := occurrences[i]
parsedRange := scip.NewRangeUnchecked(occurrence.GetRange())
if parsedRange.Compare(search) == 0 {
results = append(results, occurrence)
} else {
break
}
}
return result

return results
}
52 changes: 20 additions & 32 deletions internal/codeintel/codenav/scip_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ func (o testOccurrence) GetRange() []int32 {
return o.Ints
}

func Test_findIntersectingOccurrences(t *testing.T) {
type args[Occurrence IOccurrence] struct {
occurrences []Occurrence
search scip.Range
}
type testCase[Occurrence IOccurrence] struct {
name string
args args[Occurrence]
want []Occurrence
}
type args[Occurrence IOccurrence] struct {
occurrences []Occurrence
search scip.Range
}
type testCase[Occurrence IOccurrence] struct {
name string
args args[Occurrence]
want []Occurrence
}

func Test_findOccurrencesWithEqualRange(t *testing.T) {
tests := []testCase[testOccurrence]{
{
name: "empty",
Expand All @@ -49,7 +50,7 @@ func Test_findIntersectingOccurrences(t *testing.T) {
},
},
{
name: "intersecting match",
name: "no match",
args: args[testOccurrence]{
occurrences: []testOccurrence{
{Ints: []int32{1, 0, 3}},
Expand All @@ -58,43 +59,30 @@ func Test_findIntersectingOccurrences(t *testing.T) {
},
search: scip.NewRangeUnchecked([]int32{1, 4, 6}),
},
want: []testOccurrence{
{Ints: []int32{1, 3, 5}},
{Ints: []int32{1, 5, 7}},
},
want: []testOccurrence{},
},
{
name: "encompassing match",
name: "multi match",
args: args[testOccurrence]{
occurrences: []testOccurrence{
{Ints: []int32{1, 0, 3}},
{Ints: []int32{1, 3, 7}},
{Ints: []int32{1, 3, 7}},
{Ints: []int32{1, 3, 7}},
{Ints: []int32{1, 7, 10}},
},
search: scip.NewRangeUnchecked([]int32{1, 4, 6}),
search: scip.NewRangeUnchecked([]int32{1, 3, 7}),
},
want: []testOccurrence{
{Ints: []int32{1, 3, 7}},
},
},
{
name: "contained match",
args: args[testOccurrence]{
occurrences: []testOccurrence{
{Ints: []int32{1, 0, 1}},
{Ints: []int32{1, 3, 5}},
{Ints: []int32{1, 7, 10}},
},
search: scip.NewRangeUnchecked([]int32{1, 2, 6}),
},
want: []testOccurrence{
{Ints: []int32{1, 3, 5}},
{Ints: []int32{1, 3, 7}},
{Ints: []int32{1, 3, 7}},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := findIntersectingOccurrences(tt.args.occurrences, tt.args.search)
got := findOccurrencesWithEqualRange(tt.args.occurrences, tt.args.search)
if diff := cmp.Diff(got, tt.want); diff != "" {
t.Errorf("unexpected ranges (-want +got):\n%s", diff)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/codeintel/codenav/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,7 @@ func (s *Service) getSyntacticSymbolsAtRange(
var parseFail *scip.Occurrence = nil

// FIXME(issue: GRAPH-674): Properly handle different text encodings here.
for _, occurrence := range findIntersectingOccurrences(doc.Occurrences, scipSymbolRange) {
for _, occurrence := range findOccurrencesWithEqualRange(doc.Occurrences, scipSymbolRange) {
parsedSymbol, err := scip.ParseSymbol(occurrence.Symbol)
if err != nil {
parseFail = occurrence
Expand Down

0 comments on commit d605278

Please sign in to comment.