Skip to content

fix(macOS): validate cached find ranges before Replace#244

Draft
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/recent-commits-critical-bugs-0450
Draft

fix(macOS): validate cached find ranges before Replace#244
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/recent-commits-critical-bugs-0450

Conversation

@cursor

@cursor cursor Bot commented May 6, 2026

Copy link
Copy Markdown

Bug and impact

After the find/replace work, Replace / Replace & Find could use lastSearchHighlightRanges that no longer matched the live NSTextStorage. reapplySearchHighlights() skips out-of-bounds ranges when re-tinting after markdown restyle, but it does not clear those entries from the cache. The user can keep editing while the find UI stays open, so Replace could call replaceCharacters(in:with:) with an invalid NSRange (crash via NSRangeException) or replace the wrong slice of text.

Root cause

Stale cached highlight ranges coexisting with a mutated buffer; replace-current trusted the cache without re-validating against the current string.

Fix

  • Before replacing, require the cached range to fit the storage and to span text that still matches the find query (case-insensitive), matching how matches are discovered.
  • On failure, refresh highlights from the live document instead of mutating.
  • Extract LinkAwareTextView.cachedHighlightRangeStillMatchesQuery for reuse and add SearchReplaceStaleHighlightTests.

Validation

  • Added XCTest coverage for the validation helper (full Xcode test run not executed in this Linux agent environment).
Open in Web View Automation 

Highlight ranges can lag the document while the find bar stays open because
markdown restyle skips stale ranges in reapplySearchHighlights without
removing them from lastSearchHighlightRanges. Replace then called
replaceCharacters with an invalid range or wrong substring.

Co-authored-by: Danny Peck <dannypeck@gmail.com>
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