@@ -42,23 +42,24 @@ export class LGResource {
4242 /**
4343 * update an exist template.
4444 * @param templateName origin template name. the only id of a template.
45+ * @param newTemplateName new template Name.
4546 * @param parameters new params.
4647 * @param templateBody new template body.
4748 * @returns new LG resource.
4849 */
49- public updateTemplate ( templateName : string , parameters : string [ ] , templateBody : string ) : LGResource {
50+ public updateTemplate ( templateName : string , newTemplateName : string , parameters : string [ ] , templateBody : string ) : LGResource {
5051 const template : LGTemplate = this . Templates . find ( ( u : LGTemplate ) => u . Name === templateName ) ;
5152 if ( template === undefined ) {
5253 return this ;
5354 }
5455
55- const templateNameLine : string = this . buildTemplateNameLine ( templateName , parameters ) ;
56+ const templateNameLine : string = this . buildTemplateNameLine ( newTemplateName , parameters ) ;
5657 const newTemplateBody : string = this . convertTemplateBody ( templateBody ) ;
5758 const content : string = `${ templateNameLine } \r\n${ newTemplateBody } ` ;
5859 const startLine : number = template . ParseTree . start . line - 1 ;
5960 const stopLine : number = template . ParseTree . stop . line - 1 ;
6061
61- const newContent : string = this . ReplaceRangeContent ( this . Content , startLine , stopLine , content ) ;
62+ const newContent : string = this . replaceRangeContent ( this . Content , startLine , stopLine , content ) ;
6263
6364 return LGParser . parse ( newContent , this . Id ) ;
6465 }
@@ -97,7 +98,7 @@ export class LGResource {
9798 const startLine : number = template . ParseTree . start . line - 1 ;
9899 const stopLine : number = template . ParseTree . stop . line - 1 ;
99100
100- const newContent : string = this . ReplaceRangeContent ( this . Content , startLine , stopLine , undefined ) ;
101+ const newContent : string = this . replaceRangeContent ( this . Content , startLine , stopLine , undefined ) ;
101102
102103 return LGParser . parse ( newContent , this . Id ) ;
103104 }
@@ -106,7 +107,7 @@ export class LGResource {
106107 return this . Content ;
107108 }
108109
109- private ReplaceRangeContent ( originString : string , startLine : number , stopLine : number , replaceString : string ) : string {
110+ private replaceRangeContent ( originString : string , startLine : number , stopLine : number , replaceString : string ) : string {
110111 const originList : string [ ] = originString . split ( '\n' ) ;
111112 const destList : string [ ] = [ ] ;
112113
@@ -122,7 +123,22 @@ export class LGResource {
122123
123124 destList . push ( ...originList . slice ( stopLine + 1 ) ) ;
124125
125- return destList . join ( '\n' ) ;
126+ return this . buildNewLGContent ( destList ) ;
127+ }
128+
129+ private buildNewLGContent ( destList : string [ ] ) : string {
130+ let result : string = '' ;
131+ for ( let i : number = 0 ; i < destList . length ; i ++ ) {
132+ const currentItem : string = destList [ i ] ;
133+ result = result . concat ( currentItem ) ;
134+ if ( currentItem . endsWith ( '\r' ) ) {
135+ result = result . concat ( '\n' ) ;
136+ } else if ( i < destList . length - 1 ) {
137+ result = result . concat ( '\r\n' ) ;
138+ }
139+ }
140+
141+ return result ;
126142 }
127143
128144 private convertTemplateBody ( templateBody : string ) : string {
@@ -147,7 +163,11 @@ export class LGResource {
147163 }
148164
149165 private buildTemplateNameLine ( templateName : string , parameters : string [ ] ) : string {
150- return `# ${ templateName } (${ parameters . join ( ', ' ) } )` ;
166+ if ( parameters === undefined || parameters === null ) {
167+ return `# ${ templateName } ` ;
168+ } else {
169+ return `# ${ templateName } (${ parameters . join ( ', ' ) } )` ;
170+ }
151171 }
152172
153173 private resolveImportResources ( start : LGResource , importResolver : ImportResolverDelegate , resourcesFound : LGResource [ ] ) : void {
0 commit comments