@@ -311,27 +311,50 @@ export const handleCodeSearch: ToolHandler<'code_search'> = async (
311311 let currentFile : string | null = null
312312
313313 for ( const line of lines ) {
314- // Ripgrep output format: filename:line_number:content or filename:content
315- const colonIndex = line . indexOf ( ':' )
316- if ( colonIndex === - 1 ) {
317- // This shouldn't happen with standard ripgrep output
318- if ( currentFile ) {
319- fileGroups . get ( currentFile ) ! . push ( line )
320- }
314+ // Skip separator lines between result groups
315+ if ( line === '--' ) {
321316 continue
322317 }
323318
324- const filename = line . substring ( 0 , colonIndex )
319+ // Ripgrep output format:
320+ // - Match lines: filename:line_number:content
321+ // - Context lines (with -A/-B/-C flags): filename-line_number-content
322+
323+ // Use regex to find the pattern: separator + digits + separator
324+ // This handles filenames with hyphens/colons by matching the line number pattern
325+ let separatorIndex = - 1
326+ let filename = ''
327+
328+ // Try match line pattern: filename:digits:content
329+ const matchLinePattern = / ( .* ?) : ( \d + ) : ( .* ) $ /
330+ const matchLineMatch = line . match ( matchLinePattern )
331+ if ( matchLineMatch ) {
332+ filename = matchLineMatch [ 1 ]
333+ separatorIndex = matchLineMatch [ 1 ] . length
334+ } else {
335+ // Try context line pattern: filename-digits-content
336+ const contextLinePattern = / ( .* ?) - ( \d + ) - ( .* ) $ /
337+ const contextLineMatch = line . match ( contextLinePattern )
338+ if ( contextLineMatch ) {
339+ filename = contextLineMatch [ 1 ]
340+ separatorIndex = contextLineMatch [ 1 ] . length
341+ }
342+ }
343+
344+ if ( separatorIndex === - 1 ) {
345+ // Malformed line, skip it
346+ continue
347+ }
325348
326- // Check if this is a new file
349+ // Check if this is a valid filename (not indented, not containing tabs)
327350 if ( filename && ! filename . includes ( '\t' ) && ! filename . startsWith ( ' ' ) ) {
328351 currentFile = filename
329352 if ( ! fileGroups . has ( currentFile ) ) {
330353 fileGroups . set ( currentFile , [ ] )
331354 }
332355 fileGroups . get ( currentFile ) ! . push ( line )
333356 } else if ( currentFile ) {
334- // Continuation of previous result
357+ // This shouldn't happen with proper ripgrep output
335358 fileGroups . get ( currentFile ) ! . push ( line )
336359 }
337360 }
0 commit comments