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

Commit

Permalink
[Backport 5.5.x] Context: return lines around symbol match (#63788)
Browse files Browse the repository at this point in the history
This PR fixes an important bug in #62976, where we didn't properly
map the
symbol line match to the return type. Instead, we accidentally treated
symbol
matches like file matches and returned the start of the file.

## Test plan

Add new unit test for symbol match conversion. Extensive manual testing.
<br> Backport 004eb0f from #63773

Co-authored-by: Julie Tibshirani <[email protected]>
  • Loading branch information
sourcegraph-release-bot and jtibshirani authored Jul 11, 2024
1 parent 174c08c commit 308624f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
23 changes: 9 additions & 14 deletions cmd/frontend/internal/codycontext/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,23 +351,18 @@ func addLimitsAndFilter(plan *search.Inputs, filter fileMatcher, args GetContext
}

func fileMatchToContextMatch(fm *result.FileMatch) FileChunkContext {
if len(fm.ChunkMatches) == 0 {
var startLine int
if len(fm.Symbols) != 0 {
startLine = max(0, fm.Symbols[0].Symbol.Line-5) // 5 lines of leading context, clamped to zero
} else if len(fm.ChunkMatches) != 0 {
// To provide some context variety, we just use the top-ranked
// chunk (the first chunk) from each file match.
startLine = max(0, fm.ChunkMatches[0].ContentStart.Line-5) // 5 lines of leading context, clamped to zero
} else {
// If this is a filename-only match, return a single chunk at the start of the file
return FileChunkContext{
RepoName: fm.Repo.Name,
RepoID: fm.Repo.ID,
CommitID: fm.CommitID,
Path: fm.Path,
StartLine: 0,
}
startLine = 0
}

// To provide some context variety, we just use the top-ranked
// chunk (the first chunk) from each file

// 5 lines of leading context, clamped to zero
startLine := max(0, fm.ChunkMatches[0].ContentStart.Line-5)

return FileChunkContext{
RepoName: fm.Repo.Name,
RepoID: fm.Repo.ID,
Expand Down
34 changes: 34 additions & 0 deletions cmd/frontend/internal/codycontext/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,40 @@ func TestFileMatchToContextMatches(t *testing.T) {
StartLine: 85,
},
},
{
// With symbol match returns context around first symbol
fileMatch: &result.FileMatch{
File: result.File{
Path: "main.go",
CommitID: "abc123",
Repo: types.MinimalRepo{
Name: "repo",
ID: 1,
},
},
Symbols: []*result.SymbolMatch{
{
Symbol: result.Symbol{
Line: 23,
Name: "symbol",
},
},
{
Symbol: result.Symbol{
Line: 37,
Name: "symbol",
},
},
},
},
want: FileChunkContext{
RepoName: "repo",
RepoID: 1,
CommitID: "abc123",
Path: "main.go",
StartLine: 18,
},
},
}

for _, tc := range cases {
Expand Down

0 comments on commit 308624f

Please sign in to comment.