|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | + |
| 4 | +namespace Microsoft.PowerShell |
| 5 | +{ |
| 6 | + public partial class PSConsoleReadLine |
| 7 | + { |
| 8 | + internal enum TextObjectOperation |
| 9 | + { |
| 10 | + None, |
| 11 | + Change, |
| 12 | + Delete, |
| 13 | + } |
| 14 | + |
| 15 | + internal enum TextObjectSpan |
| 16 | + { |
| 17 | + None, |
| 18 | + Around, |
| 19 | + Inner, |
| 20 | + } |
| 21 | + |
| 22 | + private TextObjectOperation _textObjectOperation = TextObjectOperation.None; |
| 23 | + private TextObjectSpan _textObjectSpan = TextObjectSpan.None; |
| 24 | + |
| 25 | + private readonly IDictionary<TextObjectOperation, IDictionary<TextObjectSpan, KeyHandler>> _textObjectHandlers |
| 26 | + = new Dictionary<TextObjectOperation, IDictionary<TextObjectSpan, KeyHandler>> |
| 27 | + { |
| 28 | + { |
| 29 | + TextObjectOperation.Delete, |
| 30 | + new Dictionary<TextObjectSpan, KeyHandler> |
| 31 | + { |
| 32 | + {TextObjectSpan.Inner, MakeKeyHandler(ViDeleteInnerWord, "ViDeleteInnerWord")} |
| 33 | + } |
| 34 | + } |
| 35 | + }; |
| 36 | + |
| 37 | + private void ViChordDeleteTextObject(ConsoleKeyInfo? key = null, object arg = null) |
| 38 | + { |
| 39 | + _textObjectOperation = TextObjectOperation.Delete; |
| 40 | + |
| 41 | + if (!key.HasValue) |
| 42 | + { |
| 43 | + ResetTextObjectState(); |
| 44 | + throw new ArgumentNullException(nameof(key)); |
| 45 | + } |
| 46 | + |
| 47 | + _textObjectSpan = GetRequestedTextObjectSpan(key.Value); |
| 48 | + |
| 49 | + // handle text object |
| 50 | + |
| 51 | + var textObjectKey = ReadKey(); |
| 52 | + if (_viChordTextObjectsTable.TryGetValue(textObjectKey, out _)) |
| 53 | + { |
| 54 | + _singleton.ProcessOneKey(textObjectKey, _viChordTextObjectsTable, ignoreIfNoAction: true, arg: arg); |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + ResetTextObjectState(); |
| 59 | + Ding(); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private TextObjectSpan GetRequestedTextObjectSpan(ConsoleKeyInfo key) |
| 64 | + { |
| 65 | + if (key.KeyChar == 'i') |
| 66 | + { |
| 67 | + return TextObjectSpan.Inner; |
| 68 | + } |
| 69 | + else if (key.KeyChar == 'a') |
| 70 | + { |
| 71 | + return TextObjectSpan.Around; |
| 72 | + } |
| 73 | + else |
| 74 | + { |
| 75 | + System.Diagnostics.Debug.Assert(false); |
| 76 | + throw new NotSupportedException(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private static void ViHandleTextObject(ConsoleKeyInfo? key = null, object arg = null) |
| 81 | + { |
| 82 | + if ( |
| 83 | + !_singleton._textObjectHandlers.TryGetValue(_singleton._textObjectOperation, out var textObjectHandler) || |
| 84 | + !textObjectHandler.TryGetValue(_singleton._textObjectSpan, out var handler) |
| 85 | + ) |
| 86 | + { |
| 87 | + ResetTextObjectState(); |
| 88 | + Ding(); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + handler.Action(key, arg); |
| 93 | + } |
| 94 | + |
| 95 | + private static void ResetTextObjectState() |
| 96 | + { |
| 97 | + _singleton._textObjectOperation = TextObjectOperation.None; |
| 98 | + _singleton._textObjectSpan = TextObjectSpan.None; |
| 99 | + } |
| 100 | + |
| 101 | + private static void ViDeleteInnerWord(ConsoleKeyInfo? key = null, object arg = null) |
| 102 | + { |
| 103 | + var delimiters = _singleton.Options.WordDelimiters; |
| 104 | + |
| 105 | + if (TryGetArgAsInt(arg, out var numericArg, 1)) |
| 106 | + { |
| 107 | + if (_singleton._buffer.Length == 0) |
| 108 | + { |
| 109 | + if (numericArg > 1) |
| 110 | + { |
| 111 | + Ding(); |
| 112 | + } |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + // unless at the end of the buffer a single delete word should not delete backwards |
| 117 | + // so if the cursor is on an empty line, do nothing |
| 118 | + |
| 119 | + if ( |
| 120 | + numericArg == 1 && |
| 121 | + _singleton._current < _singleton._buffer.Length && |
| 122 | + _singleton._buffer.IsLogigalLineEmpty(_singleton._current) |
| 123 | + ) |
| 124 | + { |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + var start = _singleton._buffer.ViFindBeginningOfWordObjectBoundary(_singleton._current, delimiters); |
| 129 | + var end = _singleton._current; |
| 130 | + |
| 131 | + // attempting to find a valid position for multiple words |
| 132 | + // if no valid position is found, this is a no-op |
| 133 | + |
| 134 | + { |
| 135 | + while (numericArg-- > 0 && end < _singleton._buffer.Length) |
| 136 | + { |
| 137 | + end = _singleton._buffer.ViFindBeginningOfNextWordObjectBoundary(end, delimiters); |
| 138 | + } |
| 139 | + |
| 140 | + // attempting to delete too many words should ding |
| 141 | + |
| 142 | + if (numericArg > 0) |
| 143 | + { |
| 144 | + Ding(); |
| 145 | + return; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + _singleton.RemoveTextToClipboard(start, end - start); |
| 150 | + _singleton.AdjustCursorPosition(start); |
| 151 | + _singleton.Render(); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + /// <summary> |
| 156 | + /// Attempt to set the cursor at the specified position. |
| 157 | + /// </summary> |
| 158 | + /// <param name="position"></param> |
| 159 | + /// <returns></returns> |
| 160 | + private int AdjustCursorPosition(int position) |
| 161 | + { |
| 162 | + // this method might prove useful in a more general case |
| 163 | + |
| 164 | + if (_buffer.Length == 0) |
| 165 | + { |
| 166 | + _current = 0; |
| 167 | + return 0; |
| 168 | + } |
| 169 | + |
| 170 | + var maxPosition = _buffer[_buffer.Length - 1] == '\n' |
| 171 | + ? _buffer.Length |
| 172 | + : _buffer.Length - 1 |
| 173 | + ; |
| 174 | + |
| 175 | + var newCurrent = Math.Min(position, maxPosition); |
| 176 | + |
| 177 | + var beginning = GetBeginningOfLinePos(newCurrent); |
| 178 | + |
| 179 | + if (newCurrent < _buffer.Length && _buffer[newCurrent] == '\n' && (newCurrent + ViEndOfLineFactor > beginning)) |
| 180 | + { |
| 181 | + newCurrent += ViEndOfLineFactor; |
| 182 | + } |
| 183 | + |
| 184 | + _current = newCurrent; |
| 185 | + |
| 186 | + return newCurrent; |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments