@@ -55,7 +55,7 @@ export class LGResource {
55
55
56
56
const templateNameLine : string = this . buildTemplateNameLine ( newTemplateName , parameters ) ;
57
57
const newTemplateBody : string = this . convertTemplateBody ( templateBody ) ;
58
- const content = `${ templateNameLine } \r\n${ newTemplateBody } ` ;
58
+ const content = `${ templateNameLine } \r\n${ newTemplateBody } \r\n ` ;
59
59
const startLine : number = template . parseTree . start . line - 1 ;
60
60
const stopLine : number = template . parseTree . stop . line - 1 ;
61
61
@@ -79,7 +79,7 @@ export class LGResource {
79
79
80
80
const templateNameLine : string = this . buildTemplateNameLine ( templateName , parameters ) ;
81
81
const newTemplateBody : string = this . convertTemplateBody ( templateBody ) ;
82
- const newContent = `${ this . content } \r\n${ templateNameLine } \r\n${ newTemplateBody } ` ;
82
+ const newContent = `${ this . content . trimRight ( ) } \r\n\r\n ${ templateNameLine } \r\n${ newTemplateBody } \r\n ` ;
83
83
84
84
return LGParser . parse ( newContent , this . id ) ;
85
85
}
@@ -115,15 +115,55 @@ export class LGResource {
115
115
throw new Error ( `index out of range.` ) ;
116
116
}
117
117
118
- destList . push ( ...originList . slice ( 0 , startLine ) ) ;
118
+ destList . push ( ...this . trimList ( originList . slice ( 0 , startLine ) ) ) ;
119
119
120
- if ( replaceString !== undefined && replaceString . length > 0 ) {
121
- destList . push ( replaceString ) ;
120
+ if ( stopLine < originList . length - 1 ) {
121
+ // insert at the middle of the content
122
+ destList . push ( '\r\n' ) ;
123
+ if ( replaceString !== undefined && replaceString . length > 0 ) {
124
+ destList . push ( replaceString ) ;
125
+ destList . push ( '\r\n' ) ;
126
+ }
127
+
128
+ destList . push ( ...this . trimList ( originList . slice ( stopLine + 1 ) ) ) ;
129
+ } else {
130
+ // insert at the tail of the content
131
+ if ( replaceString !== undefined && replaceString . length > 0 ) {
132
+ destList . push ( '\r\n' ) ;
133
+ destList . push ( replaceString ) ;
134
+ }
122
135
}
123
136
124
- destList . push ( ...originList . slice ( stopLine + 1 ) ) ;
137
+ return this . buildNewLGContent ( this . trimList ( destList ) ) ;
138
+ }
139
+
140
+ /**
141
+ * trim the newlines at the beginning or at the tail of the array
142
+ * @param input input array
143
+ */
144
+ private trimList ( input : string [ ] ) : string [ ] {
145
+ if ( input === undefined ) {
146
+ return undefined ;
147
+ }
148
+
149
+ let startIndex = 0 ;
150
+ let endIndex = input . length ;
151
+
152
+ for ( let i = 0 ; i < input . length ; i ++ ) {
153
+ if ( input [ i ] . trim ( ) !== '' ) {
154
+ startIndex = i ;
155
+ break ;
156
+ }
157
+ }
158
+
159
+ for ( let i = input . length - 1 ; i >= 0 ; i -- ) {
160
+ if ( input [ i ] . trim ( ) !== '' ) {
161
+ endIndex = i + 1 ;
162
+ break ;
163
+ }
164
+ }
125
165
126
- return this . buildNewLGContent ( destList ) ;
166
+ return input . slice ( startIndex , endIndex ) ;
127
167
}
128
168
129
169
private buildNewLGContent ( destList : string [ ] ) : string {
@@ -133,7 +173,7 @@ export class LGResource {
133
173
result = result . concat ( currentItem ) ;
134
174
if ( currentItem . endsWith ( '\r' ) ) {
135
175
result = result . concat ( '\n' ) ;
136
- } else if ( i < destList . length - 1 ) {
176
+ } else if ( i < destList . length - 1 && ! currentItem . endsWith ( '\r\n' ) ) {
137
177
result = result . concat ( '\r\n' ) ;
138
178
}
139
179
}
0 commit comments