diff --git a/cmd/frontend/internal/codycontext/context.go b/cmd/frontend/internal/codycontext/context.go index 85263064a973c..9615c0e4ee662 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 68c7a451ffae7..5a89de66b6181 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 {