@@ -314,18 +314,20 @@ private void PerformSearch(bool searchUp, bool searchFrames)
314314 if ( string . IsNullOrEmpty ( searchText ) || searchText == SearchDefaultText ) return ;
315315
316316 FiddlerApplication . UI . SetStatusText ( $ "Searching for { searchText } ") ;
317+ var includeRoot = false ; // Don't include the currently selected node for a seach
317318 var startNode = mapiTreeView ? . SelectedNode ;
318319 if ( startNode == null && mapiTreeView ? . Nodes . Count > 0 )
319320 {
320321 startNode = mapiTreeView . Nodes [ 0 ] ;
322+ includeRoot = true ; // But if no node was selected, incude whatever node we start from
321323 }
322324
323325 if ( startNode != null )
324326 {
325327 var nodes = mapiTreeView . Nodes ;
326328 TreeNode foundNode = searchUp
327- ? FindPrevNode ( nodes , startNode , searchText , ! searchFrames )
328- : FindNextNode ( nodes , startNode , searchText , ! searchFrames ) ;
329+ ? FindPrevNode ( nodes , startNode , searchText , ! searchFrames , includeRoot )
330+ : FindNextNode ( nodes , startNode , searchText , ! searchFrames , includeRoot ) ;
329331
330332 if ( foundNode != null )
331333 {
@@ -338,6 +340,7 @@ private void PerformSearch(bool searchUp, bool searchFrames)
338340
339341 if ( searchFrames )
340342 {
343+ includeRoot = true ; // Now that we're searching new frames, always include the root node in our search
341344 var currentSession = Inspector . session ;
342345 while ( currentSession != null )
343346 {
@@ -348,8 +351,8 @@ private void PerformSearch(bool searchUp, bool searchFrames)
348351 var node = BaseStructure . AddBlock ( parseResult , false ) ;
349352 rootNode . Nodes . Add ( node ) ;
350353 var foundMatch = searchUp
351- ? FindPrevNode ( rootNode . Nodes , node , searchText , true )
352- : FindNextNode ( rootNode . Nodes , node , searchText , true ) ;
354+ ? FindPrevNode ( rootNode . Nodes , node , searchText , true , includeRoot )
355+ : FindNextNode ( rootNode . Nodes , node , searchText , true , includeRoot ) ;
353356 if ( foundMatch != null )
354357 {
355358 FiddlerApplication . UI . SelectSessionsMatchingCriteria ( s => s . id == nextSession . id ) ;
@@ -365,63 +368,84 @@ private void PerformSearch(bool searchUp, bool searchFrames)
365368 }
366369
367370 // Find next node (downwards, wraps around)
368- private TreeNode FindNextNode ( TreeNodeCollection nodes , TreeNode startNode , string searchText , bool wrap )
371+ private TreeNode FindNextNode ( TreeNodeCollection nodes , TreeNode startNode , string searchText , bool wrap , bool matchStartNode )
369372 {
370- bool foundStart = false ;
371- TreeNode firstMatch = null ;
372- foreach ( var node in FlattenNodes ( nodes ) )
373+ var allNodes = FlattenNodes ( nodes ) ;
374+ int startIndex = 0 ;
375+ for ( int i = 0 ; i < allNodes . Count ; i ++ )
373376 {
374- if ( node == startNode )
377+ if ( allNodes [ i ] == startNode )
375378 {
376- foundStart = true ;
377- continue ;
379+ startIndex = i ;
380+ break ;
378381 }
379-
380- if ( GetNodeText ( node ) . IndexOf ( searchText , StringComparison . OrdinalIgnoreCase ) >= 0 )
382+ }
383+ // Search from startNode (inclusive/exclusive) to end
384+ int first = matchStartNode ? startIndex : startIndex + 1 ;
385+ for ( int i = first ; i < allNodes . Count ; i ++ )
386+ {
387+ if ( GetNodeText ( allNodes [ i ] ) . IndexOf ( searchText , StringComparison . OrdinalIgnoreCase ) >= 0 )
388+ return allNodes [ i ] ;
389+ }
390+ // If wrap is enabled, search from beginning up to startNode (exclusive)
391+ if ( wrap )
392+ {
393+ for ( int i = 0 ; i < startIndex ; i ++ )
381394 {
382- if ( foundStart )
383- return node ;
384- if ( firstMatch == null )
385- firstMatch = node ;
395+ if ( GetNodeText ( allNodes [ i ] ) . IndexOf ( searchText , StringComparison . OrdinalIgnoreCase ) >= 0 )
396+ return allNodes [ i ] ;
386397 }
387398 }
388-
389- return wrap ? firstMatch : null ;
399+ return null ;
390400 }
391401
392402 // Find previous node (upwards, wraps around)
393- private TreeNode FindPrevNode ( TreeNodeCollection nodes , TreeNode startNode , string searchText , bool wrap )
403+ private TreeNode FindPrevNode ( TreeNodeCollection nodes , TreeNode startNode , string searchText , bool wrap , bool matchStartNode )
394404 {
395- TreeNode lastMatch = null ;
396- foreach ( var node in FlattenNodes ( nodes ) )
405+ var allNodes = FlattenNodes ( nodes ) ;
406+ int startIndex = 0 ;
407+ for ( int i = 0 ; i < allNodes . Count ; i ++ )
397408 {
398- if ( node == startNode )
409+ if ( allNodes [ i ] == startNode )
410+ {
411+ startIndex = i ;
399412 break ;
400- if ( GetNodeText ( node ) . IndexOf ( searchText , StringComparison . OrdinalIgnoreCase ) >= 0 )
401- lastMatch = node ;
413+ }
402414 }
403-
404- if ( lastMatch == null && wrap )
415+ // Search backwards from startNode (inclusive/exclusive) to beginning
416+ int first = matchStartNode ? startIndex : startIndex - 1 ;
417+ for ( int i = first ; i >= 0 ; i -- )
405418 {
406- foreach ( var node in FlattenNodes ( nodes ) )
419+ if ( GetNodeText ( allNodes [ i ] ) . IndexOf ( searchText , StringComparison . OrdinalIgnoreCase ) >= 0 )
420+ return allNodes [ i ] ;
421+ }
422+ // If wrap is enabled, search from end down to startNode (exclusive)
423+ if ( wrap )
424+ {
425+ for ( int i = allNodes . Count - 1 ; i > startIndex ; i -- )
407426 {
408- if ( GetNodeText ( node ) . IndexOf ( searchText , StringComparison . OrdinalIgnoreCase ) >= 0 )
409- lastMatch = node ;
427+ if ( GetNodeText ( allNodes [ i ] ) . IndexOf ( searchText , StringComparison . OrdinalIgnoreCase ) >= 0 )
428+ return allNodes [ i ] ;
410429 }
411430 }
412-
413- return lastMatch ;
431+ return null ;
414432 }
415433
416- // Helper: flatten all nodes in tree (preorder)
417- private System . Collections . Generic . IEnumerable < TreeNode > FlattenNodes ( TreeNodeCollection nodes )
434+ // Helper: flatten all nodes in tree (preorder) and return as a list
435+ private System . Collections . Generic . List < TreeNode > FlattenNodes ( TreeNodeCollection nodes )
418436 {
419- foreach ( TreeNode node in nodes )
437+ var result = new System . Collections . Generic . List < TreeNode > ( ) ;
438+ var stack = new System . Collections . Generic . Stack < TreeNode > ( ) ;
439+ for ( int i = nodes . Count - 1 ; i >= 0 ; i -- )
440+ stack . Push ( nodes [ i ] ) ;
441+ while ( stack . Count > 0 )
420442 {
421- yield return node ;
422- foreach ( var child in FlattenNodes ( node . Nodes ) )
423- yield return child ;
443+ var node = stack . Pop ( ) ;
444+ result . Add ( node ) ;
445+ for ( int i = node . Nodes . Count - 1 ; i >= 0 ; i -- )
446+ stack . Push ( node . Nodes [ i ] ) ;
424447 }
448+ return result ;
425449 }
426450
427451 private void AttachHexBoxKeyboardHandler ( Be . Windows . Forms . HexBox hexBox )
0 commit comments