@@ -183,11 +183,11 @@ export class VirtualSource implements SourceProvider {
183183
184184 // get
185185
186- getFolder ( path ?: string ) : VirtualFolder | undefined {
187- if ( path === undefined || path === VirtualSource . rootName ) {
186+ getFolder ( vpath ?: string ) : VirtualFolder | undefined {
187+ if ( vpath === undefined || vpath === VirtualSource . rootName ) {
188188 return this . getRoot ( ) ;
189189 } else {
190- const nameList = path . split ( '/' ) ;
190+ const nameList = vpath . split ( '/' ) ;
191191 let cur_folder : VirtualFolder = {
192192 name : '/' ,
193193 files : [ ] ,
@@ -205,11 +205,11 @@ export class VirtualSource implements SourceProvider {
205205 }
206206 }
207207
208- getFile ( path : string ) : VirtualFile | undefined {
209- const vFolder = this . getFolder ( NodePath . dirname ( path ) ) ;
208+ getFile ( vpath : string ) : VirtualFile | undefined {
209+ const vFolder = this . getFolder ( NodePath . dirname ( vpath ) ) ;
210210 if ( vFolder ) {
211- const fileName = NodePath . basename ( path ) ;
212- const index = vFolder . files . findIndex ( ( file ) => { return NodePath . basename ( file . path ) === fileName ; } ) ;
211+ const fileName = NodePath . basename ( vpath ) ;
212+ const index = vFolder . files . findIndex ( ( file ) => NodePath . basename ( file . path ) == fileName ) ;
213213 if ( index !== - 1 ) {
214214 return vFolder . files [ index ] ;
215215 }
@@ -267,51 +267,48 @@ export class VirtualSource implements SourceProvider {
267267
268268 // set
269269
270- addFile ( folder_path : string , file_path : string ) : VirtualFile | undefined {
271-
272- const folder = this . getFolder ( folder_path ) ;
273- if ( folder === undefined ) { throw new Error ( `not found virtual folder '${ folder_path } '` ) ; }
274-
275- // file is not existed, add it
276- const vFilePath = `${ folder_path } /${ NodePath . basename ( file_path ) } ` ;
277- if ( this . getFile ( vFilePath ) === undefined ) {
278- const vFile : VirtualFile = { path : this . project . toRelativePath ( file_path ) } ;
279- folder . files . push ( vFile ) ;
280- this . emit ( 'dataChanged' , 'folderChanged' ) ;
281- return vFile ;
270+ addFile ( vfolder_path : string , fspath : string ) : VirtualFile | undefined {
271+ const folder = this . getFolder ( vfolder_path ) ;
272+ if ( folder ) {
273+ const vFilePath = `${ vfolder_path } /${ NodePath . basename ( fspath ) } ` ;
274+ if ( this . getFile ( vFilePath ) === undefined ) { // file is not existed, add it
275+ const vFile : VirtualFile = { path : this . project . toRelativePath ( fspath ) } ;
276+ folder . files . push ( vFile ) ;
277+ this . emit ( 'dataChanged' , 'folderChanged' ) ;
278+ return vFile ;
279+ }
282280 }
283281 }
284282
285- addFiles ( folder_path : string , pathList : string [ ] ) : VirtualFile [ ] {
283+ addFiles ( folder_path : string , pathList : string [ ] ) : VirtualFile [ ] | undefined {
286284
287285 const folder = this . getFolder ( folder_path ) ;
288- if ( folder === undefined ) { throw new Error ( `not found virtual folder ' ${ folder_path } '` ) ; }
286+ if ( folder ) {
289287
290- const doneList : VirtualFile [ ] = [ ] ;
288+ const doneList : VirtualFile [ ] = [ ] ;
291289
292- for ( const abspath of pathList ) {
293- const vFilePath = `${ folder_path } /${ NodePath . basename ( abspath ) } ` ;
294- // file is not existed, add it
295- if ( this . getFile ( vFilePath ) === undefined ) {
296- const vFile : VirtualFile = { path : this . project . toRelativePath ( abspath ) } ;
297- folder . files . push ( vFile ) ;
298- doneList . push ( vFile ) ;
290+ for ( const abspath of pathList ) {
291+ const vFilePath = `${ folder_path } /${ NodePath . basename ( abspath ) } ` ;
292+ if ( this . getFile ( vFilePath ) === undefined ) { // file is not existed, add it
293+ const vFile : VirtualFile = { path : this . project . toRelativePath ( abspath ) } ;
294+ folder . files . push ( vFile ) ;
295+ doneList . push ( vFile ) ;
296+ }
299297 }
300- }
301298
302- if ( doneList . length > 0 ) {
303- this . emit ( 'dataChanged' , 'folderChanged' ) ;
304- }
299+ if ( doneList . length > 0 ) {
300+ this . emit ( 'dataChanged' , 'folderChanged' ) ;
301+ }
305302
306- return doneList ;
303+ return doneList ;
304+ }
307305 }
308306
309- removeFile ( path : string ) : VirtualFile | undefined {
310- const basename = NodePath . basename ( path ) ;
311- const vFolder = this . getFolder ( NodePath . dirname ( path ) ) ;
307+ removeFile ( vpath : string ) : VirtualFile | undefined {
308+ const basename = NodePath . basename ( vpath ) ;
309+ const vFolder = this . getFolder ( NodePath . dirname ( vpath ) ) ;
312310 if ( vFolder ) {
313- const index = vFolder . files .
314- findIndex ( ( f ) => { return NodePath . basename ( f . path ) === basename ; } ) ;
311+ const index = vFolder . files . findIndex ( ( f ) => NodePath . basename ( f . path ) == basename ) ;
315312 if ( index !== - 1 ) {
316313 const rmFile = vFolder . files . splice ( index , 1 ) [ 0 ] ;
317314 this . emit ( 'dataChanged' , 'folderChanged' ) ;
@@ -334,6 +331,18 @@ export class VirtualSource implements SourceProvider {
334331 }
335332 }
336333
334+ insertFolder ( parentvPath : string , nFolder : VirtualFolder ) : string | undefined {
335+ const vFolder = this . getFolder ( parentvPath ) ;
336+ if ( vFolder ) {
337+ const index = vFolder . folders . findIndex ( ( f ) => f . name == nFolder . name ) ;
338+ if ( index === - 1 ) {
339+ vFolder . folders . push ( nFolder ) ;
340+ this . emit ( 'dataChanged' , 'folderChanged' ) ;
341+ return `${ parentvPath } /${ nFolder . name } ` ;
342+ }
343+ }
344+ }
345+
337346 removeFolder ( path : string ) : VirtualFolder | undefined {
338347 const name = NodePath . basename ( path ) ;
339348 const vFolder = this . getFolder ( NodePath . dirname ( path ) ) ;
@@ -352,8 +361,8 @@ export class VirtualSource implements SourceProvider {
352361 return this . getFolder ( path ) !== undefined ;
353362 }
354363
355- renameFolder ( path : string , newName : string ) : VirtualFolder | undefined {
356- const vFolder = this . getFolder ( path ) ;
364+ renameFolder ( vpath : string , newName : string ) : VirtualFolder | undefined {
365+ const vFolder = this . getFolder ( vpath ) ;
357366 if ( vFolder ) {
358367 vFolder . name = newName ;
359368 this . emit ( 'dataChanged' , 'folderChanged' ) ;
@@ -384,7 +393,7 @@ class SourceRootList implements SourceProvider {
384393 this . project = _project ;
385394 this . _event = new events . EventEmitter ( ) ;
386395 this . srcFolderMaps = new Map ( ) ;
387- FileWatcher . on ( 'rename' , f => this . onFolderRenamed ( this . getSourceRootKeyByAbspath ( f . path ) , f ) ) ;
396+ FileWatcher . on ( 'rename' , f => this . onFileRenamed ( f ) ) ;
388397 }
389398
390399 isAutoSearchObjectFile ( ) : boolean {
@@ -414,7 +423,7 @@ class SourceRootList implements SourceProvider {
414423 }
415424
416425 private _add ( dir : File ) : SourceRootInfo {
417- const key : string = this . getSourceRootKeyByAbspath ( dir . path ) ;
426+ const key : string = this . project . toRelativePath ( dir . path ) ;
418427 const watcher = platform . createSafetyFileWatcher ( dir , true ) ;
419428 watcher . on ( 'error' , ( err ) => GlobalEvent . emit ( 'globalLog' , ExceptionToMessage ( err , 'Warning' ) ) ) ;
420429 const sourceInfo = this . newSourceInfo ( key , watcher ) ;
@@ -430,7 +439,7 @@ class SourceRootList implements SourceProvider {
430439 }
431440
432441 remove ( absPath : string ) : boolean {
433- const key = this . getSourceRootKeyByAbspath ( absPath ) ;
442+ const key = this . project . toRelativePath ( absPath ) ;
434443 return this . removeByKey ( key ) ;
435444 }
436445
@@ -467,9 +476,7 @@ class SourceRootList implements SourceProvider {
467476 if ( rootSrcUpdateList . length > 0 ) {
468477
469478 for ( const rootInfo of rootSrcUpdateList ) {
470- if ( rootInfo . isValid ( ) ) {
471- this . updateFolder ( rootInfo , [ targetDir ] ) ;
472- }
479+ this . updateFolder ( rootInfo , [ targetDir ] ) ;
473480 }
474481
475482 this . emit ( 'dataChanged' , 'folderStatusChanged' ) ;
@@ -566,10 +573,6 @@ class SourceRootList implements SourceProvider {
566573 this . _event . emit ( event , arg ) ;
567574 }
568575
569- private getSourceRootKeyByAbspath ( abspath : string ) : string {
570- return this . project . toRelativePath ( abspath ) ;
571- }
572-
573576 private newSourceInfo ( displayName : string , watcher : FileWatcher ) : SourceRootInfo {
574577 return {
575578 displayName : displayName ,
@@ -590,17 +593,19 @@ class SourceRootList implements SourceProvider {
590593 return this . srcFolderMaps . delete ( key ) ;
591594 }
592595
593- private onFolderRenamed ( folderKey : string , targetFile : File ) {
594- const rootInfo = this . srcFolderMaps . get ( folderKey ) ;
596+ private onFileRenamed ( targetFile : File ) {
597+
598+ const key = this . project . toRelativePath ( targetFile . path ) ;
599+
600+ // it's a root sources folder ?
601+ const rootInfo = this . srcFolderMaps . get ( key ) ;
595602 if ( rootInfo ) {
596603
597- // folder self has been renamed ?
598- // - if true, this folder's file watcher is invalid, dispose it
599- if ( targetFile . path == rootInfo . fileWatcher . file . path ) {
600- this . disposeWatcher ( folderKey ) ;
601- this . emit ( 'dataChanged' , 'folderStatusChanged' ) ;
602- }
604+ // this folder's file watcher is invalid, dispose it
605+ this . disposeWatcher ( key ) ;
606+ this . emit ( 'dataChanged' , 'folderStatusChanged' ) ;
603607
608+ // try update resources of it
604609 if ( rootInfo . refreshTimeout ) {
605610 rootInfo . refreshTimeout . refresh ( ) ;
606611 } else {
@@ -613,10 +618,38 @@ class SourceRootList implements SourceProvider {
613618 } , 200 , rootInfo ) ;
614619 }
615620 }
621+
622+ // it's a text file, or a sub folder, or others
623+ else {
624+
625+ const targetDir = NodePath . dirname ( targetFile . path ) ;
626+
627+ const rootSrcUpdateList = Array . from ( this . srcFolderMaps . values ( ) )
628+ . filter ( ( info ) => File . isSubPathOf ( info . fileWatcher . file . path , targetDir ) ) ;
629+
630+ if ( rootSrcUpdateList . length > 0 ) {
631+
632+ rootSrcUpdateList . forEach ( ( rootInfo ) => {
633+ if ( rootInfo . refreshTimeout ) {
634+ rootInfo . refreshTimeout . refresh ( ) ;
635+ } else {
636+ rootInfo . refreshTimeout = setTimeout ( ( folderInfo : SourceRootInfo ) => {
637+ if ( folderInfo . refreshTimeout ) {
638+ folderInfo . refreshTimeout = undefined ;
639+ this . updateFolder ( rootInfo , [ targetDir ] ) ;
640+ this . emit ( 'dataChanged' , 'folderChanged' ) ;
641+ }
642+ } , 200 , rootInfo ) ;
643+ }
644+ } ) ;
645+ }
646+ }
616647 }
617648
618649 private updateFolder ( rootFolderInfo : SourceRootInfo , targetFolderList ?: string [ ] ) {
619650
651+ console . log ( `[cl.eide] update source folder: '${ rootFolderInfo . fileWatcher . file . path } ' (${ targetFolderList ?. join ( ',' ) } )` ) ;
652+
620653 const rootFolder = rootFolderInfo . fileWatcher . file ;
621654 const folderStack : File [ ] = [ ] ;
622655
@@ -629,6 +662,10 @@ class SourceRootList implements SourceProvider {
629662 AbstractProject . getSourceFileFilter ( ) : AbstractProject . getSourceFileFilterWithoutObj ( ) ;
630663 const fileFilter = AbstractProject . getFileFilters ( ) ;
631664
665+ // if source root have no watcher, watch it !
666+ if ( rootFolderInfo . isValid ( ) && ! rootFolderInfo . fileWatcher . IsWatched ( ) )
667+ rootFolderInfo . fileWatcher . Watch ( ) ;
668+
632669 if ( targetFolderList ) { // only update target folders
633670 targetFolderList = targetFolderList . map ( ( path ) => this . project . ToAbsolutePath ( path ) ) ;
634671 targetFolderList . forEach ( ( dir ) => { // rm old record of these folders
@@ -640,9 +677,6 @@ class SourceRootList implements SourceProvider {
640677 rootFolderInfo . incList = [ ] ;
641678 rootFolderInfo . fileGroups = [ ] ;
642679 folderStack . push ( rootFolder ) ;
643- // if source root have no watcher, watch it !
644- if ( rootFolderInfo . isValid ( ) && ! rootFolderInfo . fileWatcher . IsWatched ( ) )
645- rootFolderInfo . fileWatcher . Watch ( ) ;
646680 }
647681
648682 rootFolderInfo . needUpdate = false ;
@@ -792,6 +826,10 @@ export abstract class AbstractProject implements CustomConfigurationProvider, Pr
792826 return this . GetConfiguration ( ) . config . name ;
793827 }
794828
829+ public getProjectCurrentTargetName ( ) : string {
830+ return this . getCurrentTarget ( ) ;
831+ }
832+
795833 public getProjectType ( ) : ProjectType {
796834 return this . GetConfiguration ( ) . config . type ;
797835 }
0 commit comments