Skip to content

list: fix RemoveItem using wrong index when a filter is active#970

Open
c-tonneslan wants to merge 1 commit into
charmbracelet:mainfrom
c-tonneslan:fix/remove-item-filter-index
Open

list: fix RemoveItem using wrong index when a filter is active#970
c-tonneslan wants to merge 1 commit into
charmbracelet:mainfrom
c-tonneslan:fix/remove-item-filter-index

Conversation

@c-tonneslan
Copy link
Copy Markdown

Fixes #632.

When a filter is active, the filtered list and the global items list have different 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 that shows [bar, baz], calling RemoveItem(0) would remove foo (global index 0) instead of bar (filtered index 0, global index 1). When the filter was then cleared, bar would still be present and foo would be gone.

The fix is pretty simple: when filtering is active, look up the real global index via filteredItems[index].index before removing from m.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.

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.
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.

Inconsistent & potentially misleading behaviour when calling List RemoveItem

1 participant