Conversation
…dTabAndIndex
Pagination had seven methods (goToNextPage, goToPreviousPage, goToFirstPage, goToLastPage, updatePageSize, updateOffset, applyPaginationSettings) where six followed the same shape:
guard let tabIndex = tabManager.selectedTabIndex,
tabIndex < tabManager.tabs.count,
tabManager.tabs[tabIndex].pagination.<condition> else { return }
paginateAfterConfirmation(tabIndex: tabIndex) { pagination in
pagination.<action>()
}
Six guard prologues and six tail calls collapse into one-line bodies via:
- New QueryTabManager.selectedTabAndIndex helper that returns (tab, index) atomically with the bounds check, replacing the two-step (selectedTabIndex + bounds-check + tabs[index]) pattern.
- New private paginateIfPossible(where:mutate:) that captures the selected-tab + condition + paginateAfterConfirmation chain. Each public pagination method becomes a one-liner using a KeyPath or short closure for the precondition.
Behavior unchanged. -30 LOC in Pagination, +5 in QueryTabManager. Smoke-tested all six pagination actions plus offset Go.
selectedTabAndIndex is also a foothold for further coordinator dedup — there are ~14 other selectedTabIndex + bounds-check sites in MainContentCoordinator extensions that could migrate, but each has slightly different surrounding logic so they're left for separate focused PRs.
Four cases: - nilWhenNoSelection: empty manager returns nil - returnsSelectedTabAfterAdd: addTableTab autoselects the new tab; helper returns it with index 0 - nilWhenSelectionIsStale: if selectedTabId points to a removed tab, the bounds check kicks in and returns nil rather than crashing or returning stale data - returnsCorrectPairAfterSwitch: explicit selectedTabId assignment resolves to the matching (tab, index) pair The staleness test is the load-bearing one — it locks the contract that future migrations of the other ~14 selectedTabIndex + bounds-check sites can rely on. Without this, a refactor that loosens the bounds check could silently regress.
9 tasks
datlechin
added a commit
that referenced
this pull request
Apr 29, 2026
…across all extensions (#941) * refactor(coordinator): sweep selectedTabIndex + bounds-check pattern across all extensions Migrates 12 files / 30+ call sites of the duplicated 'guard let tabIndex = tabManager.selectedTabIndex, tabIndex < tabManager.tabs.count' pattern to selectedTabAndIndex (write-back paths) or selectedTab (read-only paths). Both helpers were introduced in PR #939 and #940; this PR completes the migration outside Pagination and RowOperations. Files touched: - MainContentCoordinator.swift: runQuery, executeTableTabQueryDirectly, loadQueryIntoEditor, insertQueryFromAI, runExplainQuery, executeQueryInternal, handleSort, EXPLAIN error fallback - MainContentCoordinator+ClickHouse.swift: runVariantExplain - MainContentCoordinator+ColumnVisibility.swift: saveColumnVisibilityToTab - MainContentCoordinator+Discard.swift: handleDiscard (two sites) - MainContentCoordinator+ExecuteAll.swift: runAllStatements - MainContentCoordinator+Favorites.swift: insertFavorite, runFavoriteInNewTab - MainContentCoordinator+Filtering.swift: applyFilters, clearFiltersAndReload, restoreFiltersForTable - MainContentCoordinator+FKNavigation.swift: navigateToFKReference (four sites) - MainContentCoordinator+LoadMore.swift: loadMoreRows, fetchAllRows - MainContentCoordinator+Navigation.swift: openTableTab (four sites), promotePreviewTab - MainContentCoordinator+QueryParameters.swift: executeQueryWithParameters, executeQueryInternalParameterized, executeMultipleStatementsWithParameters - MainContentCoordinator+Refresh.swift: handleRefresh (three sites) - MainContentCommandActions.swift: formatQuery, toggleResults, previousResultTab, nextResultTab Behavior unchanged. Each migration preserves the original tab-not-selected and out-of-bounds short-circuits via selectedTabAndIndex's atomic check. Read-only paths use selectedTab (no fallback divergence — these don't use the index, so the strict-bounds requirement is moot). The dispatchParameterizedStatements / dispatchStatements helpers (called via tabIndex parameter, not selectedTabIndex) were left alone — they take the index from the caller's already-validated lookup. ExecuteAll's tabIndex parameter pattern preserved. Smoke-tested across query execution, EXPLAIN, table open from sidebar, FK navigation, filter apply/clear, refresh, discard dialog, pagination, format query, toggle results, insert favorite. Targeted tests pass. * refactor(coordinator): standardize selectedTabAndIndex destructuring as (tab, tabIndex)
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
First focused cleanup pass on
MainContentCoordinator. Pagination had seven methods, six of which followed an identical six-line shape:Each becomes a one-liner via two small extractions.
What's new
QueryTabManager.selectedTabAndIndex— returns(tab: QueryTab, index: Int)?atomically with the bounds check. Replaces the two-stepselectedTabIndex+< tabs.count+tabs[index]pattern. Five lines, single purpose.paginateIfPossible(where:mutate:)(private to Pagination) — captures the "active tab + precondition + confirm-discard + mutate" chain. Each public pagination method passes aKeyPath<PaginationState, Bool>(or short closure) for the precondition and a single-statement closure for the mutation.Result
Pagination methods that used to be 5–6 lines each:
Net −30 LOC in Pagination, +5 LOC in QueryTabManager.
What's not in this PR
There are ~14 other
selectedTabIndex+ bounds-check sites acrossMainContentCoordinator+RowOperations,+Filtering,+Navigation,+FKNavigation, etc. Each has slightly different surrounding logic — some use the index for write-back, some use the tab for read-only, some interact withconfirmDiscardChangesIfNeeded. Mechanical migration risks subtle behavior changes; each warrants its own focused PR with smoke testing.selectedTabAndIndexis the foothold for those future PRs.Test plan