代码提示优化

This commit is contained in:
mxd
2021-10-27 23:04:52 +08:00
parent f98235d8da
commit 81a2aff0c5
3 changed files with 47 additions and 18 deletions

View File

@@ -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}
},

View File

@@ -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;

View File

@@ -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() {