Skip to content

Commit

Permalink
Groovy REPL: add inner class completion
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Apr 11, 2021
1 parent 8daf291 commit 7df36e3
Showing 1 changed file with 41 additions and 10 deletions.
51 changes: 41 additions & 10 deletions groovy/src/main/java/org/jline/script/GroovyEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -767,8 +767,10 @@ private static Set<String> sourcesForPackage(String domain) {
}
String source = p.toString();
String className = source.substring(2, source.lastIndexOf(".")).replace(separator, ".");
if (onlyPackage && Character.isUpperCase(className.charAt(domain.length() + 1))) {
out.add(className);
if (onlyPackage) {
if (Character.isUpperCase(className.charAt(domain.length() + 1))) {
out.add(className);
}
} else {
out.add(className);
}
Expand Down Expand Up @@ -1038,7 +1040,7 @@ public void complete(LineReader reader, ParsedLine commandLine, List<Candidate>
Object involvedObject = inspector.getInvolvedObject();
int vs = wordbuffer.lastIndexOf('.');
String curBuf = wordbuffer.substring(0, vs + 1);
doMethodCandidates(candidates, involvedObject == null ? clazz : involvedObject, curBuf);
doMethodCandidates(candidates, involvedObject, clazz, curBuf);
}
} else if (completingConstructor(commandLine)) {
if (wordbuffer.matches("[a-z]+.*")) {
Expand Down Expand Up @@ -1081,20 +1083,23 @@ public void complete(LineReader reader, ParsedLine commandLine, List<Candidate>
String var = param.substring(0, param.indexOf('.'));
String curBuf = wordbuffer.substring(0, varsep + 1);
if (inspector.nameClass().containsKey(var)) {
Class<?> clazz = null;
Object involvedObject = null;
if (firstMethod) {
doStaticMethodCandidates(candidates, inspector.nameClass().get(var), curBuf);
clazz = inspector.nameClass().get(var);
} else if (!restrictedCompletion) {
Class<?> clazz = inspector.evaluateClass(wordbuffer.substring(eqsep + 1, varsep));
Object involvedObject = inspector.getInvolvedObject();
doMethodCandidates(candidates, involvedObject == null ? clazz : involvedObject, curBuf);
clazz = inspector.evaluateClass(wordbuffer.substring(eqsep + 1, varsep));
involvedObject = inspector.getInvolvedObject();
}
Helpers.doCandidates(candidates, retrieveDecleredClasses(clazz), curBuf, CandidateType.PACKAGE);
doMethodCandidates(candidates, involvedObject, clazz, curBuf);
} else if (inspector.hasVariable(var)) {
if (firstMethod) {
doMethodCandidates(candidates, inspector.getVariable(var), curBuf);
} else if (!restrictedCompletion) {
Class<?> clazz = inspector.evaluateClass(wordbuffer.substring(eqsep + 1, varsep));
Object involvedObject = inspector.getInvolvedObject();
doMethodCandidates(candidates, involvedObject == null ? clazz : involvedObject, curBuf);
doMethodCandidates(candidates, involvedObject, clazz, curBuf);
}
} else {
try {
Expand Down Expand Up @@ -1169,6 +1174,14 @@ private Set<String> doMetaMethodCandidates(List<Candidate> candidates, Object ob
return metaMethods;
}

private void doMethodCandidates(List<Candidate> candidates, Object object, Class<?> clazz, String curBuf) {
if (object != null) {
doMethodCandidates(candidates, object, curBuf);
} else if (clazz != null) {
doStaticMethodCandidates(candidates, clazz, curBuf);
}
}

private void doMethodCandidates(List<Candidate> candidates, Object object, String curBuf) {
if (object == null) {
return;
Expand Down Expand Up @@ -1255,6 +1268,17 @@ private Set<String> retrieveConstructors(boolean all) {
return out;
}

private Set<String> retrieveDecleredClasses(Class<?> clazz) {
Set<String> out = new HashSet<>();
if (clazz != null) {
int nameLength = clazz.getCanonicalName().length();
for (Class<?> c : access.allClasses ? clazz.getDeclaredClasses() : clazz.getClasses()) {
out.add(c.getCanonicalName().substring(nameLength + 1));
}
}
return out;
}

private Set<String> retrieveClassesWithStaticMethods() {
Set<String> out = new HashSet<>();
for (Iterator<Map.Entry<String, Class<?>>> it = inspector.nameClass().entrySet().iterator(); it.hasNext(); ) {
Expand Down Expand Up @@ -1343,15 +1367,22 @@ public Class<?> evaluateClass(String objectStatement) {
Class<?> out = null;
try {
involvedObject = execute(objectStatement);
out = involvedObject.getClass();
if (involvedObject instanceof Class) {
out = (Class<?>)involvedObject;
if (!objectStatement.endsWith(".class")) {
involvedObject = null;
}
} else {
out = involvedObject.getClass();
}
} catch (Exception e) {
Log.debug("objectStatement: ", objectStatement);
if (Log.isDebugEnabled()) {
e.printStackTrace();
}
}
try {
if (out == null || out == Class.class) {
if (out == null) {
if (!objectStatement.contains(".") ) {
out = (Class<?>)execute(objectStatement + ".class");
} else {
Expand Down

0 comments on commit 7df36e3

Please sign in to comment.