Skip to content

Commit 6f1ceca

Browse files
Use Better Text Storage In Example (#328)
### Description Updates the example app to use an NSTextStorage object instead of a plain String for the editor's contents. This should help with debugging for CodeEdit, to help compare apples to apples performance-wise. For editing any non-trivial file, `String` causes SwiftUI to perform a character-by-character comparison on the string to check if the view needs invalidating. ### Related Issues N/A ### Checklist - [x] I read and understood the [contributing guide](https://github.com/CodeEditApp/CodeEdit/blob/main/CONTRIBUTING.md) as well as the [code of conduct](https://github.com/CodeEditApp/CodeEdit/blob/main/CODE_OF_CONDUCT.md) - [x] The issues this PR addresses are related to each other - [x] My changes generate no new warnings - [x] My code builds and runs on my machine - [x] My changes are all related to the related issue above - [x] I documented my code ### Screenshots
1 parent 162f3d8 commit 6f1ceca

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

Example/CodeEditSourceEditorExample/CodeEditSourceEditorExample/Documents/CodeEditSourceEditorExampleDocument.swift

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88
import SwiftUI
99
import UniformTypeIdentifiers
1010

11-
struct CodeEditSourceEditorExampleDocument: FileDocument {
12-
var text: String
11+
struct CodeEditSourceEditorExampleDocument: FileDocument, @unchecked Sendable {
12+
enum DocumentError: Error {
13+
case failedToEncode
14+
}
15+
16+
var text: NSTextStorage
1317

14-
init(text: String = "") {
18+
init(text: NSTextStorage = NSTextStorage(string: "")) {
1519
self.text = text
1620
}
1721

@@ -25,11 +29,31 @@ struct CodeEditSourceEditorExampleDocument: FileDocument {
2529
guard let data = configuration.file.regularFileContents else {
2630
throw CocoaError(.fileReadCorruptFile)
2731
}
28-
text = String(decoding: data, as: UTF8.self)
32+
var nsString: NSString?
33+
NSString.stringEncoding(
34+
for: data,
35+
encodingOptions: [
36+
// Fail if using lossy encoding.
37+
.allowLossyKey: false,
38+
// In a real app, you'll want to handle more than just this encoding scheme. Check out CodeEdit's
39+
// implementation for a more involved solution.
40+
.suggestedEncodingsKey: [NSUTF8StringEncoding],
41+
.useOnlySuggestedEncodingsKey: true
42+
],
43+
convertedString: &nsString,
44+
usedLossyConversion: nil
45+
)
46+
if let nsString {
47+
self.text = NSTextStorage(string: nsString as String)
48+
} else {
49+
fatalError("Failed to read file")
50+
}
2951
}
3052

3153
func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
32-
let data = Data(text.utf8)
54+
guard let data = (text.string as NSString?)?.data(using: NSUTF8StringEncoding) else {
55+
throw DocumentError.failedToEncode
56+
}
3357
return .init(regularFileWithContents: data)
3458
}
3559
}

Example/CodeEditSourceEditorExample/CodeEditSourceEditorExample/Views/ContentView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ struct ContentView: View {
3939
var body: some View {
4040
GeometryReader { proxy in
4141
CodeEditSourceEditor(
42-
$document.text,
42+
document.text,
4343
language: language,
4444
theme: theme,
4545
font: font,

Example/CodeEditSourceEditorExample/CodeEditSourceEditorExample/Views/StatusBar.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct StatusBar: View {
3030
var body: some View {
3131
HStack {
3232
Menu {
33-
IndentPicker(indentOption: $indentOption, enabled: document.text.isEmpty)
33+
IndentPicker(indentOption: $indentOption, enabled: document.text.length == 0)
3434
.buttonStyle(.borderless)
3535
Toggle("Wrap Lines", isOn: $wrapLines)
3636
Toggle("Show Minimap", isOn: $showMinimap)
@@ -106,8 +106,8 @@ struct StatusBar: View {
106106
guard let fileURL else { return nil }
107107
return CodeLanguage.detectLanguageFrom(
108108
url: fileURL,
109-
prefixBuffer: document.text.getFirstLines(5),
110-
suffixBuffer: document.text.getLastLines(5)
109+
prefixBuffer: document.text.string.getFirstLines(5),
110+
suffixBuffer: document.text.string.getLastLines(5)
111111
)
112112
}
113113

0 commit comments

Comments
 (0)