Skip to content

Commit a308f24

Browse files
gselzerclaude
andcommitted
Provide code completion
Complete code in appose-python scripts in two layers: * tree-sitter, in process: names in scope, script parameters, task, builtins and keywords, instantly and without any Python. * Jedi, in a small tooling environment of its own (built in the background on first use), analyzing scripts against their own environment's interpreter: members, module contents, signatures and documentation. Answers come without blocking typing, as updates; documentation is fetched on demand. Completion never builds a script's environment or disturbs its worker: environments built earlier (in any session) are found read-only. Dependencies a script declares but its environment lacks are installed for completion only, into a separate overlay directory, once they stop changing. Signature help comes from Jedi. Also extract the engine's environment resolution into ScriptEnvironment, shared with completion, and add ResidentWorkerService#find, a lookup that never replaces a running worker. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 4d3cdc7 commit a308f24

18 files changed

Lines changed: 3483 additions & 107 deletions

File tree

‎README.md‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,34 @@ memory) it holds, is otherwise released when the application exits.
134134
Text printed to stdout and stderr appears in the Script Editor's output
135135
panes. Errors are reported with a Python traceback that refers to the
136136
script's own line numbers.
137+
138+
## Code completion
139+
140+
The Script Editor completes code in these scripts, in two layers:
141+
142+
* **Instantly, with no Python needed:** names in scope (variables,
143+
functions, classes, imports, parameters of the enclosing function), the
144+
script's `#@` parameters, `task`, builtins and keywords. This comes from
145+
parsing the script with [tree-sitter](https://tree-sitter.github.io/).
146+
* **With [Jedi](https://jedi.readthedocs.io/):** members of objects and
147+
modules, function signatures (with parameter names and types) and
148+
documentation. Jedi runs in a small environment of its own
149+
(`appose-python-completion`: Python, Jedi and Appose), which is built in
150+
the background the first time you complete code, and takes a while only
151+
then.
152+
153+
Once a script's environment is built, by running the script (in this session
154+
or an earlier one), Jedi analyzes the script against it, so that the packages
155+
installed there are known too. Packages the script declares (in its
156+
`# /// script` block) but its environment lacks, e.g. a dependency you just
157+
added, are installed for completion only, into a separate directory, a moment
158+
after you stop editing the dependencies; so they are known before you run the
159+
script. This covers PyPI packages; conda packages (`[tool.pixi.dependencies]`)
160+
and environments declared in files are known once the script has run.
161+
Completion never builds a script's environment itself, and never disturbs
162+
its worker. Script parameters are known by the Python types they have at run
163+
time, e.g. images as NumPy arrays.
164+
165+
Typing never waits for Jedi: if its answer for a word is not ready within a
166+
moment (the first answer for a package can take a few seconds), the instant
167+
completions show, and Jedi's are used once ready.

‎pom.xml‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@
9494

9595
<!-- TEMP: for Task#log(); drop once propagated to pom-scijava. -->
9696
<scijava-common.version>2.101.0</scijava-common.version>
97+
98+
<scijava-code-api.version>0.1.0-SNAPSHOT</scijava-code-api.version>
99+
<tree-sitter.version>0.26.6</tree-sitter.version>
100+
<tree-sitter-python.version>0.25.0</tree-sitter-python.version>
97101
</properties>
98102

99103
<dependencies>
@@ -106,8 +110,24 @@
106110
<groupId>org.scijava</groupId>
107111
<artifactId>script-editor</artifactId>
108112
</dependency>
113+
<dependency>
114+
<groupId>org.scijava</groupId>
115+
<artifactId>scijava-code-api</artifactId>
116+
<version>${scijava-code-api.version}</version>
117+
</dependency>
109118

110119
<!-- Third-party dependencies -->
120+
<dependency>
121+
<!-- NB: Parses Python for code completion. -->
122+
<groupId>io.github.bonede</groupId>
123+
<artifactId>tree-sitter</artifactId>
124+
<version>${tree-sitter.version}</version>
125+
</dependency>
126+
<dependency>
127+
<groupId>io.github.bonede</groupId>
128+
<artifactId>tree-sitter-python</artifactId>
129+
<version>${tree-sitter-python.version}</version>
130+
</dependency>
111131
<dependency>
112132
<groupId>com.fasterxml.jackson.core</groupId>
113133
<artifactId>jackson-databind</artifactId>

0 commit comments

Comments
 (0)