Skip to content

Add in-place question shuffle feature (no more buffer clutter!) #188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

MandalTuhin
Copy link

This PR adds a “shuffle” feature to LeetCode.nvim, allowing users to load a new random question in the same buffer, with the question info panel updating as well. Previously, using the random feature would open a new buffer each time, requiring manual cleanup. Now, users can quickly browse random questions—just like the shuffle button on the LeetCode website!

Key changes:

  • Adds a shuffle() method to the Question object
  • Updates the code and info buffers in-place
  • Keymap example: ls to shuffle in the current question buffer

Benefits:

  • Much faster random question browsing
  • No buffer clutter

I’d love feedback and am happy to make changes as needed!

I have made changes to the questions ui.
This commit tries to fix the error shown by nvim when trying to shuffle
questions.
This commit attempts to fix the random question functionality of
leetcode.nvim.
This puts the function at the bottom of the question.lua so that it
doesn't interfere with other files.
This commit tries to reload the description buffer/panel with each
shuffle to correctly represent the problem we are currently in.
Adds a new `:Leet shuffle` command that allows users to quickly load a
new random problem in the current buffer.

This feature mimics the behavior of the shuffle button on the LeetCode
website, providing a seamless way to practice different problems without
navigating back to the main menu or opening a new tab.

The implementation includes:
- A new `Question:shuffle()` method to handle fetching the new problem,
updating the buffer state, and recreating the UI.
- Correct buffer file path management (`nvim_buf_set_name`) to ensure
state consistency and prevent saving work to the wrong file.
- Proper unmounting and remounting of UI components (Description, Info,
Console).

fix(ui): resolve memory leak in console unmount

Additionally, this commit fixes a memory leak in the console layout. The
`unmount` function was incorrectly creating new UI popups instead of
destroying the existing ones. This has been corrected to prevent
orphaned windows from accumulating during operations like shuffling.
This commit addresses two follow-up issues discovered after the initial
implementation of the question shuffle feature:

1.  The `:Leet shuffle` command was not working due to a bug in the
command parser that incorrectly handled single-word commands. The parser
has been made more robust to correctly execute the command.

2.  Shuffling a question caused LSP errors (e.g., from `clangd`) because
the language server would not recognize the buffer's new file path. A
`BufRead` autocommand is now triggered after shuffling to force the LSP
to re-synchronize with the buffer.
This commit provides a definitive fix for LSP errors that occurred
during a question shuffle.

Previously, language servers like `clangd` could enter a confused state,
resulting in "non-added document" errors. The root cause was that the
LSP client was not properly detached before the buffer's underlying file
path was changed.

The new implementation resolves this by:
- Explicitly stopping the active LSP client before any modifications.
- Re-triggering LSP attachment via `BufRead` only after the shuffle is
complete.

This ensures a clean lifecycle for the LSP session, making the shuffle
feature stable and error-free.
q
A typo (`cmds1` instead of `cmds[1]`) was introduced in the command
execution logic, causing a Lua error on startup and preventing the
plugin from loading.

This commit corrects the typo, restoring the plugin's functionality.
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