Skip to content

Commit d52fab4

Browse files
ctruedenclaude
andcommitted
Provide toolkit-agnostic Jython code completion
Port the Jython completion engine from the (now-retired) script-editor-jython repo into this scripting plugin, so Jython provides both its ScriptLanguage and its CodeCompleter while depending only on scijava-common + jython — no Swing or RSyntaxTextArea. - JythonCodeCompleter: a CodeCompleterPlugin that derives the line context from a CompletionRequest and returns neutral Completions + a ParameterChoices. - JythonAutoCompletions: the analysis engine, emitting neutral org.scijava.script.complete.Completion (no RSTA), with auto-imports expressed as Completion.TextEdit and parameter assistance via ParameterChoices. - JythonScriptParser/Scope/DotAutocompletions* and an internal CompletionText: unchanged AST logic, with class discovery/javadoc via scijava-common's ClassIndex. - The legacy RSTA glue (LanguageSupportPlugin, AutocompletionProvider, AutoCompletion, FunctionCompletion) is dropped; syntax highlighting moves to script-editor. The hardcoded ImageJ ij.dir scan is replaced by locating the jython jar on the classpath. Adds an antlr-runtime dependency (Jython AST) and a temporary scijava-common version override until a release pins the new completion SPI. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 43d5afe commit d52fab4

17 files changed

Lines changed: 2405 additions & 0 deletions

‎pom.xml‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@
8484
<properties>
8585
<package-name>org.scijava.plugins.scripting.jython</package-name>
8686

