Skip to content

Commit

Permalink
Added proper indentation pasting (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsTheSky committed May 16, 2024
1 parent 7d19cd3 commit 0fedf2c
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions SkEditor/Utilities/Editor/TextEditorEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,27 +225,34 @@ public static void OnPointerPressed(object? sender, PointerPressedEventArgs e)

public static void OnTextPasting(object? sender, TextEventArgs e)
{
// TODO: Add auto-indentation for pasted text
return;

string properText = e.Text; // TODO: Handle bad indented copied code

TextEditor textEditor = ApiVault.Get().GetTextEditor();
DocumentLine line = textEditor.Document.GetLineByOffset(textEditor.CaretOffset);

string lineText = textEditor.Document.GetText(line);

int indentationSize = lineText.TakeWhile(char.IsWhiteSpace).Count();
string indentationType = lineText[..indentationSize];

string textToPaste = e.Text;
string[] lines = textToPaste.Split(new[] { Environment.NewLine }, StringSplitOptions.None);

for (int i = 0; i < lines.Length; i++)
string indentation = "";
foreach (char c in lineText)
{

if (char.IsWhiteSpace(c)) indentation += c;
else break;
}

string adjustedTextToPaste = string.Join(Environment.NewLine, lines);

e.Text = adjustedTextToPaste;

string[] pastes = properText.Split([Environment.NewLine], StringSplitOptions.None);

if (pastes.Length == 1)
{
e.Text = indentation + properText;
return;
}

StringBuilder sb = new();
foreach (string paste in pastes)
{
sb.AppendLine(indentation + paste);
}

e.Text = sb.ToString().Trim();
}

public static void CheckForSpecialPaste(object? sender, TextEventArgs e)
Expand Down Expand Up @@ -285,7 +292,6 @@ public static void CheckForSpecialPaste(object? sender, TextEventArgs e)
}
}


[GeneratedRegex(@"<##(?:[0-9a-fA-F]{3}){1,2}>", RegexOptions.Compiled)]
private static partial Regex HexRegex();
[GeneratedRegex("")]
Expand Down

0 comments on commit 0fedf2c

Please sign in to comment.