Skip to content

Commit ed99bf4

Browse files
committed
Enable function completion
Note that currently functions declared after the carat will not be given in completion requests. This could be enabled later, but it requires more changes
1 parent d278c9f commit ed99bf4

3 files changed

Lines changed: 63 additions & 14 deletions

File tree

‎scijava-lsp-groovy/src/main/java/org/scijava/lsp/groovy/GroovyLanguageServer.java‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ protected CompletionList complete(final Document doc, final int caret) {
181181
if (member) items.addAll(memberCompletions(ast, before, prefix));
182182
else {
183183
items.addAll(variableCompletions(ast.variablesAtCaret(), prefix));
184+
items.addAll(functionCompletions(ast, prefix));
184185
items.addAll(keywordCompletions(prefix));
185186
items.addAll(classCompletions(doc, prefix));
186187
}
@@ -462,7 +463,7 @@ private List<CompletionItem> scriptClassMembers(
462463
.getName()) != null) continue;
463464
out.add(field(f.getName(), f.getType().toString(false)));
464465
}
465-
for (final Overload m : scriptMethods(type, prefix, staticOnly, false)) {
466+
for (final Overload m : classMethods(type, prefix, staticOnly, false)) {
466467
out.add(m.toItem());
467468
}
468469
final List<CompletionItem> sorted = sorted(out);
@@ -483,7 +484,7 @@ private static List<CompletionItem> members(final Class<?> c,
483484
}
484485

485486
/** Methods of a class defined in the script: by prefix, or exact name. */
486-
private static List<Overload> scriptMethods(final ClassNode type,
487+
private static List<Overload> classMethods(final ClassNode type,
487488
final String name, final boolean staticOnly, final boolean exact)
488489
{
489490
final List<Overload> out = new ArrayList<>();
@@ -557,6 +558,20 @@ private static List<CompletionItem> variableCompletions(
557558
return out;
558559
}
559560

561+
private static List<CompletionItem> functionCompletions(final TypedAST ast,
562+
final String prefix)
563+
{
564+
final List<CompletionItem> out = new ArrayList<>();
565+
for (final MethodNode m : ast.scriptMethods()) {
566+
if (!matches(m.getName(), prefix) || m.getName().equals(prefix)) {
567+
continue;
568+
}
569+
out.add(methodNode(m.getName(), m).detail(m.getTypeDescriptor())
570+
.toItem());
571+
}
572+
return sorted(out);
573+
}
574+
560575
/** Groovy's keywords */
561576
private static Set<String> keywords() {
562577
// NB: The lexer literals don't contain primitives - we add those ourselves
@@ -681,8 +696,8 @@ else if (type.isPrimaryClassNode()) {
681696
if (name == null) return out;
682697
if (e.isImplicitThis()) {
683698
// A function defined in the script, or a GDK method such as println.
684-
for (final MethodNode m : ast.scriptMethods(name)) {
685-
out.add(methodNode(name, m));
699+
for (final MethodNode m : ast.scriptMethods()) {
700+
if (m.getName().equals(name)) out.add(methodNode(name, m));
686701
}
687702
out.addAll(named(gdkMethods(Object.class, name), name));
688703
return out;
@@ -692,7 +707,7 @@ else if (type.isPrimaryClassNode()) {
692707
if (type == null) return out;
693708
final boolean staticOnly = receiver instanceof ClassExpression;
694709
if (type.redirect().isPrimaryClassNode()) {
695-
return scriptMethods(type.redirect(), name, staticOnly, true);
710+
return classMethods(type.redirect(), name, staticOnly, true);
696711
}
697712
final Class<?> c = Types.box(ast.toClass(type));
698713
if (c == null) return out;

‎scijava-lsp-groovy/src/main/java/org/scijava/lsp/groovy/TypedAST.java‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -623,14 +623,13 @@ String typeName(final Expression e) {
623623
return name == null ? c.getName() : name;
624624
}
625625

626-
/** Gets the script's own methods (e.g. {@code def area(w, h)}) by name. */
627-
List<MethodNode> scriptMethods(final String name) {
628-
final List<MethodNode> methods = new ArrayList<>();
629-
if (module == null) return methods;
630-
for (final ClassNode c : module.getClasses()) {
631-
if (c.isScript()) methods.addAll(c.getMethods(name));
632-
}
633-
return methods;
626+
/**
627+
* Gets all the methods the script itself declares (e.g.
628+
* {@code def area(w, h)}), leaving out those Groovy generates for it, such
629+
* as {@code run} and {@code main}.
630+
*/
631+
List<MethodNode> scriptMethods() {
632+
return module == null ? Collections.emptyList() : module.getMethods();
634633
}
635634

636635
/** True iff the expression is, or contains, the caret marker. */

‎scijava-lsp-groovy/src/test/java/org/scijava/lsp/groovy/GroovyLanguageServerTest.java‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,27 @@ public void testCompleteArguments() {
390390
assertTrue(labels(items).contains("s"));
391391
}
392392

393+
@Test
394+
public void testCompleteScriptFunctions() {
395+
final String foo = "def foo() {\n println 'hi'\n}\n";
396+
// Completable after declaration
397+
final List<String> after = complete(foo + "x = 1\nfo");
398+
assertTrue(after.contains("foo"), String.valueOf(after));
399+
// Completable within its own body (e.g. a recursive call)
400+
final List<String> inside = complete("def foo() {\n fo");
401+
assertTrue(inside.contains("foo"), String.valueOf(inside));
402+
// Before its declaration: not offered.
403+
// TODO: Functions can be called anywhere in the script, so we could
404+
// enable this at a later date
405+
final List<String> before = completeAt("fo|\n" + foo);
406+
assertFalse(before.contains("foo"), String.valueOf(before));
407+
// Groovy gives every script class a run() method (holding the script's
408+
// top-level code) and a main(String[]) method, but the script didn't
409+
// write them, so they must not be offered as its functions.
410+
final List<String> generated = complete(foo + "ru");
411+
assertFalse(generated.contains("run"), String.valueOf(generated));
412+
}
413+
393414
// -- Helper methods --
394415

395416
/** A session about plain Groovy, with a server of its own. */
@@ -457,10 +478,16 @@ private static List<CompletionItem> items(final String code) {
457478

458479
private static List<CompletionItem> items(final ScriptSession session,
459480
final String code)
481+
{
482+
return items(session, code, code.length());
483+
}
484+
485+
private static List<CompletionItem> items(final ScriptSession session,
486+
final String code, final int caret)
460487
{
461488
try (ScriptSession s = session) {
462489
final List<CompletionItem> items = new ArrayList<>(s.completion(
463-
code, code.length()).get(30, TimeUnit.SECONDS).getItems());
490+
code, caret).get(30, TimeUnit.SECONDS).getItems());
464491
items.sort((a, b) -> a.getSortText().compareTo(b.getSortText()));
465492
return items;
466493
}
@@ -473,6 +500,14 @@ private static List<String> complete(final String code) {
473500
return labels(items(code));
474501
}
475502

503+
/** Completes at the caret, marked in the code by {@code |}. */
504+
private static List<String> completeAt(final String marked) {
505+
final int caret = marked.indexOf('|');
506+
final String code = marked.substring(0, caret) + marked.substring(caret +
507+
1);
508+
return labels(items(session(new Environment()), code, caret));
509+
}
510+
476511
private static List<String> labels(final List<CompletionItem> items) {
477512
return items.stream().map(CompletionItem::getLabel).collect(Collectors
478513
.toList());

0 commit comments

Comments
 (0)