Stop observing textStorage in SyntaxParser (#1648)

This commit is contained in:
1024jp 2024-06-26 19:54:53 +09:00
parent 3b6a179768
commit dfbd33d760
3 changed files with 7 additions and 14 deletions

View File

@ -11,6 +11,7 @@
### Fixes
- Fix the view layout in the Quick Action bar.
- Fix a potential memory leak.

View File

@ -383,10 +383,12 @@ final class DocumentViewController: NSSplitViewController, ThemeChanging, NSTool
let textStorage = notification.object as! NSTextStorage
guard
textStorage.editedMask.contains(.editedCharacters),
self.focusedTextView?.hasMarkedText() != true
else { return }
guard textStorage.editedMask.contains(.editedCharacters) else { return }
// give up the current parsing
self.document.syntaxParser.cancel()
guard self.focusedTextView?.hasMarkedText() != true else { return }
self.document.analyzer.invalidateContent()
self.outlineParseDebouncer.schedule()

View File

@ -24,7 +24,6 @@
// limitations under the License.
//
import Combine
import Foundation
import AppKit.NSTextStorage
import OSLog
@ -54,11 +53,8 @@ final class SyntaxParser: @unchecked Sendable {
private var outlineParseTask: Task<Void, any Error>?
private var highlightParseTask: Task<Void, any Error>?
private var textEditingObservationTask: Task<Void, any Error>?
private var isHighlighting = false
private var textEditingObserver: AnyCancellable?
// MARK: Lifecycle
@ -68,12 +64,6 @@ final class SyntaxParser: @unchecked Sendable {
self.textStorage = textStorage
self.syntax = syntax
self.name = name
// give up if the string is changed while parsing
self.textEditingObserver = NotificationCenter.default.publisher(for: NSTextStorage.willProcessEditingNotification, object: textStorage)
.map { $0.object as! NSTextStorage }
.filter { $0.editedMask.contains(.editedCharacters) }
.sink { [weak self] _ in self?.highlightParseTask?.cancel() }
}