Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Base subclass of NSTextStorage in Swift
import Cocoa
@objc
class SomeTextStorage: NSTextStorage {
private var storage: NSMutableAttributedString
override init() {
storage = NSMutableAttributedString(string: "", attributes: nil)
super.init()
}
required init?(coder aDecoder: NSCoder) {
fatalError("\(__FUNCTION__) is not supported")
}
required init?(pasteboardPropertyList propertyList: AnyObject, ofType type: String) {
fatalError("\(__FUNCTION__) is not supported")
}
// MARK: NSTextStorage Primitive Methods
// https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/TextStorageLayer/Tasks/Subclassing.html
override var string: String {
return storage.string
}
override func attributesAtIndex(location: Int, effectiveRange range: NSRangePointer) -> [String : AnyObject] {
return storage.attributesAtIndex(location, effectiveRange: range)
}
override func replaceCharactersInRange(range: NSRange, withString str: String) {
beginEditing()
storage.replaceCharactersInRange(range, withString: str)
edited(.EditedCharacters, range: range, changeInLength: (str as NSString).length - range.length)
endEditing()
}
override func setAttributes(attrs: [String : AnyObject]?, range: NSRange) {
beginEditing()
storage.setAttributes(attrs, range: range)
edited(.EditedAttributes, range: range, changeInLength: 0)
endEditing()
}
}
@AquaGeek

This comment has been minimized.

Copy link

commented Feb 17, 2017

private var storage: NSMutableAttributedString

FYI, using an instance of NSTextStorage here instead seems to be much more performant, especially on large documents.

@markgaensicke

This comment has been minimized.

Copy link

commented May 27, 2017

Swift 3 version:

import Cocoa

@objc
class SomeTextStorage: NSTextStorage {

    private var storage: NSMutableAttributedString
    
    override init() {
        storage = NSMutableAttributedString(string: "", attributes: nil)
        super.init()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("\(#function) is not supported")
    }
    
    required init?(pasteboardPropertyList propertyList: AnyObject, ofType type: String) {
        fatalError("\(#function) is not supported")
    }
    
    // MARK: NSTextStorage Primitive Methods
    // https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/TextStorageLayer/Tasks/Subclassing.html
    
    override var string: String {
        return storage.string
    }
    
    override func attributes(at location: Int, effectiveRange range: NSRangePointer?) -> [String : Any] {
        return storage.attributes(at: location, effectiveRange: range)
    }
    
    override func replaceCharacters(in range: NSRange, with str: String) {
        beginEditing()
        storage.replaceCharacters(in: range, with: str)
        edited(.editedCharacters, range: range, changeInLength: (str as NSString).length - range.length)
        endEditing()
    }
    
    override func setAttributes(_ attrs: [String : Any]?, range: NSRange) {
        beginEditing()
        storage.setAttributes(attrs, range: range)
        edited(.editedAttributes, range: range, changeInLength: 0)
        endEditing()
    }

}
@DivineDominion

This comment has been minimized.

Copy link

commented Jul 13, 2017

Great, thanks! Wrapping the mutating methods in beginEditing/endEditing even though it's not strictly necessary solves a lot of delegate callback problems.

But I also notice that this minimal replacement type is enough to let memory consumption spike when editing if you also replace the layout manager, like with textView.textContainer?.replaceLayoutManager(NSLayoutManager()):

  • put 14k words in text view
  • scroll down a bit
  • hit enter a couple of times

According to instruments, I can get 8 GB of allocations in 8 seconds, mostly due to the string read-only property.

The memory issue is gone when you delegate to a NSTextStorage instead of a NSAttributedString, though:
https://stackoverflow.com/questions/37952726/sub-classing-nstextstorage-causes-significant-memory-issues

class SomeTextStorage: NSTextStorage {

    private var storage = NSTextStorage()
        
    // MARK: NSTextStorage Primitive Methods
    // https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/TextStorageLayer/Tasks/Subclassing.html
    
    override var string: String {
        return storage.string
    }
    
    override func attributes(at location: Int, effectiveRange range: NSRangePointer?) -> [String : Any] {
        return storage.attributes(at: location, effectiveRange: range)
    }
    
    override func replaceCharacters(in range: NSRange, with str: String) {
        beginEditing()
        storage.replaceCharacters(in: range, with: str)
        edited(.editedCharacters, range: range, changeInLength: (str as NSString).length - range.length)
        endEditing()
    }
    
    override func setAttributes(_ attrs: [String : Any]?, range: NSRange) {
        beginEditing()
        storage.setAttributes(attrs, range: range)
        edited(.editedAttributes, range: range, changeInLength: 0)
        endEditing()
    }
}

Since beginEditing/endEditing is not needed here but works nicely, I do not call these methods on storage. Note that I am not 100% certain if the behavior will change for the better or worse in some circumstances if you do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.