Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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