From 81a2aff0c518dfd826774d3071d1d392b63ba0a1 Mon Sep 17 00:00:00 2001 From: mxd <838425805@qq.com> Date: Wed, 27 Oct 2021 23:04:52 +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 --- .../console/src/scripts/editor/completion.js | 51 ++++++++++++++----- .../console/src/scripts/editor/java-class.js | 4 +- .../src/console/src/scripts/parsing/parser.js | 10 ++-- 3 files changed, 47 insertions(+), 18 deletions(-) diff --git a/magic-editor/src/console/src/scripts/editor/completion.js b/magic-editor/src/console/src/scripts/editor/completion.js index 7eca6f31..33c56b91 100644 --- a/magic-editor/src/console/src/scripts/editor/completion.js +++ b/magic-editor/src/console/src/scripts/editor/completion.js @@ -3,7 +3,7 @@ import tokenizer from '../parsing/tokenizer.js' import {TokenStream, TokenType} from '../parsing/index.js' import {Parser} from '../parsing/parser.js' import * as monaco from 'monaco-editor' -import RequestParameter from './request-parameter.js' +import RequestParameter from "@/scripts/editor/request-parameter"; const completionImport = (suggestions, position, line, importIndex) => { let len = 0; @@ -11,6 +11,17 @@ const completionImport = (suggestions, position, line, importIndex) => { if (start === 0) { start = line.indexOf("'") + 1; } + if(start === 0){ + JavaClass.getDefineModules().forEach(module => suggestions.push({ + label: module, + filterText: module, + kind: monaco.languages.CompletionItemKind.Module, + detail: module, + insertText: module, + insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet + })) + return; + } let keyword = line.trim().substring(importIndex + 6).trim().replace(/['|"]/g, '').toLowerCase(); let importClass = JavaClass.getImportClass(); if (start !== 0 && keyword && (len = importClass.length) > 0) { @@ -53,7 +64,8 @@ const completionImport = (suggestions, position, line, importIndex) => { } } } -const completionFunction = (suggestions, input) => { +const completionFunction = (suggestions, input, env) => { + env = env || {} JavaClass.findFunction().forEach(it => { suggestions.push({ sortText: it.sortText || it.fullName, @@ -65,22 +77,22 @@ const completionFunction = (suggestions, input) => { insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet }) }) - Object.keys(RequestParameter.environmentFunction()).forEach(it => { + let known = suggestions.map(it => it.detail); + let matches = input.match(/[a-zA-Z_$]+/ig) || []; + let count = matches.length; + let vars = Object.keys(env); + vars.forEach(key => { suggestions.push({ - sortText: '00000000' + it, - label: it, - filterText: it, + label: key, + filterText: key, kind: monaco.languages.CompletionItemKind.Variable, - detail: it, - insertText: it, + detail: env[key], + insertText: key, insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet }) }) - let known = suggestions.map(it => it.detail); - let matches = input.match(/[a-zA-Z_$]+/ig); - let count = matches.length; if(count > 2){ - matches.filter((it,index) => index + 2 < count && known.indexOf(it) === -1).map(it => { + Array.from(new Set(matches)).filter((it,index) => index + 2 < count && known.indexOf(it) === -1 && vars.indexOf(it) === -1).map(it => { suggestions.push({ label: it, filterText: it, @@ -159,21 +171,26 @@ async function completionScript(suggestions, input) { let tokens = tokenizer(input); let tokenLen = tokens.length; if (tokenLen === 0) { + completionFunction(suggestions, input) return; } let parser = new Parser(new TokenStream(tokens)); const { best, env } = await parser.parseBest(input.length - 1, env); if(input.endsWith(".")){ await completionMethod(await best.getJavaType(env), suggestions) - } else { + } else if(best) { let astName = best.constructor.name; if (astName === 'MemberAccess' || astName === 'MethodCall') { await completionMethod(await best.target.getJavaType(env), suggestions) + } else { + completionFunction(suggestions, input, env) } + } else { + completionFunction(suggestions, input, env) } return suggestions; } catch (e) { - // console.error(e); + // console.log("error") } } @@ -207,6 +224,12 @@ const CompletionItemProvider = { }) } else if (value.length > 1) { await completionScript(suggestions, value) + } else { + completionFunction(suggestions, value, { + ...RequestParameter.environmentFunction(), + ...JavaClass.getAutoImportClass(), + ...JavaClass.getAutoImportModule() + }) } return {suggestions} }, diff --git a/magic-editor/src/console/src/scripts/editor/java-class.js b/magic-editor/src/console/src/scripts/editor/java-class.js index 6f0d9343..c74ba4f0 100644 --- a/magic-editor/src/console/src/scripts/editor/java-class.js +++ b/magic-editor/src/console/src/scripts/editor/java-class.js @@ -253,6 +253,7 @@ const setupOnlineFunction = (loader) => { const getOnlineFunction = (path) => { return onlineFunctionFinder && onlineFunctionFinder(path); } +const getDefineModules = () => Object.keys(scriptClass).filter(it => scriptClass[it].module) const exportValue = { findEnums, findAttributes, @@ -271,6 +272,7 @@ const exportValue = { getOnlineFunction, setupOnlineFunction, setExtensionAttribute, - getSimpleClass + getSimpleClass, + getDefineModules } export default exportValue; diff --git a/magic-editor/src/console/src/scripts/parsing/parser.js b/magic-editor/src/console/src/scripts/parsing/parser.js index be582934..0bb1b14e 100644 --- a/magic-editor/src/console/src/scripts/parsing/parser.js +++ b/magic-editor/src/console/src/scripts/parsing/parser.js @@ -120,7 +120,7 @@ export class Parser { } validateNode(node) { - if (node instanceof Literal || node instanceof VariableAccess || node instanceof MapOrArrayAccess) { + if (node instanceof Literal) { throw new ParseException('literal cannot be used alone', node.getSpan()); } } @@ -313,10 +313,14 @@ export class Parser { let opening = this.stream.consume().getSpan(); let token = this.stream.expect(TokenType.Identifier); this.checkKeyword(token.getSpan()); + let varDefine; if (this.stream.match(TokenType.Assignment, true)) { - return new VarDefine(new Span(opening, this.stream.getPrev().getSpan()), token.getText(), this.parseExpression()); + varDefine = new VarDefine(new Span(opening, this.stream.getPrev().getSpan()), token.getText(), this.parseExpression()); + } else { + varDefine = new VarDefine(new Span(opening, this.stream.getPrev().getSpan()), token.getText(), null); } - return new VarDefine(new Span(opening, this.stream.getPrev().getSpan()), token.getText(), null); + this.defines.push(varDefine); + return varDefine; } parseTryStatement() {