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