Skip to content

fix(ui): avoid reusing existing floats#69

Merged
y3owk1n merged 1 commit intomainfrom
no-reuse-floats
Apr 28, 2025
Merged

fix(ui): avoid reusing existing floats#69
y3owk1n merged 1 commit intomainfrom
no-reuse-floats

Conversation

@mtraworld
Copy link
Contributor

@mtraworld mtraworld commented Apr 28, 2025

Summary by CodeRabbit

  • Refactor
    • Improved window management throughout the UI to consistently close floating and split windows instead of just closing buffers.
    • Removed redundant window creation and simplified the logic for displaying and closing UI elements.
    • Unified the handling of window references for more reliable and predictable user interface behavior.

@coderabbitai
Copy link

coderabbitai bot commented Apr 28, 2025

Walkthrough

The changes refactor the internal window management logic across several modules. The handling of floating and split window creation and closure is updated to consistently use window handles rather than buffer handles. Keymaps and autocommands for closing UI elements now target windows directly, and redundant or duplicate floating window creation logic is removed. Additionally, the mechanism for reusing floating windows is eliminated, ensuring that each UI invocation creates a new window instance. No changes are made to public interfaces or function signatures; all modifications are internal to the implementation.

Changes

File(s) Change Summary
lua/time-machine/diff.lua Refactored preview_diff_native to create the floating window before setting keymaps, store the window handle, and ensure close operations target the window, not the buffer. Removed redundant floating window creation.
lua/time-machine/ui.lua Refactored M.show_tree, M.show_help, and M.show_log to manage window handles instead of buffer handles for closing UI. Window creation is moved earlier, close keymaps and autocommands now close windows, and redundant window creation is removed.
lua/time-machine/window.lua Removed the caching and reuse of floating windows by eliminating the native_float variable and associated logic. Now, each call to create a floating window always creates a new instance.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant UI_Module
    participant Window_Module

    User->>UI_Module: Trigger show_tree/show_help/show_log
    UI_Module->>Window_Module: create_native_float_win(bufnr, title)
    Window_Module-->>UI_Module: Returns new window handle
    UI_Module->>UI_Module: Set keymaps/autocommands to close window
    User->>UI_Module: Press close key
    UI_Module->>Window_Module: close_win(window_handle)
Loading

Poem

🐇
A window opens, floating free,
No more buffer closing spree.
Each view is fresh, each close precise,
With handles tracked, the code is nice.
Refactored paths, no more reuse—
A tidy warren, fit for muse!


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ffcd3be and 14ac0d8.

📒 Files selected for processing (3)
  • lua/time-machine/diff.lua (1 hunks)
  • lua/time-machine/ui.lua (5 hunks)
  • lua/time-machine/window.lua (0 hunks)
💤 Files with no reviewable changes (1)
  • lua/time-machine/window.lua
🔇 Additional comments (11)
lua/time-machine/diff.lua (2)

63-66: Good change to create the window reference upfront.

This change ensures the window handle is stored and accessible for proper cleanup later. This approach is more reliable as it directly targets the window for operations rather than indirectly accessing it through the buffer.


74-74: Improved window cleanup using window handle.

Changing from buffer-based closing to window-based closing with utils.close_win(win) is more direct and reliable. This aligns with the PR objective of better window lifecycle management.

lua/time-machine/ui.lua (9)

382-393: Good restructuring of window creation and cursor positioning.

Creating the window earlier in the function flow and immediately setting the cursor position to the current sequence improves the user experience by ensuring the relevant content is visible as soon as the window appears.


395-404: Properly preserving buffer variables.

This maintains the necessary state in the buffer variables, which is important for the UI functionality.


406-434: Improved autocommand for UI refreshing with better validation.

The autocommand now properly checks if the buffer is still valid before attempting to refresh, which prevents errors when the buffer has been closed.


432-433: Consistent window closure approach in autocommand.

Using utils.close_win(time_machine_win_id) in the autocommand ensures consistent window management throughout the codebase.


522-522: Improved window closing mechanism.

Changing from buffer-based closing to window-based closing with utils.close_win(time_machine_win_id) is more direct and prevents potential memory leaks.


632-633: Simplified help window creation.

Creating the window immediately and storing the handle improves code clarity and aligns with the consistent window management approach.


645-645: Consistent window closing in help UI.

Using utils.close_win(win) ensures proper cleanup of the window resource.


755-756: Streamlined log window creation.

This change follows the same pattern of immediate window creation and reference storage used in other UI components, maintaining consistency.


763-763: Consistent window closing in log UI.

Using utils.close_win(win) aligns with the window-centric approach to UI management used throughout the codebase.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@y3owk1n y3owk1n merged commit ce03a29 into main Apr 28, 2025
4 checks passed
@y3owk1n y3owk1n deleted the no-reuse-floats branch April 28, 2025 06:54
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.

2 participants