Ejemplo n.º 1
0
        /// <summary>
        /// Replace some text at the given position.  Supports undo.
        /// </summary>
        /// <param name="start">The start position to replace</param>
        /// <param name="length">The length to replace</param>
        /// <param name="replacement">The replacement text</param>
        /// <param name="instigator">The action that initiated the replace (used for undo)</param>
        /// <param name="instigatorArg">The argument to the action that initiaed the replace (used for undo)</param>
        public static void Replace(int start, int length, string replacement, Action <ConsoleKeyInfo?, object> instigator = null, object instigatorArg = null)
        {
            if (start < 0 || start > _singleton._buffer.Length)
            {
                throw new ArgumentException(PSReadLineResources.StartOutOfRange, "start");
            }
            if (length > (_singleton._buffer.Length - start))
            {
                throw new ArgumentException(PSReadLineResources.ReplacementLengthTooBig, "length");
            }

            bool useEditGroup = (_singleton._editGroupStart == -1);

            if (useEditGroup)
            {
                _singleton.StartEditGroup();
            }

            var str = _singleton._buffer.ToString(start, length);

            _singleton.SaveEditItem(EditItemDelete.Create(str, start));
            _singleton._buffer.Remove(start, length);
            if (replacement != null)
            {
                _singleton.SaveEditItem(EditItemInsertString.Create(replacement, start));
                _singleton._buffer.Insert(start, replacement);
                _singleton._current = start + replacement.Length;
            }
            else
            {
                _singleton._current = start;
            }

            if (useEditGroup)
            {
                _singleton.EndEditGroup(instigator, instigatorArg); // Instigator is needed for VI undo
                _singleton.Render();
            }
        }
Ejemplo n.º 2
0
        /// <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();
        }
Ejemplo n.º 3
0
        /// <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,
                                            ViJoinLines,
                                            arg,
                                            moveCursorToEndWhenUndo: false));

                _singleton.SaveEditItem(EditItemInsertChar.Create(' ', _singleton._current));
                _singleton._groupUndoHelper.EndGroup();
                _singleton.Render();
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Delete to the end of the word.
        /// </summary>
        public static void ViDeleteEndOfGlob(ConsoleKeyInfo?key = null, object arg = null)
        {
            int qty      = (arg is int) ? (int)arg : 1;
            int endPoint = _singleton._current;

            for (int i = 0; i < qty; i++)
            {
                endPoint = _singleton.ViFindGlobEnd(endPoint);
            }

            _singleton.SaveToClipboard(_singleton._current, 1 + endPoint - _singleton._current);
            _singleton.SaveEditItem(EditItemDelete.Create(
                                        _singleton._clipboard,
                                        _singleton._current,
                                        ViDeleteEndOfGlob,
                                        arg
                                        ));
            _singleton._buffer.Remove(_singleton._current, 1 + endPoint - _singleton._current);
            if (_singleton._current >= _singleton._buffer.Length)
            {
                _singleton._current = _singleton._buffer.Length - 1;
            }
            _singleton.Render();
        }
Ejemplo n.º 5
0
        private void DeleteCharImpl(bool orExit)
        {
            if (_visualSelectionCommandCount > 0)
            {
                int start, length;
                GetRegion(out start, out length);
                Delete(start, length);
                return;
            }

            if (_buffer.Length > 0)
            {
                if (_current < _buffer.Length)
                {
                    SaveEditItem(EditItemDelete.Create(new string(_buffer[_current], 1), _current));
                    _buffer.Remove(_current, 1);
                    Render();
                }
            }
            else if (orExit)
            {
                throw new ExitException();
            }
        }
Ejemplo n.º 6
0
        private static void ViReplaceUntilEsc(ConsoleKeyInfo?key, object arg)
        {
            if (_singleton._current >= _singleton._buffer.Length)
            {
                Ding();
                return;
            }

            int           startingCursor = _singleton._current;
            StringBuilder deletedStr     = new StringBuilder();

            ConsoleKeyInfo nextKey = ReadKey();

            while (!nextKey.EqualsNormalized(Keys.Escape) && !nextKey.EqualsNormalized(Keys.Enter))
            {
                if (nextKey.EqualsNormalized(Keys.Backspace))
                {
                    if (_singleton._current == startingCursor)
                    {
                        Ding();
                    }
                    else
                    {
                        if (deletedStr.Length == _singleton._current - startingCursor)
                        {
                            _singleton._buffer[_singleton._current - 1] = deletedStr[deletedStr.Length - 1];
                            deletedStr.Remove(deletedStr.Length - 1, 1);
                        }
                        else
                        {
                            _singleton._buffer.Remove(_singleton._current - 1, 1);
                        }
                        _singleton._current--;
                        _singleton.Render();
                    }
                }
                else
                {
                    if (_singleton._current >= _singleton._buffer.Length)
                    {
                        _singleton._buffer.Append(nextKey.KeyChar);
                    }
                    else
                    {
                        deletedStr.Append(_singleton._buffer[_singleton._current]);
                        _singleton._buffer[_singleton._current] = nextKey.KeyChar;
                    }
                    _singleton._current++;
                    _singleton.Render();
                }
                nextKey = ReadKey();
            }

            if (_singleton._current > startingCursor)
            {
                _singleton.StartEditGroup();
                string insStr = _singleton._buffer.ToString(startingCursor, _singleton._current - startingCursor);
                _singleton.SaveEditItem(EditItemDelete.Create(deletedStr.ToString(), startingCursor));
                _singleton.SaveEditItem(EditItemInsertString.Create(insStr, startingCursor));
                _singleton.EndEditGroup();
            }

            if (nextKey.EqualsNormalized(Keys.Enter))
            {
                ViAcceptLine(nextKey);
            }
        }