@@ -1539,21 +1539,25 @@ public string Fix(string scriptDefinition)
15391539 throw new ArgumentNullException ( nameof ( scriptDefinition ) ) ;
15401540 }
15411541
1542- return Fix ( new EditableText ( scriptDefinition ) ) . ToString ( ) ;
1542+ return Fix ( new EditableText ( scriptDefinition ) , null ) . ToString ( ) ;
15431543 }
15441544
15451545 /// <summary>
15461546 /// Fix the violations in the given script text.
15471547 /// </summary>
15481548 /// <param name="text">An object of type `EditableText` that encapsulates the script text to be fixed.</param>
15491549 /// <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 )
15511551 {
15521552 if ( text == null )
15531553 {
15541554 throw new ArgumentNullException ( nameof ( text ) ) ;
15551555 }
15561556
1557+ // todo validate range
1558+ var isRangeNull = range == null ;
1559+ range = isRangeNull ? null : SnapToEdges ( text , range ) ;
1560+ var previousLineCount = text . Lines . Length ;
15571561 var previousUnusedCorrections = 0 ;
15581562 do
15591563 {
@@ -1562,6 +1566,7 @@ public EditableText Fix(EditableText text)
15621566 . Select ( r => r . SuggestedCorrections )
15631567 . Where ( sc => sc != null && sc . Any ( ) )
15641568 . Select ( sc => sc . First ( ) )
1569+ . Where ( sc => isRangeNull || ( sc . Start >= range . Start && sc . End <= range . End ) )
15651570 . ToList ( ) ;
15661571
15671572 int unusedCorrections ;
@@ -1579,11 +1584,34 @@ public EditableText Fix(EditableText text)
15791584 }
15801585
15811586 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 ;
15821599 } while ( previousUnusedCorrections > 0 ) ;
15831600
15841601 return text ;
15851602 }
15861603
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+
15871615 private static IEnumerable < CorrectionExtent > GetCorrectionExtentsForFix (
15881616 IEnumerable < CorrectionExtent > correctionExtents )
15891617 {
0 commit comments