Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
00de65d
Provide code completion
gselzer Oct 6, 2026
fadd95c
Build Jedi's source from a ScriptDocument
gselzer Oct 6, 2026
ca96614
Complete via jedi-language-server
gselzer Oct 6, 2026
047532e
Offer hover and diagnostics from jedi-language-server
gselzer Oct 6, 2026
68fc9e9
Offer jedi-language-server as the Python language server
gselzer Oct 6, 2026
8633b64
Fix image parameters, and orphaned servers
gselzer Oct 6, 2026
eb58a17
Serve appose-python as language servers
gselzer Oct 7, 2026
f2de3ca
Move off scijava-code-api, now removed
gselzer Oct 7, 2026
7e8b2c1
Tell language servers when script environments change
gselzer Oct 7, 2026
a267549
Take over the Python completion front from code-lsp
gselzer Oct 8, 2026
6471f99
Follow code-lsp's packages by purpose
gselzer Oct 8, 2026
481b599
Set script files as suppliers in tests
gselzer Oct 8, 2026
dcd5ebd
Take completions as one answer in tests
gselzer Oct 8, 2026
d011a25
Combine jedi and the syntax-only server in one server
gselzer Oct 8, 2026
a8e04a5
Unify Environment's search paths and classpath
gselzer Oct 9, 2026
069b1eb
Serve plain Python: sessions declare the parameters
gselzer Oct 9, 2026
dceb23b
Name what the declarations import, so it is not completed
gselzer Oct 9, 2026
cbf8f11
Follow ScriptDialect and ScriptDocument into code-lsp's core package
gselzer Oct 9, 2026
e127481
Offer jedi-language-server as a ServerProcessPlugin
gselzer Oct 12, 2026
479ee7c
Gather the completion code into the completion package
gselzer Oct 12, 2026
cdf930b
Fold PythonSyntax into PythonSyntaxLanguageServer
gselzer Oct 12, 2026
e7b97d1
Fold DelegatingLanguageServer into PythonCompletionServer
gselzer Oct 12, 2026
cd57d60
Complete Appose Python with jedi alone
gselzer Oct 12, 2026
977ec1d
Choose each script's environment as it is analyzed
gselzer Oct 13, 2026
d71733f
Get jedi from code-lsp-jedi; route scripts between its servers
gselzer Oct 15, 2026
a107764
Serve the script language by its name, as code-lsp now asks
gselzer Oct 16, 2026
3666872
Offer only an Appose-Python dialect: completion moves to code-lsp-jedi
gselzer Oct 16, 2026
7b21a28
Test only Appose specifics: plain Python's tests move to code-lsp-jedi
gselzer Oct 16, 2026
92e0fce
Follow scijava-code-lsp's rename of runtimeType to parameterType
gselzer Oct 16, 2026
164e97e
Follow scijava-code-lsp's ScriptDialect.header
gselzer Oct 16, 2026
62f3cfa
Need scijava-code-lsp-jedi only to test
gselzer Oct 16, 2026
0c13297
Follow scijava-code-lsp: an Optional file, and a non-null live state
gselzer Oct 19, 2026
6b18ac0
Analyze scripts with the packages they declare, as the dialect says
gselzer Oct 20, 2026
e81fd83
Prebuild script environments for completion, not overlays
gselzer Oct 20, 2026
b618794
Follow scijava-code-lsp's sessions and environments
gselzer Oct 21, 2026
5f83f4f
Follow scijava-code-lsp's ScriptDialect.header Variables
gselzer Oct 23, 2026
da245ee
POM: say when scijava-common 2.101.0 needn't be pinned
gselzer Oct 23, 2026
ee7c337
Test completion with ty, rather than jedi-language-server
gselzer Nov 11, 2026
71e1a3b
Assign the parameters the completion header declares
gselzer Nov 11, 2026
18a4b12
README: describe environments prebuilt for completion
gselzer Nov 11, 2026
c955a81
Implement ScriptDialect.outputLanguage, formerly languageId
gselzer Nov 12, 2026
906844f
Take ScriptDialect.environment's file as nullable, not Optional
gselzer Nov 12, 2026
ef2c1cc
Import header modules under private aliases
gselzer Nov 13, 2026
ad90fe1
Follow scijava-code-lsp's rename to scijava-lsp
gselzer Nov 17, 2026
19473da
Modularize project
gselzer Nov 23, 2026
4f7d681
Simplify README
gselzer Nov 25, 2026
cd0f0f5
Un-export completion package
gselzer Nov 25, 2026
c8b2713
Un-export base package as well
gselzer Nov 25, 2026
eb5c05b
Add disclaimer to ApposePythonDialect.parameterType
gselzer Nov 25, 2026
38c303e
Require ResidentWorkerService for completion
gselzer Nov 25, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,11 @@ memory) it holds, is otherwise released when the application exits.
Text printed to stdout and stderr appears in the Script Editor's output
panes. Errors are reported with a Python traceback that refers to the
script's own line numbers.

## Code completion

