@@ -716,7 +716,7 @@ public static void AcceptLine(ConsoleKeyInfo? key = null, object arg = null)
716
716
_singleton . ParseInput ( ) ;
717
717
if ( _singleton . _parseErrors . Any ( e => e . IncompleteInput ) )
718
718
{
719
- _singleton . Insert ( '\n ' ) ;
719
+ Insert ( '\n ' ) ;
720
720
return ;
721
721
}
722
722
@@ -736,7 +736,7 @@ public static void AcceptLine(ConsoleKeyInfo? key = null, object arg = null)
736
736
/// </summary>
737
737
public static void AddLine ( ConsoleKeyInfo ? key = null , object arg = null )
738
738
{
739
- _singleton . Insert ( '\n ' ) ;
739
+ Insert ( '\n ' ) ;
740
740
}
741
741
742
742
/// <summary>
@@ -1124,7 +1124,7 @@ public static void Complete(ConsoleKeyInfo? key = null, object arg = null)
1124
1124
1125
1125
if ( replacementText . Length > 0 )
1126
1126
{
1127
- _singleton . Replace ( completions . ReplacementIndex , completions . ReplacementLength , replacementText ) ;
1127
+ Replace ( completions . ReplacementIndex , completions . ReplacementLength , replacementText ) ;
1128
1128
completions . ReplacementLength = replacementText . Length ;
1129
1129
1130
1130
if ( ambiguous )
@@ -1505,33 +1505,59 @@ private void MoveCursor(int offset)
1505
1505
PlaceCursor ( ) ;
1506
1506
}
1507
1507
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 )
1509
1513
{
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 ( ) ;
1514
1518
}
1515
1519
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 )
1517
1525
{
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 ( ) ;
1522
1530
}
1523
1531
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 )
1525
1539
{
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 ( ) ;
1535
1561
}
1536
1562
1537
1563
#region Undo
0 commit comments