Skip to content

Conversation

@TimelordUK
Copy link
Owner

Summary

  • Introduces StateCoordinator to centralize state synchronization logic
  • Fixes vim search Escape key handling that was preventing proper search cancellation
  • Implements phased migration strategy to gradually move state logic out of TUI

Key Changes

  1. New StateCoordinator struct (src/ui/state_coordinator.rs)

    • Centralizes mode synchronization across AppStateContainer, Buffer, and ShadowState
    • Provides static delegation methods for incremental migration
    • Handles search completion vs cancellation properly
  2. Fixed Escape key handling

    • VimSearchAdapter was intercepting Escape and not properly clearing state
    • Now Escape falls through to proper handler using StateCoordinator
    • After search (Enter): n/N navigate matches
    • After Escape: search fully cleared, N toggles line numbers
  3. Search state management

    • complete_search_with_refs() - keeps matches active for n/N navigation after Enter
    • cancel_search_with_refs() - completely clears search on Escape
    • Separate handling for n and N keys based on search state

Testing

Verified the following flow works correctly:

  1. Press / to start vim search
  2. Type search term and press Enter
  3. n navigates to next match, N navigates to previous match
  4. Press Escape to clear search
  5. n does nothing, N toggles line numbers (correct behavior)

Next Steps

This PR sets up the foundation for further refactoring:

  • Continue migrating state synchronization logic to StateCoordinator
  • Eventually have StateCoordinator own all persistent state
  • Reduce TUI complexity by delegating all state management

🤖 Generated with Claude Code

TimelordUK and others added 5 commits August 30, 2025 09:16
…mode cleanup

- Added cancel_search and cancel_search_with_refs methods to StateCoordinator
- These methods properly synchronize all state when exiting search mode
- Updated search cancellation in TUI to use StateCoordinator
- Ensures AppStateContainer, Buffer, and ShadowState stay synchronized
- Fixes issue where Escape during search didn't properly restore state

This continues the StateCoordinator migration, centralizing complex state
synchronization logic and reducing coupling in the TUI.

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
…lures

- Implemented Default trait for AppStateContainer for testing purposes
- Uses CommandHistory::default() which handles file system errors gracefully
- Updated test_proxy_with_no_buffer to use AppStateContainer::default()
- This avoids file system access issues in test environments
- All 3 tests in test_buffer_state_refactor now pass

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
Two critical fixes for vim search mode synchronization:

1. When search is applied (Enter pressed), use sync_mode instead of direct
   set_mode to ensure all state containers stay synchronized

2. When Escape is pressed during vim search navigation:
   - Properly detect if vim search is navigating
   - Exit vim navigation mode
   - Clear search patterns
   - Use StateCoordinator to sync mode back to Results

This fixes the issue where after search + navigation (/, text, Enter, n, n, Escape),
the system would be in a confused state with Command mode active but Results
mode displayed, causing keypresses to append to SQL instead of navigate.

The root cause was that mode changes weren't going through StateCoordinator's
sync_mode method, causing AppStateContainer, Buffer, and ShadowState to become
desynchronized.

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
Major improvements to search state centralization:

1. Enhanced cancel_search_with_refs to handle ALL search cleanup:
   - Clears vim search adapter state
   - Clears search patterns
   - Syncs mode properly
   - Single point of control for search cancellation

2. Added should_handle_search_navigation method:
   - Centralizes logic for determining if n/N keys should navigate search
   - Prevents n/N from navigating when search is cleared
   - Fixes issue where N wouldn't toggle line numbers after search cancel

3. Updated TUI to delegate to StateCoordinator:
   - Search cancellation now fully handled by StateCoordinator
   - n/N key behavior determined by StateCoordinator
   - Reduces coupling and improves consistency

This fixes the long-standing issue where after canceling search with Escape,
the N key wouldn't revert to toggling line numbers. The problem was that
search state wasn't being fully cleared and checks were scattered across
multiple components.

Future work: Refactor StateCoordinator to own all state rather than using
static delegation methods.

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
- Fix Escape key not properly clearing search state after search completion
- VimSearchAdapter was intercepting Escape and not clearing AppStateContainer
- Now Escape falls through to handle_results_input for proper StateCoordinator handling
- Added complete_search_with_refs() to keep matches active for n/N after Enter
- Added cancel_search_with_refs() to completely clear search on Escape
- Split should_handle_next_match/previous_match for separate n/N behavior
- Enhanced logging to track Escape handling and state transitions

After search (Enter): n/N navigate matches
After Escape: search fully cleared, N toggles line numbers

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
@TimelordUK TimelordUK merged commit 82be731 into main Aug 30, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant