Browse Source

优化代码提示

mxd 3 years ago
parent
commit
7632342429

+ 36 - 0
magic-editor/src/console/src/scripts/parsing/index.js

@@ -245,6 +245,38 @@ class LiteralToken extends Token {
     constructor(tokenType, span, valueOrTokenStream) {
         super(tokenType, span, valueOrTokenStream)
     }
+
+    getJavaType() {
+        if (this.type === TokenType.StringLiteral) {
+            return 'java.lang.String'
+        }
+        if (this.type === TokenType.DoubleLiteral) {
+            return 'java.lang.Double'
+        }
+        if (this.type === TokenType.ByteLiteral) {
+            return 'java.lang.Byte'
+        }
+        if (this.type === TokenType.FloatLiteral) {
+            return 'java.lang.Float'
+        }
+        if (this.type === TokenType.DecimalLiteral) {
+            return 'java.math.BigDecimal'
+        }
+        if (this.type === TokenType.IntegerLiteral) {
+            return 'java.lang.Integer'
+        }
+        if (this.type === TokenType.LongLiteral) {
+            return 'java.lang.Long'
+        }
+        if (this.type === TokenType.BooleanLiteral) {
+            return 'java.lang.Boolean'
+        }
+        if (this.type === TokenType.RegexpLiteral) {
+            return 'java.util.regex.Pattern'
+        }
+        return 'java.lang.Object'
+
+    }
 }
 
 class CharacterStream {
@@ -382,6 +414,10 @@ class TokenStream {
         this.end = tokens.length;
     }
 
+    getEnd() {
+        return this.end > 0 && this.tokens[this.end -1]
+    }
+
     hasMore() {
         return this.index < this.end;
     }

+ 4 - 1
magic-editor/src/console/src/scripts/parsing/parser.js

@@ -1,4 +1,4 @@
-import {ParseException, Span, TokenStream, TokenType} from './index.js'
+import {LiteralToken, ParseException, Span, TokenStream, TokenType} from './index.js'
 import tokenizer from './tokenizer.js'
 import JavaClass from '../editor/java-class.js'
 import {
@@ -758,6 +758,9 @@ export class Parser {
     }
 
     async preprocessComplection(returnJavaType, defineEnvironment) {
+        if(returnJavaType && this.stream.getEnd() instanceof LiteralToken){
+            return this.stream.getEnd().getJavaType();
+        }
         let env = {
             ...defineEnvironment,
             ...JavaClass.getAutoImportClass(),

+ 7 - 3
magic-editor/src/console/src/scripts/parsing/tokenizer.js

@@ -61,7 +61,7 @@ const regexpToken = (stream, tokens) => {
         }
         let regexpSpan = stream.endSpan();
         regexpSpan = stream.getSpan(regexpSpan.getStart() - 1, regexpSpan.getEnd());
-        tokens.push(new Token(TokenType.RegexpLiteral, regexpSpan));
+        tokens.push(new LiteralToken(TokenType.RegexpLiteral, regexpSpan));
         return true;
     }
     return false;
@@ -144,8 +144,12 @@ const tokenizerNumber = (stream, tokens) => {
         while (stream.matchDigit(true) || stream.match('_', true)) {
         }
         if (stream.match(TokenType.Period.literal, true)) {
-            type = TokenType.DoubleLiteral;
-            while (stream.matchDigit(true) || stream.match('_',true)) {
+            if (stream.hasMore()) {
+                type = TokenType.DoubleLiteral;
+                while (stream.matchDigit(true) || stream.match('_',true)) {
+                }
+            } else {
+                stream.reset(stream.getPosition() - 1)
             }
         }
         if (stream.matchAny(['b', 'B'], true)) {