The Script Editor completes code in appose-python scripts using a Python language
server (see [`scijava-lsp-ty`](https://github.com/scijava/scijava/tree/main/scijava-lsp-ty)).
The `ApposePythonDialect` plugin is used to:
- convert SciJava scripts into text that language server can understand
- derive `org.scijava.lsp.Environment`s from information contained within the script (including environment filepaths and inline script metadata).
65 changes: 58 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>

<artifactId>scripting-appose-python</artifactId>
<version>0.1.1-SNAPSHOT</version>
<version>0.2.0-SNAPSHOT</version>

<name>SciJava Scripting: Appose Python</name>
<description>Python scripting language plugin backed by Appose.</description>
Expand Down Expand Up @@ -45,11 +45,9 @@
</developers>
<contributors>
<contributor>
<!--
NB: Need at least one element to override the parent.
See: https://issues.apache.org/jira/browse/MNG-5220
-->
<name>None</name>
<name>Gabriel Selzer</name>
<url>https://imagej.net/people/gselzer</url>
<properties><id>gselzer</id></properties>
</contributor>
</contributors>

Expand Down Expand Up @@ -82,6 +80,8 @@
<package-name>org.scijava.plugins.scripting.appose.python</package-name>
<main-class>org.scijava.plugins.scripting.appose.python.Main</main-class>

<surefire.useModulePath>false</surefire.useModulePath>

<license.licenseName>bsd_2</license.licenseName>
<license.copyrightOwners>SciJava developers.</license.copyrightOwners>
<license.excludes>**/script_templates/**</license.excludes>
Expand All @@ -92,8 +92,12 @@
<appose.version>0.12.0</appose.version>
<imglib2-appose.version>0.9.0</imglib2-appose.version>

<!-- TEMP: for Task#log(); drop once propagated to pom-scijava. -->
<!-- TEMP: Remove once pom-scijava manages scijava-common 2.101.0.
NB: Task#log() needs it. -->
<scijava-common.version>2.101.0</scijava-common.version>

<scijava-lsp.version>0.1.0</scijava-lsp.version>
<scijava-lsp-ty.version>0.1.0</scijava-lsp-ty.version>
</properties>

<dependencies>
Expand All @@ -106,6 +110,18 @@
<groupId>org.scijava</groupId>
<artifactId>script-editor</artifactId>
</dependency>
<dependency>
<groupId>org.scijava</groupId>
<artifactId>scijava-lsp</artifactId>
<version>${scijava-lsp.version}</version>
</dependency>
<dependency>
<groupId>org.scijava</groupId>
<artifactId>scijava-lsp-ty</artifactId>
<version>${scijava-lsp-ty.version}</version>
<!-- NB: For integration tests; applications choose their servers. -->
<scope>test</scope>
</dependency>

<!-- Third-party dependencies -->
<dependency>
Expand Down Expand Up @@ -167,4 +183,39 @@
<url>https://maven.scijava.org/content/groups/public</url>
</repository>
</repositories>

<build>
<plugins>
<!--
NB: Tests run on the classpath since we use scijava-common's tests JAR.
That JAR shares the org.scijava module name with scijava-common itself, so
it cannot be resolved on the module path.
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!--
NB: In module mode, javac does not discover annotation processors
on the module path, so the @Plugin indexer must be declared here.
-->
<annotationProcessorPaths>
<path>
<groupId>org.scijava</groupId>
<artifactId>scijava-common</artifactId>
<version>${scijava-common.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
<executions>
<execution>
<id>default-testCompile</id>
<configuration>
<useModulePath>false</useModulePath>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
47 changes: 47 additions & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* #%L
* Python scripting language plugin backed by Appose.
* %%
* Copyright (C) 2026 SciJava developers.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/

module scripting.appose.python {

opens org.scijava.plugins.scripting.appose.python to org.scijava;
opens org.scijava.plugins.scripting.appose.python.completion to org.scijava;

requires com.fasterxml.jackson.core;
requires com.fasterxml.jackson.databind;
requires com.fasterxml.jackson.dataformat.toml;
requires java.scripting;
requires net.imagej;
requires net.imglib2;
requires net.imglib2.appose;
requires org.apposed.appose;
requires org.fife.RSyntaxTextArea;
requires org.scijava;
requires org.scijava.lsp;
requires org.scijava.ui.swing.script;
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import java.io.UncheckedIOException;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
Expand All @@ -58,7 +57,7 @@

import org.apposed.appose.Appose;
import org.apposed.appose.BuildException;
import org.apposed.appose.Builder;
import org.apposed.appose.Environment;
import org.apposed.appose.NDArray;
import org.apposed.appose.Service;
import org.apposed.appose.Service.TaskStatus;
Expand All @@ -70,7 +69,6 @@
import org.scijava.log.Logger;
import org.scijava.module.ModuleItem;
import org.scijava.plugin.Parameter;
import org.scijava.plugins.scripting.appose.python._internal.InlineMetadata;
import org.scijava.plugins.scripting.appose.python._internal.ResidentWorker;
import org.scijava.plugins.scripting.appose.python._internal.ResidentWorkerService;
import org.scijava.plugins.scripting.appose.python._internal.SciJavaTasks;
Expand Down Expand Up @@ -444,120 +442,32 @@ static boolean ownsNDArray(final Object value, final NDArray nd) {
}

/**
* Gets the resident worker for the script's Appose environment, which the
* script declares either inline, via a PEP 723 {@code # /// script}
* metadata block, or in a file named by the {@code env} (and optionally
* {@code scheme}, {@code activate}) attributes of its {@code #@script}
* directive. The environment itself is built lazily, by the worker's first
* task.
* Gets the resident worker for the script's Appose environment (see
* {@link ScriptEnvironment}). The environment itself is built lazily, by
* the worker's first task.
*/
private ResidentWorker worker(final ScriptInfo info, final String script)
throws ScriptException
{
if (info == null) throw noEnvironment();
final String envRef = info.get("env");
final String activate = info.get("activate");
final String metadata;
final ScriptEnvironment env;
try {
metadata = InlineMetadata.extract(script);
env = ScriptEnvironment.of(script, info.getPath(), info.get("env"), info
.get("scheme"), info.get("activate"));
}
catch (final IllegalArgumentException e) {
catch (final IllegalArgumentException | IOException e) {
throw scriptException(e.getMessage(), e);
}
if (metadata != null) {
if (envRef != null) {
throw new ScriptException("The script declares its environment " +
"twice: inline, and via #@script(env=\"" + envRef + "\"). " +
"Remove one of them.");
}
if (activate != null) {
throw new ScriptException("#@script(activate=\"" + activate +
"\") requires an environment file, via env=; it cannot be " +
"combined with an inline environment.");
}
return inlineWorker(metadata, info.getPath());
}
if (envRef == null) throw noEnvironment();

final File envFile = resolveEnvFile(envRef, info.getPath());
if (!envFile.isFile()) {
throw new ScriptException("Appose environment file not found: " +
envFile.getAbsolutePath());
}
final String content;
try {
content = new String(Files.readAllBytes(envFile.toPath()),
StandardCharsets.UTF_8);
}
catch (final IOException e) {
throw scriptException("Cannot read Appose environment file: " +
envFile.getAbsolutePath(), e);
}
return worker(envName(envFile), activate, content, info.get("scheme"));
}

/** Gets the resident worker for an environment declared inline. */
private ResidentWorker inlineWorker(final String metadata,
final String scriptPath) throws ScriptException
{
// Note: Name the environment after the script, as for environment
// files, so that editing the metadata updates the environment and
// replaces its worker, rather than leaving the old one running.
// Unsaved scripts, e.g. new from a template, have no path to go by.
final String envName = scriptPath == null ? "script-" + String.format(
"%08x", metadata.hashCode()) : envName(new File(scriptPath));
final String content;
try {
content = InlineMetadata.toPyProject(metadata, envName);
}
catch (final IllegalArgumentException e) {
throw scriptException(e.getMessage(), e);
}
return worker(envName, null, content, InlineMetadata.SCHEME);
if (env == null) throw noEnvironment();
return env.worker(workerService);
}

/**
* Gets the resident worker for the named environment, built from the given
* configuration.
*/
private ResidentWorker worker(final String envName, final String activate,
final String content, final String scheme)
{
return workerService.worker(envName, activate, scheme + "\n" + content,
() -> {
final Builder<?> builder = Appose.content(content);
return scheme == null ? builder : builder.scheme(scheme);
},
// Note: On Windows, importing numpy from a task hangs unless numpy
// was imported during worker initialization.
env -> env.python().init(
"try:\n import numpy\nexcept ImportError:\n pass\n"));
}

/** Resolves an (optionally relative) env file path against the script path. */
static File resolveEnvFile(final String envRef, final String scriptPath) {
final File envFile = new File(envRef);
if (envFile.isAbsolute()) return envFile;
if (scriptPath != null) {
final File parent = new File(scriptPath).getAbsoluteFile().getParentFile();
if (parent != null) return new File(parent, envRef);
}
return envFile;
}

/**
* Derives a valid Appose environment name from an environment file: its
* base name, plus a hash of its absolute path to avoid collisions between
* like-named files in different directories. E.g.:
* {@code /path/to/cellcast.toml} becomes {@code cellcast-1a2b3c4d}.
*/
static String envName(final File envFile) {
final String fileName = envFile.getName();
final int dot = fileName.lastIndexOf('.');
final String baseName = dot > 0 ? fileName.substring(0, dot) : fileName;
final String path = envFile.getAbsoluteFile().toPath().normalize().toString();
return baseName.replaceAll("[^a-zA-Z0-9_-]", "_") + "-" +
String.format("%08x", path.hashCode());
/** Creates the service of a worker in the given (built) environment. */
static Service launch(final Environment env) {
// Note: On Windows, importing numpy from a task hangs unless numpy
// was imported during worker initialization.
return env.python().init(
"try:\n import numpy\nexcept ImportError:\n pass\n");
}

private static ScriptException noEnvironment() {
Expand Down
Loading
Loading