Bläddra i källkod

优化代码提示,支持`import 'xx.xx.*'`

mxd 3 år sedan
förälder
incheckning
8809aea30d

+ 10 - 2
magic-editor/src/console/src/scripts/editor/java-class.js

@@ -179,11 +179,18 @@ const findMethods = (clazz, sort) => {
 const getExtension = (clazz) => {
     return extensions[clazz]
 }
-
-async function loadClass(className) {
+const findClass = (className) => {
     if (!className) {
         throw new Error('className is required');
     }
+    let value = scriptClass[className]
+    if(!value){
+        let index = importClass.findIndex(it => it === className)
+        value = importClass[index]
+    }
+    return value
+}
+async function loadClass(className) {
     let val = scriptClass[className];
     if (!val) {
         try {
@@ -248,6 +255,7 @@ const exportValue = {
     findMethods,
     findFunction,
     loadClass,
+    findClass,
     initClasses,
     initImportClass,
     getWrapperClass,

+ 20 - 4
magic-editor/src/console/src/scripts/parsing/ast.js

@@ -134,7 +134,15 @@ class VariableAccess extends Node {
     }
 
     async getJavaType(env) {
-        return (env && env[this.variable]) || 'java.lang.Object';
+        // @import
+        let value = (env && env[this.variable]);
+        if(!value){
+            let imports = env['@import']
+            for(let i = imports.length - 1; i >= 0 && !value; i--){
+                value = JavaClass.findClass(imports[i] + this.variable);
+            }
+        }
+        return  value|| 'java.lang.Object';
     }
 }
 
@@ -241,7 +249,15 @@ class NewStatement extends Node {
     }
 
     async getJavaType(env) {
-        return env[this.identifier] || 'java.lang.Object';
+        let value = env[this.identifier];
+        if(!value){
+            let imports = env['@import']
+            for(let i = imports.length - 1; i >= 0 && !value; i--){
+                value = JavaClass.findClass(imports[i] + this.identifier);
+            }
+            console.log(imports,this.identifier, value)
+        }
+        return  value|| 'java.lang.Object';
     }
 }
 
@@ -422,8 +438,8 @@ class BinaryOperation extends Node {
     }
 
     async getJavaType(env) {
-        var lType = await this.left.getJavaType(env);
-        var rType = await this.right.getJavaType(env);
+        let lType = await this.left.getJavaType(env);
+        let rType = await this.right.getJavaType(env);
         if (this.operator.type == TokenType.Plus || this.operator.type == TokenType.PlusEqual) {
             if (lType == 'string' || rType == 'string' || lType == 'java.lang.String' || rType == 'java.lang.String') {
                 return 'java.lang.String';

+ 8 - 5
magic-editor/src/console/src/scripts/parsing/parser.js

@@ -35,7 +35,7 @@ import {
     LanguageExpression
 } from './ast.js'
 
-export const keywords = ["import", "as", "var", "return", "break", "continue", "if", "for", "in", "new", "true", "false", "null", "else", "try", "catch", "finally", "async", "while"];
+export const keywords = ["import", "as", "var", "return", "break", "continue", "if", "for", "in", "new", "true", "false", "null", "else", "try", "catch", "finally", "async", "while", "exit", "and", "or"];
 export const linqKeywords = ["from", "join", "left", "group", "by", "as", "having", "and", "or", "in", "where", "on"];
 const binaryOperatorPrecedence = [
     [TokenType.Assignment],
@@ -688,12 +688,13 @@ export class Parser {
         let env = {
             ...defineEnvironment,
             ...JavaClass.getAutoImportClass(),
-            ...JavaClass.getAutoImportModule()
+            ...JavaClass.getAutoImportModule(),
+            '@import' : []
         }
         let expression;
         while (this.stream.hasMore()) {
             let token = this.stream.consume();
-            var index = this.stream.makeIndex();
+            let index = this.stream.makeIndex();
             try {
                 if (token.type === TokenType.Identifier && token.getText() === 'var') {
                     let varName = this.stream.consume().getText();
@@ -724,7 +725,9 @@ export class Parser {
                             varName = value.substring(index + 1)
                         }
                     }
-                    if (varName) {
+                    if(value.endsWith(".*")){
+                        env['@import'].push(value.substring(0,value.length - 1))
+                    }else if (varName) {
                         env[varName] = value;
                     }
                 } else if (token.getTokenType() === TokenType.Assignment) {
@@ -751,7 +754,7 @@ export class Parser {
     }
 
     async completion(env) {
-        var type = await this.preprocessComplection(true, env || {});
+        let type = await this.preprocessComplection(true, env || {});
         return await JavaClass.loadClass(type);
 
     }