Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
11 changes: 9 additions & 2 deletions sqlit/shared/ui/widgets_autocomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,19 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
self.filter_text: str = ""

def set_items(self, items: list[str], filter_text: str = "") -> None:
"""Set the autocomplete items and filter."""
"""Set the autocomplete items and filter by any identifier component."""
self.items = items
self.filter_text = filter_text.lower()

if self.filter_text:
self.filtered_items = [item for item in items if item.lower().startswith(self.filter_text)]
self.filtered_items = [
item
for item in items
if any(
part.lower().startswith(self.filter_text)
for part in item.split(".")
)
][:50]
else:
self.filtered_items = items[:50] # Show more items with scrolling

Expand Down
19 changes: 19 additions & 0 deletions tests/ui/test_autocomplete_dropdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@ def compose(self) -> ComposeResult:
yield AutocompleteDropdown()


class TestAutocompleteDropdownFiltering:
"""Tests for preserving items selected by the completion engine."""

@pytest.mark.asyncio
async def test_filters_qualified_names_by_any_identifier_part(self) -> None:
"""Qualified names match both their schema and table components."""
app = _DropdownTestApp()
async with app.run_test() as pilot:
dropdown = app.query_one(AutocompleteDropdown)

dropdown.set_items(["Visualsation.Latency"], "latency")
await pilot.pause()
assert dropdown.filtered_items == ["Visualsation.Latency"]

dropdown.set_items(["Visualsation.Latency", "Visualsation.Other"], "Visualsation")
await pilot.pause()
assert dropdown.filtered_items == ["Visualsation.Latency", "Visualsation.Other"]


class TestAutocompleteDropdownWidth:
"""Tests for dynamic width sizing based on filtered items."""

Expand Down