Add find & replace (replaceCurrent / replaceAll bus handlers)#93
Open
ChristineTham wants to merge 1 commit into
Open
Add find & replace (replaceCurrent / replaceAll bus handlers)#93ChristineTham wants to merge 1 commit into
ChristineTham wants to merge 1 commit into
Conversation
Extends the existing display-coordinate find with two bus notifications: `replaceCurrent` (replace the focused match, re-highlight, keep the index on the next occurrence) and `replaceAll` (replace every match in one undo step, back-to-front so ranges stay valid). Both edit the engine's own text with proper shouldChangeText/didChangeText undo registration and report the remaining count via `findResults`. Shared `findMatches` helper extracted from `handleFindQuery`. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Adds optional bus-driven find/replace capabilities to the macOS NativeTextViewCoordinator, building on the existing “display-coordinate” find logic so the engine can edit/highlight its own displayed text and report match counts back to the host UI.
Changes:
- Extracts a shared
findMatches(of:in:)helper and centralizesfindResultsposting. - Adds
replaceCurrentandreplaceAllbus handlers that perform edits with undo registration, then re-highlight and repost remaining match counts. - Extends
MarkdownEditorBuswith new optional notification names and wires observers inNativeTextViewCoordinator.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| Sources/MarkdownEngine/TextView/Coordinator/NativeTextViewCoordinator+Find.swift | Extracts find matching helper and adds replace-current / replace-all handlers with re-highlighting + result count posting. |
| Sources/MarkdownEngine/TextView/Coordinator/NativeTextViewCoordinator.swift | Registers new bus observers for replaceCurrent and replaceAll. |
| Sources/MarkdownEngine/Services/MarkdownEditorServices.swift | Adds new optional bus notification names and initializer parameters for find/replace. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+108
to
+110
| // Group as one undo; edit back-to-front so earlier ranges stay valid. | ||
| let orderedRanges = matches.reversed().map { NSValue(range: $0) } | ||
| let replacements = Array(repeating: replacement, count: matches.count) |
Comment on lines
+75
to
+76
| let matches = findMatches(of: query, in: tv.string as NSString) | ||
| guard !matches.isEmpty else { postFindResults(count: 0); return } |
Comment on lines
+105
to
+106
| let matches = findMatches(of: query, in: tv.string as NSString) | ||
| guard !matches.isEmpty else { postFindResults(count: 0); return } |
|
|
||
| /// All ranges of `query` in `haystack` (display coordinates), case- and | ||
| /// diacritic-insensitive. Shared by find and replace. | ||
| func findMatches(of query: String, in haystack: NSString) -> [NSRange] { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Extends the existing display-coordinate find with two bus notifications:
replaceCurrent— replace the focused match, re-highlight, and keep the index on the next occurrence.replaceAll— replace every match in one undo step, back-to-front so ranges stay valid.Both edit the engine's own displayed text with proper
shouldChangeText/didChangeTextundo registration and report the remaining count viafindResults. A sharedfindMatcheshelper is extracted fromhandleFindQuery.Purely additive (new optional bus names); embedders that don't set them are unaffected.
Context: extracted from the ChristineTham/swift-markdown-engine fork used by HelloNotes, which ships a ⌘F find/replace bar on top of this.