Skip to content

Commit

Permalink
Groovy REPL: complete get meta methods also as identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Apr 2, 2021
1 parent 6ffcadf commit 6567d4b
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions groovy/src/main/java/org/jline/script/GroovyEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ public static void doCandidates(List<Candidate> candidates, Collection<String> f
}

public static void doCandidates(List<Candidate> candidates, Map<String,String> fields, String curBuf, CandidateType type) {
if (fields == null) {
if (fields == null || fields.isEmpty()) {
return;
}
for (Map.Entry<String,String> entry : fields.entrySet()) {
Expand Down Expand Up @@ -817,6 +817,7 @@ public void complete(LineReader reader, ParsedLine commandLine, List<Candidate>

private static class MethodCompleter implements Completer {
private static final List<String> VALUES = Arrays.asList("true", "false");
private static final String REGEX_GET_METHOD = "get[A-Z].*";
private final GroovyEngine groovyEngine;
private final SystemRegistry systemRegistry = SystemRegistry.get();
private Inspector inspector;
Expand Down Expand Up @@ -982,9 +983,16 @@ private Set<String> doMetaMethodCandidates(List<Candidate> candidates, Object ob
ObjectInspector inspector = new ObjectInspector(object);
List<Map<String,String>> mms = inspector.metaMethods(false);
Set<String> metaMethods = new HashSet<>();
Set<String> identifiers = new HashSet<>();
for (Map<String,String> mm : mms) {
metaMethods.add(mm.get(ObjectInspector.FIELD_NAME));
String name = mm.get(ObjectInspector.FIELD_NAME);
metaMethods.add(name);
if (identifierCompletion && name.matches(REGEX_GET_METHOD)
&& mm.get(ObjectInspector.FIELD_PARAMETERS).isEmpty()) {
identifiers.add(convertGetMethod2identifier(name));
}
}
Helpers.doCandidates(candidates, identifiers, curBuf, CandidateType.IDENTIFIER);
Helpers.doCandidates(candidates, metaMethods, curBuf, CandidateType.META_METHOD);
return metaMethods;
}
Expand Down Expand Up @@ -1013,7 +1021,7 @@ private void doMethodCandidates(List<Candidate> candidates, Class<?> clazz, Stri
if (addIdentifiers) {
Set<String> identifiers = new HashSet<>();
for (String m : methods) {
if (m.matches("get[A-Z].*")) {
if (m.matches(REGEX_GET_METHOD)) {
Class<?> cc = clazz;
while (cc != null) {
try {
Expand All @@ -1022,9 +1030,7 @@ private void doMethodCandidates(List<Candidate> candidates, Class<?> clazz, Stri
} catch (NoSuchMethodException exp) {
cc.getDeclaredMethod(m);
}
char[] c = m.substring(3).toCharArray();
c[0] = Character.toLowerCase(c[0]);
identifiers.add(new String(c));
identifiers.add(convertGetMethod2identifier(m));
break;
} catch (NoSuchMethodException e) {
cc = cc.getSuperclass();
Expand All @@ -1043,6 +1049,12 @@ private void doMethodCandidates(List<Candidate> candidates, Class<?> clazz, Stri
Helpers.doCandidates(candidates, Helpers.getFields(clazz, access.allFields), curBuf, CandidateType.FIELD);
}

private String convertGetMethod2identifier(String name) {
char[] c = name.substring(3).toCharArray();
c[0] = Character.toLowerCase(c[0]);
return new String(c);
}

private void doStaticMethodCandidates(List<Candidate> candidates, Class<?> clazz, String curBuf) {
if (clazz == null) {
return;
Expand Down

0 comments on commit 6567d4b

Please sign in to comment.