Skip to content

Commit b888555

Browse files
committed
Some fixes and refactoring
1 parent 2bf93b6 commit b888555

File tree

7 files changed

+39
-46
lines changed

7 files changed

+39
-46
lines changed

PSReadLine/BasicEditing.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public static void CancelLine(ConsoleKeyInfo? key = null, object arg = null)
102102
/// </summary>
103103
public static void ForwardDeleteInput(ConsoleKeyInfo? key = null, object arg = null)
104104
{
105-
ForwardDeleteImpl(_singleton._buffer.Length);
105+
ForwardDeleteImpl(_singleton._buffer.Length, ForwardDeleteInput);
106106
}
107107

108108
/// <summary>
@@ -111,15 +111,15 @@ public static void ForwardDeleteInput(ConsoleKeyInfo? key = null, object arg = n
111111
/// </summary>
112112
public static void ForwardDeleteLine(ConsoleKeyInfo? key = null, object arg = null)
113113
{
114-
ForwardDeleteImpl(GetEndOfLogicalLinePos(_singleton._current) + 1);
114+
ForwardDeleteImpl(GetEndOfLogicalLinePos(_singleton._current) + 1, ForwardDeleteLine);
115115
}
116116

117117
/// <summary>
118118
/// Deletes text from the cursor position to the specified end position
119119
/// but does not put the deleted text in the kill ring.
120120
/// </summary>
121121
/// <param name="endPosition">0-based offset to one character past the end of the text.</param>
122-
private static void ForwardDeleteImpl(int endPosition)
122+
private static void ForwardDeleteImpl(int endPosition, Action<ConsoleKeyInfo?, object> instigator)
123123
{
124124
var current = _singleton._current;
125125
var buffer = _singleton._buffer;
@@ -128,16 +128,14 @@ private static void ForwardDeleteImpl(int endPosition)
128128
{
129129
int length = endPosition - current;
130130
var str = buffer.ToString(current, length);
131-
var adjustCursorOnUndelete = _singleton.Options.EditMode != EditMode.Vi;
132131

133132
_singleton.SaveEditItem(
134133
EditItemDelete.Create(
135134
str,
136135
current,
137-
ForwardDeleteLine,
138-
arg,
139-
adjustCursorOnUndelete
140-
));
136+
instigator,
137+
instigatorArg: null,
138+
!InViEditMode()));
141139

142140
buffer.Remove(current, length);
143141
_singleton.Render();

PSReadLine/Prediction.Views.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ private abstract class PredictionViewBase
2525
private HashSet<string> _cacheHistorySet;
2626
private List<SuggestionEntry> _cacheHistoryList;
2727

28-
internal string InputText => _inputText;
29-
3028
protected PredictionViewBase(PSConsoleReadLine singleton)
3129
{
3230
_singleton = singleton;
@@ -583,6 +581,7 @@ private class PredictionInlineView : PredictionViewBase
583581
private bool _alreadyAccepted;
584582

585583
internal string SuggestionText => _suggestionText;
584+
586585
internal PredictionInlineView(PSConsoleReadLine singleton)
587586
: base(singleton)
588587
{

PSReadLine/Prediction.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,14 @@ public static void AcceptSuggestion(ConsoleKeyInfo? key = null, object arg = nul
6969
Prediction prediction = _singleton._prediction;
7070
if (prediction.ActiveView is PredictionInlineView inlineView && inlineView.HasActiveSuggestion)
7171
{
72-
var start = inlineView.InputText.Length;
73-
7472
// Ignore the visual selection.
7573
_singleton._visualSelectionCommandCount = 0;
7674

7775
inlineView.OnSuggestionAccepted();
7876

7977
using var _ = prediction.DisableScoped();
8078

81-
_singleton._current = start;
79+
_singleton._current = _singleton._buffer.Length;
8280
Insert(inlineView.SuggestionText.Substring(_singleton._current));
8381
}
8482
}
@@ -106,12 +104,11 @@ private static void AcceptNextSuggestionWord(int numericArg)
106104
{
107105
if (_singleton._prediction.ActiveView is PredictionInlineView inlineView && inlineView.HasActiveSuggestion)
108106
{
109-
var start = Math.Max(0, inlineView.InputText.Length);
110-
var index = _singleton._buffer.Length;
111-
112107
// Ignore the visual selection.
113108
_singleton._visualSelectionCommandCount = 0;
114109

110+
int start = _singleton._buffer.Length;
111+
int index = start;
115112
while (numericArg-- > 0 && index < inlineView.SuggestionText.Length)
116113
{
117114
index = inlineView.FindForwardSuggestionWordPoint(index, _singleton.Options.WordDelimiters);

PSReadLine/ReadLine.vi.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ public static void InvertCase(ConsoleKeyInfo? key = null, object arg = null)
631631
_singleton._current,
632632
InvertCase,
633633
arg,
634-
adjustCursor: false);
634+
moveCursorToEndWhenUndo: false);
635635

636636
EditItem insEditItem = EditItemInsertChar.Create(newChar, _singleton._current);
637637
_singleton.SaveEditItem(GroupedEdit.Create(new List<EditItem>
@@ -1351,7 +1351,7 @@ public static void ViJoinLines(ConsoleKeyInfo? key = null, object arg = null)
13511351
_singleton._current,
13521352
ViJoinLines,
13531353
arg,
1354-
adjustCursor: false));
1354+
moveCursorToEndWhenUndo: false));
13551355

13561356
_singleton.SaveEditItem(EditItemInsertChar.Create(' ', _singleton._current));
13571357
_singleton._groupUndoHelper.EndGroup();

PSReadLine/Replace.vi.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ private static void ViReplaceUntilEsc(ConsoleKeyInfo? key, object arg)
7070
startingCursor,
7171
ViReplaceUntilEsc,
7272
arg,
73-
adjustCursor: false));
73+
moveCursorToEndWhenUndo: false));
7474

