Optimize url detection (close #831)

This commit is contained in:
1024jp 2018-07-31 12:44:42 +09:00
parent d72efa5f8b
commit 54742deda1
2 changed files with 18 additions and 9 deletions

View File

@ -2,7 +2,16 @@
Change Log
==========================
3.5.0 (unreleased)
3.5.0-beta.2 (unreleased)
--------------------------
### Improvements
- Optimize auto URL detection with paste to a large document.
3.5.0-beta (263)
--------------------------
### New Features

View File

@ -1255,21 +1255,21 @@ final class EditorTextView: NSTextView, Themable {
}
/// make link-like text clickable
/// make URL-like text clickable
private func detectLinkIfNeeded() {
assert(Thread.isMainThread)
guard self.isAutomaticLinkDetectionEnabled else { return }
self.undoManager?.disableUndoRegistration()
// -> use own dataDetector instead of `checkTextInDocument(_:)` due to performance issue (2018-07)
let detector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
let currentCheckingType = self.enabledTextCheckingTypes
self.enabledTextCheckingTypes = NSTextCheckingResult.CheckingType.link.rawValue
self.checkTextInDocument(nil)
self.enabledTextCheckingTypes = currentCheckingType
self.undoManager?.enableUndoRegistration()
detector.enumerateMatches(in: self.string, range: self.string.nsRange) { (result, _, _) in
guard let result = result, let url = result.url else { return }
self.textStorage?.addAttribute(.link, value: url, range: result.range)
}
}