Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Data;
- namespace Seminary3
- {
- public enum ActionName : byte
- {
- Insertion,
- Deletion,
- CursorMovemention
- }
- public class Command
- {
- public Command(ActionName typeAction, int elementPosition, char element)
- {
- TypeAction = typeAction;
- ElementPosition = elementPosition;
- Element = element;
- }
- public ActionName TypeAction { get; }
- public int ElementPosition { get; }
- public char Element { get; }
- }
- class Cursor
- {
- public static int Position { get; set; }
- }
- class TextEditor
- {
- public TextEditor(List<char> inputText)
- {
- Text = inputText;
- UndoCommandsStack = new Stack<Command>();
- RedoCommandsStack = new Stack<Command>();
- }
- private static List<char> Text { get; set; }
- private Stack<Command> UndoCommandsStack { get; }
- private Stack<Command> RedoCommandsStack { get; }
- private static Stack<Command> ExecutedCommands { get; set; }
- public void MoveCursorTo(int position)
- {
- Cursor.Position = position;
- var newCommand = new Command(ActionName.CursorMovemention, position, default);
- ExecutedCommands.Push(newCommand);
- }
- public static void InsertChar(char inputSymbol)
- {
- Text.Insert(Cursor.Position, inputSymbol);
- Cursor.Position++;
- var newCommand = new Command(ActionName.Insertion, Cursor.Position, inputSymbol);
- ExecutedCommands.Push(newCommand);
- }
- public void DeleteChar()
- {
- var delSymbol = Text[Cursor.Position];
- Text.RemoveAt(Cursor.Position);
- if (Cursor.Position > 0) Cursor.Position--;
- var newCommand = new Command(
- ActionName.Deletion, Cursor.Position, delSymbol);
- ExecutedCommands.Push(newCommand);
- }
- private bool CanUndo() => ExecutedCommands.Count != 0;
- public void Undo()
- {
- if (CanUndo())
- {
- var lastCommand = ExecutedCommands.Pop();
- if (lastCommand.TypeAction == ActionName.Insertion)
- Text.RemoveAt(lastCommand.ElementPosition);
- else if (lastCommand.TypeAction == ActionName.Deletion)
- Text.Insert(lastCommand.ElementPosition, lastCommand.Element);
- else
- Cursor.Position = lastCommand.ElementPosition;
- UndoCommandsStack.Push(lastCommand);
- }
- else
- throw new InvalidOperationException();
- }
- private bool CanRedo() => UndoCommandsStack.Count != 0;
- public void Redo()
- {
- if (CanRedo())
- {
- Command lastCommand = UndoCommandsStack.Pop();
- if (lastCommand.TypeAction == ActionName.Insertion)
- {
- Text.Insert(lastCommand.ElementPosition, lastCommand.Element);
- Cursor.Position++;
- }
- else if (lastCommand.TypeAction == ActionName.Deletion)
- {
- Text.RemoveAt(lastCommand.ElementPosition);
- if (Cursor.Position > 0) Cursor.Position--;
- }
- else
- Cursor.Position = lastCommand.ElementPosition;
- RedoCommandsStack.Push(lastCommand);
- }
- else
- throw new InvalidOperationException();
- }
- }
- class Program
- {
- public static void Main()
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement