Skip to content

Commit da5bc98

Browse files
gselzerclaude
andcommitted
Type script parameters via a JythonDialect
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent d8fbc6f commit da5bc98

2 files changed

Lines changed: 73 additions & 14 deletions

File tree

‎src/main/java/org/scijava/plugins/scripting/jython/JythonCodeCompleter.java‎

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import org.scijava.code.api.CompletionRequest;
5555
import org.scijava.code.api.Completion;
5656
import org.scijava.code.api.CompletionResult;
57+
import org.scijava.code.api.ScriptDialect;
5758
import org.scijava.code.api.SignatureHelp;
5859
import org.scijava.code.api.Signatures;
5960

@@ -72,6 +73,9 @@
7273
priority = Priority.HIGH)
7374
public class JythonCodeCompleter extends AbstractCodeCompleterPlugin {
7475

76+
/** How scripts see their parameters: as the Java objects themselves. */
77+
private static final JythonDialect DIALECT = new JythonDialect();
78+
7579
private final JythonAutoCompletions engine = new JythonAutoCompletions();
7680

7781
public JythonCodeCompleter() {
@@ -185,8 +189,8 @@ private Map<String, DotAutocompletions> predefinedVariables(
185189
: context.getService(CodeCompletionService.class);
186190
if (completionService != null) {
187191
completionService.scriptParameters(request.text()).forEach((name,
188-
type) -> vars.put(name, new VarDotAutocompletions(boxed(type)
189-
.getName())));
192+
type) -> vars.put(name, new VarDotAutocompletions(DIALECT.runtimeType(
193+
type))));
190194
}
191195
final ScriptContext scriptContext = request.context();
192196
final Bindings bindings = scriptContext == null ? null : scriptContext
@@ -221,18 +225,9 @@ private static DotAutocompletions bindingCompletions(final Object value) {
221225
return new VarDotAutocompletions(value.getClass().getName());
222226
}
223227

224-
/** Maps primitive types to their wrappers, whose members can be listed. */
225-
private static Class<?> boxed(final Class<?> type) {
226-
if (!type.isPrimitive()) return type;
227-
if (type == int.class) return Integer.class;
228-
if (type == long.class) return Long.class;
229-
if (type == double.class) return Double.class;
230-
if (type == float.class) return Float.class;
231-
if (type == boolean.class) return Boolean.class;
232-
if (type == char.class) return Character.class;
233-
if (type == short.class) return Short.class;
234-
if (type == byte.class) return Byte.class;
235-
return Object.class; // void
228+
@Override
229+
public ScriptDialect dialect() {
230+
return DIALECT;
236231
}
237232

238233
/**
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 org.scijava.code.api.ScriptDialect;
32+
33+
/**
34+
* How Jython scripts see their script parameters: as the Java objects
35+
* themselves (primitives boxed), e.g. {@code image: net.imglib2.img.Img}.
36+
*
37+
* @author Gabriel Selzer
38+
*/
39+
class JythonDialect implements ScriptDialect {
40+
41+
@Override
42+
public String runtimeType(final Class<?> type) {
43+
return boxed(type).getName();
44+
}
45+
46+
@Override
47+
public String declare(final String name, final String type) {
48+
return name + ": " + type;
49+
}
50+
51+
/** Maps primitive types to their wrappers, whose members can be listed. */
52+
private static Class<?> boxed(final Class<?> type) {
53+
if (!type.isPrimitive()) return type;
54+
if (type == int.class) return Integer.class;
55+
if (type == long.class) return Long.class;
56+
if (type == double.class) return Double.class;
57+
if (type == float.class) return Float.class;
58+
if (type == boolean.class) return Boolean.class;
59+
if (type == char.class) return Character.class;
60+
if (type == short.class) return Short.class;
61+
if (type == byte.class) return Byte.class;
62+
return Object.class; // void
63+
}
64+
}

0 commit comments

Comments
 (0)