Skip to content

fix(editor): keep autocomplete delegate attached for the editor's lifetime (#1731)#1745

Merged
datlechin merged 1 commit into
mainfrom
fix/1731-autocomplete-delegate-lifetime
Jun 21, 2026
Merged

fix(editor): keep autocomplete delegate attached for the editor's lifetime (#1731)#1745
datlechin merged 1 commit into
mainfrom
fix/1731-autocomplete-delegate-lifetime

Conversation

@datlechin

Copy link
Copy Markdown
Member

Fixes #1731.

Problem

SQL autocomplete intermittently stopped appearing for any category (keywords, tables, columns), even with the schema fully loaded. Auto-capitalization still worked. It was tied to app/editor lifecycle and only a relaunch fixed it.

Root cause

TextViewController.completionDelegate is weak, and the only strong owner was an optional @State completionAdapter that SQLEditorView.onDisappear set to nil. macOS does not guarantee onAppear fires after every onDisappear for views in native window-tab groups. When it didn't, the adapter was never recreated, controller.completionDelegate stayed nil, and every keystroke hit the trigger guard guard let completionDelegate ... else { return }, killing all completions (including schema-independent keywords) until relaunch.

Fix

The completion adapter now lives for the editor's whole lifetime.

  • SQLEditorView: completionAdapter is non-optional, created eagerly with nil schema; removed the editorReady two-phase gate and flattened the body; onDisappear no longer nils the adapter. SourceEditor.updateNSViewController continuously re-asserts a live, non-nil delegate. Keywords work from frame one; tables/columns enrich once schema loads.
  • SQLCompletionAdapter: replaced updateSchemaProvider with configure(schemaProvider:databaseType:) accepting optionals, so the eager adapter can attach before schema loads and reconfigure when it arrives.
  • SQLEditorCoordinator.revive(): reinstalls the editor-settings and window-key observers that destroy() tears down, so editor-settings and Vim live updates survive the destroy/revive cycle that tab switching now routinely exercises.

The "schema race" an earlier analysis pass suspected was not real: services.schemaProviderRegistry is .shared and the coordinator's init already calls getOrCreate, so the reconcile lookup always succeeds. No change made there.

Tests

New SQLCompletionAdapterLifecycleTests (3 tests, all passing): the engine returns keyword completions with a nil schema (with and without a database type), and the adapter survives nil <-> provider configure transitions. The SwiftUI onDisappear/onAppear delegate detachment itself is an AppKit lifecycle behavior that is not deterministically unit-testable; these tests guard the invariants the fix relies on.

Verification

  • BUILD SUCCEEDED
  • swiftlint lint --strict: 0 violations on changed files
  • New tests pass (3/3)

Needs manual confirmation in-app: type to get suggestions, switch between query tabs and connection windows and back, and confirm autocomplete keeps appearing without a relaunch.

@mintlify

mintlify Bot commented Jun 21, 2026

Copy link
Copy Markdown

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated (UTC)
TablePro 🟢 Ready View Preview Jun 21, 2026, 4:09 PM

💡 Tip: Enable Workflows to automatically generate PRs for you.

@datlechin datlechin force-pushed the fix/1731-autocomplete-delegate-lifetime branch from 9b85403 to 8d607b7 Compare June 21, 2026 16:17
@datlechin datlechin merged commit 956bcac into main Jun 21, 2026
4 checks passed
@datlechin datlechin deleted the fix/1731-autocomplete-delegate-lifetime branch June 21, 2026 16:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Autocomplete completely non-functional in SQL editor

1 participant