7575
_singleton.SaveEditItem(EditItemInsertString.Create(insStr, startingCursor));
7676
_singleton.EndEditGroup();
@@ -237,7 +237,7 @@ private static void ReplaceCharInPlace(ConsoleKeyInfo? key, object arg)
237237
_singleton._current,
238238
ReplaceCharInPlace,
239239
arg,
240-
adjustCursor: false));
240+
moveCursorToEndWhenUndo: false));
241241

242242
_singleton.SaveEditItem(EditItemInsertString.Create(nextKey.KeyStr, _singleton._current));
243243
_singleton.EndEditGroup();

PSReadLine/UndoRedo.cs

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -258,45 +258,40 @@ class EditItemDelete : EditItem
258258
private readonly string _deletedString;
259259
private readonly int _deleteStartPosition;
260260

261-
// undoing a delete operation will insert some text starting from the _deleteStartPosition.
262-
// the _adjustCursorOnUndo flag specifies whether the cursor must be adjusted.
263-
// by default the cursor will move to end of the inserted text.
264-
private readonly bool _adjustCursorOnUndo;
261+
// The undo-delete operation will insert some text starting from the '_deleteStartPosition'.
262+
// The '_moveCursorToEndWhenUndo' flag specifies whether the cursor should be moved to the end of the inserted text.
263+
private readonly bool _moveCursorToEndWhenUndo;
265264

266-
protected EditItemDelete(string str, int position, Action<ConsoleKeyInfo?, object> instigator, object instigatorArg)
267-
: this(str, position, instigator, instigatorArg, true)
268-
{
269-
}
270-
271-
protected EditItemDelete(string str, int position, Action<ConsoleKeyInfo?, object> instigator, object instigatorArg, bool adjustCursor)
265+
protected EditItemDelete(string str, int position, Action<ConsoleKeyInfo?, object> instigator, object instigatorArg, bool moveCursorToEndWhenUndo)
272266
{
273267
_deletedString = str;
274268
_deleteStartPosition = position;
275269
_instigator = instigator;
276270
_instigatorArg = instigatorArg;
277-
_adjustCursorOnUndo = adjustCursor;
271+
_moveCursorToEndWhenUndo = moveCursorToEndWhenUndo;
278272
}
279273

