Repository navigation
fix(editor): end a completion session that presented nothing so the popup can open again - #2921
Merged
Merged
Conversation
…opup can open again (#2915)
Signed-off-by: Ngô Quốc Đạt <[email protected]>
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.
Fixes #2915.
Root cause
The completion popup had no owned session lifetime.
SuggestionViewModel.activeTextViewanswered 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 wasNSWindow.willCloseNotification.Measured with an AppKit probe (
swiftc, bareNSApplication):close()posts that notification at most once per shown episode. A secondclose()with no interveningorderFront,setIsVisible(true)oraddChildWindowposts nothing. So a request that presented nothing left the model claiming the editor with no window behind it, and from that state:cursorsUpdatedtook 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;Measured live on a Debug build against a throwaway MariaDB, driven with
osascriptand watched withlog stream --debug: typingzqxjloggedshowCompletions: delegate returned nil itemsand closed the popup. Clearing the editor and typingsethen 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 afterFROMand 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, sowillClose()cancelled through a nil handle and the orphan opened the popup again over a session the user had already dismissed.Third,
SQLSchemaProvider.getColumnsswallowed every column-fetch error into a.debuglog, which macOS never persists. That is why the reporter'slog showcame back empty and why a failing column read cannot be told apart from a table with nothing to complete.The fix
SuggestionViewModelnow owns its session explicitly:isPresentedis set only after the window is actually shown, and every exit from the request task that presents nothing goes through oneendSession()that clearsactiveTextView, the delegate, the items and the highlight cache, and firescompletionWindowDidClose().cursorsUpdatedre-ranks in place only whileisPresented. 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.deferis 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 theNSWindowdirectly.isShowingCompletionsanddismissCompletions()key off the model's session rather than window visibility.dismissCompletions()still reports whether a popup was on screen, so the Escape andCtrl+Spacetoggle keeps its meaning, but it can now end a session that never got a window.getColumnslogs a failed column fetch at.errorwith 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:
completionWindowDidSelectfires 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.super.close()postswillCloseNotificationsynchronously, so cleanup ran twice. It now runs once per close, and the controller's own teardown happens beforecompletionWindowDidClose(), 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.SuggestionSessionLifetimeTestsadds 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,QueryCompletionAdapterLifecycleTestsand the rest): 254 cases, 254 passed.swiftlint --stricton every changed file: clean.EditorAutocompleteFocusUITests.testPopupReturnsAfterARequestWithNoMatchescovers the measured sequence, but it could not run locally: another TablePro was running on this machine and XCUITest terminatescom.TableProon 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
NSTextFieldand never touchesSuggestionController, so none of this reaches it.