Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a new TypeScript definition setting #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
}
},
Expand Down
39 changes: 32 additions & 7 deletions src/cloudStorageProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,23 @@ class CloudStorageProvider {

this.context = context;
this._onDidChangeFile = new vscode.EventEmitter();

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

const filePath = path.join(__dirname, '..', 'node_modules', 'playcanvas', 'build/playcanvas.d.ts');
this.typesReference = '///<reference path="' + filePath + '" />;\n';
this.playCanvasTypesReference = '';
this.additionalTypesReferences = '';

const config = vscode.workspace.getConfiguration('playcanvas');

if (config.get('usePlaycanvasTypes')){
this.playCanvasTypesReference = '///<reference path="' + filePath + '" />;\n';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.playCanvasTypesReference = '///<reference path="' + filePath + '" />;\n';
this.playCanvasTypesReference = `///<reference path="${filePath}" />;\n`;

}

let typeScriptDefinitionFilePaths = config.get('additionalTypeScriptDefinitionFiles');
if ( typeScriptDefinitionFilePaths ){
for (let index = 0; index < typeScriptDefinitionFilePaths.length; index++) {
this.additionalTypesReferences += '///<reference path="' + typeScriptDefinitionFilePaths[index] + '" />;\n';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.additionalTypesReferences += '///<reference path="' + typeScriptDefinitionFilePaths[index] + '" />;\n';
this.additionalTypesReferences += `///<reference path="${typeScriptDefinitionFilePaths[index]}" />;\n`;

}
}

this.refresh();

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
Expand Down