Skip to content
This repository has been archived by the owner on Feb 13, 2019. It is now read-only.

Swift 4.2 syntax #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Swift 4.2 syntax #20

wants to merge 2 commits into from

Conversation

timojaask
Copy link

Tried to convert to Swift 4.2 syntax. This is a bit scary since there are no tests, but most of the changes are just renaming things, so should be ok. The only part where I had to refactor a bit more is the following:

processEditing was using NSTextStorageEditActions for both platforms, like so:

override func processEditing(for textStorage: NSTextStorage, edited editMask: NSTextStorageEditActions, range newCharRange: NSRange, changeInLength delta: Int, invalidatedRange invalidatedCharRange: NSRange) {
	super.processEditing(for: textStorage, edited: editMask, range: newCharRange, changeInLength: delta, invalidatedRange: invalidatedCharRange)
	if invalidatedCharRange.location < lastParaLocation {
		//  When the backing store is edited ahead the cached paragraph location, invalidate the cache and force a complete
		//  recalculation.  We cannot be much smarter than this because we don't know how many paragraphs have been deleted
		//  since the text has already been removed from the backing store.
		lastParaLocation = 0
		lastParaNumber = 0
	}
}

However, it seems that in 4.2 it was renamed to NSTextStorage.EditActions on iOS platform, but not on macOS. So I decided to refactor it like this:

#if os(macOS)
override func processEditing(for textStorage: NSTextStorage, edited editMask:NSTextStorageEditActions, range newCharRange: NSRange, changeInLength delta: Int, invalidatedRange invalidatedCharRange: NSRange) {
    super.processEditing(for: textStorage, edited: editMask, range: newCharRange, changeInLength: delta, invalidatedRange: invalidatedCharRange)
    processEditing(invalidatedCharRange: invalidatedCharRange)
}
#else
override func processEditing(for textStorage: NSTextStorage, edited editMask:NSTextStorage.EditActions, range newCharRange: NSRange, changeInLength delta: Int, invalidatedRange invalidatedCharRange: NSRange) {
    super.processEditing(for: textStorage, edited: editMask, range: newCharRange, changeInLength: delta, invalidatedRange: invalidatedCharRange)
    processEditing(invalidatedCharRange: invalidatedCharRange)
}
#endif

func processEditing(invalidatedCharRange: NSRange) {
    if invalidatedCharRange.location < lastParaLocation {
        //  When the backing store is edited ahead the cached paragraph location, invalidate the cache and force a complete
        //  recalculation.  We cannot be much smarter than this because we don't know how many paragraphs have been deleted
        //  since the text has already been removed from the backing store.
        lastParaLocation = 0
        lastParaNumber = 0
    }
}

Hopefully this makes sense.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant