Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In VI mode, moving left | right should stick cursor on logical line #1120

Merged
merged 3 commits into from
Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 12 additions & 12 deletions PSReadLine/KeyBindings.vi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ private void SetDefaultViBindings()
{ Keys.CtrlD, MakeKeyHandler(ViAcceptLineOrExit, "ViAcceptLineOrExit" ) },
{ Keys.ShiftEnter, MakeKeyHandler(AddLine, "AddLine") },
{ Keys.Escape, MakeKeyHandler(ViCommandMode, "ViCommandMode") },
{ Keys.LeftArrow, MakeKeyHandler(BackwardChar, "BackwardChar") },
{ Keys.RightArrow, MakeKeyHandler(ForwardChar, "ForwardChar") },
{ Keys.LeftArrow, MakeKeyHandler(ViBackwardChar, "ViBackwardChar") },
{ Keys.RightArrow, MakeKeyHandler(ViForwardChar, "ViForwardChar") },
{ Keys.CtrlLeftArrow, MakeKeyHandler(BackwardWord, "BackwardWord") },
{ Keys.CtrlRightArrow, MakeKeyHandler(NextWord, "NextWord") },
{ Keys.UpArrow, MakeKeyHandler(PreviousHistory, "PreviousHistory") },
Expand Down Expand Up @@ -101,17 +101,17 @@ private void SetDefaultViBindings()
{ Keys.CtrlD, MakeKeyHandler(ViAcceptLineOrExit, "ViAcceptLineOrExit") },
{ Keys.ShiftEnter, MakeKeyHandler(AddLine, "AddLine") },
{ Keys.Escape, MakeKeyHandler(Ding, "Ignore") },
{ Keys.LeftArrow, MakeKeyHandler(BackwardChar, "BackwardChar") },
{ Keys.RightArrow, MakeKeyHandler(ForwardChar, "ForwardChar") },
{ Keys.Space, MakeKeyHandler(ForwardChar, "ForwardChar") },
{ Keys.LeftArrow, MakeKeyHandler(ViBackwardChar, "ViBackwardChar") },
{ Keys.RightArrow, MakeKeyHandler(ViForwardChar, "ViForwardChar") },
{ Keys.Space, MakeKeyHandler(ViForwardChar, "ViForwardChar") },
{ Keys.CtrlLeftArrow, MakeKeyHandler(BackwardWord, "BackwardWord") },
{ Keys.CtrlRightArrow, MakeKeyHandler(NextWord, "NextWord") },
{ Keys.UpArrow, MakeKeyHandler(PreviousHistory, "PreviousHistory") },
{ Keys.DownArrow, MakeKeyHandler(NextHistory, "NextHistory") },
{ Keys.Home, MakeKeyHandler(BeginningOfLine, "BeginningOfLine") },
{ Keys.End, MakeKeyHandler(MoveToEndOfLine, "MoveToEndOfLine") },
{ Keys.Delete, MakeKeyHandler(DeleteChar, "DeleteChar") },
{ Keys.Backspace, MakeKeyHandler(BackwardChar, "BackwardChar") },
{ Keys.Backspace, MakeKeyHandler(ViBackwardChar, "ViBackwardChar") },
{ Keys.CtrlSpace, MakeKeyHandler(PossibleCompletions, "PossibleCompletions") },
{ Keys.Tab, MakeKeyHandler(TabCompleteNext, "TabCompleteNext") },
{ Keys.ShiftTab, MakeKeyHandler(TabCompletePrevious, "TabCompletePrevious") },
Expand All @@ -131,16 +131,16 @@ private void SetDefaultViBindings()
{ Keys.ShiftF3, MakeKeyHandler(CharacterSearchBackward, "CharacterSearchBackward") },
{ Keys.A, MakeKeyHandler(ViInsertWithAppend, "ViInsertWithAppend") },
{ Keys.B, MakeKeyHandler(ViBackwardWord, "ViBackwardWord") },
{ Keys.C, MakeKeyHandler(ViChord, "ChordFirstKey") },
{ Keys.D, MakeKeyHandler(ViChord, "ChordFirstKey") },
{ Keys.C, MakeKeyHandler(ViChord, "ChordFirstKey") },
{ Keys.D, MakeKeyHandler(ViChord, "ChordFirstKey") },
{ Keys.E, MakeKeyHandler(NextWordEnd, "NextWordEnd") },
{ Keys.F, MakeKeyHandler(SearchChar, "SearchChar") },
{ Keys.G, MakeKeyHandler(ViChord, "ChordFirstKey") },
{ Keys.H, MakeKeyHandler(BackwardChar, "BackwardChar") },
{ Keys.G, MakeKeyHandler(ViChord, "ChordFirstKey") },
{ Keys.H, MakeKeyHandler(ViBackwardChar, "ViBackwardChar") },
{ Keys.I, MakeKeyHandler(ViInsertMode, "ViInsertMode") },
{ Keys.J, MakeKeyHandler(NextHistory, "NextHistory") },
{ Keys.K, MakeKeyHandler(PreviousHistory, "PreviousHistory") },
{ Keys.L, MakeKeyHandler(ForwardChar, "ForwardChar") },
{ Keys.L, MakeKeyHandler(ViForwardChar, "ViForwardChar") },
{ Keys.M, MakeKeyHandler(Ding, "Ignore") },
{ Keys.N, MakeKeyHandler(RepeatSearch, "RepeatSearch") },
{ Keys.O, MakeKeyHandler(ViAppendLine, "ViAppendLine") },
Expand All @@ -153,7 +153,7 @@ private void SetDefaultViBindings()
{ Keys.V, MakeKeyHandler(ViEditVisually, "ViEditVisually") },
{ Keys.W, MakeKeyHandler(ViNextWord, "ViNextWord") },
{ Keys.X, MakeKeyHandler(DeleteChar, "DeleteChar") },
{ Keys.Y, MakeKeyHandler(ViChord, "ChordFirstKey") },
{ Keys.Y, MakeKeyHandler(ViChord, "ChordFirstKey") },
{ Keys.Z, MakeKeyHandler(Ding, "Ignore") },
{ Keys.ucA, MakeKeyHandler(ViInsertAtEnd, "ViInsertAtEnd") },
{ Keys.ucB, MakeKeyHandler(ViBackwardGlob, "ViBackwardGlob") },
Expand Down
48 changes: 48 additions & 0 deletions PSReadLine/Movement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,54 @@ public static void BackwardChar(ConsoleKeyInfo? key = null, object arg = null)
}
}

