From 01d020690e74820be652ad6fbaa8f8e7cf5cb6bc Mon Sep 17 00:00:00 2001 From: Olga Mokshantseva Date: Fri, 30 May 2025 08:42:40 -0700 Subject: [PATCH] [Feature request] Auto insert new line at eof #1412 --- src/Notepads/Controls/TextEditor/TextEditor.xaml.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Notepads/Controls/TextEditor/TextEditor.xaml.cs b/src/Notepads/Controls/TextEditor/TextEditor.xaml.cs index dd30c6a90..c0df0451c 100644 --- a/src/Notepads/Controls/TextEditor/TextEditor.xaml.cs +++ b/src/Notepads/Controls/TextEditor/TextEditor.xaml.cs @@ -723,11 +723,23 @@ private async Task SaveContentToFileAsync(StorageFile file) var text = TextEditorCore.GetText(); var encoding = RequestedEncoding ?? LastSavedSnapshot.Encoding; var lineEnding = RequestedLineEnding ?? LastSavedSnapshot.LineEnding; + text = EnsureEndsWithLineEnding(text); await FileSystemUtility.WriteTextToFileAsync(file, LineEndingUtility.ApplyLineEnding(text, lineEnding), encoding); // Will throw if not succeeded var newFileModifiedTime = await FileSystemUtility.GetDateModifiedAsync(file); return new TextFile(text, encoding, lineEnding, newFileModifiedTime); } + private string EnsureEndsWithLineEnding(string text) + { + if (string.IsNullOrEmpty(text)) return text; + if (!text.EndsWith("\n") && !text.EndsWith("\r")) + { + var effectiveLineEnding = RequestedLineEnding ?? LastSavedSnapshot.LineEnding; + return text + LineEndingUtility.GetLineEndingName(effectiveLineEnding); + } + return text; + } + public string GetContentForSharing() { return TextEditorCore.Document.Selection.StartPosition == TextEditorCore.Document.Selection.EndPosition ?