|
@@ -41,49 +41,52 @@ class GDScriptDiagnosticSeverity {
|
|
this._subscription.dispose()
|
|
this._subscription.dispose()
|
|
}
|
|
}
|
|
|
|
|
|
- private parseGDScript(script: GDScript, request: ParseRequest) {
|
|
|
|
- // console.log("Parse GDScript ", script);
|
|
|
|
- let canonicalFile = vscode.Uri.file(request.path);
|
|
|
|
- this._subscription.delete(canonicalFile)
|
|
|
|
- if(script.valid) { // Parse symbols
|
|
|
|
- // TODO
|
|
|
|
|
|
+ validateScript(doc: vscode.TextDocument, script: any) {
|
|
|
|
+ if(doc.languageId == 'gdscript') {
|
|
|
|
+ if(script) {
|
|
|
|
+
|
|
|
|
+ let diagnostics = [
|
|
|
|
+ ...(this.validateExpression(doc)),
|
|
|
|
+ ...(this.validateUnusedSymbols(doc, script)),
|
|
|
|
+ ];
|
|
|
|
+ // Update diagnostics
|
|
|
|
+ this._subscription.set(doc.uri, diagnostics);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- // Parse errors
|
|
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private validateUnusedSymbols(doc: vscode.TextDocument,script) {
|
|
let diagnostics = [];
|
|
let diagnostics = [];
|
|
- script.errors.map( error => {
|
|
|
|
- let range = new vscode.Range(error.row-1, error.column, error.row-1, error.row + 10);
|
|
|
|
- diagnostics.push(new vscode.Diagnostic(range, error.message, DiagnosticSeverity.Error));
|
|
|
|
- });
|
|
|
|
- // Unused variables
|
|
|
|
- const checker = (name:string, line: number) => {
|
|
|
|
- const lines = request.text.split(/\r?\n/);
|
|
|
|
- const pattern = `[\\s\\+\\-\\*/%\\^\\(]${name}[^a-zA-Z_\\$]`;
|
|
|
|
- var matchs = request.text.match(new RegExp(pattern, 'gi'));
|
|
|
|
|
|
+ const text = doc.getText();
|
|
|
|
+
|
|
|
|
+ const check = (name:string, range: vscode.Range) => {
|
|
|
|
+ const pattern = `[\\s\\+\\-\\*/%\\^\\(\\[\\{]${name}[^0-9A-Za-z_]\\s*`;
|
|
|
|
+ var matchs = text.match(new RegExp(pattern, 'g'));
|
|
if(matchs.length <= 1)
|
|
if(matchs.length <= 1)
|
|
- diagnostics.push(new vscode.Diagnostic(new vscode.Range(line-1, lines[line-1].indexOf(name), line-1, lines[line-1].indexOf(name) + name.length), `${name} is never used.`, DiagnosticSeverity.Warning));
|
|
|
|
|
|
+ diagnostics.push(new vscode.Diagnostic(range, `${name} is never used.`, DiagnosticSeverity.Warning));
|
|
};
|
|
};
|
|
- for (let key of Object.keys(script.members.variables))
|
|
|
|
- checker(key, script.members.variables[key]);
|
|
|
|
- for (let key of Object.keys(script.members.constants))
|
|
|
|
- checker(key, script.members.constants[key]);
|
|
|
|
- // Update diagnostics
|
|
|
|
- this._subscription.set(canonicalFile, diagnostics);
|
|
|
|
|
|
+ // Unused variables
|
|
|
|
+ for (let key of Object.keys(script.variables))
|
|
|
|
+ check(key, script.variables[key]);
|
|
|
|
+ for (let key of Object.keys(script.constants))
|
|
|
|
+ check(key, script.variables[key]);
|
|
|
|
+ return diagnostics;
|
|
}
|
|
}
|
|
|
|
|
|
- parseDocument(doc: vscode.TextDocument) {
|
|
|
|
- if(doc.languageId == 'gdscript') {
|
|
|
|
- // console.log('[GodotTools]:start parsing document ', doc);
|
|
|
|
- const self = this;
|
|
|
|
- const request: ParseRequest = {text: doc.getText(), path: config.normalizePath(doc.fileName)};
|
|
|
|
- requestGodot({action: "parsescript",request}).then((data: any)=>{
|
|
|
|
- const result: GDScript = data.result;
|
|
|
|
- if(result && vscode.window.activeTextEditor.document == doc){
|
|
|
|
- self.parseGDScript(result, request);
|
|
|
|
- }
|
|
|
|
- }).catch(e=>{
|
|
|
|
- console.error(e);
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
|
|
+ private validateExpression(doc: vscode.TextDocument) {
|
|
|
|
+ let diagnostics = [];
|
|
|
|
+ const text = doc.getText();
|
|
|
|
+ const lines = text.split(/\r?\n/);
|
|
|
|
+ lines.map((line:string, i: number) =>{
|
|
|
|
+ const semicolonIndex = line.indexOf(';');
|
|
|
|
+ if(semicolonIndex != -1) {
|
|
|
|
+ diagnostics.push(new vscode.Diagnostic(new vscode.Range(i, semicolonIndex, i, semicolonIndex+1), "Statement ends with a semicolon.", DiagnosticSeverity.Warning));
|
|
|
|
+ }
|
|
|
|
+ if(line.match(/if|elif|else|for|while|func|class/g) && line.indexOf(":") == -1) {
|
|
|
|
+ diagnostics.push(new vscode.Diagnostic(new vscode.Range(i, 0, i, line.length), "':' expected at end of the line.", DiagnosticSeverity.Error));
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ return diagnostics;
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|