280-
public static EditItem Create(string str, int position, Action<ConsoleKeyInfo?, object> instigator = null, object instigatorArg = null, bool adjustCursor = true)
274+
public static EditItem Create(
275+
string str,
276+
int position,
277+
Action<ConsoleKeyInfo?, object> instigator = null,
278+
object instigatorArg = null,
279+
bool moveCursorToEndWhenUndo = true)
281280
{
282281
return new EditItemDelete(
283282
str,
284283
position,
285284
instigator,
286285
instigatorArg,
287-
adjustCursor);
286+
moveCursorToEndWhenUndo);
288287
}
289288

290289
public override void Undo()
291290
{
292-
var newCurrent = _deleteStartPosition;
293-
newCurrent += _adjustCursorOnUndo
294-
? _deletedString.Length
295-
: 0
296-
;
297-
298291
_singleton._buffer.Insert(_deleteStartPosition, _deletedString);
299-
_singleton._current = newCurrent;
292+
_singleton._current = _moveCursorToEndWhenUndo
293+
? _deleteStartPosition + _deletedString.Length
294+
: _deleteStartPosition;
300295
}
301296

302297
public override void Redo()
@@ -315,7 +310,7 @@ class EditItemDeleteLines : EditItemDelete
315310
private readonly int _deleteAnchor;
316311

317312
private EditItemDeleteLines(string str, int position, int anchor, Action<ConsoleKeyInfo?, object> instigator, object instigatorArg)
318-
: base(str, position, instigator, instigatorArg)
313+
: base(str, position, instigator, instigatorArg, moveCursorToEndWhenUndo: false)
319314
{
320315
_deleteAnchor = anchor;
321316
}
@@ -332,7 +327,7 @@ public override void Undo()
332327
}
333328
}
334329

335-
[DebuggerDisplay("SwapCharacters")]
330+
[DebuggerDisplay("SwapCharacters (position: {_swapPosition})")]
336331
class EditItemSwapCharacters : EditItem
337332
{
338333
private readonly int _swapPosition;
@@ -349,7 +344,7 @@ public static EditItem Create(int swapPosition)
349344

350345
public override void Redo()
351346
{
352-
Undo();
347+
_singleton.SwapCharactersImpl(_swapPosition);
353348
}
354349

355350
public override void Undo()

PSReadLine/YankPaste.vi.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,25 @@ private void SaveLinesToClipboard(int lineIndex, int lineCount)
7878
/// <param name="count"></param>
7979
/// <param name="instigator"></param>
8080
/// <param name="arg"></param>
81-
/// <param name="moveCursorToEndWhenUndo"></param>
81+
/// <param name="moveCursorToEndWhenUndoDelete">
82+
/// Use 'false' as the default value because this method is used a lot by VI operations,
83+
/// and for VI opeartions, we do NOT want to move the cursor to the end when undoing a
84+
/// deletion.
85+
/// </param>
8286
private void RemoveTextToViRegister(
8387
int start,
8488
int count,
8589
Action<ConsoleKeyInfo?, object> instigator = null,
8690
object arg = null,
87-
bool moveCursorToEndWhenUndo = false)
91+
bool moveCursorToEndWhenUndoDelete = false)
8892
{
8993
_singleton.SaveToClipboard(start, count);
9094
_singleton.SaveEditItem(EditItemDelete.Create(
9195
_viRegister.RawText,
9296
start,
9397
instigator,
9498
arg,
95-
moveCursorToEndWhenUndo));
99+
moveCursorToEndWhenUndoDelete));
96100
_singleton._buffer.Remove(start, count);
97101
}
98102

0 commit comments

Comments
 (0)