Use Single Text Storage For Documents #1740
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Right now,
CodeFileView
is reevaluated each keystroke, causing a severe lag on large files. On top of being reevaluated it does a string compare onCodeFileDocument.content
, which causes a hang for large files that then need to be compared character by character. This comparison isn't consistent, and is up to SwiftUI's heuristic for choosing when to compare the published variable.This PR changes
CodeFileDocument
to use a non-publishedNSTextStorage
object to store the document's contents. This means we have a single copy of the document across the entire app. Before, there was a copy stored in the text view's text storage, as well as in the code file document'scontent
string. This also makes editing more efficient, as the contents of the document aren't copied each keystroke from the text view's text storage to the document's Swift String. Finally, SwiftUI will not attempt to compare views using this property as it's not published and the CodeFileView has a stable identifier.This also enables listening to changes in the document via a new combine object that publishes notifications for changes in the content and cursor position changes. These are used now to update the auto-save timer rather than listening for changes in the content publisher.
Related Issues
Checklist