Skip to content

Commit ab32c92

Browse files
committed
Implemented wrap around setting logic. Now when disabled, we do not loop find matches.
1 parent b6d0a07 commit ab32c92

File tree

1 file changed

+55
-62
lines changed

1 file changed

+55
-62
lines changed

Sources/CodeEditSourceEditor/Find/FindViewController+FindPanelDelegate.swift

Lines changed: 55 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -88,54 +88,69 @@ extension FindViewController: FindPanelDelegate {
8888
self.replaceText = text
8989
}
9090

91+
private func flashCurrentMatch(emphasisManager: EmphasisManager, textViewController: TextViewController) {
92+
let newActiveRange = findMatches[currentFindMatchIndex]
93+
emphasisManager.removeEmphases(for: EmphasisGroup.find)
94+
emphasisManager.addEmphasis(
95+
Emphasis(
96+
range: newActiveRange,
97+
style: .standard,
98+
flash: true,
99+
inactive: false,
100+
selectInDocument: true
101+
),
102+
for: EmphasisGroup.find
103+
)
104+
}
105+
91106
func findPanelPrevButtonClicked() {
92107
guard let textViewController = target as? TextViewController,
93108
let emphasisManager = target?.emphasisManager else { return }
94109

95110
// Check if there are any matches
96111
if findMatches.isEmpty {
112+
NSSound.beep()
113+
BezelNotification.show(
114+
symbolName: "arrow.up.to.line",
115+
over: textViewController.textView
116+
)
97117
return
98118
}
99119

100-
// Update to previous match
101-
let oldIndex = currentFindMatchIndex
102-
currentFindMatchIndex = (currentFindMatchIndex - 1 + findMatches.count) % findMatches.count
103-
104-
// Show bezel notification if we cycled from first to last match
105-
if oldIndex == 0 && currentFindMatchIndex == findMatches.count - 1 {
120+
// Check if we're at the first match and wrapAround is false
121+
if !wrapAround && currentFindMatchIndex == 0 {
122+
NSSound.beep()
106123
BezelNotification.show(
107-
symbolName: "arrow.trianglehead.bottomleft.capsulepath.clockwise",
124+
symbolName: "arrow.up.to.line",
108125
over: textViewController.textView
109126
)
127+
if textViewController.textView.window?.firstResponder === textViewController.textView {
128+
flashCurrentMatch(emphasisManager: emphasisManager, textViewController: textViewController)
129+
return
130+
}
131+
updateEmphasesForCurrentMatch(emphasisManager: emphasisManager)
132+
return
110133
}
111134

135+
// Update to previous match∂
136+
currentFindMatchIndex = (currentFindMatchIndex - 1 + findMatches.count) % findMatches.count
137+
112138
// If the text view has focus, show a flash animation for the current match
113139
if textViewController.textView.window?.firstResponder === textViewController.textView {
114-
let newActiveRange = findMatches[currentFindMatchIndex]
115-
116-
// Clear existing emphases before adding the flash
117-
emphasisManager.removeEmphases(for: EmphasisGroup.find)
118-
119-
emphasisManager.addEmphasis(
120-
Emphasis(
121-
range: newActiveRange,
122-
style: .standard,
123-
flash: true,
124-
inactive: false,
125-
selectInDocument: true
126-
),
127-
for: EmphasisGroup.find
128-
)
129-
140+
flashCurrentMatch(emphasisManager: emphasisManager, textViewController: textViewController)
130141
return
131142
}
132143

133-
// Create updated emphases with new active state
144+
updateEmphasesForCurrentMatch(emphasisManager: emphasisManager)
145+
}
146+
147+
private func updateEmphasesForCurrentMatch(emphasisManager: EmphasisManager, flash: Bool = false) {
148+
// Create updated emphases with current match emphasized
134149
let updatedEmphases = findMatches.enumerated().map { index, range in
135150
Emphasis(
136151
range: range,
137152
style: .standard,
138-
flash: false,
153+
flash: flash,
139154
inactive: index != currentFindMatchIndex,
140155
selectInDocument: index == currentFindMatchIndex
141156
)
@@ -151,7 +166,6 @@ extension FindViewController: FindPanelDelegate {
151166

152167
// Check if there are any matches
153168
if findMatches.isEmpty {
154-
// Show "no matches" bezel notification and play beep
155169
NSSound.beep()
156170
BezelNotification.show(
157171
symbolName: "arrow.down.to.line",
@@ -160,52 +174,31 @@ extension FindViewController: FindPanelDelegate {
160174
return
161175
}
162176

163-
// Update to next match
164-
let oldIndex = currentFindMatchIndex
165-
currentFindMatchIndex = (currentFindMatchIndex + 1) % findMatches.count
166-
167-
// Show bezel notification if we cycled from last to first match
168-
if oldIndex == findMatches.count - 1 && currentFindMatchIndex == 0 {
177+
// Check if we're at the last match and wrapAround is false
178+
if !wrapAround && currentFindMatchIndex == findMatches.count - 1 {
179+
NSSound.beep()
169180
BezelNotification.show(
170-
symbolName: "arrow.triangle.capsulepath",
181+
symbolName: "arrow.down.to.line",
171182
over: textViewController.textView
172183
)
184+
if textViewController.textView.window?.firstResponder === textViewController.textView {
185+
flashCurrentMatch(emphasisManager: emphasisManager, textViewController: textViewController)
186+
return
187+
}
188+
updateEmphasesForCurrentMatch(emphasisManager: emphasisManager)
189+
return
173190
}
174191

192+
// Update to next match
193+
currentFindMatchIndex = (currentFindMatchIndex + 1) % findMatches.count
194+
175195
// If the text view has focus, show a flash animation for the current match
176196
if textViewController.textView.window?.firstResponder === textViewController.textView {
177-
let newActiveRange = findMatches[currentFindMatchIndex]
178-
179-
// Clear existing emphases before adding the flash
180-
emphasisManager.removeEmphases(for: EmphasisGroup.find)
181-
182-
emphasisManager.addEmphasis(
183-
Emphasis(
184-
range: newActiveRange,
185-
style: .standard,
186-
flash: true,
187-
inactive: false,
188-
selectInDocument: true
189-
),
190-
for: EmphasisGroup.find
191-
)
192-
197+
flashCurrentMatch(emphasisManager: emphasisManager, textViewController: textViewController)
193198
return
194199
}
195200

196-
// Create updated emphases with new active state
197-
let updatedEmphases = findMatches.enumerated().map { index, range in
198-
Emphasis(
199-
range: range,
200-
style: .standard,
201-
flash: false,
202-
inactive: index != currentFindMatchIndex,
203-
selectInDocument: index == currentFindMatchIndex
204-
)
205-
}
206-
207-
// Replace all emphases to update state
208-
emphasisManager.replaceEmphases(updatedEmphases, for: EmphasisGroup.find)
201+
updateEmphasesForCurrentMatch(emphasisManager: emphasisManager)
209202
}
210203

211204
func findPanelUpdateMatchCount(_ count: Int) {

0 commit comments

Comments
 (0)