Skip to content

Commit 67584a4

Browse files
committed
Make Insert/Replace public
Insert and Replace are now public so handlers can support undo.
1 parent 1db4656 commit 67584a4

File tree

3 files changed

+73
-23
lines changed

3 files changed

+73
-23
lines changed

PSReadLine/PSReadLineResources.Designer.cs

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PSReadLine/PSReadLineResources.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,10 @@
243243
<data name="YankPopDescription" xml:space="preserve">
244244
<value>Replace the previously yanked text with the text from the next kill ring position</value>
245245
</data>
246+
<data name="StartOutOfRange" xml:space="preserve">
247+
<value>'start' cannot be less than zero or greater than the length of the buffer</value>
248+
</data>
249+
<data name="ReplacementLengthTooBig" xml:space="preserve">
250+
<value>length is too big</value>
251+
</data>
246252
</root>

PSReadLine/ReadLine.cs

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ public static void AcceptLine(ConsoleKeyInfo? key = null, object arg = null)
716716
_singleton.ParseInput();
717717
if (_singleton._parseErrors.Any(e => e.IncompleteInput))
718718
{
719-
_singleton.Insert('\n');
719+
Insert('\n');
720720
return;
721721
}
722722

@@ -736,7 +736,7 @@ public static void AcceptLine(ConsoleKeyInfo? key = null, object arg = null)
736736
/// </summary>
737737
public static void AddLine(ConsoleKeyInfo? key = null, object arg = null)
738738
{
739-
_singleton.Insert('\n');
739+
Insert('\n');
740740
}
741741

742742
/// <summary>
@@ -1124,7 +1124,7 @@ public static void Complete(ConsoleKeyInfo? key = null, object arg = null)
11241124

11251125
if (replacementText.Length > 0)
11261126
{
1127-
_singleton.Replace(completions.ReplacementIndex, completions.ReplacementLength, replacementText);
1127+
Replace(completions.ReplacementIndex, completions.ReplacementLength, replacementText);
11281128
completions.ReplacementLength = replacementText.Length;
11291129

11301130
if (ambiguous)
@@ -1505,33 +1505,59 @@ private void MoveCursor(int offset)
15051505
PlaceCursor();
15061506
}
15071507

1508-
private void Insert(char c)
1508+
/// <summary>
1509+
/// Insert a character at the current position. Supports undo.
1510+
/// </summary>
1511+
/// <param name="c">Character to insert</param>
1512+
public static void Insert(char c)
15091513
{
1510-
SaveEditItem(EditItemInsertChar.Create(c, _current));
1511-
_buffer.Insert(_current, c);
1512-
_current += 1;
1513-
Render();
1514+
_singleton.SaveEditItem(EditItemInsertChar.Create(c, _singleton._current));
1515+
_singleton._buffer.Insert(_singleton._current, c);
1516+
_singleton._current += 1;
1517+
_singleton.Render();
15141518
}
15151519

1516-
private void Insert(string s)
1520+
/// <summary>
1521+
/// Insert a string at the current position. Supports undo.
1522+
/// </summary>
1523+
/// <param name="s">String to insert</param>
1524+
public static void Insert(string s)
15171525
{
1518-
SaveEditItem(EditItemInsertString.Create(s, _current));
1519-
_buffer.Insert(_current, s);
1520-
_current += s.Length;
1521-
Render();
1526+
_singleton.SaveEditItem(EditItemInsertString.Create(s, _singleton._current));
1527+
_singleton._buffer.Insert(_singleton._current, s);
1528+
_singleton._current += s.Length;
1529+
_singleton.Render();
15221530
}
15231531

1524-
private void Replace(int start, int length, string replacement)
1532+
/// <summary>
1533+
/// Replace some text at the given position. Supports undo.
1534+
/// </summary>
1535+
/// <param name="start">The start position to replace</param>
1536+
/// <param name="length">The length to replace</param>
1537+
/// <param name="replacement">The replacement text</param>
1538+
public static void Replace(int start, int length, string replacement)
15251539
{
1526-
StartEditGroup();
1527-
var str = _buffer.ToString(start, length);
1528-
SaveEditItem(EditItemDelete.Create(str, start));
1529-
_buffer.Remove(start, length);
1530-
SaveEditItem(EditItemInsertString.Create(replacement, start));
1531-
_buffer.Insert(start, replacement);
1532-
_current = start + replacement.Length;
1533-
EndEditGroup();
1534-
Render();
1540+
if (start < 0 || start >= _singleton._buffer.Length)
1541+
{
1542+
throw new ArgumentException(PSReadLineResources.StartOutOfRange, "start");
1543+
}
1544+
if (length > (_singleton._buffer.Length - start))
1545+
{
1546+
throw new ArgumentException(PSReadLineResources.ReplacementLengthTooBig, "length");
1547+
}
1548+
1549+
_singleton.StartEditGroup();
1550+
var str = _singleton._buffer.ToString(start, length);
1551+
_singleton.SaveEditItem(EditItemDelete.Create(str, start));
1552+
_singleton._buffer.Remove(start, length);
1553+
if (replacement != null)
1554+
{
1555+
_singleton.SaveEditItem(EditItemInsertString.Create(replacement, start));
1556+
_singleton._buffer.Insert(start, replacement);
1557+
_singleton._current = start + replacement.Length;
1558+
}
1559+
_singleton.EndEditGroup();
1560+
_singleton.Render();
15351561
}
15361562

15371563
#region Undo

0 commit comments

Comments
 (0)