// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Unity.CodeEditor.Document;
using Unity.CodeEditor.Platform;
using UnityEngine;
using UnityEngine.UIElements;
namespace Unity.CodeEditor.Editing
{
///
/// We re-use the CommandBinding and InputBinding instances between multiple text areas,
/// so this class is static.
///
internal class EditingCommandHandler
{
Dictionary _editingCommands = new Dictionary();
internal EditingCommandHandler(TextArea textArea)
{
TextArea = textArea ?? throw new ArgumentNullException(nameof(textArea));
var keymap = PlatformHotkeyConfiguration.Current;
_editingCommands.Add(new KeyGesture(KeyCode.Delete), EditingCommand.Delete);
_editingCommands.Add(new KeyGesture(KeyCode.Delete, KeyModifiers.Control), EditingCommand.DeleteNextWord);
_editingCommands.Add(new KeyGesture(KeyCode.Backspace), EditingCommand.Backspace);
_editingCommands.Add(new KeyGesture(KeyCode.Backspace, KeyModifiers.Shift), EditingCommand.Backspace); // make Shift-Backspace do the same as plain backspace
_editingCommands.Add(new KeyGesture(KeyCode.Backspace, KeyModifiers.Control), EditingCommand.DeletePreviousWord);
_editingCommands.Add(new KeyGesture(KeyCode.KeypadEnter), EditingCommand.EnterParagraphBreak);
_editingCommands.Add(new KeyGesture(KeyCode.Return), EditingCommand.EnterParagraphBreak);
_editingCommands.Add(new KeyGesture(KeyCode.Return, KeyModifiers.Shift), EditingCommand.EnterLineBreak);
_editingCommands.Add(new KeyGesture(KeyCode.Tab), EditingCommand.TabForward);
_editingCommands.Add(new KeyGesture(KeyCode.Tab, KeyModifiers.Shift), EditingCommand.TabBackward);
keymap.Copy.ForEach(g => _editingCommands.Add(g, EditingCommand.Copy));
keymap.Cut.ForEach(g => _editingCommands.Add(g, EditingCommand.Cut));
keymap.Paste.ForEach(g => _editingCommands.Add(g, EditingCommand.Paste));
_editingCommands.Add(new KeyGesture(KeyCode.Insert), EditingCommand.ToggleOverstrike);
keymap.DeleteWholeLine.ForEach(g => _editingCommands.Add(g, EditingCommand.DeleteLine));
_editingCommands.Add(new KeyGesture(KeyCode.I, keymap.CommandModifiers), EditingCommand.IndentSelection);
keymap.Undo.ForEach(g => _editingCommands.Add(g, EditingCommand.Undo));
keymap.Redo.ForEach(g => _editingCommands.Add(g, EditingCommand.Redo));
}
internal TextArea TextArea { get; }
internal void OnKeyDown(KeyDownEvent e)
{
if (TextArea?.Document == null)
return;
EditingCommand command = GetEditingCommand(e);
switch (command)
{
case EditingCommand.Delete:
Delete();
e.StopPropagation();
break;
case EditingCommand.Copy:
Copy();
e.StopPropagation();
break;
case EditingCommand.Cut:
Cut();
e.StopPropagation();
break;
case EditingCommand.Paste:
Paste();
e.StopPropagation();
break;
case EditingCommand.DeleteNextWord:
DeleteNextWord();
e.StopPropagation();
break;
case EditingCommand.Backspace:
Backspace();
e.StopPropagation();
break;
case EditingCommand.DeletePreviousWord:
DeletePreviousWord();
e.StopPropagation();
break;
case EditingCommand.EnterParagraphBreak:
EnterParagraphBreak();
e.StopPropagation();
break;
case EditingCommand.EnterLineBreak:
EnterLineBreak();
e.StopPropagation();
break;
case EditingCommand.TabForward:
TabForward();
e.StopPropagation();
break;
case EditingCommand.TabBackward:
TabBackward();
e.StopPropagation();
break;
case EditingCommand.ToggleOverstrike:
ToggleOverstrike();
e.StopPropagation();
break;
case EditingCommand.DeleteLine:
DeleteLine();
e.StopPropagation();
break;
case EditingCommand.IndentSelection:
IndentSelection();
e.StopPropagation();
break;
case EditingCommand.Undo:
ExecuteUndo();
e.StopPropagation();
break;
case EditingCommand.Redo:
ExecuteRedo();
e.StopPropagation();
break;
}
}
EditingCommand GetEditingCommand(KeyDownEvent e)
{
KeyGesture keyGesture = new KeyGesture(e.keyCode, e.GetKeyModifiers());
if (_editingCommands.TryGetValue(keyGesture, out EditingCommand command))
return command;
return EditingCommand.None;
}
void Paste()
{
OnPaste(TextArea);
}
void Cut()
{
OnCut(TextArea);
}
void IndentSelection()
{
OnIndentSelection(TextArea);
}
void DeleteLine()
{
OnDeleteLine(TextArea);
}
void ToggleOverstrike()
{
OnToggleOverstrike(TextArea);
}
void TabBackward()
{
OnShiftTab(TextArea);
}
void TabForward()
{
OnTab(TextArea);
}
void EnterLineBreak()
{
OnEnter(TextArea);
}
void EnterParagraphBreak()
{
OnEnter(TextArea);
}
void DeletePreviousWord()
{
OnDelete(TextArea, CaretMovementType.WordLeft);
}
void Backspace()
{
OnDelete(TextArea, CaretMovementType.Backspace);
}
void Copy()
{
OnCopy(TextArea);
}
void Delete()
{
OnDelete(TextArea, CaretMovementType.CharRight);
}
void DeleteNextWord()
{
OnDelete(TextArea, CaretMovementType.WordRight);
}
#region Text Transformation Helpers
private enum DefaultSegmentType
{
WholeDocument,
CurrentLine
}
///
/// Calls transformLine on all lines in the selected range.
/// transformLine needs to handle read-only segments!
///
private static void TransformSelectedLines(Action