Skip to content
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 sorting in command palette search hits #4994

Merged
merged 7 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Fixed `RichLog` writing at wrong width when `write` occurs before width is known (e.g. in `compose` or `on_mount`) https://github.com/Textualize/textual/pull/4978
- Fixed `RichLog.write` incorrectly shrinking width to `RichLog.min_width` when `shrink=True` (now shrinks to fit content area instead) https://github.com/Textualize/textual/pull/4978
- Fixed flicker when setting `dark` reactive on startup https://github.com/Textualize/textual/pull/4989
- Fixed command palette not sorting search results by their match score https://github.com/Textualize/textual/pull/4994

## [0.79.1] - 2024-08-31

Expand Down
29 changes: 19 additions & 10 deletions src/textual/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,35 +312,35 @@ async def shutdown(self) -> None:
@rich.repr.auto
@total_ordering
class Command(Option):
"""Class that holds a command in the [`CommandList`][textual.command.CommandList]."""
"""Class that holds a hit in the [`CommandList`][textual.command.CommandList]."""

def __init__(
self,
prompt: RenderableType,
command: DiscoveryHit | Hit,
hit: DiscoveryHit | Hit,
id: str | None = None,
disabled: bool = False,
) -> None:
"""Initialise the option.

Args:
prompt: The prompt for the option.
command: The details of the command associated with the option.
hit: The details of the hit associated with the option.
id: The optional ID for the option.
disabled: The initial enabled/disabled state. Enabled by default.
"""
super().__init__(prompt, id, disabled)
self.command = command
"""The details of the command associated with the option."""
self.hit = hit
"""The details of the hit associated with the option."""

def __lt__(self, other: object) -> bool:
if isinstance(other, Command):
return self.command < other.command
return self.hit < other.hit
return NotImplemented

def __eq__(self, other: object) -> bool:
if isinstance(other, Command):
return self.command == other.command
return self.hit == other.hit
return NotImplemented


Expand Down Expand Up @@ -891,7 +891,15 @@ def _refresh_command_list(
if command_list.highlighted is not None and not clear_current
else None
)
command_list.clear_options().add_options(commands)

def sort_key(command: Command) -> float:
darrenburns marked this conversation as resolved.
Show resolved Hide resolved
# Only sort by score if it exists, otherwise do no sorting.
# This ensures we only sort Hits, and not DiscoveryHits.
score = getattr(command.hit, "score", 0)
return -score

sorted_commands = sorted(commands, key=sort_key)
command_list.clear_options().add_options(sorted_commands)
if highlighted is not None and highlighted.id:
command_list.highlighted = command_list.get_option_index(highlighted.id)

Expand Down Expand Up @@ -1061,8 +1069,9 @@ def _select_command(self, event: OptionList.OptionSelected) -> None:
input = self.query_one(CommandInput)
with self.prevent(Input.Changed):
assert isinstance(event.option, Command)
input.value = str(event.option.command.text)
self._selected_command = event.option.command
hit = event.option.hit
input.value = str(hit.text)
self._selected_command = hit
input.action_end()
self._list_visible = False
self.query_one(CommandList).clear_options()
Expand Down
Loading
Loading