Skip to content

Commit 7024968

Browse files
authored
vi-mode: Use dk to delete the previous n logical lines and the current logical line in a multi-line buffer (#1737)
1 parent 804e7a0 commit 7024968

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed

PSReadLine/KeyBindings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ public static KeyHandlerGroup GetDisplayGrouping(string function)
403403
case nameof(DeleteLine):
404404
case nameof(DeleteLineToFirstChar):
405405
case nameof(DeleteNextLines):
406+
case nameof(DeletePreviousLines):
406407
case nameof(DeleteToEnd):
407408
case nameof(DeleteWord):
408409
case nameof(ForwardDeleteLine):

PSReadLine/KeyBindings.vi.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ private void SetDefaultViBindings()
230230
{ Keys.ucE, MakeKeyHandler( ViDeleteEndOfGlob, "ViDeleteEndOfGlob") },
231231
{ Keys.H, MakeKeyHandler( BackwardDeleteChar, "BackwardDeleteChar") },
232232
{ Keys.J, MakeKeyHandler( DeleteNextLines, "DeleteNextLines") },
233+
{ Keys.K, MakeKeyHandler( DeletePreviousLines, "DeletePreviousLines") },
233234
{ Keys.L, MakeKeyHandler( DeleteChar, "DeleteChar") },
234235
{ Keys.Space, MakeKeyHandler( DeleteChar, "DeleteChar") },
235236
{ Keys._0, MakeKeyHandler( BackwardDeleteLine, "BackwardDeleteLine") },

PSReadLine/PSReadLineResources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,9 @@ Or not saving history with:
805805
<data name="DeleteEndOfBufferDescription" xml:space="preserve">
806806
<value>Delete the current logical line and up to the end of the multiline buffer</value>
807807
</data>
808+
<data name="DeletePreviousLinesDescription" xml:space="preserve">
809+
<value>Deletes from the previous n logical lines in a multiline buffer to the current logical line included.</value>
810+
</data>
808811
<data name="DeleteNextLinesDescription" xml:space="preserve">
809812
<value>Deletes the current and next n logical lines in a multiline buffer</value>
810813
</data>

PSReadLine/ReadLine.vi.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,28 @@ private static void DeleteNextLines(ConsoleKeyInfo? key = null, object arg = nul
827827
}
828828
}
829829

830+
/// <summary>
831+
/// Deletes from the previous n logical lines to the current logical line included.
832+
/// </summary>
833+
public static void DeletePreviousLines(ConsoleKeyInfo? key = null, object arg = null)
834+
{
835+
if (TryGetArgAsInt(arg, out int requestedLineCount, 1))
836+
{
837+
var currentLineIndex = _singleton.GetLogicalLineNumber() - 1;
838+
var startLineIndex = Math.Max(0, currentLineIndex - requestedLineCount);
839+
840+
DeleteLineImpl(startLineIndex, currentLineIndex - startLineIndex + 1);
841+
842+
// go the beginning of the line at index 'startLineIndex'
843+
// or at the beginning of the last line
844+
startLineIndex = Math.Min(startLineIndex, _singleton.GetLogicalLineCount() - 1);
845+
var newCurrent = GetBeginningOfNthLinePos(startLineIndex);
846+
847+
_singleton._current = newCurrent;
848+
_singleton.Render();
849+
}
850+
}
851+
830852
/// <summary>
831853
/// Deletes the previous word.
832854
/// </summary>

test/BasicEditingTest.VI.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,46 @@ public void ViDeleteToEndOfBuffer()
489489
));
490490
}
491491

492+
[SkippableFact]
493+
public void ViDeletePreviousLines()
494+
{
495+
TestSetup(KeyMode.Vi);
496+
497+
int continuationPrefixLength = PSConsoleReadLineOptions.DefaultContinuationPrompt.Length;
498+
499+
Test("\"\n\"", Keys(
500+
 _.DQuote, _.Enter,
501+
"one", _.Enter,
502+
"two", _.Enter,
503+
"three", _.Enter,
504+
_.DQuote, _.Escape,
505+
"kl", // go to the 'hree' portion of "three"
506+
"2dk", CheckThat(() => AssertCursorLeftIs(continuationPrefixLength + 0))
507+
));
508+
}
509+
510+
[SkippableFact]
511+
public void ViDeletePreviousLines_LastLine()
512+
{
513+
TestSetup(KeyMode.Vi);
514+
515+
int continuationPrefixLength = PSConsoleReadLineOptions.DefaultContinuationPrompt.Length;
516+
517+
Test("\"\none\ntwo\n\"", Keys(
518+
 _.DQuote, _.Enter,
519+
"one", _.Enter,
520+
"two", _.Enter,
521+
"three", _.Enter,
522+
_.DQuote, _.Escape,
523+
"dk",
524+
CheckThat(() => AssertCursorLeftIs(continuationPrefixLength + 0)),
525+
CheckThat(() => AssertCursorTopIs(2)),
526+
CheckThat(() => AssertLineIs("\"\none\ntwo")),
527+
// finish the buffer to close the multiline string
528+
_.A, _.Enter, _.DQuote, _.Escape
529+
));
530+
}
531+
492532
[SkippableFact]
493533
public void ViDeleteToEnd()
494534
{

0 commit comments

Comments
 (0)