@@ -20,6 +20,7 @@ import { localize } from "./util/i18nUtil";
2020import { RequirementsData , resolveRequirements , resolveLclsRequirements } from "./util/requirements" ;
2121import { prepareExecutable } from "./util/javaServerStarter" ;
2222import * as helperUtil from "./util/helperUtil" ;
23+ import path = require( 'path' ) ;
2324
2425const LIBERTY_CLIENT_ID = "LANGUAGE_ID_LIBERTY" ;
2526const JAKARTA_CLIENT_ID = "LANGUAGE_ID_JAKARTA" ;
@@ -184,15 +185,49 @@ export function deactivate(): Promise<void[]> {
184185 */
185186export function registerFileWatcher ( projectProvider : ProjectProvider ) : void {
186187 const watcher : vscode . FileSystemWatcher = vscode . workspace . createFileSystemWatcher ( "{**/pom.xml,**/build.gradle,**/settings.gradle,**/src/main/liberty/config/server.xml}" ) ;
187- watcher . onDidCreate ( async ( ) => {
188- projectProvider . refresh ( ) ;
189- } ) ;
190- watcher . onDidChange ( async ( ) => {
191- projectProvider . refresh ( ) ;
192- } ) ;
193- watcher . onDidDelete ( async ( ) => {
194- projectProvider . refresh ( ) ;
195- } ) ;
188+ // Async handler for the file system events (create, change, delete)
189+ const handleUri = async ( uri : vscode . Uri ) => {
190+ const workspaceFolders = vscode . workspace . workspaceFolders ;
191+
192+ if ( ! workspaceFolders ) {
193+ return ; // No workspace folders to process
194+ }
195+
196+ // Loop through all workspace folders
197+ for ( let folder of workspaceFolders ) {
198+ const projectRoot = folder . uri . fsPath ;
199+ const relativePath = path . relative ( projectRoot , uri . fsPath ) ;
200+
201+ // Ensure that the file belongs to this project (starts with the projectRoot path)
202+ if ( ! uri . fsPath . startsWith ( projectRoot ) ) {
203+ continue ; // Skip if the file is outside the current project folder
204+ }
205+
206+ // Check if the path includes 'target' or 'build' directly under the project root
207+ if ( / ( t a r g e t \/ | b u i l d \/ ) / . test ( relativePath ) ) {
208+
209+ const fileType = path . basename ( uri . fsPath ) ;
210+ const siblingFileExists = await helperUtil . checkSiblingFilesInTargetOrBuildParent ( uri . fsPath ) ;
211+ if ( ! siblingFileExists ) {
212+ console . log ( `No sibling ${ fileType } found, refreshing project... for ` + uri . fsPath ) ;
213+ // Refresh the project if no sibling file is found
214+ await projectProvider . refresh ( ) ;
215+ } else {
216+ console . log ( `Skipping refresh: Sibling ${ fileType } found for ` + uri . fsPath ) ;
217+ return ; // Do not refresh
218+ }
219+ } else {
220+ // If the file generated is **outside** the `target` directory, always refresh
221+ console . log ( 'Refreshing project...' ) ;
222+ await projectProvider . refresh ( ) ;
223+ }
224+ }
225+ } ;
226+
227+
228+ watcher . onDidCreate ( handleUri ) ;
229+ watcher . onDidChange ( handleUri ) ;
230+ watcher . onDidDelete ( handleUri ) ;
196231}
197232
198233function startLangServer ( context : ExtensionContext , requirements : RequirementsData , isLiberty : boolean ) {
0 commit comments