@@ -1539,21 +1539,25 @@ public string Fix(string scriptDefinition)
1539
1539
throw new ArgumentNullException ( nameof ( scriptDefinition ) ) ;
1540
1540
}
1541
1541
1542
- return Fix ( new EditableText ( scriptDefinition ) ) . ToString ( ) ;
1542
+ return Fix ( new EditableText ( scriptDefinition ) , null ) . ToString ( ) ;
1543
1543
}
1544
1544
1545
1545
/// <summary>
1546
1546
/// Fix the violations in the given script text.
1547
1547
/// </summary>
1548
1548
/// <param name="text">An object of type `EditableText` that encapsulates the script text to be fixed.</param>
1549
1549
/// <returns>The same instance of `EditableText` that was passed to the method, but the instance encapsulates the fixed script text. This helps in chaining the Fix method.</returns>
1550
- public EditableText Fix ( EditableText text )
1550
+ public EditableText Fix ( EditableText text , Range range )
1551
1551
{
1552
1552
if ( text == null )
1553
1553
{
1554
1554
throw new ArgumentNullException ( nameof ( text ) ) ;
1555
1555
}
1556
1556
1557
+ // todo validate range
1558
+ var isRangeNull = range == null ;
1559
+ range = isRangeNull ? null : SnapToEdges ( text , range ) ;
1560
+ var previousLineCount = text . Lines . Length ;
1557
1561
var previousUnusedCorrections = 0 ;
1558
1562
do
1559
1563
{
@@ -1562,6 +1566,7 @@ public EditableText Fix(EditableText text)
1562
1566
. Select ( r => r . SuggestedCorrections )
1563
1567
. Where ( sc => sc != null && sc . Any ( ) )
1564
1568
. Select ( sc => sc . First ( ) )
1569
+ . Where ( sc => isRangeNull || ( sc . Start >= range . Start && sc . End <= range . End ) )
1565
1570
. ToList ( ) ;
1566
1571
1567
1572
int unusedCorrections ;
@@ -1579,11 +1584,34 @@ public EditableText Fix(EditableText text)
1579
1584
}
1580
1585
1581
1586
previousUnusedCorrections = unusedCorrections ;
1587
+
1588
+ // todo add a TextLines.NumLines property because accessing TextLines.Lines is expensive
1589
+ var lineCount = text . Lines . Length ;
1590
+ if ( ! isRangeNull && lineCount != previousLineCount )
1591
+ {
1592
+ range = new Range (
1593
+ range . Start ,
1594
+ range . End . Shift ( lineCount - previousLineCount , 0 ) ) ;
1595
+ range = SnapToEdges ( text , range ) ;
1596
+ }
1597
+
1598
+ previousLineCount = lineCount ;
1582
1599
} while ( previousUnusedCorrections > 0 ) ;
1583
1600
1584
1601
return text ;
1585
1602
}
1586
1603
1604
+ private static Range SnapToEdges ( EditableText text , Range range )
1605
+ {
1606
+ // todo add TextLines.Validate(range) and TextLines.Validate(position)
1607
+ // todo TextLines.Lines should return IList instead of array because TextLines.Lines is expensive
1608
+ return new Range (
1609
+ range . Start . Line ,
1610
+ Math . Min ( range . Start . Column , 1 ) ,
1611
+ range . End . Line ,
1612
+ Math . Max ( range . End . Column , text . Lines [ range . End . Line - 1 ] . Length ) ) ;
1613
+ }
1614
+
1587
1615
private static IEnumerable < CorrectionExtent > GetCorrectionExtentsForFix (
1588
1616
IEnumerable < CorrectionExtent > correctionExtents )
1589
1617
{
0 commit comments