Skip to content

Commit

Permalink
Groovy REPL method description: improved detection of method's Class +
Browse files Browse the repository at this point in the history
a few other minor fixes
  • Loading branch information
mattirn committed Sep 6, 2020
1 parent 9c7acb9 commit 1484c4d
Showing 1 changed file with 35 additions and 30 deletions.
65 changes: 35 additions & 30 deletions groovy/src/main/java/org/jline/script/GroovyEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,14 @@ private Object execute(String statement) {
return out;
}

private String stripVarType(String statement) {
if (statement.matches("\\w+\\s+\\w+.*")) {
int idx = statement.indexOf(' ');
return statement.substring(idx + 1);
}
return statement;
}

public void loadStatementVars(String line) {
for (String s : line.split("\\r?\\n")) {
String statement = s.trim();
Expand All @@ -895,19 +903,11 @@ public void loadStatementVars(String line) {
|| statement.matches("(\\{|})") || statement.length() == 0) {
continue;
} else if (forEachMatcher.matches()) {
statement = forEachMatcher.group(1).trim();
statement = stripVarType(forEachMatcher.group(1).trim());
String cc = forEachMatcher.group(2);
if (statement.matches("\\w+\\s+\\w+.*")) {
int idx = statement.indexOf(' ');
statement = statement.substring(idx + 1);
}
statement += "=" + cc + " instanceof Map ? " + cc + ".entrySet()[0] : " + cc + "[0]";
} else if (forMatcher.matches()) {
statement = forMatcher.group(1).trim();
if (statement.matches("\\w+\\s+\\w+.*")) {
int idx = statement.indexOf(' ');
statement = statement.substring(idx + 1);
}
statement = stripVarType(forMatcher.group(1).trim());
if (!statement.contains("=")) {
statement += " = null";
}
Expand All @@ -917,9 +917,8 @@ public void loadStatementVars(String line) {
for (String v : vars) {
statement += v + " = null; ";
}
} else if (statement.matches("\\w+\\s+.*=.*")) {
int idx = statement.indexOf(' ');
statement = statement.substring(idx + 1);
} else if (statement.contains("=")) {
statement = stripVarType(statement);
}
Brackets br = new Brackets(statement);
if (statement.contains("=") && !br.openRound() && !br.openCurly() && !br.openSquare()) {
Expand Down Expand Up @@ -989,21 +988,26 @@ private CmdDesc methodDescription(CmdLine line) {
boolean constructor = false;
Class<?> clazz = null;
String methodName = null;
if ((args.size() == 2 && args.get(0).matches("(new|\\w+=new)"))
|| (args.size() > 2 && Helpers.constructorStatement(args.get(args.size() - 2)))) {
String buffer = line.getHead();
int eqsep = Helpers.statementBegin(new Brackets(buffer));
int varsep = buffer.lastIndexOf('.');
if (varsep > 0 && varsep > eqsep) {
loadStatementVars(buffer);
methodName = buffer.substring(varsep + 1);
int ior = Brackets.indexOfOpeningRound(buffer.substring(0, varsep));
if (ior > 0 && ior < eqsep) {
eqsep = ior;
}
String st = buffer.substring(eqsep + 1, varsep);
if (st.matches("[A-Z]+\\w+\\s*\\(.*")) {
st = "new " + st;
}
clazz = evaluateClass(st);
} else if (args.size() > 1 && Helpers.constructorStatement(args.get(args.size() - 2))
&& args.get(args.size() - 1).matches("[A-Z]+\\w+\\s*\\(.*")
&& new Brackets(args.get(args.size() - 1)).openRound()) {
constructor = true;
clazz = evaluateClass(trimName(args.get(args.size() - 1)));
} else {
String buffer = line.getHead();
String wordbuffer = trimName(args.get(args.size() - 1));
Brackets brackets = new Brackets(buffer);
int varsep = wordbuffer.lastIndexOf('.');
int eqsep = Helpers.statementBegin(buffer, wordbuffer, brackets);
if (varsep > 0 && varsep > eqsep) {
loadStatementVars(buffer);
methodName = wordbuffer.substring(varsep + 1);
clazz = evaluateClass(wordbuffer.substring(eqsep + 1, varsep));
}
}
List<AttributedString> mainDesc = new ArrayList<>();
if (clazz != null) {
Expand Down Expand Up @@ -1093,7 +1097,7 @@ private CmdDesc methodDescription(CmdLine line) {
private String trimMethodDescription(StringBuilder sb) {
String out = sb.toString();
if (canonicalNames) {
out = out.replaceAll("java.lang.", "");
out = out.replaceAll("java\\.lang\\.", "");
}
return out;
}
Expand Down Expand Up @@ -1121,10 +1125,11 @@ private CmdDesc checkSyntax(CmdLine line) {
return out;
}
List<AttributedString> mainDesc = new ArrayList<>();
String objEquation = line.getHead().substring(eqsep + 1, end);
String objEquation = line.getHead().substring(eqsep + 1, end).trim();
equationLines = objEquation.split("\\r?\\n");
cuttedSize = eqsep + 1;
if (objEquation.matches("\\(\\s*\\w+\\s*[,\\s*\\w+\\s*]*\\)")) {
if (objEquation.matches("\\(\\s*\\w+\\s*[,\\s*\\w+\\s*]*\\)")
|| objEquation.matches("\\(\\s*\\)")) {
// do nothing
} else {
try {
Expand Down Expand Up @@ -1214,7 +1219,7 @@ private int errorIndex(String message, SyntaxException se) {
String line = null;
String[] mlines = message.split("\n");
for (int i = 0; i < mlines.length; i++) {
if (mlines[i].matches(".*Script[0-9]+.groovy: .*")) {
if (mlines[i].matches(".*Script[0-9]+\\.groovy: .*")) {
line = mlines[i + 1].trim();
break;
}
Expand Down

0 comments on commit 1484c4d

Please sign in to comment.