Skip to content

Commit

Permalink
New - PowerShell#333 Supports linewise yanks.
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Labelle authored and springcomp committed Oct 19, 2019
1 parent 664b168 commit e3c88dc
Show file tree
Hide file tree
Showing 9 changed files with 620 additions and 30 deletions.
2 changes: 1 addition & 1 deletion PSReadLine/BasicEditing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static void BackwardDeleteLine(ConsoleKeyInfo? key = null, object arg = n
{
if (_singleton._current > 0)
{
_singleton._clipboard = _singleton._buffer.ToString(0, _singleton._current);
_singleton._clipboard.Record(_singleton._buffer, 0, _singleton._current);
_singleton.SaveEditItem(EditItemDelete.Create(_singleton._clipboard, 0));
_singleton._buffer.Remove(0, _singleton._current);
_singleton._current = 0;
Expand Down
8 changes: 4 additions & 4 deletions PSReadLine/ReadLine.vi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public static void DeleteToEnd(ConsoleKeyInfo? key = null, object arg = null)
return;
}

_singleton._clipboard = _singleton._buffer.ToString(_singleton._current, _singleton._buffer.Length - _singleton._current);
_singleton._clipboard.Record(_singleton._buffer, _singleton._current, _singleton._buffer.Length - _singleton._current);
_singleton.SaveEditItem(EditItemDelete.Create(
_singleton._clipboard,
_singleton._current,
Expand Down Expand Up @@ -723,7 +723,7 @@ public static void DeleteLineToFirstChar(ConsoleKeyInfo? key = null, object arg
/// </summary>
public static void DeleteLine(ConsoleKeyInfo? key = null, object arg = null)
{
_singleton._clipboard = _singleton._buffer.ToString();
_singleton._clipboard.Record(_singleton._buffer);
_singleton.SaveEditItem(EditItemDelete.Create(_singleton._clipboard, 0));
_singleton._current = 0;
_singleton._buffer.Remove(0, _singleton._buffer.Length);
Expand All @@ -746,7 +746,7 @@ public static void BackwardDeleteWord(ConsoleKeyInfo? key = null, object arg = n
Ding();
return;
}
_singleton._clipboard = _singleton._buffer.ToString(deletePoint, _singleton._current - deletePoint);
_singleton._clipboard.Record(_singleton._buffer, deletePoint, _singleton._current - deletePoint);
_singleton.SaveEditItem(EditItemDelete.Create(
_singleton._clipboard,
deletePoint,
Expand Down Expand Up @@ -779,7 +779,7 @@ public static void ViBackwardDeleteGlob(ConsoleKeyInfo? key = null, object arg =
Ding();
return;
}
_singleton._clipboard = _singleton._buffer.ToString(deletePoint, _singleton._current - deletePoint);
_singleton._clipboard.Record(_singleton._buffer, deletePoint, _singleton._current - deletePoint);
_singleton.SaveEditItem(EditItemDelete.Create(
_singleton._clipboard,
deletePoint,
Expand Down
61 changes: 61 additions & 0 deletions PSReadLine/StringBuilderLinewiseExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Text;

namespace Microsoft.PowerShell
{
public class Range
{
public int Offset { get; set; }
public int Count { get; set; }
}

public static class StringBuilderLinewiseExtensions
{
/// <summary>
/// Determines the offset and the length of the fragment
/// in the specified buffer that corresponds to a
/// given number of lines starting from the specified line index
/// </summary>
/// <param name="buffer" />
/// <param name="lineIndex" />
/// <param name="lineCount" />
public static Range GetRange(this StringBuilder buffer, int lineIndex, int lineCount)
{
var length = buffer.Length;

var startPosition = 0;
var startPositionIdentified = false;

var endPosition = length - 1;
var endPositionIdentified = false;

var currentLine = 0;

for (var position = 0; position < length; position++)
{
if (currentLine == lineIndex && !startPositionIdentified)
{
startPosition = position;
startPositionIdentified = true;
}

if (buffer[position] == '\n')
{
currentLine++;
}

if (currentLine == lineIndex + lineCount && !endPositionIdentified)
{
endPosition = position;
endPositionIdentified = true;
}
}

return new Range
{
Offset = startPosition,
Count = endPosition - startPosition + 1,
};
}
}
}
42 changes: 35 additions & 7 deletions PSReadLine/UndoRedo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,18 @@ class EditItemInsertString : EditItem
{
// The string inserted tells us the length to delete on undo.
// The contents of the string are only needed for redo.
private string _insertedString;
private int _insertStartPosition;
private readonly string _insertedString;
private readonly int _insertStartPosition;

protected EditItemInsertString(string str, int position)
{
_insertedString = str;
_insertStartPosition = position;
}

public static EditItem Create(string str, int position)
{
return new EditItemInsertString
{
_insertedString = str,
_insertStartPosition = position
};
return new EditItemInsertString(str, position);
}

public override void Undo()
Expand All @@ -175,6 +177,32 @@ public override void Redo()
}
}

[DebuggerDisplay("Insert '{_insertedString}' ({_insertStartPosition}, Anchor: {_insertAnchor})")]
class EditItemInsertLines : EditItemInsertString
{
// in linewise pastes, the _insertAnchor represents the position
// of the cursor at the time paste was invoked. This is recorded
// so as to be restored when undoing the paste.
private readonly int _insertAnchor;

private EditItemInsertLines(string str, int position, int anchor)
:base(str, position)
{
_insertAnchor = anchor;
}

public static EditItem Create(string str, int position, int anchor)
{
return new EditItemInsertLines(str, position, anchor);
}

public override void Undo()
{
base.Undo();
_singleton._current = _insertAnchor;
}
}

[DebuggerDisplay("Delete '{_deletedString}' ({_deleteStartPosition})")]
class EditItemDelete : EditItem
{
Expand Down
Loading

0 comments on commit e3c88dc

Please sign in to comment.