Perform auto whitespace trimming before explicit saving (close #1444)

This commit is contained in:
1024jp 2023-03-14 10:43:50 +09:00
parent 0ed700f417
commit 250072c6e3
2 changed files with 11 additions and 2 deletions

View File

@ -20,6 +20,7 @@ Change Log
- Update the character inspector to add labels for each item and display the Unicode version.
- Deprecate the print font setting in the Print settings.
- Deprecate the cursor type option.
- Make sure to perform the automatic whitespace trimming every time before explicit saving by the user.
- Migrate the sharing interface to the modern version.
- [trivial] Migrate the Opacity toolbar item to popover in macOS 14.
- [trivial] Disable the contextual menu for shortcut fields.

View File

@ -418,9 +418,17 @@ final class Document: NSDocument, AdditionalDocumentPreparing, EncodingHolder {
override func save(to url: URL, ofType typeName: String, for saveOperation: NSDocument.SaveOperationType, completionHandler: @escaping ((any Error)?) -> Void) {
assert(Thread.isMainThread)
// break undo grouping
for layoutManager in self.textStorage.layoutManagers {
layoutManager.textViewForBeginningOfSelection?.breakUndoCoalescing()
let textViews = self.textStorage.layoutManagers.compactMap(\.textViewForBeginningOfSelection)
for textView in textViews {
textView.breakUndoCoalescing()
}
// trim trailing whitespace if needed
if !saveOperation.isAutosave, UserDefaults.standard[.autoTrimsTrailingWhitespace] {
textViews.first?.trimTrailingWhitespace(ignoresEmptyLines: !UserDefaults.standard[.trimsWhitespaceOnlyLines])
}
// workaround the issue that invoking the async version super blocks the save process