From dfbd33d7605ecc75c732756ad1b5fc9727899526 Mon Sep 17 00:00:00 2001 From: 1024jp <1024jp@wolfrosch.com> Date: Wed, 26 Jun 2024 19:54:53 +0900 Subject: [PATCH] Stop observing textStorage in SyntaxParser (#1648) --- CHANGELOG.md | 1 + CotEditor/Sources/DocumentViewController.swift | 10 ++++++---- CotEditor/Sources/SyntaxParser.swift | 10 ---------- 3 files changed, 7 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 37f21a32d..70848bcb1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ ### Fixes - Fix the view layout in the Quick Action bar. +- Fix a potential memory leak. diff --git a/CotEditor/Sources/DocumentViewController.swift b/CotEditor/Sources/DocumentViewController.swift index 1b7c6be26..45a8baa55 100644 --- a/CotEditor/Sources/DocumentViewController.swift +++ b/CotEditor/Sources/DocumentViewController.swift @@ -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() diff --git a/CotEditor/Sources/SyntaxParser.swift b/CotEditor/Sources/SyntaxParser.swift index bee79d5ef..e05c0e161 100644 --- a/CotEditor/Sources/SyntaxParser.swift +++ b/CotEditor/Sources/SyntaxParser.swift @@ -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? private var highlightParseTask: Task? - private var textEditingObservationTask: Task? 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() } }