AvaloniaEdit icon indicating copy to clipboard operation
AvaloniaEdit copied to clipboard

How to duplicate a line with a hotkey

Open Aegel5 opened this issue 1 year ago • 3 comments

I searched for this command in the file AvaloniaEditCommands.cs, but didn't find anything. Can it be added?

Aegel5 avatar Mar 21 '23 13:03 Aegel5

Ctrl+C,Ctrl+V

ZSYMAX avatar Jun 08 '23 10:06 ZSYMAX

Ctrl+C,Ctrl+V

Absolutly not. With this you need select, copy, move cursor, deselect, paste instead of a simple single hot key. In VS this combination exists and is very usefull.

Aegel5 avatar Jun 13 '23 10:06 Aegel5

you can override OnKeyDown method or add appropriate event handler Ctrl + Shift + D is hotkey in this sample

protected override async void OnKeyDown(KeyEventArgs e)
{
//...
        if (e.HasModifiers(ModifierKeys.Control))
        {
            switch (e.Key)
            {
                case Key.D:
                    if (e.HasModifiers(ModifierKeys.Shift))
                    {
                        EditorHelpers.DoubleSelectedLine(this);
                    }
                    break;
            }
        }
//...
}
    public static void DoubleSelectedLine(TextEditor editor)
    {
        editor.BeginChange();
        var line = editor.Document.Lines[editor.TextArea.Caret.Line - 1];
        string txt = editor.Document.GetText(line.Offset, line.Length);
        editor.Document.Insert(line.EndOffset,"\n"+txt);
        editor.EndChange();
    }

KrzysztofDusko avatar Nov 05 '23 19:11 KrzysztofDusko