Skip to content

Commit 048fae7

Browse files
gselzerclaude
andcommitted
Follow scijava-code-lsp's ScriptDialect.header
The dialect writes its imports, its parameters' declarations and the task's in one header line, and names what it imports only for them, to hide from completion. Its tests now test each of its methods. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent e631dcf commit 048fae7

2 files changed

Lines changed: 81 additions & 67 deletions

File tree

‎src/main/java/org/scijava/plugins/scripting/appose/python/completion/ApposePythonDialect.java‎

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import java.math.BigInteger;
3535
import java.util.ArrayList;
3636
import java.util.Collection;
37-
import java.util.Collections;
3837
import java.util.List;
3938
import java.util.Map;
4039

@@ -58,6 +57,9 @@
5857
@Plugin(type = ScriptDialect.class, name = "Appose-Python")
5958
public class ApposePythonDialect implements ScriptDialect {
6059

60+
/** The type of images, as Appose passes them. */
61+
private static final String NDARRAY = "numpy.ndarray";
62+
6163
/** The type of the {@code task} variable scripts can use. */
6264
private static final String TASK_TYPE = "appose.python_worker.Task";
6365

@@ -84,9 +86,41 @@ public Environment environment(final String text, final File file) {
8486
}
8587

8688
@Override
87-
public String parameterType(final Class<?> type) {
89+
public Header header(final Map<String, Class<?>> parameters) {
90+
final List<String> code = new ArrayList<>();
91+
final List<String> hidden = new ArrayList<>();
92+
// Each Appose-Python script has access to its task
93+
code.add("import appose.python_worker");
94+
hidden.add("appose");
95+
// And numpy is imported for the server whenever an image parameter is
96+
// provided (the script itself does not get it)
97+
if (parameters.values().stream().map(ApposePythonDialect::parameterType)
98+
.anyMatch(NDARRAY::equals))
99+
{
100+
code.add("import numpy");
101+
hidden.add("numpy");
102+
}
103+
// Declare the types of each parameter for type reasoning
104+
parameters.forEach((name, type) -> {
105+
final String t = parameterType(type);
106+
// NB: Values Appose cannot pass are not declared.
107+
if (t != null) code.add(name + ": " + t);
108+
});
109+
// Declare the type of task for type reasoning
110+
code.add("task: " + TASK_TYPE);
111+
// Combine into a Header
112+
return new Header(String.join("; ", code), hidden);
113+
}
114+
115+
/**
116+
* @param type the Java type (of a SciJava Script parameter) declared in the
117+
* script
118+
* @return the Python type of the object given to the script by Appose, or
119+
* null if Appose cannot pass it
120+
*/
121+
static String parameterType(final Class<?> type) {
88122
if (RandomAccessibleInterval.class.isAssignableFrom(type) || NDArray.class
89-
.isAssignableFrom(type)) return "numpy.ndarray";
123+
.isAssignableFrom(type)) return NDARRAY;
90124
if (type == boolean.class || type == Boolean.class) return "bool";
91125
if (type == int.class || type == long.class || type == short.class ||
92126
type == byte.class || type == Integer.class || type == Long.class ||
@@ -108,31 +142,6 @@ public String parameterType(final Class<?> type) {
108142
return null;
109143
}
110144

111-
@Override
112-
public String declare(final String name, final String type) {
113-
return name + ": " + type;
114-
}
115-
116-
@Override
117-
public List<String> imports(final Collection<String> types) {
118-
final List<String> imports = new ArrayList<>();
119-
imports.add("import appose.python_worker");
120-
if (types.contains("numpy.ndarray")) imports.add("import numpy");
121-
return imports;
122-
}
123-
124-
@Override
125-
public String imported(final String statement) {
126-
// NB: "import a.b" binds a.
127-
return statement.startsWith("import ") ? statement.substring(7).split(
128-
"[.\\s]")[0] : null;
129-
}
130-
131-
@Override
132-
public List<String> extraDeclarations() {
133-
return Collections.singletonList(declare("task", TASK_TYPE));
134-
}
135-
136145
/** Gets where scripts run, creating it on first use. */
137146
private synchronized ApposePythonEnvironments environments() {
138147
if (environments == null) {

‎src/test/java/org/scijava/plugins/scripting/appose/python/completion/ApposePythonDialectTest.java‎

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -34,44 +34,41 @@
3434
import static org.junit.Assert.assertTrue;
3535

3636
import java.io.File;
37+
import java.util.Arrays;
3738
import java.util.Collections;
39+
import java.util.LinkedHashMap;
3840
import java.util.List;
41+
import java.util.Map;
3942

4043
import net.imglib2.img.Img;
4144

4245
import org.junit.After;
4346
import org.junit.Before;
4447
import org.junit.Test;
4548
import org.scijava.Context;
46-
import org.scijava.code.lsp.LanguageServerService;
4749
import org.scijava.code.lsp.Environment;
4850
import org.scijava.code.lsp.ScriptDialect;
49-
import org.scijava.code.lsp.ScriptDocument;
51+
import org.scijava.code.lsp.ScriptDialect.Header;
5052
import org.scijava.plugin.PluginService;
5153
import org.scijava.plugins.scripting.appose.python.ScriptEnvironment;
5254
import org.scijava.plugins.scripting.appose.python._internal.ResidentWorkerService;
5355
import org.scijava.script.ScriptLanguage;
5456
import org.scijava.script.ScriptService;
5557

5658
/**
57-
* Tests what {@link ApposePythonDialect} tells Python language servers about
58-
* Python scripts run via Appose (the script as plain Python, and where it
59-
* runs), without starting any server process.
59+
* Tests {@link ApposePythonDialect}.
6060
*
6161
* @author Gabriel Selzer
6262
*/
6363
public class ApposePythonDialectTest {
6464

65-
private static final ApposePythonDialect DIALECT = new ApposePythonDialect();
66-
6765
private Context context;
6866
private ScriptLanguage language;
6967
private ApposePythonDialect dialect;
7068

7169
@Before
7270
public void setUp() {
73-
context = new Context(ScriptService.class, LanguageServerService.class,
74-
ResidentWorkerService.class, LanguageServerService.class);
71+
context = new Context(ScriptService.class, ResidentWorkerService.class);
7572
language = context.service(ScriptService.class).getLanguageByName(
7673
"appose-python");
7774
dialect = context.service(PluginService.class).createInstancesOfType(
@@ -87,23 +84,48 @@ public void tearDown() {
8784
}
8885

8986
@Test
90-
public void testServersAreOffered() {
91-
// Its scripts are plain Python, which scijava-code-lsp-jedi serves.
87+
public void testScriptsLanguageAdherence() {
88+
// Ensure the dialect supports the Appose Python script language
9289
assertTrue(dialect.supports(language));
93-
assertEquals("python", dialect.languageId());
94-
assertTrue(context.service(LanguageServerService.class).supports(
95-
language));
9690
}
9791

9892
@Test
99-
public void testScriptAsPython() {
100-
final String code = "#@ net.imglib2.img.Img image\n#@ double sigma\n" +
101-
"image.sha";
102-
// The script's parameters are typed in a preamble, so the servers know
103-
// them.
104-
assertTrue(document(code).text().startsWith(
105-
"import appose.python_worker; import numpy; image: numpy.ndarray; " +
106-
"sigma: float; task: appose.python_worker.Task\n"));
93+
public void testParameterType() {
94+
// Script parameters have the types Appose converts them to
95+
assertEquals("numpy.ndarray", ApposePythonDialect.parameterType(
96+
Img.class));
97+
assertEquals("float", ApposePythonDialect.parameterType(double.class));
98+
assertEquals("int", ApposePythonDialect.parameterType(Long.class));
99+
assertEquals("str", ApposePythonDialect.parameterType(String.class));
100+
assertEquals("bool", ApposePythonDialect.parameterType(boolean.class));
101+
assertEquals("list", ApposePythonDialect.parameterType(int[].class));
102+
assertEquals("dict", ApposePythonDialect.parameterType(Map.class));
103+
// Types Appose cannot convert are not declared
104+
assertNull(ApposePythonDialect.parameterType(Context.class));
105+
}
106+
107+
@Test
108+
public void testHeader() {
109+
// SciJava Script parameters should be declared in Python code with the
110+
// types Appose converts them to, e.g. "image: numpy.ndarray"; and
111+
// scripts can use Appose's task variable
112+
final Map<String, Class<?>> parameters = new LinkedHashMap<>();
113+
parameters.put("image", Img.class);
114+
parameters.put("sigma", double.class);
115+
parameters.put("context", Context.class);
116+
Header header = dialect.header(parameters);
117+
assertEquals("import appose.python_worker; import numpy; " +
118+
"image: numpy.ndarray; sigma: float; " +
119+
"task: appose.python_worker.Task", header.code());
120+
// The imports' names are the header's alone
121+
assertEquals(Arrays.asList("appose", "numpy"), Arrays.asList(header
122+
.hiddenNames().toArray()));
123+
// numpy is imported only when a parameter needs it
124+
header = dialect.header(Collections.singletonMap("sigma",
125+
double.class));
126+
assertEquals("import appose.python_worker; sigma: float; " +
127+
"task: appose.python_worker.Task", header.code());
128+
assertEquals(Collections.singleton("appose"), header.hiddenNames());
107129
}
108130

109131
@Test
@@ -217,25 +239,8 @@ public void testPythonOnDisk() throws Exception {
217239
}
218240
}
219241

220-
@Test
221-
public void testPythonTypes() {
222-
assertEquals("numpy.ndarray", DIALECT.parameterType(Img.class));
223-
assertEquals("float", DIALECT.parameterType(double.class));
224-
assertEquals("int", DIALECT.parameterType(Long.class));
225-
assertEquals("str", DIALECT.parameterType(String.class));
226-
assertEquals("bool", DIALECT.parameterType(boolean.class));
227-
assertEquals("list", DIALECT.parameterType(int[].class));
228-
assertNull(DIALECT.parameterType(Context.class));
229-
}
230-
231242
// -- Helper methods --
232243

233-
/** The script as plain Python, as its servers see it. */
234-
private ScriptDocument document(final String code) {
235-
return ScriptDocument.of(code, context.service(LanguageServerService.class)
236-
.scriptParameters(code), dialect);
237-
}
238-
239244
private static void delete(final java.nio.file.Path dir) throws Exception {
240245
try (java.util.stream.Stream<java.nio.file.Path> paths =
241246
java.nio.file.Files.walk(dir))

0 commit comments

Comments
 (0)