diff --git a/Text-Grab/Utilities/StringMethods.cs b/Text-Grab/Utilities/StringMethods.cs index 8c640aba..1806eb9f 100644 --- a/Text-Grab/Utilities/StringMethods.cs +++ b/Text-Grab/Utilities/StringMethods.cs @@ -741,4 +741,9 @@ public static int GetNewLineIndexToRight(ref string mainString, int index) return newLineIndex; } + + public static bool EndsWithNewline(this string s) + { + return Regex.IsMatch(s, @"\n$"); + } } diff --git a/Text-Grab/Views/EditTextWindow.xaml.cs b/Text-Grab/Views/EditTextWindow.xaml.cs index 2c0b1f1a..0335ffbc 100644 --- a/Text-Grab/Views/EditTextWindow.xaml.cs +++ b/Text-Grab/Views/EditTextWindow.xaml.cs @@ -1178,8 +1178,13 @@ private void DuplicateSelectedLine(object sender, ExecutedRoutedEventArgs e) int selectionLength = PassedTextControl.SelectionLength; SelectLine(); string lineText = PassedTextControl.SelectedText; - PassedTextControl.SelectedText = lineText + Environment.NewLine + lineText; - PassedTextControl.Select(replaceCaret + (Environment.NewLine.Length + lineText.Length), selectionLength); + bool lineEndsInNewLine = lineText.EndsWithNewline(); + PassedTextControl.SelectedText = $"{ lineText}{(lineEndsInNewLine ? "" : Environment.NewLine)}{ lineText}"; + int length = lineText.Length; + if (!lineEndsInNewLine) + length += Environment.NewLine.Length; + + PassedTextControl.Select(replaceCaret + length, selectionLength); } private void MarginsMenuItem_Checked(object sender, RoutedEventArgs e)