mirror of
https://gitee.com/ssssssss-team/magic-api.git
synced 2026-06-02 11:59:41 +08:00
优化模板字符串内的代码提示
This commit is contained in:
@@ -37,9 +37,14 @@ class Expression extends Node {
|
||||
}
|
||||
|
||||
class Literal extends Expression {
|
||||
constructor(span, javaType) {
|
||||
constructor(span, javaType, expressionList) {
|
||||
super(span);
|
||||
this.javaType = javaType;
|
||||
this.expressionList = expressionList || []
|
||||
}
|
||||
|
||||
expressions() {
|
||||
return this.expressionList
|
||||
}
|
||||
|
||||
async getJavaType() {
|
||||
|
||||
@@ -479,14 +479,15 @@ export class Parser {
|
||||
|
||||
parseAccessOrCall(target, isNew) {
|
||||
if (target === TokenType.StringLiteral || target === TokenType.Identifier) {
|
||||
let identifier = this.stream.expect(target).getSpan();
|
||||
let token = this.stream.expect(target);
|
||||
let identifier = token.getSpan();
|
||||
if (target === TokenType.Identifier && "new" === identifier.getText()) {
|
||||
return this.parseNewExpression(identifier);
|
||||
}
|
||||
if (target === TokenType.Identifier && this.stream.match(TokenType.Lambda, true)) {
|
||||
return this.parseLambdaBody(identifier, [identifier.getText()]);
|
||||
}
|
||||
let result = target === TokenType.StringLiteral ? new Literal(identifier, 'java.lang.String') : new VariableAccess(identifier, identifier.getText());
|
||||
let result = target === TokenType.StringLiteral ? this.createStringLiteral(token) : new VariableAccess(identifier, identifier.getText());
|
||||
return this.parseAccessOrCall(result, isNew);
|
||||
} else {
|
||||
while (this.stream.hasMore() && this.stream.match([TokenType.LeftParantheses, TokenType.LeftBracket, TokenType.Period, TokenType.QuestionPeriod, TokenType.ColonColon], false)) {
|
||||
@@ -717,7 +718,6 @@ export class Parser {
|
||||
|
||||
parseAccessOrCallOrLiteral(expectRightCurly) {
|
||||
let expression;
|
||||
let isString = false;
|
||||
if (expectRightCurly && this.stream.match("}", false)) {
|
||||
return null;
|
||||
} else if (this.stream.match(TokenType.Spread, false)) {
|
||||
@@ -735,8 +735,7 @@ export class Parser {
|
||||
} else if (this.stream.match(TokenType.LeftBracket, false)) {
|
||||
expression = this.parseListLiteral();
|
||||
} else if (this.stream.match(TokenType.StringLiteral, false)) {
|
||||
isString = true;
|
||||
expression = new Literal(this.stream.expect(TokenType.StringLiteral).getSpan(), 'java.lang.String');
|
||||
expression = this.createStringLiteral(this.stream.expect(TokenType.StringLiteral));
|
||||
} else if (this.stream.match(TokenType.BooleanLiteral, false)) {
|
||||
expression = new Literal(this.stream.expect(TokenType.BooleanLiteral).getSpan(), 'java.lang.Boolean');
|
||||
} else if (this.stream.match(TokenType.DoubleLiteral, false)) {
|
||||
@@ -769,6 +768,20 @@ export class Parser {
|
||||
return this.parseAccessOrCall(expression);
|
||||
}
|
||||
|
||||
createStringLiteral(token) {
|
||||
if (token.getTokenStream() == null) {
|
||||
return new Literal(token.getSpan(), 'java.lang.String');
|
||||
}
|
||||
let tempStream = this.stream;
|
||||
this.stream = token.getTokenStream();
|
||||
let expressions = [];
|
||||
while (this.stream.hasMore()) {
|
||||
expressions.push(this.parseExpression());
|
||||
}
|
||||
this.stream = tempStream;
|
||||
return new Literal(token.getSpan(), 'java.lang.String', expressions);
|
||||
}
|
||||
|
||||
findBestMatch(node, position){
|
||||
let expressions = node.expressions().filter(it => it);
|
||||
for (let index in expressions) {
|
||||
|
||||
@@ -249,9 +249,6 @@ const tokenizerTemplateString = (stream, tokens)=>{
|
||||
}
|
||||
stream.consume();
|
||||
}
|
||||
if (!matchedEndQuote) {
|
||||
throw new ParseException("模板字符串没有结束符`", stream.endSpan());
|
||||
}
|
||||
let stringSpan = stream.endSpan(begin, stream.getPosition());
|
||||
let end = stream.getPosition() - 1;
|
||||
if (end - start > 0) {
|
||||
|
||||
Reference in New Issue
Block a user