Skip to content

Commit 02b6919

Browse files
Mark Autosave Changes Immediately (#1865)
- Modifies `CodeFileView` to mark the document as changed immediately when changes occur rather than debounced. This fixes a small issue where the "Edited" marker would appear after the file was saved, because the change notification was debounced. - Adds some error logging to `CodeFileDocument`.
1 parent 4bc7548 commit 02b6919

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

CodeEdit/Features/Documents/CodeFileDocument/CodeFileDocument.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import CodeEditSourceEditor
1313
import CodeEditTextView
1414
import CodeEditLanguages
1515
import Combine
16+
import OSLog
1617

1718
enum CodeFileError: Error {
1819
case failedToDecode
@@ -26,6 +27,8 @@ final class CodeFileDocument: NSDocument, ObservableObject {
2627
let cursorPositions: [CursorPosition]
2728
}
2829

30+
static let logger = Logger(subsystem: Bundle.main.bundleIdentifier ?? "", category: "CodeFileDocument")
31+
2932
/// The text content of the document, stored as a text storage
3033
///
3134
/// This is intentionally not a `@Published` variable. If it were published, SwiftUI would do a string
@@ -121,6 +124,7 @@ final class CodeFileDocument: NSDocument, ObservableObject {
121124

122125
override func data(ofType _: String) throws -> Data {
123126
guard let sourceEncoding, let data = (content?.string as NSString?)?.data(using: sourceEncoding.nsValue) else {
127+
Self.logger.error("Failed to encode contents to \(self.sourceEncoding.debugDescription)")
124128
throw CodeFileError.failedToEncode
125129
}
126130
return data
@@ -143,6 +147,8 @@ final class CodeFileDocument: NSDocument, ObservableObject {
143147
if let validEncoding = FileEncoding(rawEncoding), let nsString {
144148
self.sourceEncoding = validEncoding
145149
self.content = NSTextStorage(string: nsString as String)
150+
} else {
151+
Self.logger.error("Failed to read file from data using encoding: \(rawEncoding)")
146152
}
147153
}
148154

CodeEdit/Features/Editor/Views/CodeFileView.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,23 @@ struct CodeFileView: View {
6767
codeFile
6868
.contentCoordinator
6969
.textUpdatePublisher
70-
.debounce(for: 1.0, scheduler: DispatchQueue.main)
7170
.sink { _ in
7271
codeFile.updateChangeCount(.changeDone)
73-
codeFile.autosave(withImplicitCancellability: false) { _ in }
72+
}
73+
.store(in: &cancellables)
74+
75+
codeFile
76+
.contentCoordinator
77+
.textUpdatePublisher
78+
.debounce(for: 1.0, scheduler: DispatchQueue.main)
79+
.sink { _ in
80+
codeFile.autosave(withImplicitCancellability: false) { error in
81+
if let error {
82+
CodeFileDocument.logger.error("Failed to autosave document, error: \(error)")
83+
} else {
84+
codeFile.updateChangeCount(.changeCleared)
85+
}
86+
}
7487
}
7588
.store(in: &cancellables)
7689

0 commit comments

Comments
 (0)