2929
3030package org .scijava .code .lsp ;
3131
32- import java .io .Reader ;
33- import java .io .StringReader ;
34- import java .io .Writer ;
35- import java .util .ArrayList ;
36- import java .util .Arrays ;
3732import java .util .Collections ;
3833import java .util .List ;
3934import java .util .Objects ;
35+ import java .util .Optional ;
4036
41- import javax .script .Bindings ;
4237import javax .script .ScriptContext ;
43- import javax .script .SimpleBindings ;
4438
4539
4640/**
5549 */
5650public final class Environment {
5751
52+ /** Path of the interpreter or executable running the script, if known. */
53+ private final Optional <String > interpreter ;
5854 /**
59- * The state of no interpreter, for code not typed into one: no variables,
60- * and none can be set.
55+ * Extra places imports are resolved from, in order: e.g. directories added
56+ * to Python's {@code sys.path}, or a JVM classpath. Each server reads them as
57+ * its language does. Never null.
6158 */
62- public static final ScriptContext NO_STATE = new NoState ();
63-
64- /** No particular environment. */
65- public static final Environment NONE = new Environment (null , null );
66-
67- private final String interpreter ;
6859 private final List <String > paths ;
69- private final ScriptContext live ;
60+ private final Optional < ScriptContext > live ;
7061
7162 /**
72- * An environment for code not typed into an interpreter (see
73- * {@link #NO_STATE}).
74- *
75- * @param interpreter Path of the interpreter or executable parsing the code.
76- * @param paths Extra places imports are resolved from, in order. Language-dependent.
63+ * No particular environment: no interpreter, no extra paths, and no live
64+ * interpreter. Others are made from it, e.g.
65+ * {@code new Environment().withInterpreter(python)}.
7766 */
78- public Environment (final String interpreter , final List < String > paths ) {
79- this (interpreter , paths , NO_STATE );
67+ public Environment () {
68+ this (Optional . empty (), Collections . emptyList (), Optional . empty () );
8069 }
8170
8271 /**
83- * @param interpreter Path of the interpreter or executable parsing the code.
84- * @param paths Extra places imports are resolved from, in order. Language-dependent.
85- * @param live The state of the interpreter the code is typed into (e.g. a
86- * REPL's), or {@link #NO_STATE}.
72+ * Since different environments may or may not have any of these,
73+ * a builder pattern is nice here.
8774 */
88- public Environment (final String interpreter , final List <String > paths ,
89- final ScriptContext live )
75+ private Environment (final Optional <String > interpreter ,
76+ final List < String > paths , final Optional < ScriptContext > live )
9077 {
9178 this .interpreter = interpreter ;
92- this .paths = paths == null ? Collections .emptyList () : Collections
93- .unmodifiableList (new ArrayList <>(paths ));
94- this .live = Objects .requireNonNull (live , "live" );
79+ this .paths = paths ;
80+ this .live = live ;
9581 }
9682
97- /** Path of the interpreter or executable running the script, or null . */
98- public String interpreter () {
83+ /** Path of the interpreter or executable running the script, if known . */
84+ public Optional < String > interpreter () {
9985 return interpreter ;
10086 }
10187
@@ -108,133 +94,55 @@ public List<String> paths() {
10894 return paths ;
10995 }
11096
97+ /** The state of the interpreter the code is typed into, if any. */
98+ public Optional <ScriptContext > live () {
99+ return live ;
100+ }
101+
111102 /**
112- * The state of the interpreter the code is typed into; or
113- * {@link #NO_STATE}, if none.
103+ * A copy of this environment, whose code is parsed by the given interpreter
104+ * or executable.
105+ *
106+ * @param path Path of the interpreter or executable.
114107 */
115- public ScriptContext live () {
116- return live ;
108+ public Environment withInterpreter (final String path ) {
109+ return new Environment (Optional .of (path ), paths , live );
110+ }
111+
112+ /**
113+ * A copy of this environment, whose imports are also resolved from the
114+ * given places, in order. Language-dependent.
115+ */
116+ public Environment withPaths (final List <String > places ) {
117+ return new Environment (interpreter , List .copyOf (places ), live );
117118 }
118119
119- /** A copy of this environment, for code typed into the given interpreter. */
120+ /**
121+ * A copy of this environment, for code typed into the given interpreter
122+ * (e.g. a REPL's).
123+ */
120124 public Environment withLive (final ScriptContext context ) {
121- return context == live ? this : new Environment (interpreter , paths ,
122- context );
125+ return new Environment (interpreter , paths , Optional .of (context ));
123126 }
124127
125128 @ Override
126129 public boolean equals (final Object o ) {
127130 if (!(o instanceof Environment )) return false ;
128131 final Environment that = (Environment ) o ;
129132 // NB: Live interpreters are the same only if they are the same one.
130- return Objects .equals (interpreter , that .interpreter ) && paths .equals (
131- that . paths ) && live == that .live ;
133+ return interpreter .equals (that .interpreter ) && paths .equals (that . paths ) &&
134+ live . orElse ( null ) == that .live . orElse ( null ) ;
132135 }
133136
134137 @ Override
135138 public int hashCode () {
136- return Objects .hash (interpreter , paths , System .identityHashCode (live ));
139+ return Objects .hash (interpreter , paths , System .identityHashCode (live
140+ .orElse (null )));
137141 }
138142
139143 @ Override
140144 public String toString () {
141- return "Environment[interpreter=" + interpreter + ", paths=" + paths +
142- (live == NO_STATE ? "" : ", live" ) + "]" ;
143- }
144-
145- /** The state of no interpreter: see {@link #NO_STATE}. */
146- private static final class NoState implements ScriptContext {
147-
148- private static final Bindings NONE = new SimpleBindings (Collections
149- .emptyMap ());
150-
151- private static final Writer NO_OUTPUT = new Writer () {
152-
153- @ Override
154- public void write (final char [] buf , final int off , final int len ) {}
155-
156- @ Override
157- public void flush () {}
158-
159- @ Override
160- public void close () {}
161- };
162-
163- @ Override
164- public void setBindings (final Bindings bindings , final int scope ) {
165- throw unmodifiable ();
166- }
167-
168- @ Override
169- public Bindings getBindings (final int scope ) {
170- return NONE ;
171- }
172-
173- @ Override
174- public void setAttribute (final String name , final Object value ,
175- final int scope )
176- {
177- throw unmodifiable ();
178- }
179-
180- @ Override
181- public Object getAttribute (final String name , final int scope ) {
182- return null ;
183- }
184-
185- @ Override
186- public Object removeAttribute (final String name , final int scope ) {
187- return null ;
188- }
189-
190- @ Override
191- public Object getAttribute (final String name ) {
192- return null ;
193- }
194-
195- @ Override
196- public int getAttributesScope (final String name ) {
197- return -1 ;
198- }
199-
200- @ Override
201- public Writer getWriter () {
202- return NO_OUTPUT ;
203- }
204-
205- @ Override
206- public Writer getErrorWriter () {
207- return NO_OUTPUT ;
208- }
209-
210- @ Override
211- public void setWriter (final Writer writer ) {
212- throw unmodifiable ();
213- }
214-
215- @ Override
216- public void setErrorWriter (final Writer writer ) {
217- throw unmodifiable ();
218- }
219-
220- @ Override
221- public Reader getReader () {
222- return new StringReader ("" );
223- }
224-
225- @ Override
226- public void setReader (final Reader reader ) {
227- throw unmodifiable ();
228- }
229-
230- @ Override
231- public List <Integer > getScopes () {
232- return Arrays .asList (ENGINE_SCOPE , GLOBAL_SCOPE );
233- }
234-
235- private static UnsupportedOperationException unmodifiable () {
236- return new UnsupportedOperationException (
237- "The state of no interpreter cannot be changed" );
238- }
145+ return "Environment[interpreter=" + interpreter .orElse ("none" ) +
146+ ", paths=" + paths + (live .isPresent () ? ", live" : "" ) + "]" ;
239147 }
240148}
0 commit comments