Skip to content

Commit 79d71b5

Browse files
committed
Use Unichars
1 parent 9b38030 commit 79d71b5

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

Sources/CodeEditTextView/InvisibleCharacters/InvisibleCharactersDelegate.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public enum InvisibleCharacterStyle: Hashable {
1414
}
1515

1616
public protocol InvisibleCharactersDelegate: AnyObject {
17-
var triggerCharacters: Set<Character> { get }
18-
func invisibleStyle(for character: Character, at range: NSRange, lineRange: NSRange) -> InvisibleCharacterStyle?
17+
var triggerCharacters: Set<UInt16> { get }
18+
func invisibleStyleShouldClearCache() -> Bool
19+
func invisibleStyle(for character: UInt16, at range: NSRange, lineRange: NSRange) -> InvisibleCharacterStyle?
1920
}

Sources/CodeEditTextView/TextLine/LineFragmentRenderer.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public final class LineFragmentRenderer {
4444
/// - context: The drawing context to draw into.
4545
/// - yPos: In the drawing context, what `y` position to start drawing at.
4646
public func draw(lineFragment: LineFragment, in context: CGContext, yPos: CGFloat) {
47+
if invisibleCharacterDelegate?.invisibleStyleShouldClearCache() == true {
48+
attributedStringCache.removeAll(keepingCapacity: true)
49+
}
50+
4751
context.saveGState()
4852
// Removes jagged edges
4953
context.setAllowsAntialiasing(true)
@@ -147,7 +151,7 @@ public final class LineFragmentRenderer {
147151

148152
lazy var offset = CTLineGetStringRange(drawingContext.ctLine).location
149153

150-
for (idx, character) in string.enumerated()
154+
for (idx, character) in string.utf16.enumerated()
151155
where delegate.triggerCharacters.contains(character) {
152156
processInvisibleCharacter(
153157
character: character,
@@ -163,7 +167,7 @@ public final class LineFragmentRenderer {
163167
// Disabling the next lint warning because I *cannot* figure out how to split this up further.
164168

165169
private func processInvisibleCharacter( // swiftlint:disable:this function_parameter_count
166-
character: Character,
170+
character: UInt16,
167171
at index: Int,
168172
in range: NSRange,
169173
offset: Int,

0 commit comments

Comments
 (0)