@@ -1376,25 +1376,29 @@ function FileResultCard({ group, hideFileNavigator }: { group: FileGroup; hideFi
13761376 const beforeStartLine = matchLine - beforeLines . length ;
13771377 const afterStartLine = matchLine + 1 ;
13781378
1379+ // Check next group to cap after-context before the next match line
1380+ const nextGroup = groupIndex < lineGroups . length - 1 ? lineGroups [ groupIndex + 1 ] : null ;
1381+ const afterLinesToShow = nextGroup
1382+ ? Math . min ( afterLines . length , Math . max ( 0 , nextGroup . line - matchLine - 1 ) )
1383+ : afterLines . length ;
1384+ const filteredAfterLines = afterLines . slice ( 0 , afterLinesToShow ) ;
1385+
13791386 // Check previous group to avoid duplicating context lines
13801387 const prevGroup = groupIndex > 0 ? lineGroups [ groupIndex - 1 ] : null ;
13811388 const prevResult = prevGroup ? prevGroup . matches [ 0 ] : null ;
1382- const prevMatchEndLine = prevResult ? prevResult . line + ( prevResult . context ?. after ?. length || 0 ) : 0 ;
1383-
1384- // Calculate how many before lines to skip
1385- const beforeLinesToSkip = prevResult ? Math . max ( 0 , prevMatchEndLine - beforeStartLine + 1 ) : 0 ;
1389+ // Calculate how many after-context lines the previous group actually displayed
1390+ const prevAfterLength = prevResult ?. context ?. after ?. length || 0 ;
1391+ const prevAfterShown = prevGroup
1392+ ? Math . min ( prevAfterLength , Math . max ( 0 , matchLine - prevGroup . line - 1 ) )
1393+ : 0 ;
1394+ const prevDisplayedEndLine = prevGroup ? prevGroup . line + prevAfterShown : 0 ;
1395+
1396+ // Calculate how many before lines to skip (already shown by previous group)
1397+ const beforeLinesToSkip = prevGroup ? Math . max ( 0 , prevDisplayedEndLine - beforeStartLine + 1 ) : 0 ;
13861398 const filteredBeforeLines = beforeLines . slice ( beforeLinesToSkip ) ;
13871399 const filteredBeforeStartLine = beforeStartLine + beforeLinesToSkip ;
13881400
1389- // Check next group for after-context overlap
1390- const nextGroup = groupIndex < lineGroups . length - 1 ? lineGroups [ groupIndex + 1 ] : null ;
1391- const nextResult = nextGroup ? nextGroup . matches [ 0 ] : null ;
1392- const nextMatchBeforeStartLine = nextResult ? nextResult . line - ( nextResult . context ?. before ?. length || 0 ) : Infinity ;
1393-
1394- const afterLinesToShow = nextResult ? Math . max ( 0 , nextMatchBeforeStartLine - afterStartLine ) : afterLines . length ;
1395- const filteredAfterLines = afterLines . slice ( 0 , afterLinesToShow ) ;
1396-
1397- const needsSeparator = prevResult && filteredBeforeStartLine > prevMatchEndLine + 1 ;
1401+ const needsSeparator = prevResult && filteredBeforeStartLine > prevDisplayedEndLine + 1 ;
13981402
13991403 const getLineLink = ( line : number ) => {
14001404 if ( repoId && ! hideFileNavigator ) {
0 commit comments