@@ -8,14 +8,15 @@ import {
88 LanguageClientOptions ,
99 StreamInfo ,
1010} from 'vscode-languageclient/node' ;
11+ import * as ext from "./lsp_ext" ;
1112
1213import { workspace } from 'vscode' ;
1314import * as child_process from 'child_process' ;
1415import * as net from 'net' ;
1516import { Logger } from "./logger" ;
1617import { existsSync , readdirSync , rmdirSync , rm } from "fs" ;
1718import * as path from "path" ;
18- import { PackageManager } from "@4dsas/package-manager" ;
19+ import { FetchResult , PackageManager } from "@4dsas/package-manager" ;
1920
2021export type CommandCallback = {
2122 call : ( ctx : Ctx ) => Commands . Cmd ;
@@ -221,18 +222,38 @@ export class Ctx {
221222 initializationOptions : this . _config . cfg ,
222223 diagnosticCollectionName : "4d" ,
223224 middleware : {
224- didOpen : async ( document , next ) => {
225+ didOpen : async ( document , next ) => {
225226 const project = this . _onFileOpened ( document ) ;
226227
227228 //If first time opening a file from that project, start downloading packages
228- if ( project && ! this . _listOpenedDB . has ( project ) ) {
229+ if ( project && ! this . _listOpenedDB . has ( project ) ) {
229230 this . _listOpenedDB . add ( project ) ;
230231 //Start downloading packages for that project
231232 const packageManager = new PackageManager ( project ) ;
232233 packageManager . initialize ( ) ;
233- await packageManager . fetch ( ) ;
234+ const params = this . _client . code2ProtocolConverter . asTextDocumentIdentifier (
235+ vscode . window . activeTextEditor . document
236+ ) ;
237+
238+ return new Promise < void > ( async ( resolve , reject ) => {
239+ next ( document ) ;
240+ if ( await this . _client . sendRequest ( ext . needFetch , params ) ) {
241+ packageManager . fetch ( ) . then ( async ( result ) => {
242+ await this . _client . sendRequest ( ext . installComponents , params ) ;
243+ } ) . catch ( ( ) => {
244+ // Handle errors if needed
245+ } ) ;
246+ }
247+ else {
248+ //TODO: should have a progress indicator
249+ await this . _client . sendRequest ( ext . installComponents , params ) ;
250+ }
251+ resolve ( ) ;
252+ } ) ;
234253 }
254+
235255 return next ( document ) ;
256+
236257 }
237258 }
238259 } ;
@@ -269,7 +290,7 @@ export class Ctx {
269290 }
270291 }
271292
272- private _onFileOpened ( document : vscode . TextDocument ) : string | undefined {
293+ private _onFileOpened ( document : vscode . TextDocument ) : string | undefined {
273294 // Callback when a file is opened
274295 Logger . debugLog ( `File opened: ${ document . fileName } (${ document . languageId } )` ) ;
275296
@@ -284,7 +305,7 @@ export class Ctx {
284305
285306 const projectName = path . basename ( currentPath ) ;
286307 const projectFilePath = path . join ( currentPath , "Project" ) ;
287- if ( existsSync ( path . join ( projectFilePath , projectName + ".4DProject" ) ) ) {
308+ if ( existsSync ( path . join ( projectFilePath , projectName + ".4DProject" ) ) ) {
288309 return projectFilePath ;
289310 }
290311
0 commit comments