Skip to content

Commit 9abe5ae

Browse files
committed
Maintain find matches when typing, respect wrap around preference when replacing
1 parent 923cb88 commit 9abe5ae

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

Sources/CodeEditSourceEditor/Find/ViewModel/FindPanelViewModel+Find.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ extension FindPanelViewModel {
1313
/// Performs a find operation on the find target and updates both the ``findMatches`` array and the emphasis
1414
/// manager's emphases.
1515
func find() {
16-
// Don't find if target or emphasisManager isn't ready or the query is empty
17-
guard let target = target, isFocused, !findText.isEmpty else {
16+
// Don't find if target isn't ready or the query is empty
17+
guard let target = target, !findText.isEmpty else {
1818
self.findMatches = []
1919
return
2020
}
@@ -36,7 +36,11 @@ extension FindPanelViewModel {
3636

3737
// Find the nearest match to the current cursor position
3838
currentFindMatchIndex = getNearestEmphasisIndex(matchRanges: findMatches) ?? 0
39-
addMatchEmphases(flashCurrent: false)
39+
40+
// Only add emphasis layers if the find panel is focused
41+
if isFocused {
42+
addMatchEmphases(flashCurrent: false)
43+
}
4044
}
4145

4246
// MARK: - Get Nearest Emphasis Index

Sources/CodeEditSourceEditor/Find/ViewModel/FindPanelViewModel+Replace.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,16 @@ extension FindPanelViewModel {
2121
replaceMatch(index: currentFindMatchIndex, textView: target.textView, matches: &findMatches)
2222

2323
self.findMatches = findMatches.enumerated().filter({ $0.offset != currentFindMatchIndex }).map(\.element)
24-
self.currentFindMatchIndex = findMatches.isEmpty ? nil : (currentFindMatchIndex) % findMatches.count
24+
25+
// Update currentFindMatchIndex based on wrapAround setting
26+
if findMatches.isEmpty {
27+
self.currentFindMatchIndex = nil
28+
} else if wrapAround {
29+
self.currentFindMatchIndex = currentFindMatchIndex % findMatches.count
30+
} else {
31+
// If we're at the end and not wrapping, stay at the end
32+
self.currentFindMatchIndex = min(currentFindMatchIndex, findMatches.count - 1)
33+
}
2534

2635
// Update the emphases
2736
addMatchEmphases(flashCurrent: true)

0 commit comments

Comments
 (0)