list: fix RemoveItem using wrong index when a filter is active#970
Open
c-tonneslan wants to merge 1 commit into
Open
list: fix RemoveItem using wrong index when a filter is active#970c-tonneslan wants to merge 1 commit into
c-tonneslan wants to merge 1 commit into
Conversation
When a filter is active, the filtered list and the global list have different item orderings. RemoveItem was using the caller's index for both, which meant it removed the wrong item from the global list. For example, with items [foo, bar, baz] and a filter showing [bar, baz], calling RemoveItem(0) would remove foo (global index 0) instead of bar (filtered index 0, global index 1). Fix: when filtering is active, use the global index stored in filteredItems[index].index to remove the correct item from m.items. Also decrement the stored global indices of any remaining filtered items that came after the removed item, keeping them consistent. Fixes charmbracelet#632.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #632.
When a filter is active, the filtered list and the global items list have different orderings.
RemoveItemwas using the caller's index for both, which meant it removed the wrong item from the global list.For example, with items
[foo, bar, baz]and a filter that shows[bar, baz], callingRemoveItem(0)would removefoo(global index 0) instead ofbar(filtered index 0, global index 1). When the filter was then cleared,barwould still be present andfoowould be gone.The fix is pretty simple: when filtering is active, look up the real global index via
filteredItems[index].indexbefore removing fromm.items. After the removal, decrement the stored global indices of any remaining filtered items that pointed past the removed position, so they stay consistent.Added two regression tests that cover both the wrong-item bug and the case where removing all filtered items eventually resets the filter state correctly.