From 8896eaef15157f3df136fce5fd20c6de4d8767ab Mon Sep 17 00:00:00 2001 From: Liam Hawkins Date: Wed, 16 Sep 2026 11:11:58 -0300 Subject: [PATCH 1/2] fix(explorer): select the highlighted filter match on Enter and center it While the explorer filter is open, arrow keys move the tree cursor, but Enter activated the node at _tree_filter_match_index, which only n/j advance. Enter therefore always opened the first match. Accept now uses the tree's cursor_node. After accepting, scroll the selected node to the vertical center of the explorer instead of leaving it on the bottom row. Fixes #333. --- .../domains/explorer/ui/mixins/tree_filter.py | 32 +++++---- .../test_filter_accept_cursor_pilot.py | 68 +++++++++++++++++++ tests/ui/explorer/test_tree_filter_search.py | 4 ++ 3 files changed, 89 insertions(+), 15 deletions(-) diff --git a/sqlit/domains/explorer/ui/mixins/tree_filter.py b/sqlit/domains/explorer/ui/mixins/tree_filter.py index 3a92727e..99fe98b7 100644 --- a/sqlit/domains/explorer/ui/mixins/tree_filter.py +++ b/sqlit/domains/explorer/ui/mixins/tree_filter.py @@ -109,21 +109,18 @@ def action_tree_filter_close(self: TreeFilterMixinHost) -> None: def action_tree_filter_accept(self: TreeFilterMixinHost) -> None: """Accept current filter selection, close filter, and activate the node.""" - # Remember the match's *data* (not the node reference) before closing. - # Closing the filter rebuilds the tree from the snapshot taken at - # filter-open time, which replaces every node object — so the - # reference we captured here would be stale after close. The data - # payload, however, is the same object on both old and new nodes - # (we pass it through unchanged in _restore_node_under), so we can - # re-locate the match by identity. + # Remember the highlighted node's *data* (not the node reference) + # before closing. Closing the filter rebuilds the tree from the + # snapshot taken at filter-open time, which replaces every node + # object, so the reference we captured here would be stale after + # close. The data payload, however, is the same object on both old + # and new nodes (we pass it through unchanged in _restore_node_under), + # so we can re-locate the node by identity. matched_data: Any = None - if ( - self._tree_filter_matches - and self._tree_filter_match_index < len(self._tree_filter_matches) - ): - current_node = self._tree_filter_matches[self._tree_filter_match_index] - if current_node and current_node.data: - matched_data = current_node.data + if self._tree_filter_matches: + cursor_node = self.object_tree.cursor_node + if cursor_node is not None and cursor_node.data: + matched_data = cursor_node.data # Close the filter self.action_tree_filter_close() @@ -149,8 +146,13 @@ def action_tree_filter_accept(self: TreeFilterMixinHost) -> None: self._select_and_activate_after_refresh(fresh_node) def _select_and_activate_after_refresh(self: TreeFilterMixinHost, node: Any) -> None: + tree = self.object_tree try: - self.object_tree.move_cursor(node) + tree.move_cursor(node) + tree.scroll_to( + y=node.line - tree.scrollable_content_region.height // 2, + animate=False, + ) except Exception: pass self._activate_tree_node(node) diff --git a/tests/ui/explorer/test_filter_accept_cursor_pilot.py b/tests/ui/explorer/test_filter_accept_cursor_pilot.py index f6eabc23..da366cdf 100644 --- a/tests/ui/explorer/test_filter_accept_cursor_pilot.py +++ b/tests/ui/explorer/test_filter_accept_cursor_pilot.py @@ -78,3 +78,71 @@ async def test_filter_accept_lands_cursor_on_matched_connection(): f"match. Expected 'test-server', got node with connection " f"name={name!r} (cursor.label={getattr(cursor, 'label', None)!r})." ) + + +@pytest.mark.asyncio +async def test_filter_accept_activates_arrow_highlighted_match(): + """Arrowing to a later match and pressing Enter selects that match, not the first one.""" + connections = [ + create_test_connection("warehouse-a", "sqlite"), + create_test_connection("warehouse-b", "sqlite"), + create_test_connection("warehouse-c", "sqlite"), + create_test_connection("other", "sqlite"), + ] + services = build_test_services( + connection_store=MockConnectionStore(connections), + settings_store=MockSettingsStore({"theme": "tokyo-night"}), + ) + app = SSMSTUI(services=services) + + async with app.run_test(size=(120, 40)) as pilot: + await pilot.pause() + app.action_focus_explorer() + await pilot.pause() + + await pilot.press("slash") + await pilot.pause() + for ch in "warehouse": + await pilot.press(ch) + await pilot.pause() + assert len(app._tree_filter_matches) == 3 + + await pilot.press("down", "down") + await pilot.pause() + assert _connection_name_of(app.object_tree.cursor_node) == "warehouse-c" + + await pilot.press("enter") + await pilot.pause() + + assert _connection_name_of(app.object_tree.cursor_node) == "warehouse-c" + + +@pytest.mark.asyncio +async def test_filter_accept_centers_selected_node(): + connections = [create_test_connection(f"conn-{i:02d}", "sqlite") for i in range(80)] + services = build_test_services( + connection_store=MockConnectionStore(connections), + settings_store=MockSettingsStore({"theme": "tokyo-night"}), + ) + app = SSMSTUI(services=services) + + async with app.run_test(size=(120, 40)) as pilot: + await pilot.pause() + app.action_focus_explorer() + await pilot.pause() + + await pilot.press("slash") + await pilot.pause() + for ch in "conn-60": + await pilot.press(ch) + await pilot.pause() + + await pilot.press("enter") + await pilot.pause() + + tree = app.object_tree + cursor = tree.cursor_node + assert _connection_name_of(cursor) == "conn-60" + height = tree.scrollable_content_region.height + assert tree.max_scroll_y > 0 + assert abs((cursor.line - tree.scroll_offset.y) - height // 2) <= 1 diff --git a/tests/ui/explorer/test_tree_filter_search.py b/tests/ui/explorer/test_tree_filter_search.py index d4e44f92..6cdf0d34 100644 --- a/tests/ui/explorer/test_tree_filter_search.py +++ b/tests/ui/explorer/test_tree_filter_search.py @@ -68,6 +68,10 @@ def select_node(self, node: MockTreeNode) -> None: def move_cursor(self, node: MockTreeNode) -> None: self.selected_node = node + @property + def cursor_node(self) -> MockTreeNode | None: + return self.selected_node + def focus(self) -> None: self.has_focus = True From cd4a8e0d5ab1f58c2f92f7acf278fee159b4b1f0 Mon Sep 17 00:00:00 2001 From: Oren Mittman Date: Sat, 10 Oct 2026 22:35:11 -0400 Subject: [PATCH 2/2] 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"