Skip to content

fix(macOS): recompute find match ranges before Replace#251

Draft
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/critical-bug-inspection-7d51
Draft

fix(macOS): recompute find match ranges before Replace#251
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/critical-bug-inspection-7d51

Conversation

@cursor
Copy link
Copy Markdown

@cursor cursor Bot commented May 20, 2026

Bug and impact

Replace / Replace & Find in the note editor used cached lastSearchHighlightRanges from the last highlight pass. If the user edited the note while the find bar stayed open, those UTF-16 ranges could point at the wrong substring or past the end of the storage. That could corrupt note content (wrong text replaced) or cause runtime failures when applying the replacement.

Root cause

replaceCurrentMatch read lastSearchHighlightRanges[focusIndex] without re-scanning textStorage.string. Highlights are only refreshed on find-bar-driven events, not on every keystroke, so the cache could go stale.

Fix

  • Recompute the match list from live storage immediately before replace, using the same needle + case-insensitive scan and 2000-match cap as applySearchHighlights.
  • Align nextMatchIndex with that same scan so the post-replace focus index stays consistent with the find UI.

Validation

  • Swift / xcodebuild tests are not available in this Linux agent environment; change is localized to LinkAwareTextView search/replace helpers in EditorView.swift and follows the existing highlight matching logic.
Open in Web View Automation 

replaceCurrentMatch used lastSearchHighlightRanges from the last
applySearchHighlights pass. Any edit while the find bar stays open
left those UTF-16 ranges stale, so Replace could corrupt the note
or hit an invalid storage range. Re-scan the live string (same
rules as the find UI) before replacing, and align nextMatchIndex
with the needle-based scan used for highlighting.

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