From 966958d3b17ff8168aa2e55c8e3fba858fccba57 Mon Sep 17 00:00:00 2001 From: mxd <838425805@qq.com> Date: Thu, 11 Nov 2021 21:13:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=8F=90=E7=A4=BA=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/console/src/scripts/editor/completion.js | 7 +++++-- magic-editor/src/console/src/scripts/editor/hover.js | 11 ++++++++--- .../src/console/src/scripts/parsing/parser.js | 6 +++--- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/magic-editor/src/console/src/scripts/editor/completion.js b/magic-editor/src/console/src/scripts/editor/completion.js index 842d1ffd..1cb27077 100644 --- a/magic-editor/src/console/src/scripts/editor/completion.js +++ b/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) } diff --git a/magic-editor/src/console/src/scripts/editor/hover.js b/magic-editor/src/console/src/scripts/editor/hover.js index 913a9d76..787a51fd 100644 --- a/magic-editor/src/console/src/scripts/editor/hover.js +++ b/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) { diff --git a/magic-editor/src/console/src/scripts/parsing/parser.js b/magic-editor/src/console/src/scripts/parsing/parser.js index e02afe9f..91e44cda 100644 --- a/magic-editor/src/console/src/scripts/parsing/parser.js +++ b/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() {