From 308624f144b34ac2df321fa2e806c5c7549900aa Mon Sep 17 00:00:00 2001
From: Release Bot <107104610+sourcegraph-release-bot@users.noreply.github.com>
Date: Thu, 11 Jul 2024 13:14:52 -0700
Subject: [PATCH] [Backport 5.5.x] Context: return lines around symbol match
(#63788)
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.
Backport 004eb0fd830755376c8ee6b895a814d79bd8f21b from #63773
Co-authored-by: Julie Tibshirani
---
cmd/frontend/internal/codycontext/context.go | 23 +++++--------
.../internal/codycontext/context_test.go | 34 +++++++++++++++++++
2 files changed, 43 insertions(+), 14 deletions(-)
diff --git a/cmd/frontend/internal/codycontext/context.go b/cmd/frontend/internal/codycontext/context.go
index 85263064a973..9615c0e4ee66 100644
--- a/cmd/frontend/internal/codycontext/context.go
+++ b/cmd/frontend/internal/codycontext/context.go
@@ -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,
diff --git a/cmd/frontend/internal/codycontext/context_test.go b/cmd/frontend/internal/codycontext/context_test.go
index 68c7a451ffae..5a89de66b618 100644
--- a/cmd/frontend/internal/codycontext/context_test.go
+++ b/cmd/frontend/internal/codycontext/context_test.go
@@ -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 {