Skip to content

Commit bcebd59

Browse files
gselzerclaude
andcommitted
code-lsp: take ScriptDialect.environment's file as nullable, not Optional
Optional is for results, not parameters: the file is now null for an unsaved script. Also reword environment's javadoc. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent acec7de commit bcebd59

4 files changed

Lines changed: 14 additions & 22 deletions

File tree

‎scijava-code-lsp/src/main/java/org/scijava/code/lsp/ScriptDialect.java‎

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,16 @@ public interface ScriptDialect extends SciJavaPlugin {
7070
String outputLanguage();
7171

7272
/**
73-
* Gets where a script runs, as far as its text says (e.g. an Appose
74-
* environment it declares), to analyze it there. Asked before each request
75-
* about the script, as that may change; its session then moves to a server
76-
* of where it runs now (see {@link ScriptSession#update}). The default
77-
* knows nothing: a blank {@link Environment}.
73+
* Describes any {@link Environment} constraints described within the
74+
* script code.
7875
*
79-
* @param text The script, as written (e.g. with its {@code #@script} line,
80-
* which its servers do not see).
81-
* @param file The script's file, if it has one (not if e.g. unsaved).
76+
* @param text SciJava script text.
77+
* @param file The location where {@code text} is saved, or null if it is
78+
* not saved.
79+
* @return An {@link Environment} describing any environmental knowledge
80+
* embedded within the script text.
8281
*/
83-
// TODO: Consider whether this is necessary: so far only Appose-Python
84-
// scripts say where they run, and dialects otherwise only translate.
85-
default Environment environment(final String text,
86-
final Optional<File> file)
87-
{
82+
default Environment environment(final String text, final File file) {
8883
return new Environment();
8984
}
9085

‎scijava-code-lsp/src/main/java/org/scijava/code/lsp/ScriptDocument.java‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import java.util.Collections;
3434
import java.util.LinkedHashSet;
3535
import java.util.Map;
36-
import java.util.Optional;
3736
import java.util.Set;
3837
import java.util.function.BiFunction;
3938
import java.util.regex.Pattern;
@@ -66,14 +65,14 @@ final class ScriptDocument {
6665
private final String text;
6766
private final String language;
6867
/** Where the script runs, as its dialect says, given its file. */
69-
private final BiFunction<String, Optional<File>, Environment> where;
68+
private final BiFunction<String, File, Environment> where;
7069
/** Number of characters prepended. */
7170
private final int headerLength;
7271
private final Set<String> hiddenNames;
7372

7473
private ScriptDocument(final String original, final String text,
7574
final String language,
76-
final BiFunction<String, Optional<File>, Environment> where,
75+
final BiFunction<String, File, Environment> where,
7776
final int headerLength, final Set<String> hiddenNames)
7877
{
7978
this.original = original;
@@ -145,9 +144,9 @@ String language() {
145144
* {@link ScriptDialect#environment}); or a blank {@link Environment}, for a
146145
* script as it is.
147146
*
148-
* @param file The script's file, if it has one.
147+
* @param file The script's file, or null if it has none.
149148
*/
150-
Environment environment(final Optional<File> file) {
149+
Environment environment(final File file) {
151150
return where.apply(original, file);
152151
}
153152

‎scijava-code-lsp/src/main/java/org/scijava/code/lsp/ScriptSession.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ private static boolean isAccessible(final Class<?> c) {
466466
* of the open document).
467467
*/
468468
private boolean follow() {
469-
final Environment env = doc.environment(Optional.ofNullable(file.get()));
469+
final Environment env = doc.environment(file.get());
470470
if (closed || environment.equals(Optional.of(env))) return false;
471471
environment = Optional.of(env);
472472
generation++;

‎scijava-code-lsp/src/test/java/org/scijava/code/lsp/LanguageServersTest.java‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -556,9 +556,7 @@ public String outputLanguage() {
556556
}
557557

558558
@Override
559-
public Environment environment(final String text,
560-
final Optional<File> file)
561-
{
559+
public Environment environment(final String text, final File file) {
562560
for (final String line : text.split("\n")) {
563561
if (line.startsWith("#@script(env=")) {
564562
final String env = line.substring(13, line.length() - 1);

0 commit comments

Comments
 (0)