forked from CodeEditApp/CodeEditSourceEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextView+TextFormation.swift
More file actions
49 lines (44 loc) · 1.29 KB
/
TextView+TextFormation.swift
File metadata and controls
49 lines (44 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//
// TextView+TextFormation.swift
// CodeEditSourceEditor
//
// Created by Khan Winter on 10/14/23.
//
import Foundation
import CodeEditTextView
import TextStory
import TextFormation
extension TextView: TextInterface {
public var selectedRange: NSRange {
get {
return selectionManager
.textSelections
.sorted(by: { $0.range.lowerBound < $1.range.lowerBound })
.first?
.range ?? .zero
}
set {
selectionManager.setSelectedRange(newValue)
}
}
public var length: Int {
textStorage.length
}
public func substring(from range: NSRange) -> String? {
return textStorage.substring(from: range)
}
/// Applies the mutation to the text view.
///
/// If the mutation is empty it will be ignored.
///
/// - Parameter mutation: The mutation to apply.
public func applyMutation(_ mutation: TextMutation) {
guard !mutation.isEmpty else { return }
_undoManager?.registerMutation(mutation)
textStorage.replaceCharacters(in: mutation.range, with: mutation.string)
selectionManager.didReplaceCharacters(
in: mutation.range,
replacementLength: (mutation.string as NSString).length
)
}
}