Skip to content

fix(editor): end a completion session that presented nothing so the popup can open again - #2921

Merged
datlechin merged 2 commits into
mainfrom
fix/2915-completion-popup
Sep 16, 2026
Merged

datlechin merged 2 commits into
mainfrom
fix/2915-completion-popup

Conversation

@datlechin

Copy link
Copy Markdown
Member

Fixes #2915.

Root cause

The completion popup had no owned session lifetime. SuggestionViewModel.activeTextView answered three different questions at once: who a request is for, whether a session is live, and whether a window is on screen. It is assigned before the delegate is awaited, and the only thing that cleared it was NSWindow.willCloseNotification.

Measured with an AppKit probe (swiftc, bare NSApplication): close() posts that notification at most once per shown episode. A second close() with no intervening orderFront, setIsVisible(true) or addChildWindow posts nothing. So a request that presented nothing left the model claiming the editor with no window behind it, and from that state:

  • cursorsUpdated took its re-rank branch and wrote items into a panel that was not on screen, returning before anything could ask for a new session;
  • dismissCompletions() was gated on window visibility, so Vim's Escape and the menu Escape route could not clear it;
  • the adapter's seeded candidate pool was never dropped, so a stale pool kept ranking.

Measured live on a Debug build against a throwaway MariaDB, driven with osascript and watched with log stream --debug: typing zqxj logged showCompletions: delegate returned nil items and closed the popup. Clearing the editor and typing se then made no request at all and showed no popup. SELECT, SET and SHOW were dead on an empty statement. It only recovered on a keystroke whose ranking against the stale pool came back empty, which is why the reporter sees table names complete while the space after FROM and the dot after an alias show nothing.

A second defect in the same method: defer { itemsRequestTask = nil } had no identity check. A cancelled, superseded task resumes after its successor's handle is stored and cleared it, so willClose() cancelled through a nil handle and the orphan opened the popup again over a session the user had already dismissed.

Third, SQLSchemaProvider.getColumns swallowed every column-fetch error into a .debug log, which macOS never persists. That is why the reporter's log show came back empty and why a failing column read cannot be told apart from a table with nothing to complete.

The fix

SuggestionViewModel now owns its session explicitly:

  • isPresented is set only after the window is actually shown, and every exit from the request task that presents nothing goes through one endSession() that clears activeTextView, the delegate, the items and the highlight cache, and fires completionWindowDidClose().
  • cursorsUpdated re-ranks in place only while isPresented. The in-flight guard keeps its existing meaning, which is legitimate while a window is up: a pending request re-reads the live cursor position after its debounce, so it is not stale.
  • The defer is fenced by a request generation, so a superseded task cannot clear the live request's handle.
  • SuggestionController.close() runs the teardown itself instead of waiting for a notification that may not arrive, exactly once per close. The observer stays for callers that close the NSWindow directly.
  • isShowingCompletions and dismissCompletions() key off the model's session rather than window visibility. dismissCompletions() still reports whether a popup was on screen, so the Escape and Ctrl+Space toggle keeps its meaning, but it can now end a session that never got a window.
  • getColumns logs a failed column fetch at .error with the table name, so the next report carries evidence.

A second review pass raised two more ways the new invariant could be broken by a conforming delegate, both fixed here:

  • completionWindowDidSelect fires synchronously from inside the presentation, and it is public, so a conformer may dismiss from it. The presentation now rechecks the request generation and the owning text view after that callback, rather than putting the panel up over a session that no longer exists.
  • For a panel that is on screen, super.close() posts willCloseNotification synchronously, so cleanup ran twice. It now runs once per close, and the controller's own teardown happens before completionWindowDidClose(), which is the other public callback a conformer can start a new session from.

Verified

  • swift test --package-path Packages/TableProEditor --force-resolved-versions: 119 tests, 0 failures.
  • SuggestionSessionLifetimeTests adds seven cases. The five that cover the reported defect were run against the old behaviour first: 11 assertion failures across all five. With the fix, all seven pass.
  • xcodebuild build (Debug): PASS.
  • TableProTests, the twelve suites that own the changed types (SQLSchemaProviderTests, SQLSchemaProviderFallbackTests, CompletionEngineTests, SQLCompletionProviderTests, QueryCompletionAdapterLifecycleTests and the rest): 254 cases, 254 passed.
  • swiftlint --strict on every changed file: clean.

EditorAutocompleteFocusUITests.testPopupReturnsAfterARequestWithNoMatches covers the measured sequence, but it could not run locally: another TablePro was running on this machine and XCUITest terminates com.TablePro on launch. CI runs it.

No before/after screenshots. The difference is a popup that appears rather than one that does not, and the wedged state is timing-dependent: two scripted attempts to capture it on the pre-fix build presented the popup instead. The deterministic evidence is the five cases above, which fail on the old code with named assertions and pass on the new one.

Not in this change

  • The filter panel has its own completion popup on an NSTextField and never touches SuggestionController, so none of this reaches it.
  • No loading placeholder while a column fetch is in flight. With the trigger no longer swallowed, the popup opens when the fetch answers.
  • A failed column read still closes the popup like any other empty result. It is now logged at a level the log store keeps.
  • A caret move during a pending request does not cancel that request, so it can still present at a position the user has left. That predates this change: the in-flight guard returned without cancelling before it too. Cancelling there naively would regress Vim, where a mode change moves the selection and the trigger model's position check does not always suppress the paired update. Worth its own change.

@datlechin
datlechin merged commit bc75739 into main Sep 16, 2026
5 checks passed
@datlechin
datlechin deleted the fix/2915-completion-popup branch September 16, 2026 09:13
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.

Column completion never appears, and no popup after an empty prefix in FROM (MySQL)

1 participant