Skip to content

Commit

Permalink
Groovy REPL: add tab-completion for groovy array methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Apr 1, 2022
1 parent 0959a40 commit d75b4f1
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion groovy/src/main/java/org/jline/script/GroovyEngine.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2021, the original author or authors.
* Copyright (c) 2002-2022, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
Expand Down Expand Up @@ -768,6 +768,9 @@ private static Map<String,String> getFields(Class<?> clazz, boolean all, boolean
}
}
}
if (clazz.getCanonicalName().endsWith("[]")) {
out.put("length", "int");
}
return out;
}

Expand Down Expand Up @@ -1206,6 +1209,12 @@ private Set<String> doMetaMethodCandidates(List<Candidate> candidates, Object ob
identifiers.add(convertGetMethod2identifier(name));
}
}
if (object.getClass().getCanonicalName().endsWith("[]")) {
if (object.getClass().getComponentType() == String.class) {
metaMethods.addAll(Arrays.asList("sort", "reverse"));
}
metaMethods.addAll(Arrays.asList("toList", "min", "max", "count", "size", "first", "last"));
}
Helpers.doCandidates(candidates, identifiers, curBuf, CandidateType.IDENTIFIER);
Helpers.doCandidates(candidates, metaMethods, curBuf, CandidateType.META_METHOD);
return metaMethods;
Expand Down Expand Up @@ -1718,6 +1727,23 @@ && new Brackets(args.get(args.size() - 1)).openRound()) {
mainDesc.add(syntaxHighlighter.highlight(trimMethodDescription(sb)));
}
}
if (clazz.getCanonicalName().endsWith("[]")) {
if (methodName.equals("sort") || methodName.equals("reverse")) {
mainDesc.add(syntaxHighlighter.highlight(clazz.getComponentType().getSimpleName() +
"[] " + methodName + "()"));
} else if (methodName.equals("first") || methodName.equals("last")
|| methodName.equals("min") || methodName.equals("max")) {
mainDesc.add(syntaxHighlighter.highlight(clazz.getComponentType().getSimpleName()
+ " " + methodName + "()"));
} else if (methodName.equals("size")) {
mainDesc.add(syntaxHighlighter.highlight("int size()"));
} else if (methodName.equals("toList")) {
mainDesc.add(syntaxHighlighter.highlight("List toList()"));
} else if (methodName.equals("count")) {
mainDesc.add(syntaxHighlighter.highlight("int count(Object)"));
mainDesc.add(syntaxHighlighter.highlight("int count(Closure)"));
}
}
}
for (Method m : Helpers.getClassMethods(clazz, access.allMethods, syntheticCompletion)) {
if (!m.getName().equals(methodName)) {
Expand Down

0 comments on commit d75b4f1

Please sign in to comment.