/// <summary> /// Swap the current character and the one before it. /// </summary> public static void SwapCharacters(ConsoleKeyInfo?key = null, object arg = null) { // if in vi command mode, the cursor can't go as far var bufferLength = _singleton._buffer.Length; int cursorRightLimit = bufferLength + ViEndOfLineFactor; if (_singleton._current <= 0 || bufferLength < 2 || _singleton._current > cursorRightLimit) { Ding(); return; } int cursor = _singleton._current; if (cursor == bufferLength) { --cursor; // if at end of line, swap previous two chars } char current = _singleton._buffer[cursor]; char previous = _singleton._buffer[cursor - 1]; _singleton.StartEditGroup(); _singleton.SaveEditItem(EditItemDelete.Create(_singleton._buffer.ToString(cursor - 1, 2), cursor - 1)); _singleton.SaveEditItem(EditItemInsertChar.Create(current, cursor - 1)); _singleton.SaveEditItem(EditItemInsertChar.Create(previous, cursor)); _singleton.EndEditGroup(); _singleton._buffer[cursor] = previous; _singleton._buffer[cursor - 1] = current; _singleton.MoveCursor(Math.Min(cursor + 1, cursorRightLimit)); _singleton.Render(); }
/// <summary> /// Invert the case of the current character and move to the next one. /// </summary> public static void InvertCase(ConsoleKeyInfo?key = null, object arg = null) { if (_singleton._current >= _singleton._buffer.Length) { Ding(); return; } int qty = (arg is int) ? (int)arg : 1; for (; qty > 0 && _singleton._current < _singleton._buffer.Length; qty--) { char c = _singleton._buffer[_singleton._current]; if (Char.IsLetter(c)) { char newChar = Char.IsUpper(c) ? Char.ToLower(c) : char.ToUpper(c); EditItem delEditItem = EditItemDelete.Create(c.ToString(), _singleton._current); EditItem insEditItem = EditItemInsertChar.Create(newChar, _singleton._current); _singleton.SaveEditItem(GroupedEdit.Create(new List <EditItem> { delEditItem, insEditItem }, InvertCase, arg )); _singleton._buffer[_singleton._current] = newChar; } _singleton._current = Math.Min(_singleton._current + 1, _singleton._buffer.Length); _singleton.PlaceCursor(); } _singleton.Render(); }
/// <summary> /// A new line is inserted above the current line. /// </summary> public static void ViInsertLine(ConsoleKeyInfo?key = null, object arg = null) { _singleton._groupUndoHelper.StartGroup(ViInsertLine, arg); _singleton.MoveToBeginningOfPhrase(); _singleton._buffer.Insert(_singleton._current, '\n'); //_singleton._current = Math.Max(0, _singleton._current - 1); _singleton.SaveEditItem(EditItemInsertChar.Create('\n', _singleton._current)); _singleton.Render(); ViInsertMode(); }
/// <summary> /// Insert a character at the current position. Supports undo. /// </summary> /// <param name="c">Character to insert</param> public static void Insert(char c) { _singleton.SaveEditItem(EditItemInsertChar.Create(c, _singleton._current)); // Use Append if possible because Insert at end makes StringBuilder quite slow. if (_singleton._current == _singleton._buffer.Length) { _singleton._buffer.Append(c); } else { _singleton._buffer.Insert(_singleton._current, c); } _singleton._current += 1; _singleton.Render(); }
/// <summary> /// Joins the current line and the next line. /// </summary> public static void ViJoinLines(ConsoleKeyInfo?key = null, object arg = null) { _singleton.MoveToEndOfPhrase(); if (_singleton.IsAtEndOfLine(_singleton._current)) { Ding(); } else { _singleton._buffer[_singleton._current] = ' '; _singleton._groupUndoHelper.StartGroup(ViJoinLines, arg); _singleton.SaveEditItem(EditItemDelete.Create("\n", _singleton._current)); _singleton.SaveEditItem(EditItemInsertChar.Create(' ', _singleton._current)); _singleton._groupUndoHelper.EndGroup(); _singleton.Render(); } }
/// <summary> /// A new line is inserted below the current line. /// </summary> public static void ViAppendLine(ConsoleKeyInfo?key = null, object arg = null) { _singleton._groupUndoHelper.StartGroup(ViInsertLine, arg); _singleton.MoveToEndOfPhrase(); int insertPoint = 0; if (_singleton.IsAtEndOfLine(_singleton._current)) { insertPoint = _singleton._buffer.Length; _singleton._buffer.Append('\n'); _singleton._current = insertPoint; } else { insertPoint = _singleton._current + 1; _singleton._buffer.Insert(insertPoint, '\n'); } _singleton.SaveEditItem(EditItemInsertChar.Create('\n', insertPoint)); _singleton.Render(); ViInsertWithAppend(); }
/// <summary> /// Invert the case of the current character and move to the next one. /// </summary> public static void InvertCase(ConsoleKeyInfo?key = null, object arg = null) { if (_singleton._current >= _singleton._buffer.Length) { Ding(); return; } int qty = arg as int? ?? 1; for (; qty > 0 && _singleton._current < _singleton._buffer.Length; qty--) { char c = _singleton._buffer[_singleton._current]; if (Char.IsLetter(c)) { char newChar = Char.IsUpper(c) ? Char.ToLower(c, CultureInfo.CurrentCulture) : char.ToUpper(c, CultureInfo.CurrentCulture); EditItem delEditItem = EditItemDelete.Create( c.ToString(), _singleton._current, InvertCase, arg, moveCursorToEndWhenUndo: false); EditItem insEditItem = EditItemInsertChar.Create(newChar, _singleton._current); _singleton.SaveEditItem(GroupedEdit.Create(new List <EditItem> { delEditItem, insEditItem }, InvertCase, arg )); _singleton._buffer[_singleton._current] = newChar; } _singleton.MoveCursor(Math.Min(_singleton._current + 1, _singleton._buffer.Length)); } _singleton.Render(); }
/// <summary> /// Swap the current character and the one before it. /// </summary> public static void SwapCharacters(ConsoleKeyInfo?key = null, object arg = null) { if (_singleton._current <= 0 || _singleton._current >= _singleton._buffer.Length) { Ding(); return; } char current = _singleton._buffer[_singleton._current]; char previous = _singleton._buffer[_singleton._current - 1]; _singleton.StartEditGroup(); _singleton.SaveEditItem(EditItemDelete.Create(_singleton._buffer.ToString(_singleton._current - 1, 2), _singleton._current - 1)); _singleton.SaveEditItem(EditItemInsertChar.Create(current, _singleton._current - 1)); _singleton.SaveEditItem(EditItemInsertChar.Create(previous, _singleton._current)); _singleton.EndEditGroup(); _singleton._buffer[_singleton._current] = previous; _singleton._buffer[_singleton._current - 1] = current; _singleton._current = Math.Min(_singleton._current + 1, _singleton._buffer.Length - 1); _singleton.PlaceCursor(); _singleton.Render(); }