Skip to content

vim: the shared status line follows the preference, not a live observer - #657

Merged
GoKubar merged 1 commit into
mainfrom
agent/kabir-claude/vim-status-lifecycle
Sep 15, 2026
Merged

GoKubar merged 1 commit into
mainfrom
agent/kabir-claude/vim-status-lifecycle

Conversation

@GoKubar

@GoKubar GoKubar commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Independent of the dispatch-policy stack — this is a lifecycle fix, not a keymap change. Rebased onto current main (post-#654). Fixes 3 red tests on main.

The bug

VimMode.Status.shared can go permanently stale, showing a phantom -- NORMAL -- row while Vim is off.

  • VimStatusLine (ContentView.swift:809) renders purely from VimMode.Status.shared.indicator. Its doc comment claims "while the preference is off indicator is nil" — nothing enforced that.
  • The only thing that cleared the singleton was VimMode.deactivate(), reached via EditorPreferences.apply(to:) → applyVimPreference, through a withObservationTracking token held weakly by a live text view's coordinator (EditorPreferences.swift:494); its onChange fires into a Task { @MainActor } that bails if the token or textView is gone.
  • So if the preference turns off while no live editor is observing, deactivate() never runs and the singleton stays at -- NORMAL -- globally.

User-visible, not just a test artifact: toggle Vim off from the menu (⌃⌘V, FlashTeXMacApp.swift:167) or the command palette with no editor window alive, and the next editor you open shows a phantom -- NORMAL -- status row with Vim off.

The fix

The preference pushes the change itself instead of waiting to be observed: the vimKeybindings setter calls VimMode.preferenceDidChange(to:).

VimMode keeps a weak registry of every live mode (NSHashTable.weakObjects(), registered at init), each with its own isOn flag. preferenceDidChange applies the preference to every view that follows it in both directions, then refreshSharedStatus() re-derives the status line from whichever modes are still on — so a second editor with Vim on keeps its row, and only "nothing is on" clears it.

Correction from an earlier revision of this PR: the first version registered only modes that were already switched on, which made the path one-directional — it could turn Vim off but never on, since turn-on found an empty registry. In the app that asymmetry is masked by the coordinator's observation; a test with no coordinator exposed it (2 failures on the Mac gate). The registry now holds every live mode, which is what this PR ships.

The caveat that makes the obvious fix wrong (and is handled): vimEnabledOverride pins Vim per-view independently of the global preference, so the clear lives on the preference transition, never as a render gate in VimStatusLine. Gating the view on the preference would break every test that uses the override — there is an explicit regression test for exactly that.

Why CI was red, and why those tests are not touched

VimStatusLinePlacementTests.testCommandLineSharesTheSameRow runs first alphabetically, passes, and its tearDown sets the preference false then destroys the window immediately — the scheduled Task { @MainActor } loses the race against coordinator teardown. Every later test then starts with the row already showing → "lost 0.0 pt" and XCTAssertNil failed: "-- NORMAL --" in testStatusLineIsTheEditorsBottomRowBesideThePreview, …WithTheProblemsPanelOpen and testVimOffTakesNoSpaceAtAll. On a fast Mac the Task wins; on a loaded CI runner it loses. The clear is now synchronous on the setter, so there is no race to lose.

Test justification (standing owner requirement): the three failing VimStatusLinePlacementTests are unchanged — the product was wrong, not the tests. Added 3 regression tests to VimModeTests.swift: preference-off with no live editor clears the singleton; a per-view override keeps its status line when the preference goes off (this one guards against the wrong fix); and the clear is synchronous rather than on a later run-loop turn. One existing line changed: tearDown's tv.vimEnabledOverride → tv?.vimEnabledOverride, because the no-live-editor test releases the view on purpose.

Rebase note: resolving onto post-#654 main required a union merge in VimModeTests.swift (both commits append tests at the end of the class, and the conflict boundary cut through the last function of each side). Both blocks are kept; brace balance and the seam were checked by hand.

Mac gate result on the rebased head posted as a comment.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu

@GoKubar
GoKubar force-pushed the agent/kabir-claude/vim-status-lifecycle branch from 498e82c to ffe53d0 Compare September 15, 2026 22:41
VimStatusLine (ContentView.swift) renders from VimMode.Status.shared,
and the only thing that cleared that singleton was VimMode.deactivate()
— reached through EditorPreferences.observeApplying(to:), an
observation held weakly by a live editor coordinator that bails when
the token or text view is gone. So switching Vim off from the menu
(⌃⌘V) or the command palette with no editor alive, or while one was
being torn down, never ran deactivate(): Status.shared stayed at
"-- NORMAL --" for the rest of the process and the next editor opened
showed a phantom status row with Vim off.

The preference now pushes the change itself: the vimKeybindings setter
calls VimMode.preferenceDidChange(to:), which switches off the views
that follow the preference and re-derives the status line. VimMode
keeps a weak registry of the modes that are actually on, so the
singleton is re-derived from what is left rather than blanked blindly.

Views that pin Vim per-view through vimEnabledOverride are deliberately
untouched and keep owning the status line: a render gate on the
preference inside VimStatusLine would have been the obvious fix and
would have broken every test that uses the override.

This is also the cause of three red CI tests in
VimStatusLinePlacementTests: testCommandLineSharesTheSameRow runs first
alphabetically and its tearDown sets the preference false, then
destroys the window immediately — the scheduled Task { @mainactor }
lost the race with coordinator teardown on a loaded runner, leaving the
row visible for every later test ("lost 0.0 pt", XCTAssertNil failed:
"-- NORMAL --"). The clear is now synchronous on the setter, so there
is no race to lose. Those tests are unchanged: the product was wrong.

Implementation-Agent: claude-fable-5 (Claude Code subagent)
Commit-Executor: claude-fable-5 (Claude Code subagent)
Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu
@GoKubar
GoKubar force-pushed the agent/kabir-claude/vim-status-lifecycle branch from ffe53d0 to 8395167 Compare September 15, 2026 22:45
@GoKubar

GoKubar commented Sep 15, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (post-#654) — ffe53d05 → 83951671. The rebase hit the expected conflict in VimModeTests.swift; note that a plain union merge would have been wrong there: the conflict boundary cut through the last function on each side, so concatenating the two blocks leaves testTabStillReachesTheEditorInInsertMode unclosed (the shared trailing } after the marker closes only one of them). Both blocks are kept with the seam closed explicitly; brace balance and the seam were verified.

Vim surface on the rebased head, run serially:

VimModeTests + VimStatusLinePlacementTests
Executed 45 tests, with 0 failures (0 unexpected) in 7.561 seconds

That includes the 4 VimStatusLinePlacementTests this PR fixes.

Full-suite pass count is re-running serially. The earlier full-gate numbers from this machine are not usable: several agents were running suites concurrently (load average peaked at 196, wall time 1106s against a normal ~270s) and the failures were load artifacts — perf assertions, Nearby network timeouts, snapshot tests — that varied run to run and included no vim test. Will post the serial number when it lands.

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.

1 participant