@@ -34,26 +34,98 @@ extension NativeTextViewCoordinator {
3434 let query = info [ " query " ] as? String else { return }
3535 let requestedIndex = info [ " currentIndex " ] as? Int ?? 0
3636
37- var allRanges : [ NSRange ] = [ ]
38- if ! query . isEmpty {
39- let haystack = tv . string as NSString
40- let opts : NSString . CompareOptions = [ . caseInsensitive , . diacriticInsensitive ]
41- var searchStart = 0
42- while searchStart < haystack . length {
43- let scope = NSRange ( location : searchStart , length : haystack . length - searchStart )
44- let found = haystack . range ( of : query , options : opts , range : scope )
45- if found . location == NSNotFound { break }
46- allRanges . append ( found )
47- searchStart = found . location + max ( found . length , 1 )
48- }
49- }
50-
37+ let allRanges = findMatches ( of : query , in : tv . string as NSString )
5138 let currentIndex = allRanges . isEmpty ? 0 : min ( max ( requestedIndex , 0 ) , allRanges . count - 1 )
5239 renderFindMatches ( allRanges , currentIndex : currentIndex )
40+ postFindResults ( count : allRanges . count )
41+ }
5342
43+ /// All ranges of `query` in `haystack` (display coordinates), case- and
44+ /// diacritic-insensitive. Shared by find and replace.
45+ func findMatches ( of query : String , in haystack : NSString ) -> [ NSRange ] {
46+ guard ! query . isEmpty else { return [ ] }
47+ var ranges : [ NSRange ] = [ ]
48+ let opts : NSString . CompareOptions = [ . caseInsensitive , . diacriticInsensitive ]
49+ var searchStart = 0
50+ while searchStart < haystack . length {
51+ let scope = NSRange ( location : searchStart , length : haystack . length - searchStart )
52+ let found = haystack . range ( of : query , options : opts , range : scope )
53+ if found . location == NSNotFound { break }
54+ ranges . append ( found )
55+ searchStart = found . location + max ( found . length , 1 )
56+ }
57+ return ranges
58+ }
59+
60+ private func postFindResults ( count : Int ) {
5461 if let resultsName = configuration . services . bus . findResults {
55- NotificationCenter . default . post ( name : resultsName , object : nil , userInfo : [ " count " : allRanges . count ] )
62+ NotificationCenter . default . post ( name : resultsName , object : nil , userInfo : [ " count " : count ] )
63+ }
64+ }
65+
66+ /// Replace the current find match with the replacement string (one undo
67+ /// step), then re-highlight and report the remaining match count.
68+ @ objc func handleReplaceCurrent ( _ notification : Notification ) {
69+ guard let tv = textView , tv . isEditable ,
70+ let info = notification . userInfo ,
71+ let query = info [ " query " ] as? String , ! query . isEmpty ,
72+ let replacement = info [ " replacement " ] as? String else { return }
73+ let requestedIndex = info [ " currentIndex " ] as? Int ?? 0
74+
75+ let matches = findMatches ( of : query , in : tv . string as NSString )
76+ guard ! matches . isEmpty else { postFindResults ( count : 0 ) ; return }
77+ let idx = min ( max ( requestedIndex , 0 ) , matches . count - 1 )
78+ let target = matches [ idx ]
79+ guard NSMaxRange ( target ) <= ( tv . string as NSString ) . length else { return }
80+
81+ tv . breakUndoCoalescing ( )
82+ isProgrammaticEdit = true
83+ defer { isProgrammaticEdit = false }
84+ guard tv . shouldChangeText ( in : target , replacementString : replacement ) else { return }
85+ tv . textStorage ? . replaceCharacters ( in : target , with : replacement )
86+ tv . didChangeText ( )
87+ tv . undoManager ? . setActionName ( " Replace " )
88+ tv . breakUndoCoalescing ( )
89+
90+ // Re-find on the edited text; keep the index so focus lands on the next
91+ // occurrence (or clamps to the last remaining one).
92+ let updated = findMatches ( of : query , in : tv . string as NSString )
93+ let nextIndex = updated . isEmpty ? 0 : min ( idx , updated . count - 1 )
94+ renderFindMatches ( updated , currentIndex : nextIndex )
95+ postFindResults ( count : updated . count )
96+ }
97+
98+ /// Replace every find match in a single undo step, then re-highlight.
99+ @ objc func handleReplaceAll ( _ notification : Notification ) {
100+ guard let tv = textView , tv . isEditable ,
101+ let info = notification . userInfo ,
102+ let query = info [ " query " ] as? String , ! query . isEmpty ,
103+ let replacement = info [ " replacement " ] as? String else { return }
104+
105+ let matches = findMatches ( of : query , in : tv . string as NSString )
106+ guard ! matches . isEmpty else { postFindResults ( count : 0 ) ; return }
107+
108+ // Group as one undo; edit back-to-front so earlier ranges stay valid.
109+ let orderedRanges = matches . reversed ( ) . map { NSValue ( range : $0 ) }
110+ let replacements = Array ( repeating : replacement , count : matches . count )
111+
112+ tv . breakUndoCoalescing ( )
113+ isProgrammaticEdit = true
114+ defer { isProgrammaticEdit = false }
115+ guard tv . shouldChangeText ( inRanges : orderedRanges , replacementStrings : replacements ) else { return }
116+ tv . textStorage ? . beginEditing ( )
117+ for match in matches . reversed ( ) {
118+ tv . textStorage ? . replaceCharacters ( in : match , with : replacement )
56119 }
120+ tv . textStorage ? . endEditing ( )
121+ tv . didChangeText ( )
122+ tv . undoManager ? . setActionName ( " Replace All " )
123+ tv . breakUndoCoalescing ( )
124+
125+ // Usually zero remain; non-zero only if the replacement contains the query.
126+ let remaining = findMatches ( of : query , in : tv . string as NSString )
127+ renderFindMatches ( remaining , currentIndex : 0 )
128+ postFindResults ( count : remaining . count )
57129 }
58130
59131 /// Highlight all matches (current one stronger) and scroll the current match into view.
0 commit comments