@@ -224,22 +224,26 @@ export async function loadChanges(files: (CurrentTextFile | CurrentBinaryFile)[]
224224 if ( isClass ( file . uri . path ) ) {
225225 // Insert/update the storage part of class definition.
226226 content = new TextDecoder ( 'utf-8' ) . decode ( await vscode . workspace . fs . readFile ( file . uri ) ) . split ( / \r ? \n / g) ;
227- let storageBegin = - 1 ;
228- let storageEnd = - 1 ;
229- let classEnd ;
227+ let storageBegin : number ; // the last "Storage ..." line
228+ let storageEnd : number ; // the first " }" after storageBegin
229+ let classEnd : number ; // the last " }"
230230 for ( let i = 0 ; i < content . length ; i ++ ) {
231- if ( content [ i ] . startsWith ( "< Storage " ) ) {
231+ if ( content [ i ] . startsWith ( "Storage " ) ) {
232232 storageBegin = i ;
233- } else if ( content [ i ] . startsWith ( "</Storage> " ) ) {
233+ } else if ( ( storageBegin !== undefined ) && ( storageEnd === undefined ) && content [ i ] . startsWith ( "} " ) ) {
234234 storageEnd = i ;
235235 } else if ( content [ i ] . startsWith ( "}" ) ) {
236236 classEnd = i ;
237237 }
238238 }
239239 let storage = ( await api . getDoc ( file . name , file . uri , undefined , true ) ) . result . content ;
240240 storage = Buffer . isBuffer ( storage ) ? new TextDecoder ( ) . decode ( storage ) . split ( / \r ? \n / g) : storage ;
241- if ( ( 0 <= storageBegin ) && ( storageBegin < storageEnd ) ) {
242- content . splice ( storageBegin , storageEnd - storageEnd , ...storage )
241+ if ( ( storageBegin && storageEnd ) !== undefined ) {
242+ // when replacing an existing storage definition, we don't need extra empty lines (if any).
243+ while ( storage [ storage . length - 1 ] == "" ) {
244+ storage . pop ( )
245+ }
246+ content . splice ( storageBegin , 1 + storageEnd - storageBegin , ...storage )
243247 } else {
244248 content . splice ( classEnd , 0 , ...storage )
245249 }
0 commit comments