Skip to content

Add find & replace (replaceCurrent / replaceAll bus handlers)#93

Open
ChristineTham wants to merge 1 commit into
nodes-app:mainfrom
ChristineTham:pr/find-replace
Open

Add find & replace (replaceCurrent / replaceAll bus handlers)#93
ChristineTham wants to merge 1 commit into
nodes-app:mainfrom
ChristineTham:pr/find-replace

Conversation

@ChristineTham

Copy link
Copy Markdown

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/didChangeText undo registration and report the remaining count via findResults. A shared findMatches helper is extracted from handleFindQuery.

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.

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>
Copilot AI review requested due to automatic review settings July 11, 2026 09:02

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 centralizes findResults posting.
  • Adds replaceCurrent and replaceAll bus handlers that perform edits with undo registration, then re-highlight and repost remaining match counts.
  • Extends MarkdownEditorBus with new optional notification names and wires observers in NativeTextViewCoordinator.

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] {
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.

2 participants