Skip to content

fix(editor): suggestion window monitor cleanup on accept#1290

Merged
datlechin merged 1 commit into
mainfrom
fix/issue-1278-suggestion-window-cleanup
May 16, 2026
Merged

fix(editor): suggestion window monitor cleanup on accept#1290
datlechin merged 1 commit into
mainfrom
fix/issue-1278-suggestion-window-cleanup

Conversation

@datlechin
Copy link
Copy Markdown
Member

Summary

Fixes #1278 — Enter and ⌘+Enter stop working after picking a table from the autocomplete dropdown.

The suggestion completion window registers a local NSEvent.keyDown monitor to intercept Return/Tab as "accept suggestion". When the user accepted, SuggestionViewModel.applySelectedItem called window?.close() — that is NSWindow.close(), which does not route through SuggestionController.close() override. The override is where removeEventMonitors() and model.willClose() run. So after acceptance the monitor stayed installed and every subsequent Enter was re-interpreted as "apply the same suggestion again" (logs show 5 silent applies for 5 Enter presses). ⌘+Enter was swallowed for the same reason.

Bonus: the double-tap handler in SuggestionContentView was passing model.activeTextView?.view.window (the editor window) into applySelectedItem. That meant double-clicking a suggestion closed the editor window instead of the suggestion window. Confirmed in the same logs (windowDidResignKeywindowWillClose right after dblclick.apply).

Fix

Two layers, Apple-correct:

  1. Decouple model from window. SuggestionViewModel.applySelectedItem no longer takes a window: parameter. It exposes an onApply closure that the controller wires in init to call its own close(). Both call sites (keyboard handler and double-tap) now just call applySelectedItem(item:), eliminating Bug A entirely.
  2. Observe NSWindow.willCloseNotification. The cleanup (removeEventMonitors, model.willClose, KVO invalidate, resign-observer teardown) moves into handleWindowWillClose(_:). The close() override only handles the popover branch + super.close(). Result: cleanup is idempotent and runs no matter who closes the window — controller.close(), window.close(), system close, or future call sites.

Tests

Four new tests in LocalPackages/CodeEditSourceEditor/Tests/.../SuggestionApplyTests.swift:

  • test_apply_invokesOnApplyCallback — model triggers the wired close callback
  • test_apply_skipsCallbackWhenActiveTextViewIsNil — guard works
  • test_nsWindowClose_clearsModelState — direct NSWindow.close() path triggers cleanup via notification
  • test_controllerClose_clearsModelState — controller path also triggers cleanup

Test plan

  • Build passes in Xcode (Debug)
  • xcodebuild test -only-testing:CodeEditSourceEditorTests/SuggestionApplyTests green
  • Manual: SELECT * FROM → pick a table with Enter → press Enter again → cursor goes to new line
  • Manual: same setup → press ⌘+Enter → query executes
  • Manual: same setup → double-click a suggestion → editor window stays open, suggestion window closes
  • Manual: ESC still dismisses the suggestion window

@datlechin datlechin merged commit 736adb2 into main May 16, 2026
2 checks passed
@datlechin datlechin deleted the fix/issue-1278-suggestion-window-cleanup branch May 16, 2026 11:26
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.

Enter and Command+Enter stop working after selecting table from inline suggestion

1 participant