Przeglądaj źródła

代码提示优化

mxd 3 lat temu
rodzic
commit
966958d3b1

+ 5 - 2
magic-editor/src/console/src/scripts/editor/completion.js

@@ -4,6 +4,7 @@ import {TokenStream} from '../parsing/index.js'
 import {Parser} from '../parsing/parser.js'
 import * as monaco from 'monaco-editor'
 import RequestParameter from "@/scripts/editor/request-parameter";
+import {VariableAccess} from "@/scripts/parsing/ast";
 
 const completionImportJavaPackage = (suggestions, keyword, start, position) => {
     let len = -1
@@ -133,7 +134,7 @@ const completionImport = (suggestions, position, line, importIndex) => {
     }
     completionImportJavaPackage(suggestions, text, start, position)
 }
-const completionFunction = async (suggestions, input, env, best) => {
+const completionFunction = async (suggestions, input, env, best, isNew) => {
     env = env || {}
     if (best && best.constructor.name === 'VariableAccess') {
         if(await best.getJavaType(env) === 'java.lang.Object'){
@@ -152,7 +153,7 @@ const completionFunction = async (suggestions, input, env, best) => {
                         command: {
                             id: 'editor.action.scrollUp1Line'
                         },
-                        insertText: className,
+                        insertText: className + (isNew ? '()' : ''),
                         additionalTextEdits: [{
                             forceMoveMarkers: true,
                             text: `import ${clazz}\r\n`,
@@ -277,6 +278,8 @@ async function completionScript(suggestions, input) {
             let astName = best.constructor.name;
             if (astName === 'MemberAccess' || astName === 'MethodCall') {
                 await completionMethod(await best.target.getJavaType(env), suggestions)
+            } else if(astName === 'NewStatement' && best.identifier instanceof VariableAccess){
+                await completionFunction(suggestions, input, env, best.identifier, true)
             } else {
                 await completionFunction(suggestions, input, env, best)
             }

+ 8 - 3
magic-editor/src/console/src/scripts/editor/hover.js

@@ -5,7 +5,7 @@ import {
     FunctionCall,
     LinqSelect,
     MapOrArrayAccess,
-    MemberAccess,
+    MemberAccess, NewStatement,
     Node,
     VarDefine,
     VariableAccess
@@ -42,7 +42,7 @@ const generateMethodDocument = (prefix,method, contents) => {
     contents.push({value: `返回类型:\`${method.returnType}\``})
 }
 
-const generateFunctionCall = (methodName, env, contents)=>{
+const generateFunctionCall = (methodName, env, contents, isNew)=>{
     let functions = JavaClass.findFunction().filter(method => method.name === methodName);
     if (functions.length > 0) {
         generateMethodDocument('', functions[0], contents);
@@ -63,7 +63,7 @@ const generateFunctionCall = (methodName, env, contents)=>{
             }
 
         } else {
-            contents.push({value: `访问变量:${methodName}`})
+            contents.push({value: `${isNew ? '创建对象' : '访问变量'}:${methodName}`})
             contents.push({value: `类型:${value || 'unknow'}`})
         }
     }
@@ -129,6 +129,11 @@ const HoverProvider = {
                 } else if (best instanceof FunctionCall) {
                     let target = best.target;
                     generateFunctionCall(target.variable, env, contents)
+                } else if (best instanceof NewStatement) {
+                    let target = best.identifier;
+                    if(target instanceof VariableAccess){
+                        generateFunctionCall(target, env, contents, true)
+                    }
                 } else if (best instanceof MapOrArrayAccess) {
                     contents.push({value: `访问Map或数组`})
                 } else if (best instanceof LinqSelect) {

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

@@ -298,14 +298,14 @@ export class Parser {
 
     parseNewExpression(opening) {
         let expression = this.parseAccessOrCall(TokenType.Identifier, true);
+        let span = new Span(opening.getSource(), opening.getStart(), this.stream.getPrev().getSpan().getEnd())
         if (expression instanceof MethodCall) {
-            let span = new Span(opening.getSource(), opening.getStart(), this.stream.getPrev().getSpan().getEnd());
             return this.parseAccessOrCall(new NewStatement(span, expression.getMethod(), expression.getArguments()));
         } else if (expression instanceof FunctionCall) {
-            let span = new Span(opening.getSource(), opening.getStart(), this.stream.getPrev().getSpan().getEnd());
             return this.parseAccessOrCall(new NewStatement(span, expression.getFunction(), expression.getArguments()));
         }
-        throw new ParseException("Expected MethodCall or FunctionCall or LambdaFunction", this.stream.getPrev().getSpan());
+        return this.parseAccessOrCall(new NewStatement(span, expression, []));
+        // throw new ParseException("Expected MethodCall or FunctionCall or LambdaFunction", this.stream.getPrev().getSpan());
     }
 
     parseArguments() {