@@ -1829,7 +1829,34 @@ module ts {
18291829 if ( version !== sourceFile . version ) {
18301830 // Once incremental parsing is ready, then just call into this function.
18311831 if ( ! disableIncrementalParsing ) {
1832- let newSourceFile = updateSourceFile ( sourceFile , scriptSnapshot . getText ( 0 , scriptSnapshot . getLength ( ) ) , textChangeRange , aggressiveChecks ) ;
1832+ let newText : string ;
1833+
1834+ // grab the fragment from the beginning of the original text to the beginning of the span
1835+ let prefix = textChangeRange . span . start !== 0
1836+ ? sourceFile . text . substr ( 0 , textChangeRange . span . start )
1837+ : "" ;
1838+
1839+ // grab the fragment from the end of the span till the end of the original text
1840+ let suffix = textSpanEnd ( textChangeRange . span ) !== sourceFile . text . length
1841+ ? sourceFile . text . substr ( textSpanEnd ( textChangeRange . span ) )
1842+ : "" ;
1843+
1844+ if ( textChangeRange . newLength === 0 ) {
1845+ // edit was a deletion - just combine prefix and suffix
1846+ newText = prefix && suffix ? prefix + suffix : prefix || suffix ;
1847+ }
1848+ else {
1849+ // it was actual edit, fetch the fragment of new text that correspond to new span
1850+ let changedText = scriptSnapshot . getText ( textChangeRange . span . start , textChangeRange . span . start + textChangeRange . newLength ) ;
1851+ // combine prefix, changed text and suffix
1852+ newText = prefix && suffix
1853+ ? prefix + changedText + suffix
1854+ : prefix
1855+ ? ( prefix + changedText )
1856+ : ( changedText + suffix ) ;
1857+ }
1858+
1859+ let newSourceFile = updateSourceFile ( sourceFile , newText , textChangeRange , aggressiveChecks ) ;
18331860 setSourceFileFields ( newSourceFile , scriptSnapshot , version ) ;
18341861 // after incremental parsing nameTable might not be up-to-date
18351862 // drop it so it can be lazily recreated later
0 commit comments