87+
<!-- TEMP: depend on the in-development scijava-common providing the
88+
org.scijava.script.complete code-completion SPI (Completion,
89+
CodeCompleterPlugin, ClassIndex, ...). Remove once a release
90+
containing it is pinned by pom-scijava. -->
91+
<scijava-common.version>2.100.2-SNAPSHOT</scijava-common.version>
92+
8793
<license.licenseName>bsd_2</license.licenseName>
8894
<license.copyrightOwners>SciJava developers.</license.copyrightOwners>
8995
<license.excludes>**/script_templates/**</license.excludes>
@@ -104,6 +110,11 @@
104110
<groupId>org.python</groupId>
105111
<artifactId>jython-slim</artifactId>
106112
</dependency>
113+
<!-- Used by JythonScriptParser to walk the Jython AST. -->
114+
<dependency>
115+
<groupId>org.antlr</groupId>
116+
<artifactId>antlr-runtime</artifactId>
117+
</dependency>
107118

108119
<!-- Test dependencies -->
109120
<dependency>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*-
2+
* #%L
3+
* Jython language support for SciJava Script Editor.
4+
* %%
5+
* Copyright (C) 2020 - 2024 SciJava developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
package org.scijava.plugins.scripting.jython;
30+
31+
import java.util.ArrayList;
32+
import java.util.List;
33+
34+
35+
public class ClassDotAutocompletions extends DefVarDotAutocompletions {
36+
final List<String> superclassNames; // List of superclasses
37+
final List<CompletionText>dotAutocompletions; // List of class methods and fields
38+
39+
/**
40+
*
41+
* @param fnName
42+
* @param superclassNames
43+
* @param argumentNames
44+
* @param dotAutocompletions
45+
* @param class_scope
46+
*/
47+
public ClassDotAutocompletions(final String fnName, List<String> superclassNames,
48+
final List<String> argumentNames, final List<CompletionText> dotAutocompletions, final Scope class_scope) {
49+
super(fnName, null, argumentNames, class_scope);
50+
this.superclassNames = superclassNames;
51+
this.dotAutocompletions = dotAutocompletions;
52+
}
53+
54+
@Override
55+
public List<CompletionText> get() {
56+
final List<CompletionText> ac = new ArrayList<>(this.dotAutocompletions);
57+
for (final String className: this.superclassNames) {
58+
List<CompletionText> fm = DotAutocompletions.getPublicFieldsAndMethods(className);
59+
if (fm.isEmpty()) {
60+
// Maybe it's a python class
61+
// get them all via the org.python.indexer.Indexer and Builtins classes, see StaticDotAutocompletions
62+
JythonDev.print("Don't know yet how to handle class " + className);
63+
}
64+
ac.addAll(fm);
65+
}
66+
return ac;
67+
}
68+
69+
public void put(final CompletionText entry) {
70+
if (this.dotAutocompletions.contains(entry)) return; // list search OK: very low N
71+
this.dotAutocompletions.add(entry);
72+
}
73+
74+
/** Make this be cda plus its own dotAutocompletions.
75+
*
76+
* @param cda
77+
*/
78+
public void mutateIntoPlus(final ClassDotAutocompletions cda)
79+
{
80+
this.className = cda.className;
81+
this.fnName = cda.fnName;
82+
this.superclassNames.clear();
83+
this.superclassNames.addAll(cda.superclassNames);
84+
this.dotAutocompletions.addAll(cda.dotAutocompletions);
85+
this.argumentNames.clear();
86+
this.argumentNames.addAll(cda.argumentNames);
87+
this.scope = cda.scope;
88+
}
89+
90+
@Override
91+
public String toString() {
92+
return "ClassDotAutocompletions: " + this.fnName + "(" + String.join(", ", this.argumentNames) + ")" + " -- " + String.join(", ", this.get().toString());
93+
}
94+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*-
2+
* #%L
3+
* JSR-223-compliant Jython scripting language plugin.
4+
* %%
5+
* Copyright (C) 2020 - 2025 SciJava developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
package org.scijava.plugins.scripting.jython;
30+
31+
import java.lang.reflect.Constructor;
32+
import java.lang.reflect.Field;
33+
import java.lang.reflect.Method;
34+
import java.lang.reflect.Parameter;
35+
import java.util.Arrays;
36+
import java.util.Collections;
37+
import java.util.List;
38+
39+
import org.scijava.script.complete.ClassIndex;
40+
41+
/**
42+
* A mutable suggestion used internally by the Jython completion engine. It holds
43+
* the replacement text plus optional documentation and (for callables) parameter
44+
* and return-type metadata. The {@link JythonCodeCompleter} converts these into
45+
* toolkit-agnostic {@link org.scijava.script.complete.Completion}s at the
46+
* boundary.
47+
*
48+
* @author Albert Cardona
49+
*/
50+
public class CompletionText {
51+
52+
private String replacementText;
53+
private String description;
54+
private String summary;
55+
private List<Parameter> method_args = Collections.emptyList();
56+
private String method_returnType = null;
57+
58+
public CompletionText(final String replacementText) {
59+
this(replacementText, (String) null, (String) null);
60+
}
61+
62+
public CompletionText(final String replacementText, final String summary,
63+
final String description)
64+
{
65+
this.replacementText = replacementText;
66+
this.summary = summary;
67+
this.description = description;
68+
}
69+
70+
public CompletionText(final String replacementText, final Class<?> c,
71+
final Field f)
72+
{
73+
this(replacementText, ClassIndex.getSummaryCompletion(f, c), null);
74+
}
75+
76+
public CompletionText(final String replacementText, final Class<?> c,
77+
final Method m)
78+
{
79+
this(replacementText, ClassIndex.getSummaryCompletion(m, c), null);
80+
this.method_args = Arrays.asList(m.getParameters());
81+
this.method_returnType = m.getReturnType().getCanonicalName();
82+
}
83+
84+
public CompletionText(final String replacementText, final Class<?> c,
85+
final Constructor<?> constructor)
86+
{
87+
this(replacementText, ClassIndex.getSummaryCompletion(constructor, c),
88+
null);
89+
this.method_args = Arrays.asList(constructor.getParameters());
90+
this.method_returnType = c.getCanonicalName();
91+
}
92+
93+
public String getReplacementText() {
94+
return replacementText;
95+
}
96+
97+
public String getDescription() {
98+
return description;
99+
}
100+
101+
public String getSummary() {
102+
return summary;
103+
}
104+
105+
public List<Parameter> getMethodArgs() {
106+
return method_args;
107+
}
108+
109+
public String getReturnType() {
110+
return method_returnType;
111+
}
112+
113+
public void setReplacementText(final String replacementText) {
114+
this.replacementText = replacementText;
115+
}
116+
117+
public void setDescription(final String description) {
118+
this.description = description;
119+
}
120+
121+
public void setSummary(final String summary) {
122+
this.summary = summary;
123+
}
124+
125+
@Override
126+
public String toString() {
127+
return replacementText + " | " + description + " | " + summary;
128+
}
129+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*-
2+
* #%L
3+
* Jython language support for SciJava Script Editor.
4+
* %%
5+
* Copyright (C) 2020 - 2024 SciJava developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
package org.scijava.plugins.scripting.jython;
30+
31+
import java.util.Arrays;
32+
import java.util.List;
33+
import java.util.stream.Collectors;
34+
35+
36+
public class ConstructorAutocompletions implements DotAutocompletions
37+
{
38+
private final Class<?> c;
39+
40+
public ConstructorAutocompletions(final Class<?> c) {
41+
this.c = c;
42+
}
43+
44+
@Override
45+
public List<CompletionText> get() {
46+
return Arrays.asList(this.c.getConstructors()).stream()
47+
.map(cons -> new CompletionText(c.getSimpleName(), c, cons))
48+
.collect(Collectors.toList());
49+
}
50+
51+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*-
2+
* #%L
3+
* Jython language support for SciJava Script Editor.
4+
* %%
5+
* Copyright (C) 2020 - 2024 SciJava developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
package org.scijava.plugins.scripting.jython;
30+
31+
import java.util.List;
32+
33+
public class DefVarDotAutocompletions extends VarDotAutocompletions {
34+
35+
String fnName;
36+
final List<String> argumentNames;
37+
Scope scope;
38+
39+
public DefVarDotAutocompletions(final String fnName, final String returnClassName, final List<String> argumentNames, final Scope scope) {
40+
super(returnClassName);
41+
this.fnName = fnName;
42+
this.argumentNames = argumentNames;
43+
this.scope = scope;
44+
}
45+
46+
public List<String> getArgumentNames() {
47+
return this.argumentNames;
48+
}
49+
50+
@Override
51+
public String toString() {
52+
return "DefVarAutocompletions:" +
53+
" Class: " + super.className +
54+
" Arguments: " + String.join(", ", this.argumentNames);
55+
}
56+
}

0 commit comments

Comments
 (0)