/// <summary>
/// Moves the cursor one character to the right on a single logical line.
/// </summary>
private static void ViForwardChar(ConsoleKeyInfo? key = null, object arg = null)
{
if (TryGetArgAsInt(arg, out var numericArg, 1))
{
ViOffsetCursorPosition(+ numericArg);
}
}

/// <summary>
/// Move the cursor one character to the left on a single logical line.
/// </summary>
private static void ViBackwardChar(ConsoleKeyInfo? key = null, object arg = null)
{
if (TryGetArgAsInt(arg, out var numericArg, 1))
{
ViOffsetCursorPosition(- numericArg);
}
}

/// <summary>
/// Moves the cursor to the left or right a certain number of characters.
/// If the count is negative, moves the cursor in the left direction.
/// </summary>
private static void ViOffsetCursorPosition(int count)
{
if (count < 0)
{
var start = GetBeginningOfLinePos(_singleton._current);
var newCurrent = Math.Max(start, _singleton._current + count);
if (_singleton._current != newCurrent)
{
_singleton.MoveCursor(newCurrent);
}
}
else
{
var end = GetEndOfLogicalLinePos(_singleton._current);
var newCurrent = Math.Min(end, _singleton._current + count);
if (_singleton._current != newCurrent)
{
_singleton.MoveCursor(newCurrent);
}
}
}

private void MoveToLine(int lineOffset)
{
// Behavior description:
Expand Down
2 changes: 1 addition & 1 deletion PSReadLine/ReadLine.vi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ public static void ViCommandMode(ConsoleKeyInfo? key = null, object arg = null)
}
_singleton._dispatchTable = _viCmdKeyMap;
_singleton._chordDispatchTable = _viCmdChordTable;
BackwardChar();
ViBackwardChar();
_singleton.ViIndicateCommandMode();
}

Expand Down
3 changes: 1 addition & 2 deletions test/BasicEditingTest.VI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ public void Defect796()
TestSetup(KeyMode.Vi);

Test("\"\n\n\"", Keys(
_.DQuote, _.Enter, _.Escape, CheckThat(() => AssertCursorTopIs(0)),
'j', CheckThat(() => AssertCursorTopIs(1)),
_.DQuote, _.Enter, _.Escape, CheckThat(() => AssertCursorTopIs(1)),
'o', CheckThat(() => AssertCursorTopIs(2)),
_.DQuote
));
Expand Down
47 changes: 46 additions & 1 deletion test/MovementTest.VI.Multiline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,51 @@ namespace Test
{
public partial class ReadLine
{
[SkippableFact]
public void ViBackwardChar()
{
TestSetup(KeyMode.Vi);

const string buffer = "\"\nline2\nline3\n\"";

var continuationPrefixLength = PSConsoleReadLineOptions.DefaultContinuationPrompt.Length;

Test(buffer, Keys(
_.DQuote, _.Enter,
"line2", _.Enter,
"line3", _.Enter,
_.DQuote,
_.Escape,
_.k, CheckThat(() => AssertCursorLeftIs(continuationPrefixLength + 0)),
// move left
_.h, CheckThat(() => AssertCursorLeftIs(continuationPrefixLength + 0)),
_.l, CheckThat(() => AssertCursorLeftIs(continuationPrefixLength + 1)),
"2h", CheckThat(() => AssertCursorLeftIs(continuationPrefixLength + 0))
));
}

[SkippableFact]
public void ViForwardChar()
{
TestSetup(KeyMode.Vi);

const string buffer = "\"\nline2\nline3\n\"";

var continuationPrefixLength = PSConsoleReadLineOptions.DefaultContinuationPrompt.Length;

Test(buffer, Keys(
_.DQuote, _.Enter,
"line2", _.Enter,
"line3", _.Enter,
_.DQuote,
_.Escape,
_.k, _.k, CheckThat(() => AssertCursorLeftIs(continuationPrefixLength + 0)),
// move right
_.l, CheckThat(() => AssertCursorLeftIs(continuationPrefixLength + 1)),
"10l", CheckThat(() => AssertCursorLeftIs(continuationPrefixLength + 4))
));
}
springcomp marked this conversation as resolved.
Show resolved Hide resolved

[SkippableFact]
public void ViMoveToFirstLogicalLineThenJumpToLastLogicalLine()
{
Expand Down Expand Up @@ -115,4 +160,4 @@ private void ViJumpMustDing(string expectedResult, params object[] keys)
TestMustDing(expectedResult, Keys(keys));
}
}
}
}