Skip to content

Commit 1e3ba36

Browse files
committed
Add autcompletion framework
1 parent f7457f0 commit 1e3ba36

File tree

9 files changed

+192
-0
lines changed

9 files changed

+192
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
import org.scijava.plugin.AbstractRichPlugin;
3737
import org.scijava.plugin.PluginInfo;
38+
import org.scijava.script.autocompletion.AutoCompleter;
39+
import org.scijava.script.autocompletion.DefaultAutoCompleter;
3840

3941
/**
4042
* Abstract superclass for {@link ScriptLanguage} implementations.
@@ -70,6 +72,11 @@ public String getLanguageName() {
7072
if (info != null) name = info.getName();
7173
return name != null && !name.isEmpty() ? name : inferNameFromClassName();
7274
}
75+
76+
@Override
77+
public AutoCompleter getAutoCompleter() {
78+
return new DefaultAutoCompleter(getScriptEngine());
79+
}
7380

7481
// -- Helper methods --
7582

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
package org.scijava.script;
3333

34+
import org.scijava.script.autocompletion.DefaultAutoCompleter;
35+
import org.scijava.script.autocompletion.AutoCompleter;
3436
import java.util.List;
3537

3638
import javax.script.ScriptEngine;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
package org.scijava.script;
3333

34+
import org.scijava.script.autocompletion.AutoCompleter;
3435
import java.util.Collections;
3536
import java.util.List;
3637

@@ -150,5 +151,7 @@ else if (key.equals(ScriptEngine.LANGUAGE_VERSION)) {
150151
default String getEngineVersion() {
151152
return "0.0";
152153
}
154+
155+
public AutoCompleter getAutoCompleter();
153156

154157
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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%
30+
*/
31+
package org.scijava.script.autocompletion;
32+
33+
import java.util.ArrayList;
34+
import java.util.HashMap;
35+
import java.util.Map;
36+
import javax.script.ScriptEngine;
37+
38+
/**
39+
*
40+
* @author Hadrien Mary
41+
*/
42+
public abstract class AbstractAutoCompleter implements AutoCompleter {
43+
44+
protected ScriptEngine engine = null;
45+
46+
public AbstractAutoCompleter(ScriptEngine engine) {
47+
this.engine = engine;
48+
}
49+
50+
@Override
51+
public Map<String, Object> autocomplete(String code) {
52+
return autocomplete(code, 0);
53+
}
54+
55+
@Override
56+
public Map<String, Object> autocomplete(String code, int i) {
57+
Map<String, Object> result = new HashMap<>();
58+
result.put("matches", new ArrayList<>());
59+
result.put("startIndex", 0);
60+
return result;
61+
}
62+
63+
@Override
64+
public ScriptEngine getScriptEngine() {
65+
return this.engine;
66+
}
67+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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%
30+
*/
31+
package org.scijava.script.autocompletion;
32+
33+
import java.util.Map;
34+
import javax.script.ScriptEngine;
35+
36+
/**
37+
*
38+
* @author Hadrien Mary
39+
*/
40+
public interface AutoCompleter {
41+
42+
public Map<String, Object> autocomplete(String code);
43+
44+
public Map<String, Object> autocomplete(String code, int i);
45+
46+
public ScriptEngine getScriptEngine();
47+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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%
30+
*/
31+
package org.scijava.script.autocompletion;
32+
33+
import javax.script.ScriptEngine;
34+
35+
/**
36+
*
37+
* @author Hadrien Mary
38+
*/
39+
public class DefaultAutoCompleter extends AbstractAutoCompleter {
40+
41+
public DefaultAutoCompleter(ScriptEngine engine) {
42+
super(engine);
43+
}
44+
45+
}

src/test/java/org/scijava/script/ScriptEngineTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
package org.scijava.script;
3333

34+
import org.scijava.script.autocompletion.DefaultAutoCompleter;
35+
import org.scijava.script.autocompletion.AutoCompleter;
3436
import static org.junit.Assert.assertEquals;
3537
import static org.junit.Assert.assertNotNull;
3638

@@ -99,6 +101,11 @@ public List<String> getNames() {
99101
public List<String> getExtensions() {
100102
return Arrays.asList("rot13");
101103
}
104+
105+
@Override
106+
public AutoCompleter getAutoCompleter() {
107+
return new DefaultAutoCompleter(getScriptEngine());
108+
}
102109
}
103110

104111
private static class Rot13Engine extends AbstractScriptEngine {

src/test/java/org/scijava/script/ScriptFinderTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
package org.scijava.script;
3333

34+
import org.scijava.script.autocompletion.DefaultAutoCompleter;
35+
import org.scijava.script.autocompletion.AutoCompleter;
3436
import static org.junit.Assert.assertEquals;
3537
import static org.junit.Assert.assertTrue;
3638

@@ -241,6 +243,11 @@ public ScriptEngine getScriptEngine() {
241243
throw new IllegalStateException();
242244
}
243245

246+
@Override
247+
public AutoCompleter getAutoCompleter() {
248+
return new DefaultAutoCompleter(getScriptEngine());
249+
}
250+
244251
}
245252

246253
}

src/test/java/org/scijava/script/ScriptInfoTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
package org.scijava.script;
3333

34+
import org.scijava.script.autocompletion.DefaultAutoCompleter;
35+
import org.scijava.script.autocompletion.AutoCompleter;
3436
import static org.junit.Assert.assertEquals;
3537
import static org.junit.Assert.assertFalse;
3638
import static org.junit.Assert.assertSame;
@@ -285,6 +287,11 @@ public List<String> getNames() {
285287
public List<String> getExtensions() {
286288
return Arrays.asList("bsizes");
287289
}
290+
291+
@Override
292+
public AutoCompleter getAutoCompleter() {
293+
return new DefaultAutoCompleter(getScriptEngine());
294+
}
288295
}
289296

290297
// -- Test script langauge --

0 commit comments

Comments
 (0)