Skip to content

Commit

Permalink
Groovy REPL: added Groovy options noSyntaxCheck and restrictedCompletion
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Sep 8, 2020
1 parent 786dcd8 commit bb8b6fe
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions groovy/src/main/java/org/jline/script/GroovyEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public enum Format {JSON, GROOVY, NONE}
public static final String NANORC_SYNTAX = "nanorcSyntax";
public static final String NANORC_VALUE = "nanorcValue";
public static final String GROOVY_COLORS = "GROOVY_COLORS";
public static final String NO_SYNTAX_CHECK = "noSyntaxCheck";
public static final String RESTRICTED_COMPLETION = "restrictedCompletion";

private static final String VAR_GROOVY_OPTIONS = "GROOVY_OPTIONS";
private static final String REGEX_SYSTEM_VAR = "[A-Z]+[A-Z_]*";
Expand Down Expand Up @@ -652,6 +654,7 @@ public MethodCompleter(GroovyEngine engine){
public void complete(LineReader reader, ParsedLine commandLine, List<Candidate> candidates) {
assert commandLine != null;
assert candidates != null;
boolean restrictedCompletion = groovyEngine.groovyOption(RESTRICTED_COMPLETION, false);
String wordbuffer = commandLine.word();
String buffer = commandLine.line().substring(0, commandLine.cursor());
Brackets brackets;
Expand All @@ -669,7 +672,7 @@ public void complete(LineReader reader, ParsedLine commandLine, List<Candidate>
int eqsep = Helpers.statementBegin(brackets);
if (brackets.numberOfRounds() > 0 && brackets.lastCloseRound() > eqsep) {
int varsep = buffer.lastIndexOf('.');
if (varsep > 0 && varsep > brackets.lastCloseRound()) {
if (varsep > 0 && varsep > brackets.lastCloseRound() && !restrictedCompletion) {
Class<?> clazz = inspector.evaluateClass(buffer.substring(eqsep + 1, varsep));
int vs = wordbuffer.lastIndexOf('.');
String curBuf = wordbuffer.substring(0, vs + 1);
Expand Down Expand Up @@ -723,14 +726,14 @@ public void complete(LineReader reader, ParsedLine commandLine, List<Candidate>
if (inspector.nameClass().containsKey(var)) {
if (firstMethod) {
doStaticMethodCandidates(candidates, inspector.nameClass().get(var), curBuf, p);
} else {
} else if (!restrictedCompletion) {
Class<?> clazz = inspector.evaluateClass(wordbuffer.substring(eqsep + 1, varsep));
doMethodCandidates(candidates, clazz, curBuf, p);
}
} else if (inspector.hasVariable(var)) {
if (firstMethod) {
doMethodCandidates(candidates, inspector.getVariable(var).getClass(), curBuf, p);
} else {
} else if (!restrictedCompletion) {
Class<?> clazz = inspector.evaluateClass(wordbuffer.substring(eqsep + 1, varsep));
doMethodCandidates(candidates, clazz, curBuf, p);
}
Expand Down Expand Up @@ -805,6 +808,8 @@ private static class Inspector {
private final Map<String,Class<?>> nameClass;
private PrintStream nullstream;
private boolean canonicalNames = false;
private final boolean noSyntaxCheck;
private final boolean restrictedCompletion;
private String[] equationLines;
private int cuttedSize;
private final String nanorcSyntax;
Expand All @@ -815,6 +820,8 @@ public Inspector(GroovyEngine groovyEngine) {
this.nameClass = groovyEngine.nameClass;
this.canonicalNames = groovyEngine.groovyOption(CANONICAL_NAMES, canonicalNames);
this.nanorcSyntax = groovyEngine.groovyOption(NANORC_SYNTAX, DEFAULT_NANORC_SYNTAX);
this.noSyntaxCheck = groovyEngine.groovyOption(NO_SYNTAX_CHECK, false);
this.restrictedCompletion = groovyEngine.groovyOption(RESTRICTED_COMPLETION, false);
String gc = groovyEngine.groovyOption(GROOVY_COLORS, null);
groovyColors = gc != null && Styles.isAnsiStylePattern(gc) ? gc : DEFAULT_GROOVY_COLORS;
groovyEngine.getObjectCloner().markCache();
Expand Down Expand Up @@ -891,6 +898,9 @@ private String stripVarType(String statement) {
}

public void loadStatementVars(String line) {
if (restrictedCompletion) {
return;
}
for (String s : line.split("\\r?\\n")) {
String statement = s.trim();
try {
Expand All @@ -900,7 +910,7 @@ public void loadStatementVars(String line) {
if (statement.matches("^(if|while)\\s*\\(.*") || statement.matches("(}\\s*|^)else(\\s*\\{|$)")
|| statement.matches("(}\\s*|^)else\\s+if\\s*\\(.*") || statement.matches("^break[;]+")
|| statement.matches("^case\\s+.*:") || statement.matches("^default\\s+:")
|| statement.matches("(\\{|})") || statement.length() == 0) {
|| statement.matches("([{}])") || statement.length() == 0) {
continue;
} else if (forEachMatcher.matches()) {
statement = stripVarType(forEachMatcher.group(1).trim());
Expand Down Expand Up @@ -962,7 +972,9 @@ public CmdDesc scriptDescription(CmdLine line) {
out = methodDescription(line);
break;
case SYNTAX:
out = checkSyntax(line);
if (!noSyntaxCheck) {
out = checkSyntax(line);
}
break;
}
} catch (Throwable e) {
Expand Down Expand Up @@ -1002,7 +1014,9 @@ private CmdDesc methodDescription(CmdLine line) {
if (st.matches("[A-Z]+\\w+\\s*\\(.*")) {
st = "new " + st;
}
clazz = evaluateClass(st);
if (!restrictedCompletion || new Brackets(st).numberOfRounds() == 0) {
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()) {
Expand Down Expand Up @@ -1128,7 +1142,7 @@ private CmdDesc checkSyntax(CmdLine line) {
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+]*\\)")
|| objEquation.matches("\\(\\s*\\)")) {
// do nothing
} else {
Expand Down

0 comments on commit bb8b6fe

Please sign in to comment.