Skip to content

Commit 54202e6

Browse files
committed
duplicate line improvement
1 parent 03bfd61 commit 54202e6

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

Text-Grab/Utilities/StringMethods.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,4 +741,9 @@ public static int GetNewLineIndexToRight(ref string mainString, int index)
741741

742742
return newLineIndex;
743743
}
744+
745+
public static bool EndsWithNewline(this string s)
746+
{
747+
return Regex.IsMatch(s, @"\n$");
748+
}
744749
}

Text-Grab/Views/EditTextWindow.xaml.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,8 +1178,13 @@ private void DuplicateSelectedLine(object sender, ExecutedRoutedEventArgs e)
11781178
int selectionLength = PassedTextControl.SelectionLength;
11791179
SelectLine();
11801180
string lineText = PassedTextControl.SelectedText;
1181-
PassedTextControl.SelectedText = lineText + Environment.NewLine + lineText;
1182-
PassedTextControl.Select(replaceCaret + (Environment.NewLine.Length + lineText.Length), selectionLength);
1181+
bool lineEndsInNewLine = lineText.EndsWithNewline();
1182+
PassedTextControl.SelectedText = $"{ lineText}{(lineEndsInNewLine ? "" : Environment.NewLine)}{ lineText}";
1183+
int length = lineText.Length;
1184+
if (!lineEndsInNewLine)
1185+
length += Environment.NewLine.Length;
1186+
1187+
PassedTextControl.Select(replaceCaret + length, selectionLength);
11831188
}
11841189

11851190
private void MarginsMenuItem_Checked(object sender, RoutedEventArgs e)

0 commit comments

Comments
 (0)