Skip to content

Commit 261ece1

Browse files
committed
Fix Warnings, Add Test Case
1 parent ac9cbf9 commit 261ece1

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

Sources/CodeEditTextView/EmphasisManager/EmphasisManager.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public final class EmphasisManager {
7070
handleSelections(for: emphases)
7171

7272
// Handle flash animations
73-
for flashingLayer in emphasisGroups[id, default: []].filter { $0.emphasis.flash } {
73+
for flashingLayer in emphasisGroups[id, default: []].filter({ $0.emphasis.flash }) {
7474
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
7575
guard let self = self else { return }
7676
self.applyFadeOutAnimation(to: flashingLayer.layer, textLayer: flashingLayer.textLayer) {
@@ -204,7 +204,7 @@ public final class EmphasisManager {
204204
}
205205
let lineHeight = layoutManager.estimateLineHeight()
206206
let lineBottomPadding = (lineHeight - (lineHeight / layoutManager.lineHeightMultiplier)) / 4
207-
var path = NSBezierPath()
207+
let path = NSBezierPath()
208208
for rect in layoutManager.rectsFor(range: range) {
209209
path.move(to: NSPoint(x: rect.minX, y: rect.maxY - lineBottomPadding))
210210
path.line(to: NSPoint(x: rect.maxX, y: rect.maxY - lineBottomPadding))

Sources/CodeEditTextView/TextLayoutManager/TextLayoutManager+Public.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,7 @@ extension TextLayoutManager {
172172

173173
// Don't make rects in between characters
174174
let realRangeStart = textStorage.rangeOfComposedCharacterSequence(at: range.lowerBound)
175-
?? NSRange(location: range.lowerBound, length: 0)
176175
let realRangeEnd = textStorage.rangeOfComposedCharacterSequence(at: range.upperBound - 1)
177-
?? NSRange(location: range.upperBound - 1, length: 0)
178176

179177
// Fragments are relative to the line
180178
let relativeRange = NSRange(

Sources/CodeEditTextView/TextLayoutManager/TextLayoutManager.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,7 @@ public class TextLayoutManager: NSObject {
212212
/// Lays out all visible lines
213213
func layoutLines(in rect: NSRect? = nil) { // swiftlint:disable:this function_body_length
214214
assertNotInLayout()
215-
guard layoutView?.superview != nil,
216-
let visibleRect = rect ?? delegate?.visibleRect,
215+
guard let visibleRect = rect ?? delegate?.visibleRect,
217216
!isInTransaction,
218217
let textStorage else {
219218
return
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import Testing
2+
import Foundation
3+
@testable import CodeEditTextView
4+
5+
@Suite()
6+
struct EmphasisManagerTests {
7+
@Test() @MainActor
8+
func testFlashEmphasisLayersNotLeaked() {
9+
// Ensure layers are not leaked when switching from flash emphasis to any other emphasis type.
10+
let textView = TextView(string: "Lorem Ipsum")
11+
textView.frame = NSRect(x: 0, y: 0, width: 1000, height: 100)
12+
textView.layoutManager.layoutLines(in: CGRect(origin: .zero, size: CGSize(width: 1000, height: 100)))
13+
textView.emphasisManager?.addEmphasis(
14+
Emphasis(range: NSRange(location: 0, length: 5), style: .standard, flash: true),
15+
for: "e"
16+
)
17+
18+
// Text layer and emphasis layer
19+
#expect(textView.layer?.sublayers?.count == 2)
20+
#expect(textView.emphasisManager?.getEmphases(for: "e").count == 1)
21+
22+
textView.emphasisManager?.replaceEmphases(
23+
[Emphasis(range: NSRange(location: 0, length: 5), style: .underline(color: .red), flash: false)],
24+
for: "e"
25+
)
26+
27+
#expect(textView.layer?.sublayers?.count == 2)
28+
#expect(textView.emphasisManager?.getEmphases(for: "e").count == 1)
29+
30+
textView.emphasisManager?.removeAllEmphases()
31+
32+
// No emphases
33+
#expect(textView.layer?.sublayers?.count == nil)
34+
#expect(textView.emphasisManager?.getEmphases(for: "e").count == 0)
35+
}
36+
}

0 commit comments

Comments
 (0)