@@ -384,7 +384,7 @@ class SourceRootList implements SourceProvider {
384384 this . project = _project ;
385385 this . _event = new events . EventEmitter ( ) ;
386386 this . srcFolderMaps = new Map ( ) ;
387- FileWatcher . on ( 'rename' , f => this . onFolderRenamed ( this . getSourceRootKeyByAbspath ( f . path ) , f ) ) ;
387+ FileWatcher . on ( 'rename' , f => this . onFileRenamed ( f ) ) ;
388388 }
389389
390390 isAutoSearchObjectFile ( ) : boolean {
@@ -414,7 +414,7 @@ class SourceRootList implements SourceProvider {
414414 }
415415
416416 private _add ( dir : File ) : SourceRootInfo {
417- const key : string = this . getSourceRootKeyByAbspath ( dir . path ) ;
417+ const key : string = this . project . toRelativePath ( dir . path ) ;
418418 const watcher = platform . createSafetyFileWatcher ( dir , true ) ;
419419 watcher . on ( 'error' , ( err ) => GlobalEvent . emit ( 'globalLog' , ExceptionToMessage ( err , 'Warning' ) ) ) ;
420420 const sourceInfo = this . newSourceInfo ( key , watcher ) ;
@@ -430,7 +430,7 @@ class SourceRootList implements SourceProvider {
430430 }
431431
432432 remove ( absPath : string ) : boolean {
433- const key = this . getSourceRootKeyByAbspath ( absPath ) ;
433+ const key = this . project . toRelativePath ( absPath ) ;
434434 return this . removeByKey ( key ) ;
435435 }
436436
@@ -467,9 +467,7 @@ class SourceRootList implements SourceProvider {
467467 if ( rootSrcUpdateList . length > 0 ) {
468468
469469 for ( const rootInfo of rootSrcUpdateList ) {
470- if ( rootInfo . isValid ( ) ) {
471- this . updateFolder ( rootInfo , [ targetDir ] ) ;
472- }
470+ this . updateFolder ( rootInfo , [ targetDir ] ) ;
473471 }
474472
475473 this . emit ( 'dataChanged' , 'folderStatusChanged' ) ;
@@ -566,10 +564,6 @@ class SourceRootList implements SourceProvider {
566564 this . _event . emit ( event , arg ) ;
567565 }
568566
569- private getSourceRootKeyByAbspath ( abspath : string ) : string {
570- return this . project . toRelativePath ( abspath ) ;
571- }
572-
573567 private newSourceInfo ( displayName : string , watcher : FileWatcher ) : SourceRootInfo {
574568 return {
575569 displayName : displayName ,
@@ -590,17 +584,19 @@ class SourceRootList implements SourceProvider {
590584 return this . srcFolderMaps . delete ( key ) ;
591585 }
592586
593- private onFolderRenamed ( folderKey : string , targetFile : File ) {
594- const rootInfo = this . srcFolderMaps . get ( folderKey ) ;
587+ private onFileRenamed ( targetFile : File ) {
588+
589+ const key = this . project . toRelativePath ( targetFile . path ) ;
590+
591+ // it's a root sources folder ?
592+ const rootInfo = this . srcFolderMaps . get ( key ) ;
595593 if ( rootInfo ) {
596594
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- }
595+ // this folder's file watcher is invalid, dispose it
596+ this . disposeWatcher ( key ) ;
597+ this . emit ( 'dataChanged' , 'folderStatusChanged' ) ;
603598
599+ // try update resources of it
604600 if ( rootInfo . refreshTimeout ) {
605601 rootInfo . refreshTimeout . refresh ( ) ;
606602 } else {
@@ -613,6 +609,32 @@ class SourceRootList implements SourceProvider {
613609 } , 200 , rootInfo ) ;
614610 }
615611 }
612+
613+ // it's a text file, or a sub folder, or others
614+ else {
615+
616+ const targetDir = NodePath . dirname ( targetFile . path ) ;
617+
618+ const rootSrcUpdateList = Array . from ( this . srcFolderMaps . values ( ) )
619+ . filter ( ( info ) => File . isSubPathOf ( info . fileWatcher . file . path , targetDir ) ) ;
620+
621+ if ( rootSrcUpdateList . length > 0 ) {
622+
623+ rootSrcUpdateList . forEach ( ( rootInfo ) => {
624+ if ( rootInfo . refreshTimeout ) {
625+ rootInfo . refreshTimeout . refresh ( ) ;
626+ } else {
627+ rootInfo . refreshTimeout = setTimeout ( ( folderInfo : SourceRootInfo ) => {
628+ if ( folderInfo . refreshTimeout ) {
629+ folderInfo . refreshTimeout = undefined ;
630+ this . updateFolder ( rootInfo , [ targetDir ] ) ;
631+ this . emit ( 'dataChanged' , 'folderChanged' ) ;
632+ }
633+ } , 200 , rootInfo ) ;
634+ }
635+ } ) ;
636+ }
637+ }
616638 }
617639
618640 private updateFolder ( rootFolderInfo : SourceRootInfo , targetFolderList ?: string [ ] ) {
@@ -629,6 +651,10 @@ class SourceRootList implements SourceProvider {
629651 AbstractProject . getSourceFileFilter ( ) : AbstractProject . getSourceFileFilterWithoutObj ( ) ;
630652 const fileFilter = AbstractProject . getFileFilters ( ) ;
631653
654+ // if source root have no watcher, watch it !
655+ if ( rootFolderInfo . isValid ( ) && ! rootFolderInfo . fileWatcher . IsWatched ( ) )
656+ rootFolderInfo . fileWatcher . Watch ( ) ;
657+
632658 if ( targetFolderList ) { // only update target folders
633659 targetFolderList = targetFolderList . map ( ( path ) => this . project . ToAbsolutePath ( path ) ) ;
634660 targetFolderList . forEach ( ( dir ) => { // rm old record of these folders
@@ -640,9 +666,6 @@ class SourceRootList implements SourceProvider {
640666 rootFolderInfo . incList = [ ] ;
641667 rootFolderInfo . fileGroups = [ ] ;
642668 folderStack . push ( rootFolder ) ;
643- // if source root have no watcher, watch it !
644- if ( rootFolderInfo . isValid ( ) && ! rootFolderInfo . fileWatcher . IsWatched ( ) )
645- rootFolderInfo . fileWatcher . Watch ( ) ;
646669 }
647670
648671 rootFolderInfo . needUpdate = false ;
0 commit comments