JavaScript Highlighter: Throttle rehighlighting to improve performance
while typing
This commit is contained in:
parent
6b39e90ea4
commit
4c65ff612d
|
@ -7,12 +7,13 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
import AppKit
|
import AppKit
|
||||||
|
import Combine
|
||||||
|
|
||||||
class JavaScriptEditorView: NSTextView {
|
class JavaScriptEditorView: NSTextView {
|
||||||
|
|
||||||
var highlighter = JavaScriptHighlighter()
|
var highlighter = JavaScriptHighlighter()
|
||||||
private var isRehighlighting = false
|
private var isRehighlighting = false
|
||||||
|
|
||||||
override var string: String {
|
override var string: String {
|
||||||
get {
|
get {
|
||||||
super.string
|
super.string
|
||||||
|
@ -23,6 +24,9 @@ class JavaScriptEditorView: NSTextView {
|
||||||
rehighlight()
|
rehighlight()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private let rehighlightSubject = PassthroughSubject<Void, Never>()
|
||||||
|
private var debouncedRehighlightCancellable: AnyCancellable!
|
||||||
|
|
||||||
func rehighlight() {
|
func rehighlight() {
|
||||||
isRehighlighting = true
|
isRehighlighting = true
|
||||||
|
@ -34,6 +38,13 @@ class JavaScriptEditorView: NSTextView {
|
||||||
super.awakeFromNib()
|
super.awakeFromNib()
|
||||||
|
|
||||||
textStorage!.delegate = self
|
textStorage!.delegate = self
|
||||||
|
|
||||||
|
typingAttributes = [
|
||||||
|
.font: NSFont.monospacedSystemFont(ofSize: 13, weight: .regular),
|
||||||
|
.foregroundColor: NSColor.textColor
|
||||||
|
]
|
||||||
|
|
||||||
|
debouncedRehighlightCancellable = rehighlightSubject.throttle(for: .milliseconds(40), scheduler: RunLoop.main, latest: false).sink(receiveValue: rehighlight)
|
||||||
}
|
}
|
||||||
|
|
||||||
override func shouldChangeText(in affectedCharRange: NSRange, replacementString: String?) -> Bool {
|
override func shouldChangeText(in affectedCharRange: NSRange, replacementString: String?) -> Bool {
|
||||||
|
@ -117,6 +128,6 @@ extension JavaScriptEditorView: NSTextStorageDelegate {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rehighlight()
|
rehighlightSubject.send()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue