-
Notifications
You must be signed in to change notification settings - Fork 844
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
fix(listview): update index after items removed #5135
Merged
darrenburns
merged 19 commits into
Textualize:main
from
TomJGooding:fix-listview-update-index-after-items-removed
Nov 28, 2024
+143
−12
Merged
Changes from 12 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
4bd7edf
fix(listview): update index after pop
TomJGooding 2abce6d
tests(listview): import future for type hints
TomJGooding 643c2d9
ensure pop error is original index rather than normalized
TomJGooding 5a8dd24
fix(listview): update index after remove_items
TomJGooding 6d572c4
Merge branch 'main' into fix-listview-update-index-after-items-removed
TomJGooding d85fdec
update changelog
TomJGooding 0a12640
Merge branch 'main' into fix-listview-update-index-after-items-removed
TomJGooding 68e205e
reinstate always_update to index reactive
TomJGooding 336a954
Revert "reinstate always_update to index reactive"
TomJGooding d330ecd
handle unchanged index without always_update
TomJGooding 014bba1
Merge branch 'main' into fix-listview-update-index-after-items-removed
TomJGooding 7287f33
update changelog
TomJGooding c51d4eb
Merge branch 'main' into fix-listview-update-index-after-items-removed
TomJGooding 9a7d30b
update changelog
TomJGooding 9314cde
add docstrings
TomJGooding 5f8123e
update changelog
TomJGooding fc36e21
Merge branch 'main' into fix-listview-update-index-after-items-removed
willmcgugan 25901b7
Merge branch 'main' into fix-listview-update-index-after-items-removed
darrenburns e507eb3
Merge branch 'main' into fix-listview-update-index-after-items-removed
darrenburns File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
from typing_extensions import TypeGuard | ||
|
||
from textual._loop import loop_from_index | ||
from textual.await_complete import AwaitComplete | ||
from textual.await_remove import AwaitRemove | ||
from textual.binding import Binding, BindingType | ||
from textual.containers import VerticalScroll | ||
|
@@ -275,7 +276,7 @@ def insert(self, index: int, items: Iterable[ListItem]) -> AwaitMount: | |
await_mount = self.mount(*items, before=index) | ||
return await_mount | ||
|
||
def pop(self, index: Optional[int] = None) -> AwaitRemove: | ||
def pop(self, index: Optional[int] = None) -> AwaitComplete: | ||
"""Remove last ListItem from ListView or | ||
Remove ListItem from ListView by index | ||
|
||
|
@@ -286,13 +287,30 @@ def pop(self, index: Optional[int] = None) -> AwaitRemove: | |
An awaitable that yields control to the event loop until | ||
the DOM has been updated to reflect item being removed. | ||
""" | ||
if index is None: | ||
await_remove = self.query("ListItem").last().remove() | ||
else: | ||
await_remove = self.query("ListItem")[index].remove() | ||
return await_remove | ||
|
||
def remove_items(self, indices: Iterable[int]) -> AwaitRemove: | ||
if len(self) == 0: | ||
raise IndexError("pop from empty list") | ||
|
||
index = index if index is not None else -1 | ||
item_to_remove = self.query("ListItem")[index] | ||
normalized_index = index if index >= 0 else index + len(self) | ||
|
||
async def do_pop(): | ||
await item_to_remove.remove() | ||
if self.index is not None: | ||
if normalized_index < self.index: | ||
self.index -= 1 | ||
elif normalized_index == self.index: | ||
old_index = self.index | ||
# Force a re-validation of the index | ||
self.index = self.index | ||
# If the index hasn't changed, the watcher won't be called | ||
# but we need to update the highlighted item | ||
if old_index == self.index: | ||
self.watch_index(old_index, self.index) | ||
|
||
return AwaitComplete(do_pop()) | ||
|
||
def remove_items(self, indices: Iterable[int]) -> AwaitComplete: | ||
"""Remove ListItems from ListView by indices | ||
|
||
Args: | ||
|
@@ -303,8 +321,28 @@ def remove_items(self, indices: Iterable[int]) -> AwaitRemove: | |
""" | ||
items = self.query("ListItem") | ||
items_to_remove = [items[index] for index in indices] | ||
await_remove = self.remove_children(items_to_remove) | ||
return await_remove | ||
normalized_indices = set( | ||
index if index >= 0 else index + len(self) for index in indices | ||
) | ||
|
||
async def do_remove_items(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. docstrings please |
||
await self.remove_children(items_to_remove) | ||
if self.index is not None: | ||
removed_before_highlighted = sum( | ||
1 for index in normalized_indices if index < self.index | ||
) | ||
if removed_before_highlighted: | ||
self.index -= removed_before_highlighted | ||
elif self.index in normalized_indices: | ||
old_index = self.index | ||
# Force a re-validation of the index | ||
self.index = self.index | ||
# If the index hasn't changed, the watcher won't be called | ||
# but we need to update the highlighted item | ||
if old_index == self.index: | ||
self.watch_index(old_index, self.index) | ||
|
||
return AwaitComplete(do_remove_items()) | ||
|
||
def action_select_cursor(self) -> None: | ||
"""Select the current item in the list.""" | ||
|
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docstring and typing por favor. Since its internal, and essentially private, the docstrings can be written for future Textual core-devs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added docstrings in 9314cde, but feel free to tweak!