@@ -2,7 +2,7 @@ import * as vscode from 'vscode';
22import * as Commands from "./commands" ;
33import { Config } from "./config" ;
44import { LabeledVersion } from './labeledVersion' ;
5- import { ResultUpdate , ToolPreparator } from "./toolPreparator" ;
5+ import { ResultUpdate , ToolPreparator } from "./tool4D/ toolPreparator" ;
66import {
77 LanguageClient ,
88 LanguageClientOptions ,
@@ -15,6 +15,7 @@ import * as net from 'net';
1515import { Logger } from "./logger" ;
1616import { existsSync , readdirSync , rmdirSync , rm } from "fs" ;
1717import * as path from "path" ;
18+ import { PackageManager } from "@4dsas/package-manager" ;
1819
1920export type CommandCallback = {
2021 call : ( ctx : Ctx ) => Commands . Cmd ;
@@ -25,6 +26,7 @@ export class Ctx {
2526 private _extensionContext : vscode . ExtensionContext ;
2627 private _commands : Record < string , CommandCallback > ;
2728 private _config : Config ;
29+ private _listOpenedDB : Set < string > = new Set ( ) ;
2830
2931 constructor ( ctx : vscode . ExtensionContext ) {
3032 this . _client = null ;
@@ -218,6 +220,21 @@ export class Ctx {
218220 } ,
219221 initializationOptions : this . _config . cfg ,
220222 diagnosticCollectionName : "4d" ,
223+ middleware : {
224+ didOpen : async ( document , next ) => {
225+ const project = this . _onFileOpened ( document ) ;
226+
227+ //If first time opening a file from that project, start downloading packages
228+ if ( project && ! this . _listOpenedDB . has ( project ) ) {
229+ this . _listOpenedDB . add ( project ) ;
230+ //Start downloading packages for that project
231+ const packageManager = new PackageManager ( project ) ;
232+ packageManager . initialize ( ) ;
233+ await packageManager . fetch ( ) ;
234+ }
235+ return next ( document ) ;
236+ }
237+ }
221238 } ;
222239 // Create the language client and start the client.
223240 this . _client = new LanguageClient (
@@ -252,6 +269,34 @@ export class Ctx {
252269 }
253270 }
254271
272+ private _onFileOpened ( document : vscode . TextDocument ) : string | undefined {
273+ // Callback when a file is opened
274+ Logger . debugLog ( `File opened: ${ document . fileName } (${ document . languageId } )` ) ;
275+
276+ if ( document . languageId === '4d' || document . languageId === '4qs' ) {
277+ //Find 4D Project path and store it, recursive solution going up the folder tree until .4DProject is found
278+ //The .4DProject is in a folder called Project
279+
280+ let folderPath = vscode . workspace . getWorkspaceFolder ( document . uri ) ?. uri . fsPath ;
281+ let currentPath = path . dirname ( document . uri . fsPath ) ;
282+
283+ while ( currentPath && currentPath !== folderPath ) {
284+
285+ const projectName = path . basename ( currentPath ) ;
286+ const projectFilePath = path . join ( currentPath , "Project" ) ;
287+ if ( existsSync ( path . join ( projectFilePath , projectName + ".4DProject" ) ) ) {
288+ return projectFilePath ;
289+ }
290+
291+ const parentPath = path . dirname ( currentPath ) ;
292+ if ( parentPath === currentPath ) {
293+ break ; // Reached the root directory
294+ }
295+ currentPath = parentPath ;
296+ }
297+ }
298+ }
299+
255300 public registerCommands ( ) {
256301
257302 this . _commands = {
0 commit comments