Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
import SwiftUI
import UniformTypeIdentifiers

struct CodeEditSourceEditorExampleDocument: FileDocument {
var text: String
struct CodeEditSourceEditorExampleDocument: FileDocument, @unchecked Sendable {
enum DocumentError: Error {
case failedToEncode
}

var text: NSTextStorage

init(text: String = "") {
init(text: NSTextStorage = NSTextStorage(string: "")) {
self.text = text
}

Expand All @@ -25,11 +29,31 @@ struct CodeEditSourceEditorExampleDocument: FileDocument {
guard let data = configuration.file.regularFileContents else {
throw CocoaError(.fileReadCorruptFile)
}
text = String(decoding: data, as: UTF8.self)
var nsString: NSString?
NSString.stringEncoding(
for: data,
encodingOptions: [
// Fail if using lossy encoding.
.allowLossyKey: false,
// In a real app, you'll want to handle more than just this encoding scheme. Check out CodeEdit's
// implementation for a more involved solution.
.suggestedEncodingsKey: [NSUTF8StringEncoding],
.useOnlySuggestedEncodingsKey: true
],
convertedString: &nsString,
usedLossyConversion: nil
)
if let nsString {
self.text = NSTextStorage(string: nsString as String)
} else {
fatalError("Failed to read file")
}
}

func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
let data = Data(text.utf8)
guard let data = (text.string as NSString?)?.data(using: NSUTF8StringEncoding) else {
throw DocumentError.failedToEncode
}
return .init(regularFileWithContents: data)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct ContentView: View {
var body: some View {
GeometryReader { proxy in
CodeEditSourceEditor(
$document.text,
document.text,
language: language,
theme: theme,
font: font,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct StatusBar: View {
var body: some View {
HStack {
Menu {
IndentPicker(indentOption: $indentOption, enabled: document.text.isEmpty)
IndentPicker(indentOption: $indentOption, enabled: document.text.length == 0)
.buttonStyle(.borderless)
Toggle("Wrap Lines", isOn: $wrapLines)
Toggle("Show Minimap", isOn: $showMinimap)
Expand Down Expand Up @@ -106,8 +106,8 @@ struct StatusBar: View {
guard let fileURL else { return nil }
return CodeLanguage.detectLanguageFrom(
url: fileURL,
prefixBuffer: document.text.getFirstLines(5),
suffixBuffer: document.text.getLastLines(5)
prefixBuffer: document.text.string.getFirstLines(5),
suffixBuffer: document.text.string.getLastLines(5)
)
}

Expand Down