22// Licensed under the MIT license.
33
44import * as path from "path" ;
5- import { commands , Disposable , FileSystemWatcher , Uri , workspace } from "vscode" ;
5+ import { commands , Disposable , FileSystemWatcher , RelativePattern , Uri , workspace } from "vscode" ;
66import { instrumentOperation } from "vscode-extension-telemetry-wrapper" ;
77import { Commands } from "./commands" ;
88import { NodeKind } from "./java/nodeData" ;
@@ -19,10 +19,11 @@ class SyncHandler implements Disposable {
1919 private disposables : Disposable [ ] = [ ] ;
2020
2121 public updateFileWatcher ( autoRefresh ?: boolean ) : void {
22+ this . dispose ( ) ;
2223 if ( autoRefresh ) {
2324 instrumentOperation ( ENABLE_AUTO_REFRESH , ( ) => this . enableAutoRefresh ( ) ) ( ) ;
2425 } else {
25- instrumentOperation ( DISABLE_AUTO_REFRESH , ( ) => this . dispose ( ) ) ( ) ;
26+ instrumentOperation ( DISABLE_AUTO_REFRESH , ( ) => { } ) ( ) ;
2627 }
2728 }
2829
@@ -40,9 +41,25 @@ class SyncHandler implements Disposable {
4041 this . refresh ( ) ;
4142 } ) ) ;
4243
43- const fileSystemWatcher : FileSystemWatcher = workspace . createFileSystemWatcher ( "**/{*.java,src/**}" ) ;
44- this . setupWatchers ( fileSystemWatcher ) ;
45- this . disposables . push ( fileSystemWatcher ) ;
44+ try {
45+ const result : IListCommandResult | undefined = await commands . executeCommand < IListCommandResult > ( Commands . EXECUTE_WORKSPACE_COMMAND ,
46+ Commands . JAVA_PROJECT_LIST_SOURCE_PATHS ) ;
47+ if ( ! result || ! result . status || ! result . data || result . data . length === 0 ) {
48+ throw new Error ( "Failed to list the source paths" ) ;
49+ }
50+
51+ for ( const sourcePathData of result . data ) {
52+ const normalizedPath : string = Uri . file ( sourcePathData . path ) . fsPath ;
53+ const pattern : RelativePattern = new RelativePattern ( normalizedPath , "**/*" ) ;
54+ const watcher : FileSystemWatcher = workspace . createFileSystemWatcher ( pattern ) ;
55+ this . disposables . push ( watcher ) ;
56+ this . setupWatchers ( watcher ) ;
57+ }
58+ } catch ( e ) {
59+ const fileSystemWatcher : FileSystemWatcher = workspace . createFileSystemWatcher ( "**/{*.java,src/**}" ) ;
60+ this . disposables . push ( fileSystemWatcher ) ;
61+ this . setupWatchers ( fileSystemWatcher ) ;
62+ }
4663 }
4764
4865 private setupWatchers ( watcher : FileSystemWatcher ) : void {
@@ -100,4 +117,17 @@ class SyncHandler implements Disposable {
100117 }
101118}
102119
120+ interface ISourcePath {
121+ path : string ;
122+ displayPath : string ;
123+ projectName : string ;
124+ projectType : string ;
125+ }
126+
127+ interface IListCommandResult {
128+ status : boolean ;
129+ message : string ;
130+ data ?: ISourcePath [ ] ;
131+ }
132+
103133export const syncHandler : SyncHandler = new SyncHandler ( ) ;
0 commit comments