Skip to content

Commit 82a2056

Browse files
gselzerclaude
andcommitted
lsp-groovy: complete functions declared after the caret
A script's functions are methods of its class, so Groovy can call them from anywhere in the script, including above their declaration. Offer them by analyzing the whole script, with the caret marked; if that doesn't parse (e.g. the caret's line is unfinished), fall back to the functions before the caret. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ed99bf4 commit 82a2056

2 files changed

Lines changed: 39 additions & 8 deletions

File tree

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ 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));
184+
items.addAll(functionCompletions(scriptFunctions(ast, text,
185+
replaceStart, caret), prefix));
185186
items.addAll(keywordCompletions(prefix));
186187
items.addAll(classCompletions(doc, prefix));
187188
}
@@ -558,11 +559,28 @@ private static List<CompletionItem> variableCompletions(
558559
return out;
559560
}
560561

561-
private static List<CompletionItem> functionCompletions(final TypedAST ast,
562-
final String prefix)
562+
/**
563+
* {@link MethodNode}s defined in a script: in the whole script, with the
564+
* caret marked, if it parses; else before the caret.
565+
*/
566+
private List<MethodNode> scriptFunctions(final TypedAST before,
567+
final String text, final int replaceStart, final int caret)
568+
{
569+
// NB: A newline ends the text, in case it ends with a line comment.
570+
final String code = TypedAST.closeBrackets(TypedAST.markCaret(text
571+
.substring(0, replaceStart)) + text.substring(caret) + "\n");
572+
if (code != null) {
573+
final TypedAST whole = TypedAST.of(code, loader());
574+
if (whole.isParsed()) return whole.scriptMethods();
575+
}
576+
return before.scriptMethods();
577+
}
578+
579+
private static List<CompletionItem> functionCompletions(
580+
final List<MethodNode> functions, final String prefix)
563581
{
564582
final List<CompletionItem> out = new ArrayList<>();
565-
for (final MethodNode m : ast.scriptMethods()) {
583+
for (final MethodNode m : functions) {
566584
if (!matches(m.getName(), prefix) || m.getName().equals(prefix)) {
567585
continue;
568586
}

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,11 +399,24 @@ public void testCompleteScriptFunctions() {
399399
// Completable within its own body (e.g. a recursive call)
400400
final List<String> inside = complete("def foo() {\n fo");
401401
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
402+
// Completable before its declaration, as Groovy can call it from there
405403
final List<String> before = completeAt("fo|\n" + foo);
406-
assertFalse(before.contains("foo"), String.valueOf(before));
404+
assertTrue(before.contains("foo"), String.valueOf(before));
405+
// ...including inside a block
406+
final List<String> block = completeAt("if (true) {\n fo|\n}\n" + foo);
407+
assertTrue(block.contains("foo"), String.valueOf(block));
408+
// ...but not if what follows the caret doesn't parse
409+
final List<String> broken = completeAt("fo|\n" + foo + "x = (1 +]\n");
410+
assertFalse(broken.contains("foo"), String.valueOf(broken));
411+
// Completable on its own line
412+
final List<String> same = completeAt("def foo() { fo| }\n");
413+
assertTrue(same.contains("foo"), String.valueOf(same));
414+
// Completable after its declaration, even if the caret's line is
415+
// unfinished (so the whole script doesn't parse)
416+
final List<String> open = completeAt(foo +
417+
"println(fo|\ndef bar() {}\n");
418+
assertTrue(open.contains("foo"), String.valueOf(open));
419+
assertFalse(open.contains("bar"), String.valueOf(open));
407420
// Groovy gives every script class a run() method (holding the script's
408421
// top-level code) and a main(String[]) method, but the script didn't
409422
// write them, so they must not be offered as its functions.

0 commit comments

Comments
 (0)