@@ -77,22 +77,18 @@ export class RemoveChange implements Change {
77
77
order : number ;
78
78
description : string ;
79
79
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 ) {
86
82
throw new Error ( 'Negative positions are invalid' ) ;
87
83
}
88
- this . description = `Removed ${ toRemove } into position ${ pos } of ${ path } ` ;
84
+ this . description = `Removed text in position ${ pos } to ${ end } of ${ path } ` ;
89
85
this . order = pos ;
90
86
}
91
87
92
88
apply ( host : Host ) : Promise < void > {
93
89
return host . read ( this . path ) . then ( content => {
94
90
const prefix = content . substring ( 0 , this . pos ) ;
95
- const suffix = content . substring ( this . pos + this . toRemove . length ) ;
91
+ const suffix = content . substring ( this . end ) ;
96
92
97
93
// TODO: throw error if toRemove doesn't match removed string.
98
94
return host . write ( this . path , `${ prefix } ${ suffix } ` ) ;
@@ -109,7 +105,7 @@ export class ReplaceChange implements Change {
109
105
110
106
constructor (
111
107
public path : string ,
112
- private pos : number ,
108
+ public pos : number ,
113
109
public oldText : string ,
114
110
public newText : string
115
111
) {
@@ -150,14 +146,19 @@ export function createReplaceChange(
150
146
151
147
export function createChangeRecorder (
152
148
tree : Tree ,
153
- path : Path ,
154
- changes : ReplaceChange [ ]
149
+ path : string ,
150
+ changes : Change [ ]
155
151
) : UpdateRecorder {
156
152
const recorder = tree . beginUpdate ( path ) ;
157
153
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
+ }
161
162
}
162
163
return recorder ;
163
164
}
0 commit comments