diff --git a/README.md b/README.md index eb64fe5..4c29e1c 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ The extension has just 2 settings: * `playcanvas.usePlaycanvasTypes`: Automatically adds a reference to PlayCanvas types files for code suggestions. Line is not saved. Default is true. * `playcanvas.maxSearchResults`: Maximum number of search results to display. +* `playcanvas.additionalTypeScriptDefinitionFiles`: Automatically adds references to user specified TypeScript definition files (.d.ts). These references are not saved. Paths must be absolute. A PlayCanvas Access Token is requested when you add a project. Generate an access token on your [account page](https://playcanvas.com/account). diff --git a/package.json b/package.json index 86b9d1f..b05922b 100644 --- a/package.json +++ b/package.json @@ -67,6 +67,10 @@ "minimum": 1, "maximum": 100, "description": "Maximum number of search results to show [1..100]" + }, + "playcanvas.additionalTypeScriptDefinitionFiles":{ + "type":"array", + "description": "An array of file paths to TypeScript definition files (.d.ts). These reference paths will be prefixed to a source file. Paths must be absolute." } } }, diff --git a/src/cloudStorageProvider.js b/src/cloudStorageProvider.js index cb37fa3..142b05b 100644 --- a/src/cloudStorageProvider.js +++ b/src/cloudStorageProvider.js @@ -15,9 +15,23 @@ class CloudStorageProvider { this.context = context; this._onDidChangeFile = new vscode.EventEmitter(); - + const filePath = path.join(__dirname, '..', 'node_modules', 'playcanvas', 'build/playcanvas.d.ts'); - this.typesReference = '///;\n'; + this.playCanvasTypesReference = ''; + this.additionalTypesReferences = ''; + + const config = vscode.workspace.getConfiguration('playcanvas'); + + if (config.get('usePlaycanvasTypes')){ + this.playCanvasTypesReference = '///;\n'; + } + + let typeScriptDefinitionFilePaths = config.get('additionalTypeScriptDefinitionFiles'); + if ( typeScriptDefinitionFilePaths ){ + for (let index = 0; index < typeScriptDefinitionFilePaths.length; index++) { + this.additionalTypesReferences += '///;\n'; + } + } this.refresh(); @@ -121,8 +135,8 @@ class CloudStorageProvider { const config = vscode.workspace.getConfiguration('playcanvas'); - if (config.get('usePlaycanvasTypes') && (asset.file.filename.endsWith('.js') || asset.file.filename.endsWith('.mjs'))) { - return new TextEncoder().encode(this.typesReference + asset.content); + if ((config.get('usePlaycanvasTypes') || config.get('additionalTypeScriptDefinitionFiles')) && (asset.file.filename.endsWith('.js') || asset.file.filename.endsWith('.mjs'))) { + return new TextEncoder().encode(this.playCanvasTypesReference + this.additionalTypesReferences + asset.content); } return new TextEncoder().encode(asset.content); @@ -191,10 +205,21 @@ class CloudStorageProvider { // remove reference line before saving const config = vscode.workspace.getConfiguration('playcanvas'); - if (config.get('usePlaycanvasTypes') && (asset.file.filename.endsWith('.js') || asset.file.filename.endsWith('.mjs'))) { + if ((config.get('usePlaycanvasTypes') || config.get('additionalTypeScriptDefinitionFiles')) && (asset.file.filename.endsWith('.js') || asset.file.filename.endsWith('.mjs'))) { let strContent = new TextDecoder().decode(content); - if (strContent.startsWith(this.typesReference)) { - strContent = strContent.substring(this.typesReference.length); + let contentChanged = false; + + if (strContent.startsWith(this.playCanvasTypesReference)) { + strContent = strContent.substring(this.playCanvasTypesReference.length); + contentChanged = true; + } + + if (strContent.startsWith(this.additionalTypesReferences)){ + strContent = strContent.substring(this.additionalTypesReferences.length); + contentChanged = true; + } + + if(contentChanged){ content = Buffer.from(strContent); } }