Skip to content

Commit

Permalink
GroovyEngine: display method descriptions with short type names
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Jun 22, 2020
1 parent dbf4118 commit f181773
Showing 1 changed file with 41 additions and 8 deletions.
49 changes: 41 additions & 8 deletions groovy/src/main/java/org/jline/script/GroovyEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
*/
public class GroovyEngine implements ScriptEngine {
public enum Format {JSON, GROOVY, NONE};
public static final String OPTION_SHORT_NAMES = "shortNames";

private static final String VAR_GROOVY_OPTIONS = "GROOVY_OPTIONS";
private static final String REGEX_SYSTEM_VAR = "[A-Z]+[A-Z_]*";
private static final String REGEX_VAR = "[a-zA-Z_]+[a-zA-Z0-9_]*";
private static final Pattern PATTERN_FUNCTION_DEF = Pattern.compile("^def\\s+(" + REGEX_VAR + ")\\s*\\(([a-zA-Z0-9_ ,]*)\\)\\s*\\{(.*)?\\}(|\n)$"
Expand Down Expand Up @@ -348,6 +351,21 @@ public CmdDesc scriptDescription(CmdLine line) {
return new Inspector(this).scriptDescription(line);
}

@SuppressWarnings("unchecked")
private Map<String,Object> groovyOptions() {
return hasVariable(VAR_GROOVY_OPTIONS) ? (Map<String, Object>) get(VAR_GROOVY_OPTIONS)
: new HashMap<>();
}

@SuppressWarnings("unchecked")
private <T>T groovyOption(String option, T defval) {
T out = defval;
try {
out = (T) groovyOptions().getOrDefault(option, defval);
} catch (Exception e) {
}
return out;
}

private Completer compileCompleter() {
List<Completer> completers = new ArrayList<>();
Expand Down Expand Up @@ -766,10 +784,12 @@ private static class Inspector {
private Map<String,String> imports = new HashMap<>();
private Map<String,Class<?>> nameClass = new HashMap<>();
PrintStream nullstream;
boolean shortNames = true;

public Inspector(GroovyEngine groovyEngine) {
this.imports = groovyEngine.imports;
this.nameClass = groovyEngine.nameClass;
this.shortNames = groovyEngine.groovyOption(OPTION_SHORT_NAMES, shortNames);
for (Map.Entry<String, Object> entry : groovyEngine.find().entrySet()) {
Object obj = groovyEngine.getObjectCloner().clone(entry.getValue());
sharedData.setVariable(entry.getKey(), obj);
Expand Down Expand Up @@ -936,14 +956,19 @@ private CmdDesc methodDescription(CmdLine line) throws Exception {
if (constructor) {
for (Constructor<?> m : clazz.getConstructors()) {
StringBuilder sb = new StringBuilder();
sb.append(m.getName());
String name = m.getName();
if (shortNames) {
int idx = name.lastIndexOf('.');
name = name.substring(idx + 1);
}
sb.append(name);
sb.append("(");
boolean first = true;
for(Class<?> p: m.getParameterTypes()) {
if (!first) {
sb.append(", ");
}
sb.append(p.getTypeName());
sb.append(shortNames ? p.getSimpleName() : p.getTypeName());
first = false;
}
sb.append(")");
Expand All @@ -954,10 +979,10 @@ private CmdDesc methodDescription(CmdLine line) throws Exception {
} else {
sb.append(", ");
}
sb.append(e.getCanonicalName());
sb.append(shortNames ? e.getSimpleName() : e.getCanonicalName());
first = false;
}
mainDesc.add(java.highlight(sb.toString().replaceAll("java.lang.", "")));
mainDesc.add(java.highlight(trimMethodDescription(sb)));
}
} else {
List<String> addedMethods = new ArrayList<>();
Expand All @@ -973,7 +998,7 @@ private CmdDesc methodDescription(CmdLine line) throws Exception {
if (Modifier.isStatic(m.getModifiers())) {
sb.append("static ");
}
sb.append(m.getReturnType().getCanonicalName());
sb.append(shortNames ? m.getReturnType().getSimpleName() : m.getReturnType().getCanonicalName());
sb.append(" ");
sb.append(methodName);
sb.append("(");
Expand All @@ -982,7 +1007,7 @@ private CmdDesc methodDescription(CmdLine line) throws Exception {
if (!first) {
sb.append(", ");
}
sb.append(p.getTypeName());
sb.append(shortNames ? p.getSimpleName() : p.getTypeName());
first = false;
}
sb.append(")");
Expand All @@ -993,12 +1018,12 @@ private CmdDesc methodDescription(CmdLine line) throws Exception {
} else {
sb.append(", ");
}
sb.append(e.getCanonicalName());
sb.append(shortNames ? e.getSimpleName() : e.getCanonicalName());
first = false;
}
if (!addedMethods.contains(sb.toString())) {
addedMethods.add(sb.toString());
mainDesc.add(java.highlight(sb.toString().replaceAll("java.lang.", "")));
mainDesc.add(java.highlight(trimMethodDescription(sb)));
}
}
clazz = clazz.getSuperclass();
Expand All @@ -1009,6 +1034,14 @@ private CmdDesc methodDescription(CmdLine line) throws Exception {
return out;
}

private String trimMethodDescription(StringBuilder sb) {
String out = sb.toString();
if (!shortNames) {
out = out.replaceAll("java.lang.", "");
}
return out;
}

}

private static class ObjectCloner implements Cloner {
Expand Down

0 comments on commit f181773

Please sign in to comment.