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

Commit 308624f

Browse files
[Backport 5.5.x] Context: return lines around symbol match (#63788)
This PR fixes an important bug in #62976, where we didn&#39;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 <julietibs@apache.org>
1 parent 174c08c commit 308624f

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

cmd/frontend/internal/codycontext/context.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -351,23 +351,18 @@ func addLimitsAndFilter(plan *search.Inputs, filter fileMatcher, args GetContext
351351
}
352352

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

365-
// To provide some context variety, we just use the top-ranked
366-
// chunk (the first chunk) from each file
367-
368-
// 5 lines of leading context, clamped to zero
369-
startLine := max(0, fm.ChunkMatches[0].ContentStart.Line-5)
370-
371366
return FileChunkContext{
372367
RepoName: fm.Repo.Name,
373368
RepoID: fm.Repo.ID,

cmd/frontend/internal/codycontext/context_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,40 @@ func TestFileMatchToContextMatches(t *testing.T) {
6464
StartLine: 85,
6565
},
6666
},
67+
{
68+
// With symbol match returns context around first symbol
69+
fileMatch: &result.FileMatch{
70+
File: result.File{
71+
Path: "main.go",
72+
CommitID: "abc123",
73+
Repo: types.MinimalRepo{
74+
Name: "repo",
75+
ID: 1,
76+
},
77+
},
78+
Symbols: []*result.SymbolMatch{
79+
{
80+
Symbol: result.Symbol{
81+
Line: 23,
82+
Name: "symbol",
83+
},
84+
},
85+
{
86+
Symbol: result.Symbol{
87+
Line: 37,
88+
Name: "symbol",
89+
},
90+
},
91+
},
92+
},
93+
want: FileChunkContext{
94+
RepoName: "repo",
95+
RepoID: 1,
96+
CommitID: "abc123",
97+
Path: "main.go",
98+
StartLine: 18,
99+
},
100+
},
67101
}
68102

69103
for _, tc := range cases {

0 commit comments

Comments
 (0)