From 5ecc03bf0ad1f3ee0b5113a2abebeaec7a2505c0 Mon Sep 17 00:00:00 2001 From: Oren Mittman Date: Sat, 10 Oct 2026 22:35:11 -0400 Subject: [PATCH] fix(explorer): move the filter cursor to the first match after layout Tree.move_cursor reads node._line, which is only set during the next layout pass, not when add() runs. _jump_to_current_match called it right after each keystroke's snapshot restore, so the cursor stayed on line 0 (the connection). Now that Enter activates cursor_node, filtering to a table under a connection and pressing Enter selected the connection, and arrow presses counted from the connection row. Defer the move via call_after_refresh, as accept already does, and share that deferral in a _call_after_refresh helper. Refs #333. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../domains/explorer/ui/mixins/tree_filter.py | 34 ++++++----- .../test_filter_accept_cursor_pilot.py | 60 +++++++++++++++++++ 2 files changed, 80 insertions(+), 14 deletions(-) diff --git a/sqlit/domains/explorer/ui/mixins/tree_filter.py b/sqlit/domains/explorer/ui/mixins/tree_filter.py index 99fe98b7..c0114844 100644 --- a/sqlit/domains/explorer/ui/mixins/tree_filter.py +++ b/sqlit/domains/explorer/ui/mixins/tree_filter.py @@ -11,7 +11,7 @@ from sqlit.shared.ui.protocols import TreeFilterMixinHost if TYPE_CHECKING: - pass + from collections.abc import Callable @dataclass @@ -57,6 +57,23 @@ def _restore_node_under(parent: Any, snap: _NodeSnapshot) -> None: _restore_node_under(child, grandchild) +def _call_after_refresh(host: TreeFilterMixinHost, callback: Callable[[], None]) -> None: + """Run `callback` after the next refresh, once nodes added by a snapshot + restore have been laid out. + + Textual's Tree.move_cursor reads `node._line`, which is set during the + next layout pass — not when the node is `add()`-ed. Calling move_cursor + right after a snapshot restore sees stale `_line` values and parks the + cursor on the wrong row. + """ + call_after = getattr(host, "call_after_refresh", None) + if callable(call_after): + call_after(callback) + else: + # Synchronous fallback (used in unit tests with a mock host). + callback() + + class TreeFilterMixin: """Mixin providing tree filter functionality.""" @@ -132,18 +149,7 @@ def action_tree_filter_accept(self: TreeFilterMixinHost) -> None: if fresh_node is None: return - # Textual's Tree.move_cursor reads `node._line`, which is set during - # the next layout pass — not when the node is `add()`-ed. Since the - # snapshot restore that just ran in action_tree_filter_close added - # all fresh nodes synchronously, calling move_cursor right now sees - # stale `_line` values and parks the cursor on the wrong row. - # Defer the move (and the activation) until after the next refresh. - call_after = getattr(self, "call_after_refresh", None) - if callable(call_after): - call_after(lambda: self._select_and_activate_after_refresh(fresh_node)) - else: - # Synchronous fallback (used in unit tests with a mock host). - self._select_and_activate_after_refresh(fresh_node) + _call_after_refresh(self, lambda: self._select_and_activate_after_refresh(fresh_node)) def _select_and_activate_after_refresh(self: TreeFilterMixinHost, node: Any) -> None: tree = self.object_tree @@ -193,7 +199,7 @@ def _jump_to_current_match(self: TreeFilterMixinHost) -> None: # Expand ancestors to make node visible self._expand_ancestors(node) # Move cursor to node - self.object_tree.move_cursor(node) + _call_after_refresh(self, lambda: self.object_tree.move_cursor(node)) def _expand_ancestors(self: TreeFilterMixinHost, node: Any) -> None: """Expand all ancestor nodes to make a node visible.""" diff --git a/tests/ui/explorer/test_filter_accept_cursor_pilot.py b/tests/ui/explorer/test_filter_accept_cursor_pilot.py index da366cdf..0f652e81 100644 --- a/tests/ui/explorer/test_filter_accept_cursor_pilot.py +++ b/tests/ui/explorer/test_filter_accept_cursor_pilot.py @@ -10,6 +10,7 @@ import pytest +from sqlit.domains.explorer.domain.tree_nodes import FolderNode, TableNode from sqlit.domains.shell.app.main import SSMSTUI from ..mocks import ( @@ -146,3 +147,62 @@ async def test_filter_accept_centers_selected_node(): height = tree.scrollable_content_region.height assert tree.max_scroll_y > 0 assert abs((cursor.line - tree.scroll_offset.y) - height // 2) <= 1 + + +def _add_tables_under_first_connection(app: SSMSTUI, names: list[str]) -> None: + """Hang a Tables folder with the given tables under the first connection, + as if they had been lazy-loaded, so the matches sit below line 0.""" + connection = app.object_tree.root.children[0] + tables = connection.add("Tables", data=FolderNode(folder_type="tables"), expand=True) + for name in names: + tables.add_leaf(name, data=TableNode(database=None, schema="main", name=name)) + connection.expand() + + +def _nested_table_services(): + return build_test_services( + connection_store=MockConnectionStore([create_test_connection("warehouse", "sqlite")]), + settings_store=MockSettingsStore({"theme": "tokyo-night"}), + ) + + +@pytest.mark.asyncio +async def test_filter_moves_cursor_to_first_nested_match(): + """Typing a filter puts the cursor on the first match even when it is + nested, so Enter without arrowing selects that match.""" + app = SSMSTUI(services=_nested_table_services()) + + async with app.run_test(size=(120, 40)) as pilot: + await pilot.pause() + app.action_focus_explorer() + _add_tables_under_first_connection(app, ["customers", "orders_2023", "orders_2024"]) + await pilot.pause() + + await pilot.press("slash", *"orders") + await pilot.pause() + assert app.object_tree.cursor_node.label.plain == "orders_2023" + + await pilot.press("enter") + await pilot.pause() + assert app.object_tree.cursor_node.label.plain == "orders_2023" + + +@pytest.mark.asyncio +async def test_filter_accept_activates_arrow_highlighted_nested_match(): + app = SSMSTUI(services=_nested_table_services()) + + async with app.run_test(size=(120, 40)) as pilot: + await pilot.pause() + app.action_focus_explorer() + _add_tables_under_first_connection(app, ["customers", "orders_2023", "orders_2024", "orders_2025"]) + await pilot.pause() + + await pilot.press("slash", *"orders") + await pilot.pause() + await pilot.press("down", "down") + await pilot.pause() + assert app.object_tree.cursor_node.label.plain == "orders_2025" + + await pilot.press("enter") + await pilot.pause() + assert app.object_tree.cursor_node.label.plain == "orders_2025"