Skip to content

Commit fe87b35

Browse files
committed
Merge branch 'scripting-cleanup'
This provides miscellaneous fixes to the SciJava script framework. In particular, it fixes a big problem with the implicit result output.
2 parents 6fa4c8e + 5551858 commit fe87b35

File tree

9 files changed

+153
-155
lines changed

9 files changed

+153
-155
lines changed

src/main/java/org/scijava/script/AbstractAutoCompleter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
* %%
99
* Redistribution and use in source and binary forms, with or without
1010
* modification, are permitted provided that the following conditions are met:
11-
*
11+
*
1212
* 1. Redistributions of source code must retain the above copyright notice,
1313
* this list of conditions and the following disclaimer.
1414
* 2. Redistributions in binary form must reproduce the above copyright notice,
1515
* this list of conditions and the following disclaimer in the documentation
1616
* and/or other materials provided with the distribution.
17-
*
17+
*
1818
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1919
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2020
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE

src/main/java/org/scijava/script/AutoCompletionResult.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,32 @@
1-
/*
2-
* To change this license header, choose License Headers in Project Properties.
3-
* To change this template file, choose Tools | Templates
4-
* and open the template in the editor.
1+
/*-
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2017 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
530
*/
631

732
package org.scijava.script;

src/main/java/org/scijava/script/CodeGenerator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
* Code Generator Interface
3636
*
3737
* @author Grant Harris
38+
* @deprecated To be removed in SciJava Common 3.0.0.
3839
*/
40+
@Deprecated
3941
public interface CodeGenerator {
4042

4143
/** Adds delimiter character between arguments (typically a ','). */

src/main/java/org/scijava/script/CodeGeneratorJava.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
* {@link CodeGenerator} for Java.
3636
*
3737
* @author Grant Harris
38+
* @deprecated To be removed in SciJava Common 3.0.0.
3839
*/
40+
@Deprecated
3941
public class CodeGeneratorJava implements CodeGenerator {
4042

4143
static final String lsep = System.getProperty("line.separator");

src/main/java/org/scijava/script/DefaultScriptInterpreter.java

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.scijava.log.LogService;
4343
import org.scijava.plugin.Parameter;
4444
import org.scijava.prefs.PrefService;
45+
import org.scijava.util.LastRecentlyUsed;
4546

4647
/**
4748
* The default implementation of a {@link ScriptInterpreter}.
@@ -363,4 +364,113 @@ private static <T> T callMethod(final Object object, final String methodName,
363364
return null;
364365
}
365366

367+
// -- Helper classes --
368+
369+
/** Container for a script language's interpreter history. */
370+
private static class History {
371+
372+
@SuppressWarnings("unused")
373+
protected static final long serialVersionUID = 2L;
374+
375+
private static final String PREFIX = "History.";
376+
private final int MAX_ENTRIES = 1000;
377+
378+
private final PrefService prefs;
379+
private final String name;
380+
private final LastRecentlyUsed<String> entries =
381+
new LastRecentlyUsed<>(MAX_ENTRIES);
382+
private String currentCommand = "";
383+
private int position = -1;
384+
385+
/**
386+
* Constructs a history object for a given scripting language.
387+
*
388+
* @param name the name of the scripting language
389+
*/
390+
public History(final PrefService prefs, final String name) {
391+
this.prefs = prefs;
392+
this.name = name;
393+
}
394+
395+
/**
396+
* Read back a persisted history.
397+
*/
398+
public void read() {
399+
entries.clear();
400+
for (final String item : prefs.getIterable(getClass(), PREFIX + name)) {
401+
entries.addToEnd(item);
402+
}
403+
}
404+
405+
/**
406+
* Persist the history.
407+
*
408+
* @see PrefService
409+
*/
410+
public void write() {
411+
prefs.putIterable(getClass(), entries, PREFIX + name);
412+
}
413+
414+
/**
415+
* Adds the most recently issued command.
416+
*
417+
* @param command the most recent command to add to the history
418+
*/
419+
public void add(final String command) {
420+
entries.add(command);
421+
position = -1;
422+
currentCommand = "";
423+
}
424+
425+
public boolean replace(final String command) {
426+
if (position < 0) {
427+
currentCommand = command;
428+
return false;
429+
}
430+
return entries.replace(position, command);
431+
}
432+
433+
/**
434+
* Navigates to the next (more recent) command.
435+
* <p>
436+
* This method wraps around, i.e. it returns {@code null} when there is no
437+
* more-recent command in the history.
438+
* </p>
439+
*
440+
* @return the next command
441+
*/
442+
public String next() {
443+
position = entries.next(position);
444+
return position < 0 ? currentCommand : entries.get(position);
445+
}
446+
447+
/**
448+
* Navigates to the previous (i.e less recent) command.
449+
* <p>
450+
* This method wraps around, i.e. it returns {@code null} when there is no
451+
* less-recent command in the history.
452+
* </p>
453+
*
454+
* @return the previous command
455+
*/
456+
public String previous() {
457+
position = entries.previous(position);
458+
return position < 0 ? currentCommand : entries.get(position);
459+
}
460+
461+
@Override
462+
public String toString() {
463+
final StringBuilder builder = new StringBuilder();
464+
int pos = -1;
465+
for (;;) {
466+
pos = entries.previous(pos);
467+
if (pos < 0) break;
468+
if (builder.length() > 0) builder.append(" -> ");
469+
if (this.position == pos) builder.append("[");
470+
builder.append(entries.get(pos));
471+
if (this.position == pos) builder.append("]");
472+
}
473+
return builder.toString();
474+
}
475+
}
366476
}

src/main/java/org/scijava/script/History.java

Lines changed: 0 additions & 145 deletions
This file was deleted.

src/main/java/org/scijava/script/InvocationObject.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
* the parameters that were passed to it.
3939
*
4040
* @author Grant Harris
41+
* @deprecated To be removed in SciJava Common 3.0.0.
4142
*/
43+
@Deprecated
4244
public class InvocationObject {
4345

4446
public String moduleCalled;

src/main/java/org/scijava/script/ParameterObject.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
* Holds a parameter, its type and value, for a recorded macro.
3636
*
3737
* @author Grant Harris
38+
* @deprecated To be removed in SciJava Common 3.0.0.
3839
*/
40+
@Deprecated
3941
public class ParameterObject {
4042

4143
public ParameterObject(final String param, final Class<?> type,

src/main/java/org/scijava/script/ScriptInfo.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ private void parseParam(final String param,
432432
varName = tokens[1];
433433
}
434434
final Class<?> type = scriptService.lookupClass(typeName);
435-
addItem(varName, type, attrs);
435+
addItem(varName, type, attrs, true);
436436

437437
if (ScriptModule.RETURN_VALUE.equals(varName)) {
438438
// NB: The return value variable is declared as an explicit OUTPUT.
@@ -460,11 +460,11 @@ private void checkValid(final boolean valid, final String param)
460460
private void addReturnValue() {
461461
final HashMap<String, Object> attrs = new HashMap<>();
462462
attrs.put("type", "OUTPUT");
463-
addItem(ScriptModule.RETURN_VALUE, Object.class, attrs);
463+
addItem(ScriptModule.RETURN_VALUE, Object.class, attrs, false);
464464
}
465465

466466
private <T> void addItem(final String name, final Class<T> type,
467-
final Map<String, Object> attrs)
467+
final Map<String, Object> attrs, final boolean explicit)
468468
{
469469
final DefaultMutableModuleItem<T> item =
470470
new DefaultMutableModuleItem<>(this, name, type);
@@ -477,7 +477,7 @@ private <T> void addItem(final String name, final Class<T> type,
477477
registerOutput(item);
478478
// NB: Only append the return value as an extra
479479
// output when no explicit outputs are declared.
480-
appendReturnValue = false;
480+
if (explicit) appendReturnValue = false;
481481
}
482482
}
483483

0 commit comments

Comments
 (0)