Skip to content

Commit

Permalink
duplicate line improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
TheJoeFin committed Mar 4, 2024
1 parent 03bfd61 commit 54202e6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Text-Grab/Utilities/StringMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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$");
}
}
9 changes: 7 additions & 2 deletions Text-Grab/Views/EditTextWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 54202e6

Please sign in to comment.