Skip to content

Commit 8f8c81a

Browse files
committed
Added replace current match functionality.
1 parent 88a364a commit 8f8c81a

File tree

5 files changed

+52
-5
lines changed

5 files changed

+52
-5
lines changed

Sources/CodeEditSourceEditor/Find/FindPanelDelegate.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ protocol FindPanelDelegate: AnyObject {
1717
func findPanelDidUpdateReplaceText(_ text: String)
1818
func findPanelPrevButtonClicked()
1919
func findPanelNextButtonClicked()
20+
func findPanelReplaceButtonClicked()
2021
func findPanelUpdateMatchCount(_ count: Int)
2122
func findPanelClearEmphasis()
2223
}

Sources/CodeEditSourceEditor/Find/FindViewController+FindPanelDelegate.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ extension FindViewController: FindPanelDelegate {
1212
var findPanelMode: FindPanelMode { mode }
1313
var findPanelWrapAround: Bool { wrapAround }
1414
var findPanelMatchCase: Bool { matchCase }
15-
var findPanelReplaceText: String { replaceText }
1615

1716
func findPanelOnSubmit() {
1817
findPanelNextButtonClicked()
@@ -202,6 +201,11 @@ extension FindViewController: FindPanelDelegate {
202201
updateEmphasesForCurrentMatch(emphasisManager: emphasisManager)
203202
}
204203

204+
func findPanelReplaceButtonClicked() {
205+
guard !findMatches.isEmpty else { return }
206+
replaceCurrentMatch()
207+
}
208+
205209
func findPanelUpdateMatchCount(_ count: Int) {
206210
findPanel.updateMatchCount(count)
207211
}

Sources/CodeEditSourceEditor/Find/FindViewController+Operations.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,46 @@ extension FindViewController {
5353
currentFindMatchIndex = getNearestEmphasisIndex(matchRanges: findMatches) ?? 0
5454
}
5555

56+
func replaceCurrentMatch() {
57+
guard let target = target,
58+
!findMatches.isEmpty else { return }
59+
60+
// Get the current match range
61+
let currentMatchRange = findMatches[currentFindMatchIndex]
62+
63+
// Set cursor positions to the match range
64+
target.setCursorPositions([CursorPosition(range: currentMatchRange)])
65+
66+
// Replace the text using the cursor positions
67+
if let textViewController = target as? TextViewController {
68+
textViewController.textView.insertText(replaceText, replacementRange: currentMatchRange)
69+
}
70+
71+
// Adjust the length of the replacement
72+
let lengthDiff = replaceText.utf16.count - currentMatchRange.length
73+
74+
// Update the current match index
75+
if findMatches.isEmpty {
76+
currentFindMatchIndex = 0
77+
findPanel.findDelegate?.findPanelUpdateMatchCount(0)
78+
} else {
79+
// Update all match ranges after the current match
80+
for index in (currentFindMatchIndex + 1)..<findMatches.count {
81+
findMatches[index].location += lengthDiff
82+
}
83+
84+
// Remove the current match from the array
85+
findMatches.remove(at: currentFindMatchIndex)
86+
87+
// Keep the current index in bounds
88+
currentFindMatchIndex = min(currentFindMatchIndex, findMatches.count - 1)
89+
findPanel.findDelegate?.findPanelUpdateMatchCount(findMatches.count)
90+
}
91+
92+
// Update the emphases
93+
addEmphases()
94+
}
95+
5696
func addEmphases() {
5797
guard let target = target,
5898
let emphasisManager = target.emphasisManager else { return }

Sources/CodeEditSourceEditor/Find/PanelView/FindPanelView.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,11 @@ struct FindPanelView: View {
133133
}
134134
HStack(spacing: 4) {
135135
ControlGroup {
136-
Button(action: {
137-
// TODO: Replace action
138-
}, label: {
136+
Button(action: viewModel.replaceButtonClicked) {
139137
Text("Replace")
140138
.opacity(viewModel.findText.isEmpty || viewModel.matchCount == 0 ? 0.33 : 1)
141139
.frame(width: viewModel.findControlsWidth/2 - 12 - 0.5)
142-
})
140+
}
143141
// TODO: disable if there is not an active match
144142
.disabled(viewModel.findText.isEmpty || viewModel.matchCount == 0)
145143
Divider()

Sources/CodeEditSourceEditor/Find/PanelView/FindPanelViewModel.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ class FindPanelViewModel: ObservableObject {
101101
delegate?.findPanelNextButtonClicked()
102102
}
103103

104+
func replaceButtonClicked() {
105+
delegate?.findPanelReplaceButtonClicked()
106+
}
107+
104108
func toggleWrapAround() {
105109
wrapAround.toggle()
106110
delegate?.findPanelDidUpdateWrapAround(wrapAround)

0 commit comments

Comments
 (0)