@@ -77,22 +77,18 @@ export class RemoveChange implements Change {
7777 order : number ;
7878 description : string ;
7979
80- constructor (
81- public path : string ,
82- private pos : number ,
83- private toRemove : string
84- ) {
85- if ( pos < 0 ) {
80+ constructor ( public path : string , public pos : number , public end : number ) {
81+ if ( pos < 0 || end < 0 ) {
8682 throw new Error ( 'Negative positions are invalid' ) ;
8783 }
88- this . description = `Removed ${ toRemove } into position ${ pos } of ${ path } ` ;
84+ this . description = `Removed text in position ${ pos } to ${ end } of ${ path } ` ;
8985 this . order = pos ;
9086 }
9187
9288 apply ( host : Host ) : Promise < void > {
9389 return host . read ( this . path ) . then ( content => {
9490 const prefix = content . substring ( 0 , this . pos ) ;
95- const suffix = content . substring ( this . pos + this . toRemove . length ) ;
91+ const suffix = content . substring ( this . end ) ;
9692
9793 // TODO: throw error if toRemove doesn't match removed string.
9894 return host . write ( this . path , `${ prefix } ${ suffix } ` ) ;
@@ -109,7 +105,7 @@ export class ReplaceChange implements Change {
109105
110106 constructor (
111107 public path : string ,
112- private pos : number ,
108+ public pos : number ,
113109 public oldText : string ,
114110 public newText : string
115111 ) {
@@ -150,14 +146,19 @@ export function createReplaceChange(
150146
151147export function createChangeRecorder (
152148 tree : Tree ,
153- path : Path ,
154- changes : ReplaceChange [ ]
149+ path : string ,
150+ changes : Change [ ]
155151) : UpdateRecorder {
156152 const recorder = tree . beginUpdate ( path ) ;
157153 for ( const change of changes ) {
158- const action = < any > change ;
159- recorder . remove ( action . pos , action . oldText . length ) ;
160- recorder . insertLeft ( action . pos , action . newText ) ;
154+ if ( change instanceof InsertChange ) {
155+ recorder . insertLeft ( change . pos , change . toAdd ) ;
156+ } else if ( change instanceof RemoveChange ) {
157+ recorder . remove ( change . pos , change . end - change . pos ) ;
158+ } else if ( change instanceof ReplaceChange ) {
159+ recorder . remove ( change . pos , change . oldText . length ) ;
160+ recorder . insertLeft ( change . pos , change . newText ) ;
161+ }
161162 }
162163 return recorder ;
163164}
0 commit comments